Nodes/MF-PipoNodes/MF Line Counter
ComfyUI Node

MF Line Counter

Count the lines in your prompt list before you start indexing it

By MomentFactory·Created 11 months ago·Updated 7 months ago· 1
MF Line Counter
    • line_count_int
    • line_count_str
    textLine 1 Line 2 Line 3

    The most common ComfyUI bug that isn't a dependency error is an index out of range: you wrote five prompts into a list, fed an index of 6, and got something weird downstream. MF Line Counter (MF_LineCounter) is the boring-but-useful guard against exactly that - it counts the lines in a multiline string so you can validate a list before you index into it. From the MF PipoNodes pack (pierreb-mf / Moment Factory).

    How it works

    One required input, two outputs:

    • text (multiline) - the string to count.
    • line_count_int - the count as an integer, for math nodes and comparisons.
    • line_count_str - the same count as a string, for text-based logic.

    The counting logic is where the details live. It normalizes every line-ending style first - \n, \r\n, and lone \r all count as a break - then splits. Empty lines count (two consecutive newlines are two lines, not one), which matches how MF Line Select indexes, so the two stay consistent. And if the input is empty or all whitespace, you get 0, not 1 - the author special-cased that so "nothing" reads as "no lines," which is the sane default.

    One quirk worth knowing: because empty lines count, a text file saved with a trailing newline will report one more line than you might expect. That's correct behavior for indexing (Line Select counts it too), just surprising the first time.

    Why you'd reach for it

    This node is a building block, and its best use is defensive:

    • Validate before selecting. Wire text to both Line Counter and Line Select; if line_count_int is less than your intended index, branch and skip the render instead of generating garbage.
    • Loop control. In a batch loop, compare the current step (from a counter or MF Modulo Advanced) against the line count to know when the list is exhausted.
    • Progress reporting. Count lines, log the total with MF Save Log File, and you have "prompt 3 of 7" in your batch log without manual bookkeeping.

    Install

    cd ComfyUI/custom_nodes/
    git clone https://github.com/pierreb-mf/ComfyUI-MF-PipoNodes
    cd ComfyUI-MF-PipoNodes
    pip install -r requirements.txt
    

    Restart after (or ComfyUI Manager → "MF PipoNodes"). The pack only needs aiohttp and pyyaml; there's nothing to download.

    Gotchas

    • Empty vs. whitespace-only: a string of just blank lines returns 0, not the number of blanks. If you're counting a deliberately blank-line-separated list, this can undercount.
    • Trailing newline inflation: a list pasted from an editor often ends with a newline, so text may count one line higher than the visible number of items. Pair it with Line Select, which uses the same splitting logic, and they'll never disagree with each other.
    CategoryMF_PipoNodes/Utilities

    Inputs (1)

    NameTypeDefaultDescription
    textSTRINGLine 1 Line 2 Line 3

    Outputs (2)

    NameTypeDescription
    line_count_intINT
    line_count_strSTRING