Comfy-Org / Comfy-Org/ComfyUI

[Feature Request] foreach loop / reducer

Open
#3,241 11 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
133k
Forks
15.7k
Avg merge
1d 6h
Merged PRs (30d)
155

Description

It would be great if there was some way to loop over lists so that we can process things like "SEGS" (from Impact Pack) one item at a time. For example, I can detect all the faces an image and then output that to a list of masks. For each mask I want to upscale/enhance that part of the image and paste it into the image.

I was thinking about how this would look in the UI, and I realized this is exactly like a "reducer" in many programming languages. It could look like this:

![image](https://github.com/comfyanonymous/ComfyUI/assets/1112998/a823a7ff-61fe-4b45-b60d-fd5256439bf6)
(The mockup in the screenshot is just a shell of a node)

On the left I have some node that produces a list, doesn't matter what, and that would go into the `array` input. `item` would output the first entry from the list/array, which I could process however I please, but it would most likely be merged into `accum` (accumulator), which would most likely be an output image (e.g. paste the enhanced face back into the image). Then you take that output and feed it back into the input `accum` of the `Reduce` node, thus forming a loop. `Reduce` then runs again with the next item in the input `array` and the updated `accum`, which repeats until the list is done, and then outputs the final result on `final`. `init` is the initial value for the output `accum`, for the first iteration of the loop. It's optional and will be `None` if not specified, just like [a normal reduce function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce).

I would implement this myself as a custom node, but as far as I know I can't output/return more than once during a single "Queue Prompt" cycle like this node needs to do.

Contributor guide

Open the contributing guide

Research direction

Start with the reducer mockup and the Queue Prompt execution behavior identified in the issue; no repository file or test is named. Done would mean list or array items can be processed sequentially, with an updated accumulator passed to the next iteration and a final result exposed when the list is complete.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
ai, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.