Interpreting gcode_macro variable values as python code is confusing

Describe your issue:

For example this

[gcode_macro EXAMPLE]
variable_example: true
gcode:
  M117

will not work.

Or doing something like this:

[gcode_macro EXAMPLE]
variable_example: printer['...'].x|default(0)
gcode:
  M117

When I started learning klipper and writing my own macros, I was very surprised that valid jinja code does not work for initializing variables (I think wrapping them in {} did not help either).

By looking at the implementation, I found out that the initialization expression are parsed as a python ast, which is why you have to write True instead of true.

A small comment in the example mentioning this particularity, would be very helpful. I don’t think this can be fixed in a backwards compatible way?
https://www.klipper3d.org/Command_Templates.html#variables

Maybe I didn’t get what you are looking for and maybe you are looking für jinja syntax, not python?
Depending to the example:

variable_bed_temp: 0

defines bed_temp and set it to value 0

SET_GCODE_VARIABLE MACRO=start_probe VARIABLE=bed_temp VALUE={printer.heater_bed.target}

load the value of “printer target temp” into bed_temp

 M140 S{printer["gcode_macro start_probe"].bed_temp}

returns the bed_temp value from start_probe macro

There are a pretty bunch of examples from line 80 to the end. May be this will help a bit

Keep in mind thar klipper macros will not be evaluad line by line.

I don’t understand what you are asking.

What is it that you are expecting that macro to do? As written, this macro has no function except to issue the M117 command with no arguments, which simply clears the display from whatever message was last displayed with the M117 command (if any). The macro defines a variable but never uses it, so the variable is unnecessary and can simply be deleted, unless you intend for it to be used by some other macro.

I am not searching for solutions, I already know about them. I want to report an issue that is caused by a lack of documentation/bad design decisions.

What the macro does is not important. My problem is that nothing mentions that whatever you write as the default value for the variable must be a valid python literal.

When you paste the macro into klipper, you get:

Option ‘variable_example’ in section ‘gcode_macro EXAMPLE’ is not a valid literal: malformed node or string on line 1: <ast.Name object at 0xffffad6d5ba0>

This is because in python, you write True, instead of true. In Jinja, you can write not only True, but true as well. So one might think that one could write jinja code as the default expression (which is not the case).

You may read here:
https://www.klipper3d.org/Command_Templates.html#macro-parameters

I see. You do have a point there. Klipper does use a modified jinja2 implementation. Some built-in jinja2 filters simply don’t exist in Klipper’s implementation, and some of the formatting conventions are different (simpler) too. For me, it took some trial-and-error and experimentation to figure out how to do what I wanted to do.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.