Math Evaluator
A real calculator in your graph — math nodes that don't need a Python pack
- a
- b
- a
- b
Every workflow ends up with a number that should be computed, not typed. Steps from a frame count, a denoise value derived from a batch size, a CFG formula you keep recalculating in your head. SBL_MathEvaluator is a small multiline calculator that turns math written in Python-ish syntax into real FLOAT and INT outputs you can wire anywhere.
The mechanism is worth knowing because it's the safe kind of "run code" node. It does not use Python's exec() or eval(). Instead it parses your expressions into an AST and walks only an allow-list of syntax and functions: assignments, arithmetic, if/for/while, math.*, statistics.*, cmath.*, plus a handful of built-ins like round, min, max, len. No file access, no subprocesses, no imports of arbitrary modules. The README calls it "a restricted AST-based interpreter," and that's a meaningful safety difference from the many packs that just eval() whatever you type into a workflow.
How it actually works
You get one input, expressions (multiline STRING), with the default:
a = w/x
b = y+z
The interpreter seeds four variables - w, x, y, z - all starting at 0. Then it runs your lines and reads out the variables a and b. Those come back twice: as FLOAT (a, b) and as INT (a, b), four outputs total, so you can grab whichever type your target node wants.
Three things to know before you lean on it:
- Only
aandbbecome outputs. Name a variabletotaland nothing comes out the other side. It's a fixed two-slot evaluator; if you need five numbers, you'll be writing five of these. - The defaults divide by zero. With
w=x=0, the stock expression is0.0/0.0, which the node catches and returns as 0 - you're expected to replace it with your own math, not run the sample. - INT truncates. The float-to-int conversion floors toward zero, which is usually what you want for pixel counts and steps.
What you'd reach for it
Anything that's arithmetic between other nodes: width * height for a megapixel budget, steps = max(12, int(total_frames / 8)), a sharpness ramp computed from a slider. It's the middle ground between core ComfyUI's single-operation primitives and the heavyweight full-Python packs - no giant dependency tree, just math.
Install and troubleshooting
Install is the whole pack: ComfyUI Manager → search SBL, or git clone https://github.com/SoftByteLabs/SBL into ComfyUI/custom_nodes, then restart. Zero extra pip dependencies, no model files.
If a run throws, the node catches it, prints MathEvaluator Error: ... to your console, and returns 0s rather than crashing the graph - handy, but it also means a silent 0 can hide a typo. If an output surprises you, check the console log first; it's usually a syntax slip or an unsupported function (remember: no os, no open, no import requests). And because the interpreter only allows imports of math, statistics, and cmath, anything else fails loudly with a clear "Import is not allowed" message.
Inputs (1)
| Name | Type | Default | Description |
|---|---|---|---|
| expressions | STRING | a = w/x b = y+z | — |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| a | FLOAT | — |
| b | FLOAT | — |
| a | INT | — |
| b | INT | — |