Nodes/ComfyUiNodes/M Int Mod (a % b)
ComfyUI Node

M Int Mod (a % b)

Wrap-around math for looping batches and grids

By Madlumi·Created 8 months ago·Updated 8 months ago· 0
M Int Mod (a % b)
    • a_mod_b
    a0
    b1

    Modulo is the most underrated operation in ComfyUI because it does one thing nothing else does: wrap around. a % b keeps a running number pinned inside a range - 0 through b-1 - forever. That's the heartbeat of looping, cycling, and grid layout logic, and M Int Mod is it as a node, returning a_mod_b.

    The uses that make it click:

    • Cyclic batch indexing. Take a counter that grows forever, take % batch_size, and you get an index that loops: image 0,1,2,...,n-1,0,1,2... Useful for pairing a growing frame counter with a finite set of seeds or prompts.
    • Grid positions. x % tile_width gives you the column of a tile, y % tile_height the row - the same math that powers tiling layouts, kept visible in the graph.
    • "Every Nth frame" gating. frame % N == 0 is the classic "do something every N steps" condition, and with this pack's comparison nodes you can wire that check directly into a gate.

    Behavior worth knowing: a defaults to 0, b to 1, both signed 32-bit INT. Modulo on negative numbers follows Python's semantics - the result takes the sign of the divisor, so -1 % 5 is 4, not -1. And divide by zero doesn't throw; the node returns 0 instead, which is safe but easy to forget about if you ever feed it a computed divisor that hits zero.

    If you've already got M Int DivMod installed, this is the "remainder only" sibling - DivMod gives you quotient and remainder together, this gives you just the wrap. Pick whichever matches whether you need both halves.

    It ships in ComfyUiNodes (repo Madlumi/ComfyUiNodes, author "Madanie"), a small personal pack of integer math, string helpers, and workflow-glue nodes. Install via ComfyUI Manager - search "ComfyUiNodes" - or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/Madlumi/ComfyUiNodes
    

    Restart ComfyUI after that. No pip requirements or model downloads. It's under Add Node → mnodes → int.

    Honest verdict: if your workflows are linear - one pass, one image - you'll rarely touch modulo. The moment you build loops, batches, or grids, it becomes the node you reach for constantly.

    Categorymnodes/int

    Inputs (2)

    NameTypeDefaultDescription
    aINT0-2147483648–2147483647
    bINT1-2147483648–2147483647

    Outputs (1)

    NameTypeDescription
    a_mod_bINT