Nodes/ComfyPanel/SwitchMaskAuto
ComfyUI Node

SwitchMaskAuto

The mask that knows when you actually painted something

By Ginolazy·Created 6 months ago·Updated 2 days ago· 23
SwitchMaskAuto
  • mask_a
  • mask_b
  • mask

Most mask switches ask you to flip a boolean and remember what it means. SwitchMaskAuto decides for you, based on whether a mask actually contains anything: it looks at two masks, detects which one has real painted content, and returns that one. It's built for the "did the artist actually edit this?" problem that shows up constantly in inpainting and Photoshop-driven workflows.

The motivating case comes from ComfyPanel's own design - a Photoshop bridge where you're painting masks live. When you paint a new stroke, that mask suddenly contains non-uniform pixels. When you haven't touched it, it's a flat, single-value tensor. This node is the automated referee between the "freshly edited" mask and the "last good" one.

How it works

Two optional MASK inputs - mask_a and mask_b - and one mask output. The logic is short and worth reading:

  • If only one mask is connected, that one wins.
  • If both are connected, it checks whether mask_b is "edited" - meaning it contains more than one unique pixel value. If mask_b has been painted (multiple unique values) and mask_a is flat, mask_b wins.
  • Otherwise mask_a wins.

So the general rule you can rely on: plug your freshly painted mask into mask_b, and it takes over the moment it stops being blank. mask_a is your fallback - the previous mask, the default region, whatever you want to keep until real edits show up.

Why "unique values" matters

The edited-detection is purely about pixel content. A mask that's entirely black (all zeros) or entirely white (all ones) has exactly one unique value, so it counts as unedited and loses to the other mask. Only a mask with actual variation - a stroke, a gradient, a soft edge - counts as edited. That's a clever heuristic and a subtle one: it can't tell the difference between "I painted this mask all white deliberately" and "this mask was never touched." Both are single-value, so both are treated as blank. For most workflows that's the right behavior; just know it's judging pixels, not your intentions.

Where it fits

It's the mask counterpart to the pack's SwitchAuto: an automatic fallback that removes the manual toggle from a very specific decision. In an inpainting pipeline it's handy when you want "use the current painted mask, but if it's empty, fall back to the previous one." If you find yourself needing explicit control instead of heuristics, the pack's plain ImageMask_Switch is right there - but this one is what you want when the graph should be self-driving.

Installing it

It ships in ComfyPanel:

cd ComfyUI/custom_nodes
git clone https://github.com/Ginolazy/ComfyPanel

Restart ComfyUI, or search ComfyPanel in ComfyUI Manager. Standard requirements (torch/numpy/Pillow plus kornia, scipy, opencv-python), no model downloads, and no need for the Photoshop plugin to use it standalone.

CategoryComfyPanel/Image or Mask

Inputs (2)

NameTypeDefaultDescription
mask_aoptMASK
mask_boptMASK

Outputs (1)

NameTypeDescription
maskMASK