# Interpreting gcode\_macro variable values as python code is confusing

**URL:** <https://klipper.discourse.group/t/interpreting-gcode-macro-variable-values-as-python-code-is-confusing/13852>\
**Category:** General Discussion\
**Created:** [February 5, 2024, 8:24am UTC](https://klipper.discourse.group/t/interpreting-gcode-macro-variable-values-as-python-code-is-confusing/13852 "2024-02-05T08:24:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luro02](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/luro02/32/7510_2.png) [@Luro02](https://klipper.discourse.group/u/Luro02)\
**Post date:** [February 5, 2024, 8:24am UTC](https://klipper.discourse.group/t/interpreting-gcode-macro-variable-values-as-python-code-is-confusing/13852/1 "2024-02-05T08:24:18Z")

</div>

### Describe your issue:

For example this

```ini
[gcode_macro EXAMPLE]
variable_example: true
gcode:
  M117

```

will not work.

Or doing something like this:

```ini
[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?

> **[Commands templates - Klipper documentation](https://www.klipper3d.org/Command_Templates.html#variables)**

---

<div class="post-metadata">

**Author:** ![linuxpaul](https://avatars.discourse-cdn.com/v4/letter/l/f9ae1b/32.png) [@linuxpaul](https://klipper.discourse.group/u/linuxpaul)\
**Post date:** [February 6, 2024, 9:05pm UTC](https://klipper.discourse.group/t/interpreting-gcode-macro-variable-values-as-python-code-is-confusing/13852/2 "2024-02-06T21:05:58Z")

</div>

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:

```auto
variable_bed_temp: 0

```

defines bed\_temp and set it to value 0

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

```

load the value of “printer target temp” into bed\_temp

```auto
 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

> <https://github.com/Klipper3d/klipper/blob/master/config/sample-mmu2s-diy.cfg>

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

---

<div class="post-metadata">

**Author:** ![theophile](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/theophile/32/31_2.png) [@theophile](https://klipper.discourse.group/u/theophile)\
**Post date:** [February 7, 2024, 8:54pm UTC](https://klipper.discourse.group/t/interpreting-gcode-macro-variable-values-as-python-code-is-confusing/13852/3 "2024-02-07T20:54:18Z")

</div>

I don’t understand what you are asking.

> [@Luro02](#):
>
> For example this
> 
> ```auto
> [gcode_macro EXAMPLE]
> variable_example: true
> gcode:
> M117
> 
> ```
> 
> will not work.

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.

---

<div class="post-metadata">

**Author:** ![Luro02](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/luro02/32/7510_2.png) [@Luro02](https://klipper.discourse.group/u/Luro02)\
**Post date:** [February 8, 2024, 11:19am UTC](https://klipper.discourse.group/t/interpreting-gcode-macro-variable-values-as-python-code-is-confusing/13852/4 "2024-02-08T11:19:09Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![EddyMI3D](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/eddymi3d/32/1981_2.png) [@EddyMI3D](https://klipper.discourse.group/u/EddyMI3D)\
**Post date:** [February 8, 2024, 1:18pm UTC](https://klipper.discourse.group/t/interpreting-gcode-macro-variable-values-as-python-code-is-confusing/13852/5 "2024-02-08T13:18:48Z")

</div>

> [@Luro02](#):
>
> My problem is that nothing mentions that whatever you write as the default value for the variable **must be a valid python literal**

You may read here:  
[https://www.klipper3d.org/Command\_Templates.html#macro-parameters](https://www.klipper3d.org/Command_Templates.html#macro-parameters)

---

<div class="post-metadata">

**Author:** ![theophile](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/theophile/32/31_2.png) [@theophile](https://klipper.discourse.group/u/theophile)\
**Post date:** [February 8, 2024, 6:24pm UTC](https://klipper.discourse.group/t/interpreting-gcode-macro-variable-values-as-python-code-is-confusing/13852/6 "2024-02-08T18:24:26Z")

</div>

> [@Luro02](#):
>
> So one might think that one could write jinja code as the default expression (which is not the case).

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.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/klipper/original/1X/64419bf2aac6639e6ef5cef200dcd456b0a01ac3.png) [@system](https://klipper.discourse.group/u/system)\
**Post date:** [April 8, 2024, 6:25pm UTC](https://klipper.discourse.group/t/interpreting-gcode-macro-variable-values-as-python-code-is-confusing/13852/7 "2024-04-08T18:25:21Z")

</div>

Closed on April 8, 2024, 6:25pm UTC

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