While Loop Open | akatz-loops
The Top of the Loop That ComfyUI Says You Can't Have
- initial_value0
- initial_value1
- initial_value2
- initial_value3
- initial_value4
- FLOW_CONTROL
- value0
- value1
- value2
- value3
- value4
Here's a sentence you'll find all over r/comfyui: "you can't loop in ComfyUI - it complains about the loop." And it's mostly true of the naive approach - feed a node's output back into its own input and ComfyUI just refuses the graph. WhileLoopOpen is one half of the pack's answer, built on a technique called execution inversion: instead of making the graph cycle, the loop re-expands itself - each iteration generates a fresh copy of the loop body, runs it, and does it again until a condition says stop. No cycles, no "Loop Errors", just repeated graph expansion that looks identical to you but is legal to the engine.
WhileLoopOpen is the top of the pair. It takes a condition (BOOLEAN, default true) and emits a FLOW_CONTROL socket plus up to five value0–value4 outputs. The condition decides whether the loop body runs; the FLOW_CONTROL wire is what marks the loop region. Everything downstream of that wire, up to the matching WhileLoopClose, is the body that gets re-expanded per iteration.
The value-passing trick that matters
This is the part people get burned by, and it's the pack's real contribution: the five value0–value4 outputs carry data out of the loop, and the matching inputs on WhileLoopClose (its initial_value0–initial_value4) carry the previous iteration's results back in for the next pass. So the classic complaint from the community - "you can't use the output as input for the next iteration" - is exactly what this mechanism solves: wire iteration N's result into the close node's initial_value, and it arrives at the next open as the value to work on.
Set the condition to a fixed true and the loop never terminates by itself - that's the infinite-loop footgun, and it will happily expand forever. In practice you drive the condition from something inside the body: a counter that increments each pass, a comparison that flips false when you hit your target, an ExecutionBlocker upstream. With WhileLoopOpen/WhileLoopClose, ComfyUI genuinely does not prove your loop terminates - it just runs until the condition says stop, so you are the termination guarantee.
Install
Part of Akatz-Loop-Nodes (akatz-ai). Via ComfyUI Manager, search "Akatz-Loop-Nodes"; or:
cd ComfyUI/custom_nodes
git clone https://github.com/akatz-ai/Akatz-Loop-Nodes
Restart ComfyUI. The pack's only dependency is opencv-python (for its image nodes), no model downloads. The custom_nodes.ComfyUI-Execution-Inversion module path on listings is the pack's ancestry from BadCafeCode's execution-inversion demo - you don't install that repo separately.
One real-world caveat from people running big loop workflows: deeply nested loops (loops inside subgraphs inside loops) can hit a maximum recursion depth exceeded error during prompt validation on recent ComfyUI builds. The community workaround that's been confirmed to fix it is installing ComfyUI-GraphConstantFolder, which prunes the validation traversal - no settings to change, just add it to custom_nodes and restart. Worth knowing before you blame your loop wiring.
Inputs (6)
| Name | Type | Default | Description |
|---|---|---|---|
| condition | BOOLEAN | true | — |
| initial_value0opt | * | — | |
| initial_value1opt | * | — | |
| initial_value2opt | * | — | |
| initial_value3opt | * | — | |
| initial_value4opt | * | — |
Outputs (6)
| Name | Type | Description |
|---|---|---|
| FLOW_CONTROL | FLOW_CONTROL | — |
| value0 | * | — |
| value1 | * | — |
| value2 | * | — |
| value3 | * | — |
| value4 | * | — |