Nodes/ComfyForEach/Check If Last Index
ComfyUI Node

Check If Last Index

The 'is this the last one?' flag your batch loop runs on

By pupba·Created about a year ago·Updated about a year ago· 4
Check If Last Index
    • is_last
    index0
    total1

    Every batch loop has the same structural problem: how do you know when you're done? If you're iterating over a folder of images by index, "done" is the moment your counter hits the last one - and that moment is when you might want to do something different: fire a webhook, send a notification, write a manifest, shut down an EC2 instance. IsLastIndexNode (display name "Check If Last Index") is the tiny flag that tells you. Give it the current index and the total count, and it returns a BOOLEAN: true when the current index is the final one, false otherwise.

    It's the pack's loop-terminator, and the intended consumer is the pack's own EventBridgeTriggerNode - you only want to send that "task finished" event once, at the end, not on every iteration. That "do the special thing exactly once at the end" pattern is the entire reason this node ships.

    How it works

    The whole mechanism is one comparison: index == total - 1. Zero-based lists again - if your folder has 10 files, indexes run 0 through 9, so the last index is 9, not 10. The node takes index (INT, default 0, min 0) and total (INT, default 1, min 1) and returns is_last (BOOLEAN). If you feed it index=9, total=10, you get true. There's no state, no memory of previous calls - it's a pure function of its two inputs, which makes it trivially safe to drop into any loop.

    Inputs and outputs

    • index - the current loop index. Default 0, min 0.
    • total - how many items the loop has. Default 1, min 1.

    Output: is_last (BOOLEAN). Wire it into the is_last input of EventBridgeTriggerNode, or into any boolean-aware switcher.

    Install

    Same pack, same steps as the rest:

    cd ComfyUI/custom_nodes
    git clone https://github.com/pupba/Comfy_ForEach
    cd Comfy_ForEach
    pip install -r requirements.txt
    

    Or search ComfyForEach in ComfyUI Manager and click install. Restart after. Dependencies are pillow, boto3, opencv-python-headless, numpy, and torch (ComfyUI's own) - nothing heavy, no models.

    Where people get burned

    • Off-by-one, every single time. total - 1 is the last index. If your counter increments to total before checking, you'll never see true. If it stops at total - 2, you'll never see the last item at all.
    • total is a fixed number, not "the list length minus one." The natural source for total is whatever you know the item count to be - for the folder loader that's the length of the name list. Feed it the actual count, not the last index.
    • It doesn't hold state. It's tempting to read it as "the loop is now over," but it's really "this specific index equals this specific total minus one." For a single-pass loop over a list, that's identical. For anything weirder, remember it's just a comparison.

    This is one of those nodes that looks almost too simple to matter until you realize it's the piece that lets you run a batch pipeline headless and have the last iteration trigger something meaningful. Small, honest, and exactly as complicated as it needs to be - which for loop plumbing is a compliment.

    CategoryComfyForEach/Logic

    Inputs (2)

    NameTypeDefaultDescription
    indexINT0
    totalINT1

    Outputs (1)

    NameTypeDescription
    is_lastBOOLEAN