ComfyUI Node

MF Modulo

Modulo with a cycle counter — wrap your list and know which lap you're on

By MomentFactory·Created 11 months ago·Updated 7 months ago· 1
MF Modulo
    • modulo_result_int
    • modulo_result_string
    • cycle_count_int
    • cycle_count_string
    input_number0
    modulo_value10

    Modulo is the single most useful math operation in batch ComfyUI work, because it's how you make an ever-increasing counter wrap around a fixed-size list. MF Modulo (MF_ModuloAdvanced) does the wrap and tells you which full loop you're in - so a 500-step batch over a 5-prompt list doesn't just cycle, it tells you you're on cycle 99. From the MF PipoNodes pack (pierreb-mf / Moment Factory).

    How it works

    Two required inputs:

    • input_number - the value to divide (typically your step counter).
    • modulo_value - the divisor, minimum 1 (typically the length of your list).

    Four outputs:

    • modulo_result_int - the remainder (input mod value). The thing you feed to MF Line Select as an index.
    • modulo_result_string - the remainder as text.
    • cycle_count_int - how many complete loops have passed: input // value (integer division). A 5-prompt list, step 12 → cycle 2.
    • cycle_count_string - same, as text.

    It's fully stateless - everything derives from the current inputs, so it plays perfectly with ComfyUI's caching and never needs a reset. The node also shows its math on the canvas (🔢 12 mod 5 = 2 / 🔄 Cycle: 2) and prints to the console, which is a nice touch for batch progress.

    The pattern it exists for

    This is the pack's flagship "infinite prompt cycling" recipe:

    Step counter ──▶ MF Modulo Advanced (mod 5)
         │                    ├── modulo_result_int ──▶ MF Line Select (index)
         │                    └── cycle_count_int ────▶ MF Save Log File / filename
         └──▶ every step renders, list wraps forever
    

    Steps 0–4 land on cycle 0, steps 5–9 on cycle 1, and so on - the remainder drives which prompt renders, the cycle tells you your position in the long run. Toss the cycle count into your filename or log and a 200-image batch becomes self-documenting.

    The one thing to know about negatives

    This is Python, so % returns a non-negative result even for negative inputs: -7 % 3 is 2, not -1 (which is what C and JavaScript return). If your counter ever goes negative - e.g. you subtract a skip offset before the mod - the index stays in 0..value-1 instead of going negative. That's usually what you want for indexing, but if you were expecting C-style semantics, it'll surprise you.

    Install

    cd ComfyUI/custom_nodes/
    git clone https://github.com/pierreb-mf/ComfyUI-MF-PipoNodes
    cd ComfyUI-MF-PipoNodes
    pip install -r requirements.txt
    

    Restart after, or ComfyUI Manager → "MF PipoNodes". Pack needs only aiohttp and pyyaml; nothing to download.

    Gotchas

    • modulo_value has a floor of 1. Mod by zero isn't possible through the UI - but a value of 1 always returns 0 and increments the cycle every step, which is only useful if that's what you want.
    • Remainder output is input_number % modulo_value with the input's sign behavior - for negative inputs the result is non-negative (see above), and the cycle count is integer division, which rounds toward negative infinity (-1 // 3 is -1).
    CategoryMF_PipoNodes/Math

    Inputs (2)

    NameTypeDefaultDescription
    input_numberINT0-999999–999999
    modulo_valueINT101–999999

    Outputs (4)

    NameTypeDescription
    modulo_result_intINT
    modulo_result_stringSTRING
    cycle_count_intINT
    cycle_count_stringSTRING