First, I see that the Macro category is only for ready-made macros. So I don’t know where to put this question.
Is it possible to use variable within another variable. See my example code below (e{params.T}_f). This is just a macro for testing and to show the problem here.
What I want is to avoid having to ask if it is T0, T1 or T2. And thus save many extra lines of code. So that one can create a macro that will work on printers with different numbers of extruders.
[gcode_macro M307]
variable_e0_f: 0
variable_e1_f: 0
variable_e2_f: 0
variable_e0_s: 0
variable_e1_s: 0
variable_e2_s: 0
description: M307 [T extruder nr.] [F retract feedrate] [S retract length]
gcode:
{% if 'T' in params %}
{% set extrudert = params.T %}
{% endif %}
{% set extruderc = printer.toolhead.extruder - "extruder" %}
{% if extruderc == "" %}
{% set extruderc = "0" %}
{% endif %}
{% if 'F' in params %}
M118 SET_RETRACTION RETRACT_SPEED={params.F}
{% endif %}
{% if 'S' in params %}
M118 SET_RETRACTION RETRACT_SPEED={params.F}
{% endif %}
{% if 'T' in params %}
{% if 'F' in params %}
SET_GCODE_VARIABLE MACRO=M307 VARIABLE=e{params.T}_f VALUE={params.F}
{% endif %} ; ^^^^^^^^^^
Well, it’s happened before, and it’s sure to happen again. It turns out that the example I gave actually worked. There were other things that went wrong. And I messed it up. But here’s the code that works, in case anyone else wants to do something similar.
[gcode_macro M307]
variable_e0_f: 0
variable_e1_f: 0
variable_e2_f: 0
variable_e0_s: 0
variable_e1_s: 0
variable_e2_s: 0
description: M307 [T extruder nr.] [F retract feedrate] [S retract length]
gcode:
{% if 'T' in params %}
{% set extrudert = params.T %}
{% endif %}
{% set extruderc = (printer.toolhead.extruder)|int %}
{% if extruderc == "" %}
{% set extruderc = "0" %}
{% endif %}
{% if 'F' in params %}
M118 SET_RETRACTION RETRACT_SPEED={params.F}
{% endif %}
{% if 'S' in params %}
M118 SET_RETRACTION RETRACT_SPEED={params.F}
{% endif %}
{% if 'T' in params %}
{% if 'F' in params %}
SET_GCODE_VARIABLE MACRO=M307 VARIABLE=e{params.T}_f VALUE={params.F}
{% endif %}
{% endif %}
1 Like
I think I’ll wait a day or two before concluding.
My conclusion so far is:
The first line is ok because there are no braces (curly brackets) inside other braces.
In the second line there is, and thus will not work in Klipper.
But can anyone help, so that I can get something similar to work as second line?
SET_GCODE_VARIABLE MACRO=M307 VARIABLE=e{params.T}_f VALUE={params.F}
{% if printer["gcode_macro M307"].{variablea} == 1 %}
Don’t nest moustaches. You’re already in a Jinja context, so there’s no need for additional Jinja delimiters. Also, you have to use the normal accessor syntax instead of the more limited dot accessor (which only works for static strings that are valid Python identifiers.)
{% if printer["gcode_macro M307"][variablea] == 1 %}
1 Like
Works, thanks.
Since I asked so broadly, can the thread be left open? There are 2 correct answers already, maybe there are more.