Nodes/ComfyUI-RMAutomation/RM Math Expression
ComfyUI Node

RM Math Expression

Do the math in your graph, not in your head

By Moser9815·Created 7 months ago·Updated 7 days ago· 0
RM Math Expression
  • a
  • b
  • c
  • INT
  • FLOAT
expression

Every workflow eventually needs a number that isn't directly available: "frame rate × seconds," "half the image width," "seed plus one." The stock answer is a chain of arithmetic nodes, which is a miserable way to read (a * b) + c spread across three boxes. RMMathExpression is a text-box calculator inside your graph - type the expression, reference three optional inputs as a, b, c, and get both an INT and a FLOAT out. It's a standalone reimplementation of pysssss's MathExpression with no external dependency.

How it works

It parses the expression into an abstract syntax tree and evaluates it against an allowlist - standard arithmetic, comparisons (==, !=, >, <, etc., returning 1 or 0), boolean ops, bitwise ops, and a small set of functions: round, ceil, floor, min, max, sqrt, int, iif (inline if: iif(condition, then, else)), plus randomint and randomchoice. Because it's an AST walk rather than eval(), the surface for nasty surprises is small.

The genuinely useful trick: if an input is a LATENT or IMAGE, you can ask for its size - a.width and a.height resolve to the tensor's dimensions (a latent's are multiplied by 8 to get pixel dimensions, since latents are 8× smaller). So "half the width of my image" is a.width / 2 with the image wired into a. Any expression containing "random" makes the node re-run every execution - IS_CHANGED returns NaN - so randomint behaves like a dice roll rather than a cached value.

Inputs and outputs that matter

  • expression (STRING, multiline, required) - the formula.
  • a, b, c (*, optional) - any typed inputs: numbers, or LATENT/IMAGE for .width/.height.
  • Outputs: INT and FLOAT - same result, both types, so you can wire whichever the target expects. It's also an output node, so the value displays on the canvas.

How to install it

Ships in the RMAutomation pack:

cd ComfyUI/custom_nodes
git clone https://github.com/Moser9815/ComfyUI-RMAutomation

Restart ComfyUI; it's under RMAutomation/Utils. Uses only the Python standard library - genuinely zero dependencies.

Common issues

The failures are loud and specific, which is a good thing: using an undefined variable raises "Name not found," using a function outside the allowlist raises "Invalid function call," and feeding a LATENT into arithmetic directly raises "Complex types (LATENT/IMAGE) need to reference their width/height." The one that surprises people is trying to use a non-allowlisted function - abs, say - which won't parse; check the supported list in the source rather than assuming Python's full stdlib is available. And remember inputs arrive as whatever type they are: a / 2 on an INT gives you a float, so pick your output accordingly.

CategoryRMAutomation/Utils

Inputs (4)

NameTypeDefaultDescription
expressionSTRING
aopt*
bopt*
copt*

Outputs (2)

NameTypeDescription
INTINT
FLOATFLOAT