Skip to content

Python

Dynamic Macros's most powerful feature allows you to run Python code directly from within a macro.

Running Python from within a Macro

Disclaimer

This functionality allows Dynamic Macros to gain significant control over your printer and Klipper host. I am not responsible for whatever happens if you download a malicious macro.

There are three main reasons why this could be helpful:

  1. Allowing for deeper control of Klipper and the Klipper host
  2. A learning bridge for creating Klipper plugins/extras
  3. A tool to help develop Klipper plugins/extras without restarting Klipper

To run Python from within a Dynamic Macro, use either the python() utility function, or the python_file() utility function. The python() function accepts python code as a multiline string, and the python_file() function accepts a filename (relative to your printer.cfg folder).

Tip

When using the python() utiltiy function, Jinja2 (which converts the macro to GCode) may throw errors during parsing. If you are getting errors, it is recommended to switch to python_file().

Here are a few examples:

Python Math

macros.cfg
1
2
3
4
5
6
7
8
9
[gcode_macro MATH]
gcode:
    {% set value = python("""
    a = kwargs['a']
    b = kwargs['b']
    c = a + b
    output(c)
    """, a=1, b=2) %}
    RESPOND MSG={value}

Python File Running GCode

macros.cfg
1
2
3
4
[gcode_macro PYFILE]
gcode:
    {% set value = python_file("test.py") %}
    RESPOND MSG={value}
test.py
1
2
3
print("Hello from Python!")
gcode("G28\nG1 X100 Y100 Z100 F1200")
output("GCode Executed")

Comments