Nodes/Vantage-Nodes/Index Wrap / Clamp
ComfyUI Node

Index Wrap / Clamp

The tiny node that ends off-by-one loops for good

By vantagewithai·Created 8 months ago·Updated 25 days ago· 25
Index Wrap / Clamp
    • index
    index0
    size1
    wraptrue

    Index Wrap / Clamp is a two-behavior utility that solves one recurring annoyance in ComfyUI: keeping an integer inside a valid range. You hand it an index and a size, and it either wraps the index around (like a modulo) or clamps it to the edges. That's it. It's the kind of node you don't think about until you're three layers deep in an animation loop and the tenth frame sends index 12 into a list that only has five entries.

    How it works

    The math is deliberately minimal:

    • If size <= 0, it returns 0 - it refuses to divide by nothing.
    • With wrap on, output is index % size. Python's modulo is always non-negative, so you get a value in [0, size-1], no manual fiddling with negatives.
    • With wrap off, it clamps: max(0, min(index, size - 1)). Index 12 into size 5 becomes 4; index -3 becomes 0.

    Inputs:

    • index (INT, default 0) - the value to normalize.
    • size (INT, default 1) - the count, not the max index.
    • wrap (BOOLEAN, default true) - wrap vs. clamp.

    Output: index (INT).

    Where it earns its place

    The canonical use is looping. Drive a frame counter forward forever and route it through this node with size set to your batch or list length, and the output cycles 0, 1, 2, ..., n-1, 0, 1, ... instead of running off the end. Feed that into the pack's Switch Any (By Index) and you've got per-frame option switching with no bounds checking anywhere else. Clamp mode is the safer default when you'd rather hold the last valid value than cycle - say, for a progress-style selector that shouldn't jump back to the start.

    It also pairs naturally with StringListStepper: the stepper already handles its own wrap internally, but Index Wrap is what you reach for when you're doing the indexing yourself - e.g., turning a raw count into a valid latent-batch index.

    The one thing that trips people

    size is the length of your collection, not the highest valid index. Five items means size = 5, and wrapped outputs are 0–4. Pass 5 thinking "max index" and index 5 wraps to 0 instead of clamping to 4 - a silent bug that's genuinely annoying to catch. When in doubt, count the items, not the slots.

    Installation

    Part of Vantage Nodes. In ComfyUI Manager search "Vantage Nodes," or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/vantagewithai/Vantage-Nodes.git
    pip install -r requirements.txt
    

    Restart after install. Like the rest of the pack's math/control nodes, this one has no extra runtime dependencies beyond the base install - the heavy requirements.txt is for the GGUF and Qwen TTS pieces.

    Troubleshooting

    • Output never leaves a small range: that's wrap mode working as intended. Switch wrap off if you wanted clamping instead.
    • Unexpected 0: check size - if it's 0 or negative the node returns 0 by design.
    CategoryVantage/Math

    Inputs (3)

    NameTypeDefaultDescription
    indexINT0
    sizeINT1
    wrapBOOLEANtrue

    Outputs (1)

    NameTypeDescription
    indexINT