Nodes/ComfyUI-HappNodeSet/SVG PathLength
ComfyUI Node

SVG PathLength

The true length of an SVG path — stroke width ignored, curves sampled properly

By mikemojen·Created 8 months ago·Updated 3 months ago· 0
SVG PathLength
    • total_length
    • formatted_result
    svg_content
    decimal_places4

    If you've ever asked "how long is this path?" and been told to just count the pixels, you know why that's wrong. SVGPathLength parses an SVG's <path> data and computes the actual length of every path in the document - ignoring stroke width entirely, which is the whole point. A 5px-thick line and a 0.5px-thin line that follow the same route are the same length, and this node knows it. For estimating laser cut time, material usage, or thread/tape length off a vector file, this is the number you actually want.

    How it works

    It tokenizes the SVG path d attribute (the M/L/C/A/... command soup) into commands and coordinates, then integrates length command by command: straight segments are exact, cubic and quadratic Beziers are sampled and summed as line segments (100 samples per curve - plenty for real-world estimates), and elliptical arcs are computed via chord and radius math with a large-arc correction. All paths in the document are summed. Stroke width never enters the calculation. decimal_places (0–10, default 4) controls the rounding on the formatted output.

    What comes out

    • total_length (FLOAT) - the raw summed length, for downstream math.
    • formatted_result (STRING) - a human-readable number at your chosen precision, for Show Text.

    One caveat that applies to every SVG tool: the units are whatever the SVG says they are. If the document uses mm, the length is in mm; if it's unitless user units, it's in user units. There's no assumption hidden here - the number is only as meaningful as the document's coordinate system.

    Where it fits

    Chain it after RasterToUniformSVG: vectorize a diagram, hand the svg_string over, and get a cut length for the whole drawing. Or paste in an SVG you generated elsewhere and get an estimate without opening an editor. It pairs naturally with SVGDimensions if you're normalizing units first.

    Install

    Part of ComfyUI-HappNodeSet (mikemojen). ComfyUI Manager: search HappNodeSet. Manual:

    cd ComfyUI/custom_nodes
    git clone https://github.com/mikemojen/ComfyUI-HappNodeSet.git
    pip install -r ComfyUI-HappNodeSet/requirements.txt
    

    Restart ComfyUI. Pure Python (regex tokenizer plus math), so it adds nothing beyond the pack's standard requirements.

    Common issues

    The most common failure is an empty result, which almost always means the path d data wasn't where the node expected it - some SVGs wrap paths in <defs> or use transforms, and this node reads plain d attributes. Long, tight Bezier curves are slightly underestimated by sampling (100 samples per curve); if you need engineering precision on hairpin curves, this isn't that tool - but for cut-time and material estimates it's more than close enough. And remember: it sums all paths, so a document with both the artwork and a hidden crop-mark path will report both. Check the document, trust the number.

    Categoryutils/svg

    Inputs (2)

    NameTypeDefaultDescription
    svg_contentSTRING
    decimal_placesoptINT40–10

    Outputs (2)

    NameTypeDescription
    total_lengthFLOAT
    formatted_resultSTRING