Nodes/ComfyUI_AC_FUN-V1.0/Math_数学表达式
ComfyUI Node

Math_数学表达式

A calculator inside your ComfyUI graph — ditch the add-node chains

By A719689614·Created 3 years ago·Updated 8 days ago· 3
Math_数学表达式
    • STRING
    Expression数学表达式
    tips分母不能为0,不然会报错

    Every ComfyUI workflow eventually needs arithmetic, and the default answer is a pile of tiny add/multiply nodes strung together until your canvas looks like a game of snakes and ladders. Math_数学表达式 ("math expression") is the shorter answer: one node, one text box, you type (w * 2 + 64) / 8 and out comes the result. The community's take on this pattern is pretty unanimous - when somebody built a whole calculator node for frame math, the top response was basically "you can just use a math expression node with infinitely more flexibility." That's exactly what this is.

    How it works

    Under the hood it hands your text to the simpleeval library (simple_eval()), a deliberately sandboxed expression evaluator. That's an important detail: this is a safe calculator, not a code executor. You get standard arithmetic (+ - * / ** %), comparisons, and a handful of builtins like round(), min(), max(), and abs(), but no imports, no arbitrary Python, no way to poke at your filesystem. If someone shares a workflow with a scary-looking expression, the worst it can do is math.

    The inputs you actually set:

    • Expression - the multiline box where the math goes. It's a self-contained expression: plain numbers, operators, and the builtins below. There's no variable wiring - X and Y aren't magically defined, so don't type them expecting the pack's variable nodes to show up.
    • tips - a static label the author uses to warn "denominator can't be 0, or it errors." Ignore it, it's just decoration.

    Output is a single STRING socket. And here's the quirk worth knowing: the socket is typed STRING, but if your expression evaluates to a number, the value flowing out is a number. Most INT/FLOAT inputs downstream will happily accept it anyway, so in practice it doesn't bite - just don't be surprised if the widget preview shows a float.

    Where it earns its keep

    Wire it into anything numeric: compute a latent width from a height and aspect ratio, derive steps from a formula, scale a batch count, turn a CFG or strength into a computed value. To get a variable value into the expression, convert the Expression widget to an input and feed it a string built by a text node - clunky, but it works. You can also feed it from any numeric output by converting a widget to an input.

    Division by zero is the one real failure mode - simpleeval raises, and the node goes red with a traceback. The author's tip field literally tells you about it, which is your sign that it's the thing that bites people. Guard against it with (x + 1) in the denominator or just don't send a zero.

    Installing the pack

    • ComfyUI Manager: search "ComfyUI_AC_FUN" and install.
    • Manual:
    cd ComfyUI/custom_nodes
    git clone https://github.com/A719689614/ComfyUI_AC_FUN-V1.0
    

    Restart ComfyUI. The pack is by Cc啊程, a Bilibili creator and architect-turned-visual-programmer, and most of it is Chinese-named wrappers around core nodes - this is one of the few genuinely custom bits. No models to download. The only real dependency is simpleeval, a tiny pure-Python package. If you get a ModuleNotFoundError: simpleeval on first run, install it into ComfyUI's Python environment by hand:

    pip install simpleeval
    

    (The pack's requirements.txt technically lists it, but the line is mangled with a trailing = - fine on most pip versions, occasionally trips up Manager's installer.)

    Bottom line

    This is the pack's most genuinely useful node, and it's the one I'd reach for if I only kept a couple from this suite. One expression box replacing five math nodes is a readability win your future self will thank you for - and the sandboxing means you don't have to trust the workflow's author, only their arithmetic.

    Category🌌AC_FUNV1.0/🔯MathTool

    Inputs (2)

    NameTypeDefaultDescription
    ExpressionSTRING数学表达式
    tipsSTRING分母不能为0,不然会报错

    Outputs (1)

    NameTypeDescription
    STRINGSTRING