Skip to main content

Command Palette

Search for a command to run...

SpriteDX - Stuck on ForEach

Updated
3 min read
SpriteDX - Stuck on ForEach

I’m stuck. It’s been a week, and I still haven’t been cracked Stage 4 Pipeline JSON integration. Stage 4 is different — it’s where the pipeline schema has to grow up and handle loop semantics.

Meanwhile, my coursework is sliding behind, which is frustrating because I honestly thought I’d have the space to juggle both Sprited and school at the same time. Turns out…not so much.

Supporting loop behavior was rather simple. We just needed to add forEach to stage definition and have it map to a array variable.

That’s simple enough, but the scary thing is the implications. Let’s start with some of them.

Problem 1: Each iteration should yield in different file names otherwise, we will overwrite previous iteration results.

Each iteration needs to yield different output files otherwise we will be overwriting the previous outputs.

The solution I’ve implemented is that we will pass in a filename prefix into the ComfyUI workflow so ComfyUI will produce different output file names.

This works, but pipeline json becomes rather complex with this approach.

  1. Need to add a computed property to parse input filename then extract a key out of it using some regex matching. This is error prone and requires JS script.

  2. Output files will come with <file name prefix>_00000_.png and I need a way to rewrite this to remove extra prefixes. To be fair, this problem exists even without the loop semantics.

Solutions:

Postfix issue has been implemented by full javascript replace semantics but the JSON becomes rather complex. I think we should replace it with a simpler switch to something as simple as removePostfix: true.

Alternative to extracting keys from file names, we could simply consider whatever happens inside the iteration to be isolated. Then file names can be prefixed with a iteration prefix like iter_0__ or perhaps [i] postfix at the end of each generated files. But this basically means we don’t have any string keys at the end.

Yet another alternative is to require JSON to specify a key for each iteration. This approach works but not too different from the computed property approach.

The thing is all these approaches are possible but comes with dirty artifacts.

Proposal: At least, using filename as a “key” seems to be realistic approach whether it uses computed property or not. We can do something like:

"forEach": {
   "array": "clips",
   "key": "$item.filename"
}

This isn’t perfect but it at least ties iteration to a unique key, and we can by default set $key to be $index.

Problem 2: Non-image outputs

In the future, if ComfyUI workflows were to return different types of outputs like primitives, we need to think about how they would map to the output variables. Right now all outputs are images, so there isn’t much to be concerned about but I don’t want to shackle myself here.

Proposal: We will ignore this problem. The thinking process is that sustainability of the project is more important than perfection. The momentum needs to be prioritized.


Outcome

Here is a implementation of Stage 4 isolated.

I got it working, but the mapping outputs is getting complex. We would need to setup some house-rules.

  1. All outputs of “forEach” stage needs to be an “array.”

  2. We only support “image” or “array of image“ outputs.

  3. We somehow need to figure out how to make this more robust.

— Sprited Dev 🌱

SpriteDX

Part 1 of 50

Tracks development of sprite generator AI tool. https://spritedx.com