Expression
A mini calculator that runs Python-ish expressions in your graph
- a
- b
- c
- d
- INT
- FLOAT
- STRING
- BOOLEAN
Every ComfyUI user eventually needs a number that isn't just a slider. Aspect-ratio math, a value clamped into a range, a prompt that's conditional on another value, "give me a random float divisible by 0.05." You can do all of that with a pile of arithmetic nodes, or you can write one line in a text box. Utility Expression from ComfyUI Extended is that text box: a safe, sandboxed evaluator that runs Python-flavored expressions mid-graph and hands you the result as INT, FLOAT, STRING, and BOOLEAN all at once.
It's the pack's most powerful node, and it's the closest thing in here to rgthree's Power Puter - the "evaluate python-like expressions mid-graph" niche - but with zero dependencies.
How it works
The expression input is the whole show. It uses variables a, b, c, d (the four optional ANY inputs), plus the constants pi, e, i, and phi. Anything the node doesn't recognize gets rejected with an error - this is a whitelist evaluator that parses the expression into an AST and only allows operators and functions it knows, so no eval(), no arbitrary code.
That function list is genuinely generous: arithmetic, comparisons, string ops (replace, split, startswith...), regex (re_sub, re_findall...), statistics (mean, stdev, normal_cdf...), and randomness (random, randint, choice, gauss). The author's own example workflow uses split(c, " ")[a] if d else b - real string slicing and a conditional in one line.
The four outputs are all the same result rendered four ways: INT (rounded), FLOAT, STRING, and BOOLEAN. Grab whichever representation you need; they're not four different computations.
The 2-3 things that matter
- Curly braces for strings. Direct variable access works for math (
a + b), but inside a string literal you need"Hello {a}"- braces get substituted before evaluation. - Error output is a feature. On any failure the node returns
(0, 0.0, "<error message>", False). Wire the STRING output into the pack's Preview Text node and the error prints right on the canvas. That's the intended debugging loop. - Feed it a tensor? Inputs marked ANY get normalized: an image becomes a list of its shape (
[B, H, W, C]), soa[0]on an image input gives you the batch size. Handy, if a little surprising.
Install
cd /path/to/your/ComfyUI/custom_nodes
git clone https://github.com/rookiepsi/comfyui-extended.git
Restart ComfyUI; it's under comfyui-extended/utility. Or ComfyUI Manager → "ComfyUI Extended" → install → restart. Zero dependencies, zero downloads.
Common issues
- Docs say
rand()but the function israndom(). The README's own examplerand() < 0.3will error - the whitelist registersrandom. It's a docs bug, not yours. - Unknown function = error. The node is deliberately strict.
sqrtyes,absyes,importno, method calls on arbitrary objects no. - Random expressions force re-runs. If the expression contains
random/randint/choice/gauss,IS_CHANGEDreturnsNaNso it re-evaluates every queue run. That's how randomness survives caching.
The honest take: it's the node you reach for when the arithmetic-node stack gets silly. A single expression beats a chain of five math nodes, and the error-in-STRING-output trick makes it self-debugging. Learn the curly-brace rule and it's plain sailing.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| expression | STRING | — | |
| aopt | * | — | |
| bopt | * | — | |
| copt | * | — | |
| dopt | * | — |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| INT | INT | — |
| FLOAT | FLOAT | — |
| STRING | STRING | — |
| BOOLEAN | BOOLEAN | — |