if/elif/.../else
Real if/elif/else, without contorting your graph into a switch
- then
- then_0
- else
- result
This is the closest thing ComfyUI has to a real if/elif/.../else statement, and it's the node people recommend when a beginner asks "how do I route to different paths based on a condition?" - search the subreddit and it comes up in exactly those answers. It's the flow-control workhorse of Basic data handling, StableLlama's zero-dependency pack: a boolean if, a then value, any number of elif_0/then_0 pairs, and a final else. Pick the first condition that's true, hand back its value.
How it works
The node evaluates if first. If true, it returns then and stops. If false, it walks the elif pairs in order - elif_0 and its then_0, then elif_1/then_1, and so on - returning the first then whose condition is true. If nothing matched, it returns else.
The clever part is lazy evaluation. The node's check_lazy_status only asks ComfyUI to compute the branches it actually needs. If if is true, it requests then and never touches any elif or the else. If no condition matches, it requests only else. That means you can put expensive operations in the branch values (a whole image load, say) and the ones you don't take cost you nothing. This is the mechanism that makes conditional routing in ComfyUI practical instead of wasteful.
The inputs and the output
if(BOOLEAN) - the first condition. Wire it from a comparison node, a boolean operation, an IsNull - anything producing a real boolean.then(ANY) - the value returned whenifis true.elif_0(BOOLEAN) andthen_0(ANY) - dynamic pairs; add more with "+". Evaluated in order afteriffails.else(ANY) - the fallback when every condition is false.result(output, ANY) - whichever branch value won.
Installing it
Install Basic data handling from ComfyUI Manager (search the pack title), or:
cd ComfyUI/custom_nodes
git clone https://github.com/StableLlama/ComfyUI-basic_data_handling
Restart ComfyUI. No Python dependencies, no models.
Troubleshooting
- "It always picks the false/else branch." This is a real, reported issue with this family of nodes - someone on r/comfyui was burning through a workflow where
if/else"seems to always be choosing the false branch." The usual culprits: (1) the condition input isforceInput, so it wants a wired BOOLEAN, not a toggle you eyeball - if you think it's connected but it isn't, it reads as false and you silently land inelse; (2) the BOOLEAN feedingifisn't the value you think it is (anotnode in between, or a comparison with the operands swapped). Trace the wire, confirm the boolean, and remember: with nothing true and noelseconnected, you getNone. - "Two conditions are both true and it picks the wrong one." It picks the first true branch, top to bottom - that's if/elif semantics. Reorder to put the most specific condition first.
- "I want a switch, not a ladder." If you're choosing by an index rather than conditions, that's the pack's
switch/casenode. Different tool. - Don't conflate with IsNull. If you want "when this value is missing, use a fallback," chain IsNull into
ifand put the fallback inthen. Theelsebranch is for "value exists," which is the opposite of what feels natural.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| if | BOOLEAN | — | |
| then | * | — | |
| elif_0opt | BOOLEAN | — | |
| then_0opt | * | — | |
| elseopt | * | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| result | * | — |