ComfyUI Node

Math Expression Solver

One expression instead of a tower of math nodes

By DebugPadawan·Created about a year ago·Updated 5 months ago· 2
Math Expression Solver
    • float_val
    • int_val
    expressiona * (b + 10)
    a1.00
    b1.00

    Want to compute a * (b + 10)? ComfyUI's "proper" way is a chain of at least two math nodes plus an addition - and God help you when the formula has three levels of parentheses. Math Expression Solver is the shortcut: you type the whole formula into one string field, feed in two values, and get the answer. It's the pack's answer to the question "why is arithmetic in a node graph so much work?"

    It's part of DebugPadawan's ComfyUI Essentials, a small MIT-licensed utility pack. Think of it as the budget version of rgthree's Power Puter - less capable, but also a whole lot simpler.

    How it works

    The expression string is evaluated with Python's eval(), but behind a deliberately locked-down namespace: only a, b, math, abs, round, min, and max are available, and __builtins__ is disabled. The author's own comment in the source is honest about this - "BE CAREFUL: Uses eval()" - and recommends keeping it strictly math-only. Within that sandbox you can write things like:

    • a * (b + 10)
    • max(a, b) * 2
    • round(a / b, 2)
    • math.sqrt(a ** 2 + b ** 2)

    On any error, the node doesn't crash - it prints [MathExpression Error] ... to the console and returns 0.0 / 0.

    The gotchas that will bite you

    • Only a and b are in scope. This is the big one. You cannot reference other node outputs by name inside the expression - every value you want in the formula has to be wired into a or b first. Two inputs means two variables; anything more and you're chaining this node.
    • Failures are silent in the graph. A typo in the expression gives you 0.0 and a console line you might never see. If the output is always zero, suspect the expression before the math.
    • int_val truncates. float_val is the true result; int_val is int() of it. Use the float output if precision matters.

    Inputs and outputs

    • expression - the formula, a STRING (default a * (b + 10)).
    • a, b - the two FLOAT variables, default 1.

    Outputs: float_val (FLOAT) and int_val (INT).

    Install

    Standard custom-node fare:

    cd ComfyUI/custom_nodes
    git clone https://github.com/DebugPadawan/DebugPadawans-ComfyUI-Essentials.git
    

    Restart ComfyUI, or install via ComfyUI Manager by searching "DebugPadawan". requirements.txt lists numpy, torch, and opencv-python - numpy and torch already ship with ComfyUI and the shipped code never imports cv2, so no heavy downloads, no model files, no API keys.

    When to reach for it

    Anytime a formula would take three or more math nodes to express - a weighted blend, a scaled offset, a conditional-pick via max. Just remember the sandbox: two variables in, formula in the box, result out. For anything more ambitious, rgthree's Power Puter or an Evaluate/Execute node is the bigger hammer.

    CategoryDebugPadawan/Math

    Inputs (3)

    NameTypeDefaultDescription
    expressionSTRINGa * (b + 10)
    aFLOAT1.00
    bFLOAT1.00

    Outputs (2)

    NameTypeDescription
    float_valFLOAT
    int_valINT