Nodes/Image Processing Suite for ComfyUI/Mask Mathematics (AnotherUtils)
ComfyUI Node

Mask Mathematics (AnotherUtils)

Add, Subtract, AND and OR Your Masks Like It's Boolean Algebra

By marcoc2·Created 2 years ago·Updated 5 months ago· 1
Mask Mathematics (AnotherUtils)
  • mask_a
  • mask_b
  • MASK
operationmultiply
value1.00

Masks are the duct tape of ComfyUI - every serious workflow ends up combining, inverting, or blending them. "Add the face region to the hand region." "Subtract the background mask so it doesn't leak into the subject." "Where do these two segmentations overlap?" There are a dozen dedicated mask-pack nodes for this, but if you want a single, honest little node that does six operations on two masks, AnotherMaskMath is it. No fancy UI, no learning curve, just math.

It's from AnotherUtils (marcoc2/ComfyUI-AnotherUtils), and like most of the pack it's pure PyTorch - a few tensor ops wrapped in a dropdown. You'll find it most useful in the middle of a segmentation pipeline, right after the pack's depth or SAM2 outputs hand you raw masks.

How it works

You give it a primary mask (mask_a), pick an operation, and then either a second mask (mask_b) or a plain scalar value. The math is what you'd hope:

  • multiply - both masks, so only the overlap survives. The default, and the most useful.
  • add - union, but the values can exceed 1 (it's real addition, not a clamp), so it's better for accumulating coverage than for a clean union.
  • subtract - mask_a minus mask_b (or minus the scalar). Great for carving a background mask out of a subject mask.
  • and - element-wise min: the true boolean AND, always clamped to the overlap.
  • or - element-wise max: the true boolean OR, union without overflow.
  • xor - symmetric difference: regions covered by exactly one mask. Handy for finding what changed between two segmentations.

The scalar path matters more than it looks: mask_a + and with value is a threshold, add with a value pushes whole-mask brightness, and multiply with a fractional value dims a mask's feathering. One node, six operations, two modes.

The inputs

  • mask_a - required, the primary mask.
  • operation - the six-way dropdown, default multiply.
  • mask_b - optional; if you don't connect it, the node uses value instead.
  • value - optional float (0–10, default 1). The scalar stand-in for mask_b.

What comes out

A single MASK tensor, same size as your inputs. It feeds straight into a compositor, an inpaint region, a conditioning mask, or the pack's Animated Composite Masked node.

Installing it

Standard AnotherUtils install:

cd ComfyUI/custom_nodes
git clone https://github.com/marcoc2/ComfyUI-AnotherUtils.git

Restart ComfyUI, or search "AnotherUtils" in ComfyUI Manager. No models, no dependencies.

Where people get burned

The two traps are both about expectations. First: add doesn't clamp, so "add two masks" can give you a hot, over-bright region - if you wanted a clean union, use or. Second: the scalar path silently takes over whenever mask_b is disconnected, which can produce mystifying results if you thought you connected a second mask but the wire didn't land. And the classic segmentation mistake this node exists to fix: don't feed a soft, feathered depth mask into subtract and expect crisp edges - masks you subtract should be thresholded first, or you'll get a gray smudge instead of a clean cut.

CategoryAnotherUtils/utils

Inputs (4)

NameTypeDefaultDescription
mask_aMASK
operationCOMBOmultiply6 options: multiply, add, subtract, and, or, xor
mask_boptMASK
valueoptFLOAT1.000–10

Outputs (1)

NameTypeDescription
MASKMASK