Nodes/ComfyUI-Text-Utility/Replace Variables and Process Wildcard (Loop)
ComfyUI Node

Replace Variables and Process Wildcard (Loop)

Chain variables, wildcards, and cleanup in one node — with a loop for the nasty nesting

By watarika·Created 2 years ago·Updated 10 months ago· 3
Replace Variables and Process Wildcard (Loop)
    • prompt
    text
    seed0
    remove_linefeed
    normalize_commasfalse
    remove_undefined_variablesfalse
    process_conditional_tagsfalse
    loop_count1

    This is the pack's flagship - the node that ties everything else together. It takes text that can contain $variable="value" definitions, $variable references, and __wildcard__ / {a|b|c} expressions, then runs "replace variables → expand wildcards" in a loop, loop_count times, and optionally cleans up the result.

    The loop is the point. When a variable's value contains a wildcard, or a wildcard file contains variable references, a single pass isn't enough - one substitution unlocks new text that itself needs expanding. The README's example is $adj="beautiful" $thing="__objects__" A $adj $thingA beautiful flower. Notice $thing's value is a wildcard; only the second pass resolves it. That's what makes this node more than just two nodes glued together.

    How it works, in order

    1. Extract every $name="value" definition and delete them from the text.
    2. loop_count times: substitute $name references, then run Impact Pack's wildcard process() with your seed.
    3. Optionally strip $var references that were never defined (remove_undefined_variables, off by default).
    4. Optionally run the Conditional Tag Processor's <ADD:...> / <REMOVE:...> pass exactly once at the end (process_conditional_tags).
    5. Optionally remove linefeeds (No / All / Blank Lines Only) and normalize commas (collapse doubles, fix spacing, drop BREAK, and trailing commas).

    The two cleanup toggles are the same code the Remove Comments node uses, so behavior carries over.

    Inputs that matter

    • text (STRING, forceInput, multiline) - note forceInput: this field is meant to be wired from another node, not typed. The whole point is text flows in from upstream.
    • seed (INT) - makes the wildcard roll reproducible.
    • loop_count (INT, default 1, min 1) - replace→expand passes. Start at 1; raise it only when something isn't resolving.
    • remove_linefeed, normalize_commas, remove_undefined_variables, process_conditional_tags - the post-processing toggles, all off by default.
    • Output: prompt (STRING) → your text encoder.

    Where it fits

    You've got a batch pipeline that mixes templates, per-run variables, and random wildcard choices - this collapses what would be four or five nodes into one. It's also the gentlest way to try the Conditional Tag Processor: flip process_conditional_tags on and put your <ADD:...>/<REMOVE:...> directives in the text, no separate node needed.

    Troubleshooting

    • Requires ComfyUI-Impact-Pack. The wildcard engine is Impact Pack's; without it the node fails at import or run. Install Impact Pack first, then this pack, restart.
    • If remove_undefined_variables is off (default) and an unresolved $name shows up in the output as literal text, the definition was probably misspelled or its value wasn't in double quotes.
    • Values must be "quoted" - $thing=__objects__ without quotes isn't a definition.
    • Loop count isn't free: every pass re-runs wildcard processing, and runaway nesting (a wildcard whose value expands back to itself) is theoretically possible. Keep loop_count small and intentional.

    Installing

    cd ComfyUI/custom_nodes
    git clone https://github.com/ltdrdata/ComfyUI-Impact-Pack   # first - required
    git clone https://github.com/watarika/ComfyUI-Text-Utility
    # restart ComfyUI
    

    Or via ComfyUI Manager for both. No pip requirements, no models in this pack.

    Categorytext

    Inputs (7)

    NameTypeDefaultDescription
    textSTRING
    seedINT00–18446744073709550000Determines the random seed to be used for wildcard processing.
    remove_linefeedCOMBO3 options: No, All, Blank Lines Only
    normalize_commasBOOLEANfalse
    remove_undefined_variablesBOOLEANfalse
    process_conditional_tagsBOOLEANfalse
    loop_countINT1

    Outputs (1)

    NameTypeDescription
    promptSTRING