Nodes/ComfyUI_EmAySee_CustomNodes/Math Expression (EmAySee)
ComfyUI Node

Math Expression (EmAySee)

Max(w, h) and friends — the mini calculator that speaks Python

By EmAySee·Created about a year ago·Updated 4 months ago· 2
Math Expression (EmAySee)
    • INT
    expressionmax(w, h)
    w0
    h0

    ComfyUI's math nodes give you add, subtract, multiply, divide - one operation per node, and they pile up fast. EmAySee Math Expression collapses all of that into a single text box where you type an actual expression: max(w, h), round(w * 1.5), min(1024, h + 256). One node, one line, done.

    It's built around two integer inputs, w and h, and evaluates your expression with them in scope. The default is literally max(w, h), and that tells you what the author designed it for: image and latent dimension math. Get an image's size, drop width and height into w/h, and compute the resolution you want to feed a sampler, an upscaler, or a resize node - all in one place instead of a chain of math nodes.

    The genuinely nice touch is the environment it evaluates in. The source (py/EmAySee_MathShit.py) exposes w, h, max, min, abs, pow, round, and the full math module, with __builtins__ stripped out. So you can write math.ceil(w / 8) * 8 to round up to a model-friendly multiple of 8, or int(math.sqrt(w * h)) for a diagonal. Because it evaluates a restricted subset rather than importing a full language, it stays a calculator instead of becoming a code execution hole - though "restricted eval" still means "don't paste expressions from strangers into it."

    How it works

    The node passes your expression string to Python's eval() inside a sandboxed namespace (builtins disabled, only the whitelisted names available), then coerces the result to an int and returns it. If the expression throws - bad syntax, a name you forgot to define - it prints the error to the console and returns 0 rather than crashing the workflow. That silent 0 is the one behavior that will surprise you: a typo doesn't scream, it just quietly zeroes your output. If downstream math suddenly collapses to nothing, check the console.

    The inputs that matter

    • expression - the formula, multiline enabled, default max(w, h).
    • w, h - the two integers available inside the expression. Both default to 0; feed them from an image-size node or a resolution calculator.

    Output is INT - the evaluated result, always an integer.

    Install

    Ships in the EmAySee pack, a one-author 60+ node collection with no extra dependencies:

    cd ComfyUI/custom_nodes
    git clone https://github.com/EmAySee/ComfyUI_EmAySee_CustomNodes
    

    Restart ComfyUI (or Manager → "ComfyUI_EmAySee_CustomNodes"). It lands under the plain EmAySee category.

    Gotchas

    Two things to keep in mind. First, the result is always an int - if you need a float out (for a denoise strength or a weight), this isn't the node. Second, it only knows w and h plus the whitelisted functions; there's no variable for a batch count or a third number, so anything more complex than two-variable math means reaching for a different math utility. It's the right tool for dimension arithmetic and a frustrating one for everything else. And as with everything in this pack, it's unsupported hobbyware - the README says as much - but for a max(w, h) you really can't go wrong.

    CategoryEmAySee

    Inputs (3)

    NameTypeDefaultDescription
    expressionSTRINGmax(w, h)
    wINT00–8192
    hINT00–8192

    Outputs (1)

    NameTypeDescription
    INTINT