Nodes/ComfyUI-Practical-Tools/Mask Batch Replace Empty
ComfyUI Node

Mask Batch Replace Empty

Fill the empty masks in a batch with the last good one — Mask Batch Replace Empty

By wenchengxiang·Created about a month ago·Updated a day ago· 1
Mask Batch Replace Empty
  • masks
  • MASK

You're running a detection loop over a video - a face tracker, a segmenter - and the detector just gave you 120 masks. Eleven of them are black. The subject turned away, the frame went blurry, the model had a moment. Feed those empties into an inpainting pass and you get wasted runs on frames where nothing is actually masked. wcx_MaskBatchReplaceEmpty is the janitor for exactly this: it sweeps through a mask batch and replaces every empty mask with the most recent valid one.

The mechanism is a simple carry-forward. It walks the batch in order, and any mask whose mean pixel value is at or below 0.001 - effectively black, i.e. nothing masked - gets overwritten with the last mask that was valid. Content frames keep their own masks; the empties inherit whatever the previous good frame produced. If no frame in the batch is valid at all, the batch passes through unchanged.

Input is a single masks (MASK batch), output is MASK - the same batch with the empties patched.

Where this is genuinely the right tool:

  • Detector gaps in a batch pass. The KB's masking doc is full of detector failure modes - a face lost for a few frames is the classic. Instead of a blank mask silently no-op'ing the inpaint, the frame gets the previous mask, so the region still gets its detail pass.
  • Batching with a stricter joiner downstream. If the next step (like this pack's own MaskBatch) requires consistent masks, pre-patching the empties keeps the batch honest.

The caveats are the shape of all carry-forward logic:

  • It duplicates, it doesn't skip. The empty frames get a mask - the previous frame's. If the subject moved between frames, that mask is wrong, just less wrong than nothing. This is a "better than a crash, worse than a retry" tool.
  • The empty test is a mean, not a total. Any frame averaging above 0.001 is treated as valid, even a nearly-empty one with a few pixels. Fine in practice, but it means "empty" is fuzzy at the edges.
  • The first valid mask sets the tone. Everything before the first good frame inherits that mask too, so a detector that fails on frames 0–5 will stamp frame 6's mask all over the openers.

Standard light install, no dependencies:

# ComfyUI Manager: search "ComfyUI-Practical-Tools" → Install → restart

# or manually:
cd ComfyUI/custom_nodes
git clone https://github.com/wenchengxiang/ComfyUI-Practical-Tools
# restart ComfyUI

It's the kind of small node that nobody mentions in the workflow shares, but it's the difference between a 120-frame batch that runs clean and one you have to babysit frame by frame.

CategoryPractical-Tools/Mask

Inputs (1)

NameTypeDefaultDescription
masksMASK

Outputs (1)

NameTypeDescription
MASKMASK