What is the trick to using gcode_buttons in a macro?

YES IT IS

ill use that from now.

2 Likes

Can you please post your whole LOOP_TEST macro in one code block as Preformatted text?

I just re-ran it to confirm the behavior

[gcode_macro LOOP_TEST]
gcode:
  {% for step in range(1) %}
    Z_DEC

    
    M118 {zpos}
    G91
    G1 Z+3.0
    G90
    G4 P3000
    {% endfor %}

gives

Error loading template ‘LOOP_TEST’: jinja2.exceptions.TemplateSyntaxError: Unexpected end of template. Jinja was looking for the following tags: ‘endfor’ or ‘else’. The innermost block that needs to be closed is ‘for’.

If i comment the jinja expressions it runs without error

[gcode_macro LOOP_TEST]
gcode:
  #{% for step in range(1) %}
    Z_DEC

    
    M118 {zpos}
    G91
    G1 Z+3.0
    G90
    G4 P3000
   #{% endfor %}

for compleatness this is Z_DEC

[gcode_macro Z_DEC]
varaible_zpos=0
gcode:
    G91
    {% set num = params.NUM|default(1)|int %}
    {% if num <= 100 %}
    G4 P100
    QUERY_BUTTON BUTTON=zswitch
       {% if printer["gcode_button zswitch"].state == 'PRESSED' %}
       {% set num = 100 %}
       {% else %}
       G1 Z-0.05
       #M118 {printer.toolhead.position.z}
       Z_DEC NUM={num+1} # Count u  
       {% endif %}
    {% else %}
       RESPOND MSG="Done Decrementing Z"
       {% set zpos = update("zpos", printer.toolhead.position.z) %}
    {% endif %}
    G90

Can you unindent the {% endfor %} line so it is at the same indentation level as the {% for block?

[gcode_macro LOOP_TEST]
gcode:
  {% for step in range(2) %}
    Z_DEC

    
    M118 {zpos}
    G91
    G1 Z+3.0
    G90
    G4 P3000
  {% endfor %}

this code generates

Error loading template ‘LOOP_TEST’: jinja2.exceptions.TemplateSyntaxError: Unexpected end of template. Jinja was looking for the following tags: ‘endfor’ or ‘else’. The innermost block that needs to be closed is ‘for’.

You can’t have the two-line space in a loop or if statement. DynamicMacros splits the macros across the two-line spaces. Your macro is being run as two macros:

{% for step in range(2) %}
    Z_DEC

and

    M118 {zpos}
    G91
    G1 Z+3.0
    G90
    G4 P3000
{% endfor %}

Also, the zpos variable doesn’t exist in the LOOP_TEST macro. This macro should work:

[gcode_macro LOOP_TEST]
gcode:
  {% for step in range(2) %}
    Z_DEC
    REPORT_Z
    G91
    G1 Z+3.0
    G90
    G4 P3000
  {% endfor %}

[gcode_macro REPORT_Z]
gcode:
    M118 {printer.toolhead.position.z}

Also, the update function doesn’t transfer variables between macros. It only transfers variables across the two-line spaces.

1 Like

Thanks ! ill give it a bash

Hi
Seems you are trying to solve similar task which was already solved.
Take a look here: Secondary needle calibration
Maybe you can adapt it to your needs.