SmartMask
The guard that inverts a dead-black mask
- mask
- MASK
The name oversells it. SmartMask contains no intelligence - it contains a single if statement. But that if statement rescues a surprisingly common failure mode. Mask-producing nodes (segmentation models, depth estimates, background removers) sometimes hand you an all-black mask, and if you pipe that into an inpainting step, the model regenerates nothing and you stare at a wasted run wondering what broke. SmartMask checks for that and flips the mask when it happens.
It's part of comfyui_davcha, the author's "personal QoL and experimental nodes" pack. This is the "QoL" in its purest form.
How it works
The node sums every pixel of the incoming mask. If the sum is greater than zero - meaning there's at least some white, some region to act on - it passes the mask through untouched. If the sum is exactly zero, the mask is all black, and the node returns the inverted mask instead, which is fully white. Instead of inpainting nothing, you get the whole image as the masked region.
Input: mask (MASK). Output: MASK, unchanged or inverted.
When you'd reach for it
- A safety valve after flaky detectors - put it between a segmentation/background-removal node and your inpainting step so a failed detection becomes "regenerate everything" rather than "silently do nothing."
- Guarding batch runs - in an automated or batched workflow, one dead mask shouldn't sink the whole run.
- Asserting that a mask exists - as a cheap sanity gate before an expensive sampler.
Installing it
# ComfyUI Manager → Install Custom Nodes → search "comfyui_davcha" → Install → Restart
# or manually:
cd ComfyUI/custom_nodes
git clone https://github.com/dchatel/comfyui_davcha
cd comfyui_davcha
pip install -r requirements.txt
Standard pack catch: nodes.py imports llama_cpp and cv2 at module load while requirements.txt only lists webp and rapidfuzz. Pack missing from your menu? pip install llama-cpp-python opencv-python and restart.
Two honest caveats. First, "invert a dead mask" is a guess, not a recovery - the inverted mask makes the sampler regenerate the whole image, which is a loud failure you can notice, not a fix for whatever made the mask empty. Second, an all-white mask passes through too, which is correct behavior but means a genuinely blank-white input isn't caught either. It's a tripwire with a sensible default, and that's exactly the right scope for it.
Inputs (1)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| MASK | MASK | — |