Nodes/ComfyMath-NG/IntUnaryOperationConditional
ComfyUI Node

IntUnaryOperationConditional

Run an integer operation only when a condition is true — guarded Abs, Inc, Factorial and friends

By rabanti-github·Created 2 months ago·Updated 7 days ago· 0
IntUnaryOperationConditional
    • INT
    conditionfalse
    fallback_mode
    fallback_value0
    op
    a0

    The name is a mouthful, but the idea is simple: take an integer, optionally transform it, and let a boolean decide whether the transform actually happens. IntUnaryOperationConditional applies one of eight unary operations to a when condition is true, and when it's false it hands back either a unchanged or a constant you provide. It's a guard wrapped around a math op.

    Why would you want that? Because a few of the integer operations are landmines if you run them blind. Factorial throws on negative inputs, and a factorial of a large number explodes into a number with dozens of digits. Sqr and Cube can overflow int64 fast (the node uses signed 64-bit integers). The conditional form lets you write "only run the scary op when it's safe to." Feed condition from an IntUnaryCondition - say, IsPositive - and now Factorial only fires on non-negative inputs, with a graceful fallback otherwise. That's the pattern this node exists for.

    The inputs that matter

    • condition - BOOLEAN, default false. The gate. True means do the operation.
    • op - one of eight: Abs, Neg, Inc, Dec, Sqr, Cube, bitwise Not, Factorial.
    • a - INT, the value operated on.
    • fallback_mode - A (return a untouched) or constant (return fallback_value).
    • fallback_value - INT, default 0. Only read when fallback_mode is constant.

    Output is a single INT: the result when true, your chosen fallback when false.

    How it works

    Mechanically it's exactly what it looks like - op(a) if condition else fallback. The source is one Python function with an if/else, so there's no hidden magic. Inc adds 1, Dec subtracts 1, Neg flips the sign, Not is bitwise (so Not 4 is -5, not False), Sqr/Cube raise to powers, and Factorial is math.factorial - which is where the uncaught negative-input crash comes from. ComfyMath deliberately does not catch math errors; that's the README's stated limitation, and it's exactly why you reach for the conditional variant instead of the plain operation node.

    Real-world shapes

    A couple of graphs worth stealing:

    • "Increment a counter but never let it go negative" - condition from an IsNegative check... actually the cleaner read is: increment only when you want to, else pass through.
    • "Step a batch size up, but only in the second pass" - gate on whatever boolean marks that pass, use Inc or Dec, fallback A.

    The honest limitation: the operations are all unary, so any conditional math that needs two values (like guarded division) belongs in the binary conditional node next door.

    Installing it

    It's part of ComfyMath, published as ComfyMath-NG. Install via ComfyUI Manager (search "ComfyMath") or clone:

    cd ComfyUI/custom_nodes
    git clone https://github.com/rabanti-github/ComfyMath.git ComfyMath-NG
    

    Restart ComfyUI. Remove or disable the original ComfyMath first - both register the same node IDs. Only dependency is numpy, already present.

    The trap to remember

    The gate is a real BOOLEAN socket. If you're tempted to feed it an integer "1 for yes," don't - convert with IntToBool first. And remember the fallback: when condition is false, the node does not skip the op silently; it returns the fallback you chose. Pick A when passthrough is what you want, constant when you want a hard-coded safety value.

    Categorymath/int

    Inputs (5)

    NameTypeDefaultDescription
    conditionBOOLEANfalse
    fallback_modeCOMBO2 options: A, constant
    fallback_valueINT0
    opCOMBO8 options: Abs, Neg, Inc, Dec, Sqr, Cube, +2
    aINT0

    Outputs (1)

    NameTypeDescription
    INTINT