Nodes/comfyui_AcademiaSD/Academia SD Loop Counter ๐Ÿ”„
ComfyUI Node

Academia SD Loop Counter ๐Ÿ”„

A loop counter that won't double-count inside a single queue

By AcademiaSDยทCreated about a year agoยทUpdated 4 days agoยท 76
Academia SD Loop Counter ๐Ÿ”„
    • loop_index
    โ—„reset_counterfalseโ–บ
    โ—„start_value1โ–บ
    โ—„project_pathtest/rosarojaโ–บ

    ComfyUI has a dirty secret for anyone trying to build a loop: the same node can execute more than once inside a single queue if it's in the execution path twice, which makes naive counters lie. This node exists to solve that specific problem - a loop counter that increments once per queue run, no matter how many times the workflow path touches it.

    How it works

    The cleverness is in how it detects a "new run." ComfyUI assigns a unique prompt ID to each queue action, so the node watches last_prompt_id and caches its result: if the current execution shares a prompt ID with the last one, it returns the cached value instead of counting again. Every new queue bumps the count. That's the fix for the classic double-execution bug.

    State is persisted to a text file in your output folder named after project_path (e.g. test/rosaroja becomes .../output/test/rosaroja_loop_counter.txt), which means the count survives restarts. start_value sets where the loop begins (default 1), and reset_counter (boolean, default off) forces it back to the start value when toggled. The output is loop_index (INT), which is what you wire into a batch loader's index or a multi-prompt selector.

    The inputs that matter

    • start_value - where counting begins; bump this instead of manually editing files.
    • project_path - namespaces your counter file. Give different loops different project paths or they'll share state and fight.
    • reset_counter - one-shot reset back to start_value.

    Install

    Standard pack install: ComfyUI Manager โ†’ "AcademiaSD", or git clone https://github.com/AcademiaSD/comfyui_AcademiaSD, restart. No extra dependencies - it's pure ComfyUI server API.

    The caveats

    First, the reset is a toggle, not a button - leave reset_counter on and every queue pins the loop to start_value instead of advancing. Flip it for one run, then turn it off. Second, because state lives in a file keyed by project_path, two workflows sharing a path will step on each other's counters; treat the path as a name you own. And finally, it increments per queue, not per image - if your workflow batches multiple images per run and you need per-image indexing, this is the wrong counter for that. It's built for the "one loop iteration per queue click" pattern, which is exactly the pattern video keyframing and dataset stepping run on.

    CategoryAcademia SD

    Inputs (3)

    NameTypeDefaultDescription
    reset_counterBOOLEANfalseโ€”
    start_valueINT10โ€“9999โ€”
    project_pathSTRINGtest/rosarojaโ€”

    Outputs (1)

    NameTypeDescription
    loop_indexINTโ€”