Jinja and String Literals

Calling all JINJA experts!

For the record - I am not a Jinja syntax fan. Like, at all :slight_smile:

I have been able to do some fairly complex gcode macros with it to adapt to variables that CURA spits out, but for some reason, I cannot get it to interpret my string variables.

Goal:
In CURA 4.13+, you can get access to material parameters. I found this extremely useful to dynamically control things, like pressure advance parameters based on the material in use in the active extruder. I have a START_PRINT macro that uses variables set from the CRUA start gcode block. My T0 and T1 gcode macros read some of the START_PRINT variables and use them directly, or to set other gcode macro variables (like pressure advance). See snippets below:

CURA Start Gcode Block:

SET_GCODE_VARIABLE MACRO=START_PRINT VARIABLE=material_type_0 VALUE='"{material_type}"'
SET_GCODE_VARIABLE MACRO=START_PRINT VARIABLE=material_type_1 VALUE='"{material_type, 1}"'

T0/T1 Custom GCODE:

#Change toolhead T0
[gcode_macro T0]
gcode:
	ACTIVATE_EXTRUDER EXTRUDER=extruder
    M109 S{(printer["gcode_macro START_PRINT"].material_print_temperature_initial_0)}      ;# wait for print temp
	POSITION_E0
    SET_GCODE_VARIABLE MACRO=SET_MATERIAL_PRESSURE_ADVANCE VARIABLE=material_type VALUE='"{(printer["gcode_macro START_PRINT"].material_type_0)}"'
    SET_MATERIAL_PRESSURE_ADVANCE

And, this is the gcode macro that decoded the material type to change the pressure advance setting:

[gcode_macro SET_MATERIAL_PRESSURE_ADVANCE]
variable_material_type: '"PLA"'
gcode:
    {% if material_type == "PLA" %}
        ;M117 PLA
        SET_PRESSURE_ADVANCE ADVANCE=0.055
    {% elif material_type == "ABS" %}
        ;M117 ABS
        SET_PRESSURE_ADVANCE ADVANCE=0.035
    {% else %}
        ;M117 ???
        SET_PRESSURE_ADVANCE ADVANCE=0.000
    {% endif%}

Below is a snippet of what the gcode file looks like from CURA from a slice using PLA and PVA from the Start Gcode block:

;Generated with Cura_SteamEngine 4.13.1
T0
M82 ;absolute extrusion mode
SET_GCODE_VARIABLE MACRO=START_PRINT VARIABLE=material_type_0 VALUE='"PLA"'
SET_GCODE_VARIABLE MACRO=START_PRINT VARIABLE=material_type_1 VALUE='"PVA"'
SET_GCODE_VARIABLE MACRO=START_PRINT VARIABLE=extruders_enabled_count VALUE=2
SET_GCODE_VARIABLE MACRO=START_PRINT VARIABLE=material_standby_temperature_0 VALUE=195.0
SET_GCODE_VARIABLE MACRO=START_PRINT VARIABLE=material_standby_temperature_1 VALUE=175
SET_GCODE_VARIABLE MACRO=START_PRINT VARIABLE=material_bed_temperature_layer_0 VALUE=60.0
SET_GCODE_VARIABLE MACRO=START_PRINT VARIABLE=material_print_temperature_layer_0_0 VALUE=210.0
SET_GCODE_VARIABLE MACRO=START_PRINT VARIABLE=material_print_temperature_layer_0_1 VALUE=230.0
SET_GCODE_VARIABLE MACRO=START_PRINT VARIABLE=material_print_temperature_initial_0 VALUE=200.0
SET_GCODE_VARIABLE MACRO=START_PRINT VARIABLE=material_print_temperature_initial_1 VALUE=220.0
; Execute START_PRINT GCODE defined in Klipper config file
START_PRINT
G92 E0
;START_EXTRUDER_1
G92 E0
G1 F2400 E-3
;LAYER_COUNT:297
;LAYER:0
M107
M104 T1 S175
G0 F3600 X105.527 Y123.667 Z0.3
;TYPE:SKIRT
G1 F2400 E0
G1 F1800 X106.314 Y123.055 E0.04974
G1 X106.448 Y122.956 E0.05805
G1 X107.267 Y122.389 E0.10775
...

I keep getting errors when running the
SET_GCODE_VARIABLE MACRO=SET_MATERIAL_PRESSURE_ADVANCE VARIABLE=material_type VALUE="{(printer["gcode_macro START_PRINT"].material_type_0)}"
command saying that the result is not a literal. I have tried all kinds of combinations with single and double quotes and get errors ranging from ā€œinternal errors from SET_GCODE_VARIABLEā€ to ā€œUnable to parse ā€˜PLAā€™ as a literalā€.

I could sure use some guidance on strings, and maybe a good reference to this languag!

Iā€™d appreciate any help. Thanks!

I believe the fix is to reverse the order of single and double quotes when assigning the VALUE.

SET_GCODE_VARIABLE MACRO=START_PRINT VARIABLE=material_type_0 VALUE="'{material_type}'"

While I donā€™t necessarily hate the Jinja syntax I do hate all of this escaping. Its really impossible to keep track of. The lack of line numbers in the error messages from Klipper is not helpful either.