Skip to content

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.

Carry every item of a heterogeneous list through a loop (#16345)

Lukas Bucka84f954bc645
diff --git a/comfy_extras/nodes_loop.py b/comfy_extras/nodes_loop.py
index 8a6242049..566207834 100644
--- a/comfy_extras/nodes_loop.py
+++ b/comfy_extras/nodes_loop.py
@@ -218,7 +218,7 @@ class StartLoop(io.ComfyNode):
close_id,
values,
list_items,
- initial_iteration_value[0] if initial_iteration_value else None,
+ loop["inputs"].get("initial_iteration_value"),
_cache_enabled(cache_iterations),
)
close = dynprompt.get_node(close_id)
@@ -252,7 +252,13 @@ class LoopIteration(io.ComfyNode):
io.AnyType.Input("current_iteration_value", optional=True),
io.Boolean.Input("reuse_cache"),
],
- outputs=[io.Int.Output(), io.Boolean.Output(), io.Boolean.Output(), io.AnyType.Output(), io.AnyType.Output()],
+ outputs=[
+ io.Int.Output(),
+ io.Boolean.Output(),
+ io.Boolean.Output(),
+ io.AnyType.Output(),
+ io.AnyType.Output(is_output_list=True),
+ ],
is_dev_only=True,
accept_all_inputs=True,
)
@@ -273,7 +279,7 @@ class LoopIteration(io.ComfyNode):
is_first[0],
is_last[0],
list_item[0] if list_item else None,
- current_iteration_value[0] if current_iteration_value else None,
+ current_iteration_value,
)
@classmethod
diff --git a/tests-unit/execution_test/test_nested_loop_execution.py b/tests-unit/execution_test/test_nested_loop_execution.py
index 1fb45a817..e7078f0bf 100644
--- a/tests-unit/execution_test/test_nested_loop_execution.py
+++ b/tests-unit/execution_test/test_nested_loop_execution.py
@@ -179,6 +179,24 @@ class ListBackedScalar:
return ([[value]],)
+class RecordCarried:
+ values = []
+
+ @classmethod
+ def INPUT_TYPES(cls):
+ return {"required": {"value": ("*",)}}
+
+ RETURN_TYPES = ("*",)
+ OUTPUT_IS_LIST = (True,)
+ INPUT_IS_LIST = True
+ FUNCTION = "execute"
+
+ def execute(self, value):
+ value = list(value)
+ self.values.append(value)
+ return (value,)
+
+
class AppendIndex:
values = []
@@ -289,6 +307,7 @@ def register_internal_loop_nodes(monkeypatch):
"TestPair": Pair,
"TestListBackedScalar": ListBackedScalar,
"TestAppendIndex": AppendIndex,
+ "TestRecordCarried": RecordCarried,
"TestEmptyList": EmptyList,
"TestIntegerList": IntegerList,
"TestCaptureLoopState": CaptureLoopState,
@@ -469,6 +488,32 @@ def test_loop_executes_final_carried_value_without_output():
assert Increment.calls == [1, 2]
+def test_loop_carries_every_item_of_a_heterogeneous_list():
+ RecordCarried.values = []
+ prompt = {
+ "pair": {"class_type": "TestPair", "inputs": {"value": 7}},
+ "loop": {
+ "class_type": "StartLoop",
+ "inputs": {
+ "mode": "simple",
+ "mode.num_iterations": 2,
+ "initial_iteration_value": ["pair", 0],
+ },
+ },
+ "record": {"class_type": "TestRecordCarried", "inputs": {"value": ["loop", 4]}},
+ "close": {
+ "class_type": "EndLoop",
+ "inputs": {
+ "output_value": ["record", 0],
+ "next_iteration_value": ["record", 0],
+ "accumulate": False,
+ },
+ },
+ }
+ execute_prompt(prompt, "heterogeneous-carry-loop-test", ["close"])
+ assert RecordCarried.values == [[7, "7"], [7, "7"]]
+
+
def test_loop_preserves_list_backed_carried_value():
AppendIndex.values = []
prompt = {