Can you do math in a custom button?

I am using the custom button plugin with a CNC 3018 and the CNC plugin and getting almost exactly the results I want. I have a button to probe the "0" corner of the workpiece using the cutter. When the cutter touches the workpiece, the coordinate is set to - 1/2 the cutter diameter. Then "0" will be the middle of the cutter.

I have an input field and my G code for that step is "G92 X%(value)s". It works - so long as you enter a negative value that is 1/2 the tool diameter. I have a calculator and I am smart enough to tie my own shoes, so it's not a debilitating step to enter the correct number, but it sure would be nice to just enter a positive value for the diameter and have the machine do "-1/2*%(value)s". Of course, when I tried that, it errored out. Is there a way to get the machine to do the math in a custom button?

you could probably achieve this with scripts, not sure if you can do it within the control itself.

so you could use

{% set radius = -0.5 * parameters.diameter %}
G92 X{{ radius }}

in your gco file

or maybe even this

{% set radius = parameters.diameter / 2 %}
G92 X-{{ radius }}

OK. Thanks. Now that can't be done in the gcode "box" that I set up for my button, can it? I have to do it as a script?

Correct, has to be done as a script. The scripts are rendered with Jinja2, which allows for template syntax to be used to do the calculations. The standard gcode scripts are just sent straight to the printer with not much processing done.

You might also be able to do it with a JavaScript snippet to be executed when you press the button. At that point you're halfway to making your own plugin :slightly_smiling_face:

This sounds like the information I need. Thank you very much.

That's a good point Charlie, a javascript snippet would be a possibility using data in the function call if I'm reading the docs properly. So something like this?

- children:
  - input:
    - default: 10
      name: Diameter
      parameter: diameter
    javascript: OctoPrint.control.sendGcode("G92 X" + (parseFloat(data.input[0].value()) * -0.5))
    name: X
  name: My Homing Routine