Layer Number and Total Layers from API

Hi,

Is it possible to get the current printing layer number and the total number of layers from the OctoPrint API.

I am working on an external streaming "thing" that will display some information as text overlay. I would like some sort of progress information. From reading API docs I can see that there is a progress percentage available that is based on the gcode file size. Just wondering if layer numbers are also available.

Thanks,
Adi

Job-based

https://docs.octoprint.org/en/master/api/job.html#retrieve-information-about-the-current-job

https://docs.octoprint.org/en/master/api/datamodel.html#sec-api-datamodel-jobs-progress

File-based

https://docs.octoprint.org/en/master/api/files.html#retrieve-a-specific-file-s-or-folder-s-information

https://docs.octoprint.org/en/master/api/datamodel.html#sec-api-datamodel-files-gcodeanalysis

I've not seen layers or current layer as present within this interface. An event called ZChange is fired off for layer change, which may be useful.

Within the underlying OctoPrint Python-based model appears to be currentZ which is the Z height in mm; assuming you know the layer height, you could calculate the current layer.

Thank you! I think my best bet for now is to simply settle for progress as calculated and presented by the API. Rounded to 2 digits I can do:

curl -s -H "X-Api-Key: REDACTED" http://octopi.example.com/api/job | jq -r ".progress.completion" | xargs printf "%.*f\n" 2

Adi

1 Like

@adilinden btw you can use the rest-api of the DisplayLayerProgressPlugin to get the layer informations: https://github.com/OllisGit/OctoPrint-DisplayLayerProgress#rest---api

2 Likes

@OllisGit

I want to use the total layers and current layers for a project im working on. I need to pull the values to be read by my arduino but im not sure how to do that can i do that with the octoprint API library if i have your plugin installed?

I am not sure what you mean with OP-Library.
If you use a microcontroller you need to use some kind of httpClient on your device (https://www.arduino.cc/en/Tutorial/LibraryExamples/HttpClient) and with this library you are able to send a call to your OP-Instance like this (not tested):

void loop() {
  HttpClient client;
// pull data
  client.addHeader("Content-Type", "application/json");
  client.addHeader("X-Api-Key", "<your api key. found in settings>");  
  client.get("http://<ip of op instance:port>/plugin/DisplayLayerProgress/values");
..do something with the response

// sleep

If you need "realtime" data you can also use a WebSocket-Connection (no pulling, just listening), but I am not sure if there is an Arduino-lib for that feature.

Hi OllisGit

Thanks for replying its really appreciated.

The library i was talking about can be found here GitHub - chunkysteveo/OctoPrintAPI: Library for use with Arduino compatible micro controllers (web enabled) to access the Octoprint API on Raspberry Pi's running the Octoprint 3D printer web server by the brilliant Gina Häußge, aka @foosel..

I basically need the current layer and total layers as an integer to be used in the code. I don't want to get into specifics but it's a mechatronics project. I'm in my final year studying product design technology so I have just have a very basic knowledge of programming arduinos to be honest so forgive me if i'm wrong on any of the terms.

With that library i can connect to the octoprint instance but i was just wondering if it would also read your plugin or how i would go about doing that.

Thanks for your help.

From my perspective the mentioned lib only use the OP-Endpoints (/api/)
and there is no support for plugin calls (/plugin/...). So you need to this by your own or reuse some methods from the lib.

Take a look into the lib-sourcecode how a op-connection could be enabled:

String response = sendRequestToOctoprint("GET", "/plugin/DisplayLayerProgress/values", NULL);
// handle response
...