Nodes/ComfyUI-CustomNodePacks/Conditional Switch
ComfyUI Node

Conditional Switch

The ternary operator, as a node — pick between two values of any type based on a boolean

By Code2Collapse·Created 6 months ago·Updated a day ago· 52
Conditional Switch
  • value_true
  • value_false
  • out
conditiontrue

If you've written code, you know the ternary operator: result = condition ? a : b. ConditionalSwitchMEC is exactly that, as a ComfyUI node - and the useful part is that both inputs are wildcard *, so it works on images, latents, strings, models, masks, whatever you throw at it. One boolean, two values, one output. That's the whole node.

Why do you need a ternary in a node graph? Same reason as in code: routing. You have a settings widget (preset_enabled), and downstream you want either the preset value or the manual value depending on it. You have a batch of frames and want to conditionally pick the upscaled branch or the raw branch. Or you're building a workflow that doubles as a template with a "fast mode" toggle that switches between two different model paths. Anywhere you'd write if in a script, this node gives you the graph equivalent.

How it works

It's almost suspiciously simple - the pick function in nodes/helpers/helpers.py does literally value_true if bool(condition) else value_false. No caching, no cleverness, no magic. The condition is a boolean widget (or wired from a comparison/probe node), and value_true and value_false are wildcard sockets that accept anything.

The inputs and output

  • condition - a BOOLEAN, default true.
  • value_true - any type (*).
  • value_false - any type (*).

Output is out (*), which carries whatever type the chosen branch was.

Installing it

Part of Code2Collapse/ComfyUI-CustomNodePacks. ComfyUI Manager → search "CustomNodePacks", or:

cd ComfyUI/custom_nodes
git clone https://github.com/Code2Collapse/ComfyUI-CustomNodePacks.git

Restart ComfyUI. No dependencies, no models - it's one function call. (Same pack-wide warning: don't pip install -r requirements.txt over ComfyUI's bundled torch/numpy.)

Gotchas

Here's the thing nobody tells you the first time: both branches execute. ComfyUI computes everything upstream of a node before the node runs, so wiring a heavy model path into value_false and expecting it to be skipped when condition is true will not save you compute - both sides still run, and you just waste VRAM/time. The node routes values, it doesn't skip work. That's the single biggest misunderstanding with every switch node, and this one is no exception.

Second: since it's a wildcard node, both sockets must be connected with a consistent type for downstream nodes to accept the output - ComfyUI can't always infer the type through a wildcard, and you may need to drop a Reroute or type-stabilizing node in between. And keep the branches genuinely equivalent in type; switching an IMAGE with a STRING makes the graph technically happy but nothing downstream will be. For an actual lazy branching node that doesn't compute the unused path, you want a scheduler/conditional-execution setup, not this - but for a plain "pick which value flows through," it's the cleanest two-wire answer in the pack.

CategoryC2C/Helpers

Inputs (3)

NameTypeDefaultDescription
conditionBOOLEANtrue
value_true*
value_false*

Outputs (1)

NameTypeDescription
out*