cp2004 / OctoPrint-GCodeMacros

Configure custom gcode macros you can use anywhere!
https://plugins.octoprint.org/plugins/gcode_macro
GNU Affero General Public License v3.0
7 stars 2 forks source link

Jinja2 Rendering #2

Closed nionio6915 closed 3 years ago

nionio6915 commented 3 years ago

Leaving this hear to request rendering like Octoprint scripts, using Jinja2 as directed.

Was from thread on Discord about trying to mimic conditional gcode from RRF to use random() function to generate random start point for primeline.

cp2004 commented 3 years ago

This is implemented & will be released soon. Converting the script you posted on discord to the Jinja syntax makes it look like this:

G1 X{{ 25 + (range(126) | random)}} Y{{ 286 + (range(8) | random) }} Z0.6 F3000.0;
G92 E0.0 ; set E position to 0
G91
G1 Z0.3 X100.0 E20.0 F1000.0

This was what I got as a result of calling the macro 3 times:

[...]
Send: G1 X125 Y288 Z0.6 F3000.0
Recv: ok
Send: G92 E0.0
Recv: ok
Send: G91
Recv: ok
Send: G1 Z0.3 X100.0 E20.0 F1000.0
Recv: ok
[...]
Send: G1 X139 Y292 Z0.6 F3000.0
Recv: ok
Send: G92 E0.0
Recv: ok
Send: G91
Recv: ok
Send: G1 Z0.3 X100.0 E20.0 F1000.0
Recv: ok
[...]
Send: G1 X35 Y293 Z0.6 F3000.0
Recv: ok
Send: G92 E0.0
Recv: ok
Send: G91
Recv: ok
Send: G1 Z0.3 X100.0 E20.0 F1000.0
Recv: ok

Jinja is the same template engine used for OctoPrint's gcode scripts, but without the context of things like pause_position etc. that are injected into those.

Documentation on the templates can be found here: https://jinja.palletsprojects.com/en/2.11.x/templates/ - note it is 2.11.x that OctoPrint uses for now, since we need Python 2 & 3 compatibility.

cp2004 commented 3 years ago

It's been released as v1.1.0, available through OctoPrint's software update. It may take up to 24 hours for the update notification to show up.