Nodes/ComfyUI-Custom-Scripts/Math Expression 🐍
ComfyUI Node Runs on cloud

Math Expression 🐍

Do arithmetic on graph values without a chain of Add/Multiply nodes

By pythongosssssΒ·Created 3 years agoΒ·Updated 6 months agoΒ· 3,150
Math Expression 🐍
  • a
  • b
  • c
  • INT
  • FLOAT
β—„expressionβ€”β–Ί

Sooner or later every ComfyUI graph needs a derived number - half your resolution for a second pass, a seed offset, steps scaled by some percentage, a latent dimension pulled straight off an image. The stock way to do that is a chain of Add/Multiply/Divide nodes, which works but eats canvas space fast and gets unreadable the moment you nest a few of them. Math Expression is the pack's answer: one node, one text box, type the formula.

It's part of ComfyUI-Custom-Scripts, the utility-node pack from pythongosssss - one of the six people who founded Comfy Org in June 2024 when the project split from Stability AI. This isn't the flashiest node in his pack, but it's one of the more genuinely useful ones if you build anything with computed values.

How it works

Type an expression (a multiline string) and wire up to three arbitrary values into a, b, and c - the README is specific that these can be INT, FLOAT, IMAGE, or LATENT. Supported operators cover the basics you'd expect: + - * / plus // for floor division, ** for power, ^ for xor, and % for mod. There are also a handful of functions: floor(num, dp?), ceil(num), and randomint(min, max).

The IMAGE/LATENT support is the clever bit - you don't need a separate node to extract dimensions first. If a is a LATENT or IMAGE, you can reference a.width or a.height directly inside the expression. So a.width // 2 gets you half the width of whatever image you wired in, no intermediate nodes required.

You can also reach outside the three named inputs entirely: the README describes referencing another node's widget value directly, using the name set in that node's Node name for S&R property (found in the Properties panel), or its visible title. That's a genuinely powerful shortcut for building formulas that track a value set somewhere else in your graph without wiring a wire for it.

The node computes both an INT and a FLOAT output simultaneously from the same expression - wire whichever one the downstream node actually wants and ignore the other. It's also flagged as an output node, so it'll happily preview the result on the node itself, which doubles as a sanity check while you're tuning the formula.

Math Expression vs. the newer alternative

If you've come across rgthree-comfy, its Power Puter node does something similar and more - full python-like expressions plus the ability to pull LoRA names and trigger words straight out of a Power Lora Loader. It's newer (2025) and flashier. But if all you need is arithmetic across a couple of graph values, Math Expression is the smaller, older, more focused tool, and it's been doing exactly this since before Power Puter existed. No reason to install a second pack just for basic math.

Installing it

  • ComfyUI Manager - search "ComfyUI-Custom-Scripts", install, restart.
  • Manual - cd ComfyUI/custom_nodes && git clone https://github.com/pythongosssss/ComfyUI-Custom-Scripts, restart.

No models, no extra dependencies.

Common issues

Expression errors on execution, not while typing. ComfyUI doesn't validate the formula as you write it - you'll only find out it's broken when you queue a run. Do a cheap test execution before you build three more nodes on top of the output.

Referenced a node by title and it broke after a rename. Titles change; the Node name for S&R property doesn't unless you touch it. If you're relying on cross-node references and plan to rename things later, set that property explicitly instead of leaning on whatever the title happens to say.

Fed an IMAGE or LATENT straight into an arithmetic operator. You can't do math on a tensor directly - pull .width or .height out first. Treating a as a number when it's actually an image throws an error, not a silent wrong answer, so this one's at least easy to spot.

Categoryutils

Inputs (4)

NameTypeDefaultDescription
expressionSTRINGβ€”
aopt*β€”
bopt*β€”
copt*β€”

Outputs (2)

NameTypeDescription
INTINTβ€”
FLOATFLOATβ€”