MF Modulo
Modulo with a cycle counter — wrap your list and know which lap you're on
- modulo_result_int
- modulo_result_string
- cycle_count_int
- cycle_count_string
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 toMF Line Selectas 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_valuehas 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_valuewith 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 // 3is-1).
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| input_number | INT | 0-999999–999999 | — |
| modulo_value | INT | 101–999999 | — |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| modulo_result_int | INT | — |
| modulo_result_string | STRING | — |
| cycle_count_int | INT | — |
| cycle_count_string | STRING | — |