ComfyUI Node

any math

One expression, up to four inputs, image/mask/value out

By 807502278·Created 2 years ago·Updated 11 months ago· 20
any math
  • a
  • b
  • c
  • d
  • image1
  • mask1
  • value1
expressiona+b
PreprocessingTensortrue
RGBA_to_RGBtrue
image_clamptrue

any math is the simpler, single-expression version of any_math_v2 in this same pack: four inputs instead of ten, one expression and one result set instead of three. If you only need to run one formula against a couple of images or values, this is the leaner node to reach for - any_math_v2 exists for when you need more variables or want several results computed from the same graph in one node instead of three.

The inputs and outputs that matter

You write your formula into expression (default a+b), referencing whichever of a, b, c, d you've wired in - all four are typed as "any," so they'll happily accept images, masks, or plain values. Masks get treated as 3-channel images for the math, so mixing an image and a mask in the same expression works without a manual conversion step first. The node supports a subset of plain Python methods inside the expression too, which opens the door to more than pure arithmetic, but it also means you need to pay attention to what type your expression actually returns.

Three toggles shape how inputs get prepped before your expression runs. PreprocessingTensor (on by default) unifies batch size, resolution, and channel count across your inputs - the setting to check first if you're mixing images and masks or two differently-sized images and getting a shape error. RGBA_to_RGB converts any 4-channel input down to 3-channel automatically. image_clamp clips the result to the 0–1 range - turn it off if you're feeding this node's output straight into another math node afterward, since an early clamp can throw away values a second pass needs.

Outputs are image1, mask1, and value1 - routed the same way as on the v2 node: whichever type your expression actually evaluates to is the output that gets populated, and the other two come back empty. An expression that resolves to a tensor gives you an image; one that resolves to a plain number lands in value1 instead.

Installing it

Comes bundled with ComfyUI-WJNodes. ComfyUI Manager: search "ComfyUI-WJNodes," install, restart. Manual:

cd ComfyUI/custom_nodes
git clone https://github.com/807502278/ComfyUI-WJNodes.git

Find it under WJNode → Math, right next to any_math_v2. No models, no extra dependencies for this node.

Where people get burned

Start here rather than v2 if your task is genuinely one formula and a handful of inputs - it's less to configure and easier to debug when something goes wrong. Reach for any_math_v2 only once you need a fifth input or more, or want multiple independent results computed from the same set of inputs in one node instead of chaining several of these together. If an expected output comes back blank, check whether your expression is actually producing a tensor rather than a scalar - simplify to something like just a on its own to confirm your inputs are wired correctly before layering complexity back in.

CategoryWJNode/Math

Inputs (8)

NameTypeDefaultDescription
expressionSTRINGa+b
PreprocessingTensorBOOLEANtrue
RGBA_to_RGBBOOLEANtrue
image_clampBOOLEANtrue
aopt*
bopt*
copt*
dopt*

Outputs (3)

NameTypeDescription
image1IMAGE
mask1MASK
value1*