Nodes/ComfyPanel/ImageMask_SwitchAuto
ComfyUI Node

ImageMask_SwitchAuto

Let the graph pick the first branch that actually has content

By Ginolazy·Created 6 months ago·Updated 2 days ago· 23
ImageMask_SwitchAuto
    • IMAGE
    • MASK

    ImageMask_SwitchAuto is the "don't make me choose" switch: it takes any number of image-and-mask inputs and automatically passes through the first one that actually has a value. No selector, no boolean, no clicking. You feed it several alternative branches, and whichever one is live wins. It's the classic first-non-null fallback switch - the same shape rgthree's Any Switch made famous, per the KB's node-plumbing doc - specialized for image+mask pairs.

    The key word in that description is automatic, and it's worth being precise about what "first" means. The node has no required inputs in its schema; instead the frontend gives it dynamic ports - image_1/mask_1, image_2/mask_2, and so on - and you wire up as many candidate branches as you have. At runtime it walks those inputs in order, and the first image_N that isn't None is the one that comes out, along with its matching mask_N. The mechanism is dead simple: the Python side just iterates the image_* kwargs it received and returns the lowest-indexed non-null one.

    So the graph does the choosing, and you control the rules by how you build the branches upstream. The canonical use is a chain of increasingly-processed alternatives: "use the refined mask if a refinement ran; otherwise fall back to the raw mask; otherwise pass through the original image." Each upstream branch produces None (or stays disconnected) when it's not applicable, and the switch hands you the first branch that has anything. That's the null-output placeholder pattern the KB describes - a branch deliberately emits nothing to leave the input unconnected at runtime, and the fallback logic skips it.

    When this node shines: conditional pipelines where "is this stage active?" is a runtime question, not a design-time one - an optional detail pass, an optional upscale, a source that might or might not have been segmented. When to avoid it: when you actually want to select between two equally-live branches. A fallback switch can't A/B - if both inputs have content it always returns the first one, and there's no way to point it at the second. For explicit choice you want the boolean-driven ImageMask_Switch in this same pack.

    The outputs are just IMAGE and MASK. No settings on the node, nothing to tune. If none of the inputs are connected at all, it returns (None, None), so guard against expecting a fallback where no fallback exists.

    Installing it

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

    or ComfyUI Manager → "ComfyPanel". No models, standard deps. One thing to know: the dynamic ports are added by the pack's frontend extension, so if you see only the outputs and no inputs after install, the JS extension didn't load - hard-refresh the browser, and check that ComfyUI fully restarted after the clone.

    CategoryComfyPanel/Image & Mask

    Inputs (0)

    No inputs

    Outputs (2)

    NameTypeDescription
    IMAGEIMAGE
    MASKMASK