Nodes/🐰 MaraScott Nodes/🐰 For Loop While Close - v1 /l
ComfyUI Node

🐰 For Loop While Close - v1 /l

The node that actually loops β€” how ComfyUI re-runs a subgraph

By MaraScottΒ·Created 3 years agoΒ·Updated 10 months agoΒ· 179
🐰 For Loop While Close - v1 /l
  • flow_control
  • initial_value0
  • initial_value1
  • initial_value2
  • initial_value3
  • initial_value4
  • value0
  • value1
  • value2
  • value3
  • value4
β—„conditionβ€”β–Ί

The standard For Loop Open/Close pair is sugar on top of two lower-level nodes, and For Loop While Close is the deeper one: the node that holds the actual "should I run again?" logic and rebuilds the loop body every pass. You rarely need to touch it directly - the high-level Close node wires it up for you - but if you're building a while-style loop by hand, this is the bottom half.

It pairs with For Loop While Open. Open declares the loop and gives you a flow_control wire; Close receives it, looks at the condition BOOLEAN, and either finishes or re-runs.

How it works

This is where ComfyUI's dynamic graph expansion does its thing. On each pass, While Close walks the workflow's dependency graph to find every node sitting between the Open and Close nodes, builds a fresh copy of that whole subgraph, re-wires it, and - if the condition is still true - runs it again with the carried values from the previous pass. When condition is false it stops and emits the final values. That's why loops in this pack work at all: ComfyUI's normal execution has no cycle support, so this node fakes the cycle by cloning the graph.

Inputs: flow_control (required, from the Open node), condition (required BOOLEAN - forceInput, so wire it from a real node rather than a widget), plus initial_value0…initial_value4 (any type, the carried values). Outputs: value0…value4, the values after the loop finishes.

Why you'd hand-build one

The high-level Close always decrements and loops until zero - a "for" loop. If you want a while loop (loop until some condition is true, however many passes that takes), you build it yourself: While Open β†’ your body β†’ a condition check (say For Loop ToBoolNode on a value that changes each pass) β†’ While Close. The condition input is where your stopping rule plugs in. It's more setup, but it's the difference between "run N times" and "run until done."

Installing it

Ships in the MaraScott pack: ComfyUI Manager β†’ search "MaraScott" β†’ Install β†’ restart. Or:

cd ComfyUI/custom_nodes
git clone https://github.com/MaraScott/ComfyUI_MaraScott_Nodes

Then restart. The pack's install is on the heavy side (transformers==4.37.2 pinned, opencv-python, numba, blend_modes) but this node itself needs none of those at runtime.

Common issues

Graph expansion is a modern-ComfyUI feature; if the loop silently does nothing or errors, update ComfyUI core first. The condition input being forceInput trips people up - you can't just type a value in, you must wire a BOOLEAN source. And the classic loop hazard: if your condition never turns false, you get an effectively infinite expansion that visibly grinds the queue. When hand-building while loops, always make sure some node inside the body is changing the thing your condition reads, or you'll be cancelling the queue a lot.

Category🐰 MaraScott/Loop/Flow

Inputs (7)

NameTypeDefaultDescription
flow_controlFLOW_CONTROLβ€”
conditionBOOLEANβ€”
initial_value0opt*β€”
initial_value1opt*β€”
initial_value2opt*β€”
initial_value3opt*β€”
initial_value4opt*β€”

Outputs (5)

NameTypeDescription
value0*β€”
value1*β€”
value2*β€”
value3*β€”
value4*β€”