Nodes/comfyui-sampler-peek/Step Expression
ComfyUI Node

Step Expression

A calculator that knows which step you're on

By Prohect·Created 3 months ago·Updated 3 months ago· 0
Step Expression
    • value
    • condition
    step_index1
    total_steps20
    expressionstep % 5 == 0

    StepExpression is a tiny math evaluator that runs an expression in the context of a sampling step and returns both the numeric result and its boolean truth. Think of it as the shared brain of the comfyui-sampler-peek pack: it uses the exact same safe expression engine that SamplerPeekAdvanced uses for its decode_expr and cfg_expr, but exposed as a standalone node you can poke at and reuse anywhere.

    The name undersells it a little. This isn't a sampler node at all - it's a scheduling utility. Feed it "which step are we on?", it tells you "here's a number, and here's whether the condition is true." That one shape covers a lot of ground.

    The inputs

    • step_index - the current step, 1-based.
    • total_steps - the total, so expressions can talk about "the last 10%" or "the halfway point".
    • expression - the math expression itself (defaults to step % 5 == 0).

    The available vocabulary is the standard safe set: step, n, and total_steps as variables, plus clamp, lerp, the trig and log functions, and ordinary Python operators (and, or, not, comparisons, %, **, //). So step >= n * 0.8 means "in the final fifth", and clamp(step / n * 8, 1, 8) is a linear ramp you can plug into anything that takes a float.

    The two outputs

    • value - the expression evaluated as a float.
    • condition - its boolean truth.

    They're the same evaluation, two views. step % 2 == 0 gives value = 1.0 and condition = True on even steps, value = 0.0 and condition = False on odd ones. That's how the same node can drive both a numeric schedule (wire value into a CFG or denoise-ish input) and a gate (wire condition into PeekConditionGate).

    The catch: it doesn't know the step itself

    StepExpression won't tell you the current step on its own - you have to feed step_index in. That's what makes it useful in two very different setups:

    • Inside a sampler run: take the step_indices output of SamplerPeekAdvanced, run it through the pack's PeekStepIndex node to get a scalar, and feed that in. Now your expression is evaluated against the real steps of the generation.
    • In a loop or a manual workflow: drive step_index from whatever counter you already have (a loop node, an AnimateDiff frame index, a Primitive you bump by hand) and you've got step-relative logic without any sampling involved.

    Install

    Same pack as the rest. Manager search comfyui-sampler-peek, or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/Prohect/comfyui-sampler-peek
    

    Restart, done - no dependencies, no models. Note the README's clone URL is stale (points at a repo that 404s); the one above is current.

    Gotchas

    Division by zero and other math errors are real Python errors, and they'll just surface - there's no silent handling here like the sampler's own expression field. And remember the value output is a float: for a boolean expression, True becomes 1.0, which is handy for multiplying something on and off, but it'll surprise you if you expected it to stay boolean. For a node with two outputs and three inputs, that's about all there is to trip over.

    Categorysampling/peek

    Inputs (3)

    NameTypeDefaultDescription
    step_indexINT11–10000Current step index (1-based)
    total_stepsINT201–10000Total number of sampling steps
    expressionSTRINGstep % 5 == 0Math expression to evaluate. Variables: step (current), n (total steps). Returns both float value and bool condition.

    Outputs (2)

    NameTypeDescription
    valueFLOATEvaluated expression as a float
    conditionBOOLEANBoolean truth of the expression