Indirectly indexing through extruder objects

I’m have a tool changing printer with multiple extruders that I’m trying to write a macro that dynamically stores the current temperature of a given extruder when called. I know this can be done with an if statement and already have it working with something like this:

{% if tool == "0" %}
   SET_GCODE_VARIABLE MACRO=test VARIABLE=curr_temp VALUE={printer.extruder.temperature}
{% elif tool == "1" %}
  SET_GCODE_VARIABLE MACRO=test VARIABLE=curr_temp VALUE={printer.extruder1.temperature}
{% elif tool == "2" %}
  SET_GCODE_VARIABLE MACRO=test VARIABLE=curr_temp VALUE={printer.extruder2.temperature}
{% endif %}

I’ve been trying to clean up code and not have to write so many cases for such things and I’ve found I can call macros dynamically like the one below will call a macro called macro2

[gcode_macro test1]
variable_index:2
gcode:
  macro{index}

The trouble I’m having is being able to do something like that with objects doesn’t work not matter what I try. None of the attempts in the example will work and I get syntax errors. Is this even possible?

[gcode_macro test2]
variable_tool: 1
variable_curr_temp: 0
gcode:
  #first attempt was to combine it all
  SET_GCODE_VARIABLE MACRO=test2 VARIABLE=curr_temp VALUE={printer.extruder{tool}.temperature}

#1st attempt to break it down but I believe it was actually trying to store "printer.extruder1.temperature" into the variable
  {% set ext_name = "extruder"~tool %}
  {% set val = "printer."~ext_name~".temperature" %}
  SET_GCODE_VARIABLE MACRO=test2 VARIABLE=curr_temp VALUE={val}

  #tried again but I think it was basically the same as above
  {% set ext_name = "extruder"~tool %}
  {% set val = "printer."~ext_name~".temperature" %}
  SET_GCODE_VARIABLE MACRO=test2 VARIABLE=curr_temp VALUE=val

  #I also tried is this way thinking I might get somewhere but it still wouldn't compile
  {% set ext = printer.extruder{tool} %}
  SET_GCODE_VARIABLE MACRO=test2 VARIABLE=curr_temp VALUE={ext.temperature}

Like I said, I know I can do it with IF statements, but I want to find a way to write it such that a case does not have to be made for every extruder and thus more universal

Nevermind, I found it. You put the dynamic part in square brackets.

    {% set ext = "extruder"~tool %}
    SET_GCODE_VARIABLE MACRO=test VARIABLE=curr_temp VALUE={printer[ext].temperature}
1 Like

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