Nodes/ComfyUI-AusBoss/Math Expression πŸ†Ž
ComfyUI Node

Math Expression πŸ†Ž

A calculator on the canvas that can't run your code

By ausbossΒ·Created about a month agoΒ·Updated 3 days agoΒ· 2
Math Expression πŸ†Ž
    • float
    • int
    β—„expressiona + bβ–Ί
    β—„a0.000β–Ί
    β—„b0.000β–Ί
    β—„c0.000β–Ί

    Math Expression πŸ†Ž is a calculator you type into, with the numbers coming in on wires. You get three inputs, a, b, and c, and one expression box that says what to do with them - (a // 8) * 8 to snap a width to a multiple of 8, a * 0.85 for a strength ramp, ceil(a / b) to work out a frame count. It returns the answer twice: as a float and as an int, so it plugs straight into latent sizes, sampler steps, or any other numeric socket without a convert-in-the-middle step.

    The reason this one is worth a look over the handful of other expression nodes is the security story, which the pack is quietly serious about. The expression is parsed with Python's ast and walked against a strict whitelist - numbers, a/b/c, the operators + - * / // % **, parentheses, and the functions min, max, abs, round, floor, ceil, sqrt. It is never eval()'d. That matters because ComfyUI workflows get shared constantly, and a shared workflow is executable code that lands on your machine. A node that evaluates arbitrary Python from a downloaded workflow is a code-execution hole dressed up as a convenience. This one refuses anything outside the whitelist by name - no attribute access, no subscripts, no keyword arguments - so the worst a malicious workflow can do is make your width 7 instead of 8.

    There are two details the author got right. Everything runs in floats, so 9 ** 9 ** 9 overflows into a clean error instead of building a number with hundreds of millions of digits. And the int output rounds halves away from zero - 2.5 becomes 3, -2.5 becomes -3 - which reads as sane on screen, where Python's default banker's rounding (2.5 β†’ 2) looks like a bug.

    How it works

    Three optional float inputs (a, b, c, default 0 each) plus the expression string (default a + b). Wire values in, type the formula, and the outputs are the float result and the rounded int. Expressions are capped at 4096 characters - longer than any honest formula, shorter than a payload. Errors are named and friendly: "divided by zero," "does not allow the function d," "square root of a negative number" all arrive as readable messages instead of a raw traceback.

    Install

    ComfyUI Manager is the easy route: search for ComfyUI-AusBoss under Custom Nodes and hit Install, then restart. Or manually:

    cd ComfyUI/custom_nodes
    git clone https://github.com/ausboss/ComfyUI-AusBoss.git
    

    Restart ComfyUI and Math Expression πŸ†Ž sits under πŸ†Ž AusBoss/Utility. No extra Python dependencies. Minimum ComfyUI 0.27.1, and hard-refresh with Ctrl+Shift+R after frontend updates.

    Common issues

    You only get a, b, and c - there's no d, no named variables, no string functions. If a formula needs four inputs, chain two nodes. And the safety is also a limit: no subscripts, no attribute access, so anything fancier belongs in a proper script node. The rounding detail is worth remembering when you wire the int output into a size - it rounds to nearest, not down, so (a / 8) at a non-multiple gives you the nearest multiple, not a floor. Use // when you want to floor.

    CategoryπŸ†Ž AusBoss/Utility

    Inputs (4)

    NameTypeDefaultDescription
    expressionSTRINGa + bArithmetic over a, b and c: numbers, + - * / // % **, parentheses, and min/max/abs/round/floor/ceil/sqrt.
    aoptFLOAT0.000-1000000000000000–1000000000000000The value of a in the expression.
    boptFLOAT0.000-1000000000000000–1000000000000000The value of b in the expression.
    coptFLOAT0.000-1000000000000000–1000000000000000The value of c in the expression.

    Outputs (2)

    NameTypeDescription
    floatFLOATThe result as a float.
    intINTThe result rounded to the nearest whole number (halves round away from zero).