ComfyUI Node

PVL Math

Do real math inside your graph, not on a calculator

By pvlprk·Created about a year ago·Updated 8 months ago· 1
PVL Math
  • a
  • b
  • c
  • INT
  • FLOAT
value

ComfyUI is full of situations where a number has to be derived, not typed: "I want the short side of this image, halved", "steps should be 20 + seed % 5", "width × 2 for the upscale pass". Core has a math node but it's one binary operation at a time, which means chaining three of them and a prayer. PVL Math lets you type the whole thing as a single expression and get an INT and a FLOAT out.

If you've used ComfyUI Essentials' SimpleMath, this is the same idea - the source literally calls itself a clone of it, which is worth knowing for one reason: any workflow that references the essentials math node can be rebuilt with this one. It lives in pvlprk/comfyui-pvl-api-nodes ("ComfyUI Assistant Node"), the grab-bag pack.

How it works

The value field is an expression string. The node parses it with Python's ast module and walks the tree itself, evaluating only a whitelist of operators and functions. That's important: it is not eval(). No arbitrary code runs. What you get:

  • arithmetic: + - * / // % **, unary minus
  • comparisons: == != < <= > >= (returning 1/0)
  • logic: and, or, not
  • functions: min(), max(), round(), sum(), len()
  • variables: a, b, c from the wildcard inputs - so you can wire a width from one node and a seed from another into the formula
  • even subscripts, like sizes[0]

So min(a, b) * 2 + 1 works. So does round(a / 8) * 8. If anything fails to parse, or a variable name isn't one of a/b/c, it quietly returns 0.0 - the safety valve is also the silent-failure trap. A typo in the expression doesn't crash; it just zeroes.

One neat behavior: if you wire a tensor (say, a model's or image's shape) into a, it converts to the tensor's shape list first, so a[0] gives you the batch size. Handy in automation.

Inputs and outputs

  • value - the expression (STRING). The only required input.
  • a, b, c - wildcard inputs, default 0. Wire numbers in or leave them.
  • Outputs: INT and FLOAT, both computed from the same result (INT is the rounded float).

Installing

cd ComfyUI/custom_nodes
git clone https://github.com/pvlprk/comfyui-pvl-api-nodes

restart, or use ComfyUI Manager and search for "ComfyUI Assistant Node". No models, no extra dependencies beyond ComfyUI's own torch/numpy - the pack's heavy requirements (OpenAI, Google, fal clients) are for its other API nodes.

The honest caveats

The whitelist is small. No abs(), no floor()/ceil() (use round down/round up via... well, not here - you're limited to round), no trig. For anything beyond arithmetic-plus-a-few-functions, this isn't it. But for the 90% case - computing dimensions, steps, seed-derived values - it collapses a chain of core math nodes into one readable line, and the two typed outputs mean you can feed either an INT consumer or a FLOAT consumer without a converter node.

Categoryessentials_mb/utilities

Inputs (4)

NameTypeDefaultDescription
valueSTRING
aopt*0
bopt*0
copt*0

Outputs (2)

NameTypeDescription
INTINT
FLOATFLOAT