Mask math
Combine and feather masks with an expression, not a chain of Mask ops
- a
- b
- c
- d
- MASK
Mask math runs a math expression over up to four masks. The default a*(1-w)+b*w is a mask blend, but the real win is that you can write the operations you'd otherwise assemble from three separate mask nodes in one line: clamp(a+b,0,1) for a union, a*b for an intersection, 1-a for an inversion. Masks are single-channel, values 0–1, and this node treats them as plain tensors - so any expression you can write, you can apply to a matte.
How it works
A MASK in ComfyUI is a (batch, height, width) float tensor. The node evaluates your expression elementwise, with a, b, c, d bound to the input masks and w, x, y, z as float sliders. You also get the positional variables - X/Y, W/H, B, T - plus the ability to call the pack's filter functions right inside the expression: blur(x, sigma), dilate(x), erode(x), morph_open, morph_close. So a feathered, eroded matte is one expression rather than a node chain. That's the entire appeal: masks are boring tensor math, and this node lets you treat them like it.
The length_mismatch combo handles different batch sizes - tile, error (default), or pad.
The inputs that matter
a- required mask.b,c,doptional.Mask- the expression. Defaulta*(1-w)+b*w.length_mismatch-tile/error/pad.w,x,y,z- float knobs.
Output is a single MASK.
Installing it
It's part of More Math (mcDandy/more_math). ComfyUI Manager - search "More Math" - or:
cd ComfyUI/custom_nodes
git clone https://github.com/mcDandy/more_math
cd more_math
pip install -r requirements.txt
Restart. Dependencies: antlr4-python3-runtime and torch, no downloads. As with the whole pack, keep ComfyUI current or the nodes won't load.
Where people get burned
The usual suspect: values outside 0–1. Masks are expected to stay in that range, and a+b on two bright masks overflows to 2.0. Wrap everything that can exceed in clamp(...,0,1), and remember a*b is the safe intersection because it can never exceed either input.
Second, length_mismatch defaults to error. A single mask against a batch of masks throws a ValueError - set tile when you deliberately want the one repeated across the batch (a common pattern in video compositing, where you apply one static matte to every frame).
The classic form here is deprecated in favor of an autogrow "Mask math" sibling with the same name and more inputs - both ship. If you see two "Mask math" entries in the menu, the autogrow one is the one to build on.
Inputs (10)
| Name | Type | Default | Description |
|---|---|---|---|
| a | MASK | — | |
| Mask | STRING | a*(1-w)+b*w | Expression to apply on input masks |
| length_mismatch | COMBO | error | How to handle mismatched mask batch sizes. tile: repeat shorter inputs; error: raise error on mismatch; pad: treat missing frames as zero. |
| bopt | MASK | — | |
| copt | MASK | — | |
| dopt | MASK | — | |
| wopt | FLOAT | 0.00 | — |
| xopt | FLOAT | 0.00 | — |
| yopt | FLOAT | 0.00 | — |
| zopt | FLOAT | 0.00 | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| MASK | MASK | — |