Nodes/ComfyUI-XJNodes/Loop Start (XJ)
ComfyUI Node

Loop Start (XJ)

The for-loop ComfyUI kept promising you

By alexjx·Created 10 months ago·Updated 4 months ago· 0
Loop Start (XJ)
  • value1
  • value2
  • value3
  • value4
  • value5
  • value6
  • value7
  • value8
  • value9
  • value10
  • value11
  • value12
  • value13
  • value14
  • value15
  • value16
  • value17
  • value18
  • value19
  • value20
  • flow
  • index
  • value1
  • value2
  • value3
  • value4
  • value5
  • value6
  • value7
  • value8
  • value9
  • value10
  • value11
  • value12
  • value13
  • value14
  • value15
  • value16
  • value17
  • value18
  • value19
  • value20
total1

People have been asking for loops in ComfyUI since 2023, when the answer was basically "go write a custom node." XJNodes ships one of the few that actually work: Loop Start + Loop End. Loop Start is the top of that loop - you give it a total count and up to 20 values, and it begins an iteration that runs whatever sits between it and its matching Loop End.

The one-sentence mental model: anything between Loop Start and Loop End is the loop body, index tells you which pass you're on, and the 20 value slots are how you pass state around the loop - a seed that climbs each pass, an image that accumulates, a list you're consuming. If you've ever wanted to run a KSampler N times with the seed stepping each iteration without copy-pasting the sampler N times, this is the missing piece.

How it works

Under the hood it uses ComfyUI's subgraph expansion - each iteration, the loop body graph is actually reconstructed and re-executed, which is why this works at all in a system not designed for loops. The node tracks an index (0-based) internally, hidden from the UI, and Loop End decides whether to continue or bail out.

The feature the README is proudest of: lists are preserved as-is. A lot of loop implementations auto-expand lists - iterate a 5-image list and the loop silently runs the body 5 times, which is not what you wanted when you were accumulating into that list. XJNodes deliberately passes lists through intact, so you can accumulate images across iterations without them being exploded into individual loop passes. That's the difference between a loop and a trap.

total deserves one special note: set it to 0 and the loop body is skipped entirely, returning the initial values unchanged. Handy for "run 0 times" toggles.

Inputs and outputs

  • total - INT, 0 to 100,000, default 1. Number of iterations
  • value1 … value20 - up to 20 any-type slots. Initial values passed into the first iteration

Outputs: flow (the FLOW_CONTROL connection that feeds Loop End), index (the 0-based iteration counter - wire it into seeds, prompts, anything that should change per pass), and value1 … value20 (the per-iteration values).

Installing it

One node in the XJNodes pack. ComfyUI Manager → search "ComfyUI-XJNodes" → install → restart. Or:

cd ComfyUI/custom_nodes
git clone https://github.com/alexjx/ComfyUI-XJNodes
# restart ComfyUI

No model downloads, no dependencies beyond ComfyUI itself. Category: XJNodes/Flow.

Wiring it right

The pattern that works: Loop Start → wire flow and the values into your body nodes → body nodes feed Loop End → Loop End's outputs (which are the final values after all iterations) go wherever the result should land. The values also get fed back to Loop Start for the next pass automatically. The two nodes are a pair; a Loop Start without its Loop End is just an error waiting to happen. If you're accumulating images, keep the pack's list utilities in mind - a list in, a list through, a list out - that's the accumulation pattern the README calls out. And if your loop isn't behaving, check the index wiring first: the counter is the one thing every iteration depends on, and it's the first thing people wire wrong. Loop End has its own page and its own oddities - read that one too, because the exit half is where the real quirks live.

CategoryXJNodes/Flow

Inputs (21)

NameTypeDefaultDescription
totalINT10–100000
value1opt*
value2opt*
value3opt*
value4opt*
value5opt*
value6opt*
value7opt*
value8opt*
value9opt*
value10opt*
value11opt*
value12opt*
value13opt*
value14opt*
value15opt*
value16opt*
value17opt*
value18opt*
value19opt*
value20opt*

Outputs (22)

NameTypeDescription
flowFLOW_CONTROL
indexINT
value1*
value2*
value3*
value4*
value5*
value6*
value7*
value8*
value9*
value10*
value11*
value12*
value13*
value14*
value15*
value16*
value17*
value18*
value19*
value20*