Nodes/ComfyUI-KYNode/Math expression eval
ComfyUI Node

Math expression eval

Write the formula instead of chaining ten nodes

By yorkane·Created 2 years ago·Updated 6 months ago· 10
Math expression eval
  • a
  • b
  • c
  • int
  • float
  • text
d0.00
expression

Anyone who's built a ComfyUI graph with actual conditional logic knows the pain: a "pick a random size" or "clamp this value" step that should be one line of code ends up as a small forest of Add/Multiply/Compare nodes wired together. This node collapses that back into one line - you write the formula, it evaluates it.

What you can actually write

Per the node's own description, the supported vocabulary is: round(n), ceil(n), floor(n), int(n), sqrt(n), min(n1, n2, n3...), max(n1, n2, n3...), randomint(min, max), randomchoice(n1, n2, n3...) (randomly picks one of its arguments), and iif(condition, value_if_true, value_if_false) - a one-line ternary, e.g. iif(1>2, c, d) returns d. That's the documented function set; treat it as the ceiling rather than assuming general Python is available underneath.

You reference up to four variables in your expression: a, b, c (all optional, wildcard-typed - feed in numbers, or presumably anything coercible) and d (a required FLOAT, which can just sit as a plain literal if you don't need it wired from elsewhere).

Inputs and outputs

Required: d (float, default 0) and expression (the formula string itself, no default). Optional: a, b, c. The result comes back on three parallel outputs simultaneously - int, float, and text - so whichever type your next node actually wants, you've got it without needing a separate cast node.

How it compares to rgthree's Power Puter

If you've used rgthree-comfy's Power Puter - the actively-maintained pack's own inline-expression node - this is the lighter-weight cousin. Power Puter is a full python-like evaluator (string formatting, comparisons, list comprehension, lookups across other nodes by id or title, all stepped through an AST allowlist rather than a raw eval); KY_MathExpression is scoped to numbers, a handful of math functions, and one conditional. If you just need arithmetic and a ternary, this is simpler to reach for; if you need to manipulate strings or reference other nodes by name, that's Power Puter's job, not this one's.

Installing it

ComfyUI Manager: search ComfyUI-KYNode, install, restart. Or:

cd ComfyUI/custom_nodes
git clone https://github.com/yorkane/ComfyUI-KYNode

Restart ComfyUI. No external dependencies - pure expression evaluation.

Common issues

Stick to the documented function list. If your expression references something outside round/ceil/floor/int/sqrt/min/max/randomint/randomchoice/iif, don't assume it'll work just because it's valid Python - this isn't a general interpreter, it's a scoped evaluator.

If your expression references a variable (say, b) that you never actually wired anything into, expect an error or an unset/None treatment rather than a silent default - always make sure what you type matches what you've plugged in.

randomint and randomchoice mean this node isn't deterministic between runs by design - if you need reproducible output, don't lean on those two functions, or accept that re-running the graph will give you a different result each time.

CategoryKYNode/Utils

Inputs (5)

NameTypeDefaultDescription
dFLOAT0.00
expressionSTRING
aopt*
bopt*
copt*

Outputs (3)

NameTypeDescription
intINT
floatFLOAT
textSTRING