Modulo
The node for every Nth frame, batch and cycle
- value_a
- value_b
- MATCHTYPE
Modulo is the most underrated math node in the pack, because it's the one that solves "every Nth thing" problems that otherwise take three nodes and a headache. It takes two numbers and returns the remainder of dividing one by the other. 10 % 3 is 1. That's it - and that remainder is surprisingly powerful in workflows.
The canonical use is cycling. Take a frame index or a batch index, mod it by a period, and you get a counter that loops: 0, 1, 2, 0, 1, 2, ... Feed that into a compare node and you've got "run this only every Nth image" or "alternate every other frame" without any state or scripting. It's the standard trick for sampling a subset of a batch, interleaving two styles, or scheduling different steps for different frames. AnimateDiff and video workflows in particular lean on this constantly.
How it works
The mechanism is Python's modulo operator (%) wrapped in a node. value_a is the dividend, value_b is the divisor, and the output is the remainder. Both inputs accept ints or floats via the type template, and the output type matches the input.
One behavior is worth knowing up front: if value_b is 0, the node returns 0 rather than crashing with a ZeroDivisionError. That's a deliberate guard - mod by zero is undefined in math and an exception in Python, so the node bails out with a harmless 0. It means your workflow won't die if a divisor ever comes in as 0, but it also means you should not rely on that 0 being meaningful. If you're debugging a loop that looks wrong, check whether the divisor is actually what you think it is.
Also worth remembering: modulo and remainders behave a bit differently for negative numbers than the intuition suggests, and for floats too. Keep your use to positive integers and the cycle math, and it'll do exactly what you expect.
The inputs that matter
value_a- the number you're dividing.value_b- the divisor, i.e. the cycle length or period.
Output is the remainder as a MATCHTYPE number.
A beginner trap worth naming: modulo is not "give me the result of division." If you want a quotient, use MathDivide. The two look adjacent and serve completely different purposes.
Install
This node ships in the ComfyUI-LogicMath pack. Install via ComfyUI Manager (search "ComfyUI-LogicMath", Install, restart) or:
cd ComfyUI/custom_nodes
git clone https://github.com/silveroxides/ComfyUI-LogicMath
No pip dependencies, no model downloads. The pack targets ComfyUI's newer extension API, so if nodes are missing after a restart, update ComfyUI itself.
Common issues
- "Every Nth" logic is off by one - remember the sequence starts at 0, not 1. Compare against
(index % N) == 0and the first hit lands on the 0th item. - Odd results with negative or float inputs - stick to positive integers for cycle work and modulo behaves like the textbook.
If you've ever hardcoded "process frames 1, 5, 9, 13" into a workflow, you already know why this exists. Let the math do the counting.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| value_a | COMFY_MATCHTYPE_V3 | — | |
| value_b | COMFY_MATCHTYPE_V3 | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| MATCHTYPE | COMFY_MATCHTYPE_V3 | — |