Nodes/More Math/Mask math
ComfyUI Node

Mask math

Do everything to a mask that ComfyUI forgot

By mcDandy·Created about a year ago·Updated 3 days ago· 5
Mask math
  • V
  • F
  • Expression
  • stack
  • MASK
  • STACK
length_mismatcherror
batching0
remember_stackfalse

Masks are the fiddliest part of any inpainting or compositing workflow. ComfyUI has a handful of mask nodes, but the moment you want to feather an edge nonlinearly, combine three masks with per-pixel logic, or drive a mask from a formula, you're suddenly chaining a dozen nodes. Mask math from More Math lets you type it all into one expression instead.

What it actually does

The node runs your expression over the mask tensor, and because a mask is just a single-channel float image, you get the full image-toolkit here: X, Y, W, H, B for the batch, C for channel. Blend masks with I0*(1-F0)+I1*F0 (the default, same lerp pattern as the image node). Feather edges with smoothstep(x, e0, e1) or blur. Combine masks with tmax(I0, I1) or tmin instead of fighting two "Mask Composite" nodes. There's even dilate, erode, morph_open, and morph_close in the shared function library, plus remap for rescaling mask values - say, squashing a soft mask into a harder one.

The expression language itself has if/else and comparison operators, so if(I0>0.5, I1, I0) is a legitimate way to say "take mask 1 where mask 0 is bright". Since masks come back as 0–1 float tensors, clamp and smoothstep are your friends for keeping outputs valid.

Inputs and outputs

V holds the autogrow mask inputs (V0, V1, ...), F holds floats (F0, ...), and Expression is the formula. length_mismatch (default error, plus tile/pad) handles inputs with different batch sizes - worth knowing because masks from segmentation nodes often come back with a batch of 1 while your images have a batch of 8. batching controls frames per pass; leave it at 0 unless memory complains.

Outputs are MASK (a list) and STACK. Wire the mask into anything that takes a mask - KSampler denoise masking, an inpainting setup, or a "Mask to Image" node.

Installing it

Standard for this pack:

cd ComfyUI/custom_nodes
git clone https://github.com/mcDandy/more_math
cd more_math
pip install -r requirements.txt

Restart ComfyUI, or install "More math" from ComfyUI Manager. Dependency is just antlr4-python3-runtime on top of torch. No model downloads.

Where people trip up

Default length_mismatch is error, so the very first thing most people hit with this node is a batch-size mismatch crash when blending a mask with an image-derived tensor - flip it to tile or pad if that's expected. Also remember mask values live in 0–1; if you do I0*2 to brighten, clamp before sending it on or downstream nodes will treat >1 values oddly. And a quick tip from the pack's own design: when a tensor is expected, plain lists usually get promoted automatically, so [0.1, 0.2] can sometimes stand in for a vector where you'd expect a tensor.

The pack is young and solo-maintained, so expect the occasional sharp edge, but for mask wrangling this node genuinely replaces three or four nodes in most workflows.

CategoryMore math

Inputs (7)

NameTypeDefaultDescription
VCOMFY_AUTOGROW_V3
FCOMFY_AUTOGROW_V3
ExpressionSTRING,SYNTAX_TREEV0Expression to apply on input masks
length_mismatchCOMBOerrorHow to handle mismatched mask batch sizes. tile: repeat shorter inputs; error: raise error on mismatch; pad: treat missing frames as zero.
batchingINT0
remember_stackBOOLEANfalseIf enabled, stack is copied at output leading to changes being remembered during batch operations (node runs multiple times in sucession). If disabled each batch gets it's own copy of the stack.
stackoptSTACKAccess stack between nodes

Outputs (2)

NameTypeDescription
MASKMASK
STACKSTACK