ComfyUI Node

πŸ–© Calculation

A real math evaluator inside your graph

By alt-key-projectΒ·Created 3 years agoΒ·Updated 2 years agoΒ· 114
πŸ–© Calculation
    • FLOAT
    • INT
    β—„expressiona + b + c - (r * s * t)β–Ί
    β—„a_int0β–Ί
    β—„b_int0β–Ί
    β—„c_int0β–Ί
    β—„r_float0.00β–Ί
    β—„s_float0.00β–Ί
    β—„t_float0.00β–Ί

    ComfyUI gives you primitives, and then it gives you way too many of them to wire into one formula. Calculation [Dream] is the escape hatch: type an expression like sqrt(a*a + b*b) or pow(2, t) * 0.1, feed it up to six values, and get a float and an int out. It's the pack's answer to "I don't want to chain six math nodes to compute one number."

    How it works

    The expression is evaluated with the evalidate library - a deliberately locked-down Python expression evaluator, not an eval() free-for-all. The node exposes the standard arithmetic operators plus a healthy chunk of Python's math module: sin, cos, tan, sqrt, log, log2, log10, exp, pow, abs, round, floor, ceil, min, max, factorial, dist, atan2, and the constants like pi. Anything outside that whitelist is refused, so you can't accidentally (or maliciously) execute arbitrary code in your own graph - it errors out and logs instead.

    The inputs are named after their types: a_int, b_int, c_int (INTs) and r_float, s_float, t_float (FLOATs). In the expression you refer to them simply as a, b, c, r, s, t. You don't have to use all six - the expression can reference any subset.

    The inputs that matter

    • expression - the formula, multiline. The default is a + b + c - (r * s * t), which is fine as a starting template, but you'll almost always replace it.
    • a_int / b_int / c_int - integer values, all default 0.
    • r_float / s_float / t_float - float values, all default 0.

    It outputs two values: FLOAT (the exact result) and INT (the result rounded to the nearest integer). Pick whichever your downstream node wants.

    Where it wires in

    Anytime you need a derived number in the middle of a graph. Drive a curve's offset from a calculation, derive a zoom speed from a beat, turn two primitives into a ratio. It pairs naturally with the pack's frame counter: wire the counter's frame number into an int input and compute something that changes every frame.

    Installing it

    Ships with Dream Project Animation Nodes - ComfyUI Manager (search Dream Project Animation) or clone https://github.com/alt-key-project/comfyui-dream-project into custom_nodes, pip install -r requirements.txt, restart. Note this node is the reason evalidate is in the pack's requirements - it's the one dependency that exists purely for this safe-eval feature.

    Common issues

    If the expression is malformed or uses a function that isn't whitelisted, the node logs an error and returns 0.0 / 0 rather than crashing the whole workflow - which is graceful, but easy to miss if you're not watching the console. Check the log when your numbers come back as zero. And remember the inputs are type-specific: a, b, c are integers, so a / 2 does integer division. If you want float math, feed the floats or write a / 2.0.

    Category✨ Dream/πŸ›  utils

    Inputs (7)

    NameTypeDefaultDescription
    expressionSTRINGa + b + c - (r * s * t)β€”
    a_intoptINT0β€”
    b_intoptINT0β€”
    c_intoptINT0β€”
    r_floatoptFLOAT0.00β€”
    s_floatoptFLOAT0.00β€”
    t_floatoptFLOAT0.00β€”

    Outputs (2)

    NameTypeDescription
    FLOATFLOATβ€”
    INTINTβ€”