Hinting for Mainsail's macro parameter detection

Mainsail uses a regex to identify what params it exposes in its UI for executing macros. I noticed this breaking for a number of my macros (because a regex can’t parse language grammar). So, I came up with the below hacky solution of adding a dummy parameter block at the end of my macro.

Example

This block at the end of your macro…

  # Dummy argument block for Mainsail
  {% set dummy = None if True else "
  {% set d = params.LENGTH|default(load_length)|float %}
  {% set d = params.SPEED|default(load_speed)|float %}
  {% set d = params.EXTRUDER|default(extruder)|string %}
  {% set d = params.TARGET|default(cur_temp)|int %}
  " %} # End argument block for Mainsail

Will be rendered by Mainsail as this…

Screenshot 2022-02-20 063437

Explanation

The “parameter block” is really just an unused, multi-line string with a fake params statement on each line in the block. Since Mainsail’s regex runs per-line, it interprets each fake statement in the string as a real statement accessing the params object. This shouldn’t impact performance at all, since the string itself gets discarded during template compilation (due to the None if True else condition in the assignment).

You’ll want to put the block at the end of your macro, so it overrides whatever parameters Mainsail had previously inferred. One nice benefit of this approach is that Mainsail uses the default() argument as hint text in the UI for the field value. And since none of this is actually code, you can just fill the space between those parentheses with whatever descriptive text you want (e.g. a field description, default value, name of a config parameter, etc.).

2 Likes