ComfyUI Node

Mask Sub (UTK)

Carve one mask out of another — the 'except this part' operator

By whmc76·Created about a year ago·Updated 2 months ago· 72
Mask Sub (UTK)
  • mask1
  • mask2
  • mask

The "except" operation in masking is subtraction: cover everything in mask1, then remove whatever mask2 covers. Mask Sub (UTK) is mask1 - mask2 clamped to the 0–1 range - the "protect all of the person except their eyes" move, or "the whole background minus the window region." It's the third leg of the ComfyUI-UniversalToolkit mask trio, and it's the one that expresses exclusion.

If And says "both" and Add says "either," Sub says "first one, minus the second." That ordering matters: subtraction is not symmetric. mask1 - mask2 is not the same as mask2 - mask1, so which mask you put in which port is the entire difference between "everything except the face" and "the face minus everything else."

How it works

Under the hood: torch.clamp(mask1 - mask2, 0, 1), elementwise and batch-aware. The clamp keeps the result from going negative - a mask value of 0.8 minus 0.6 becomes 0.2, not −0.2. The practical effect: where mask2 is white, the output is forced to zero (fully subtracted); where mask2 is black, the output equals mask1 unchanged. Gray regions in mask2 partially subtract, which gives you feathered "subtract a bit" control if you want it. Like its siblings, it demands matching shapes and errors loudly if you feed it mismatched masks.

Inputs and outputs

  • mask1 (MASK) - the mask you're carving from.
  • mask2 (MASK) - the mask being removed. Order matters!

Single output: mask (MASK).

Install and gotchas

Install via ComfyUI Manager (search "ComfyUI-UniversalToolkit") or:

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

Restart ComfyUI. No models, no keys.

The two traps: getting the order backwards (the most common one - swap the ports and your "protect everything but the eyes" becomes "protect the eyes," which on a protect-mask pipeline inverts your whole result), and forgetting the clamp's side effect. If mask2 overlaps mask1 with gray values, subtraction makes those areas darker, not hard-zero - so a slightly gray mask2 that you thought of as "off" is actually eating away at the result. Check your mask2 isn't carrying near-white feathering you forgot about. For a beginner doing masked inpainting, Sub is how you say "inpaint the whole image, but leave my subject alone" - put the full image region in mask1 and the subject mask in mask2.

CategoryUniversalToolkit/Mask

Inputs (2)

NameTypeDefaultDescription
mask1MASK
mask2MASK

Outputs (1)

NameTypeDescription
maskMASK