Nodes/ComfyUI_SVG-Suite/SVG Element Manipulator
ComfyUI Node

SVG Element Manipulator

Add, delete, duplicate, and reorder SVG elements from the graph

By MushroomFleet·Created about a year ago·Updated 5 months ago· 9
SVG Element Manipulator
    • STRING
    ◄svg_string—►
    â—„operationmodify_elementâ–º
    â—„selectorpathâ–º
    â—„new_tagrectâ–º
    â—„attributeswidth="100" height="100" x="10" y="10" fill="red"â–º
    â—„contentâ–º
    â—„target_index0â–º
    â—„reorder_position0â–º
    â—„duplicate_count1â–º

    Most svg-suite string nodes change what's already there. SVG Element Manipulator is the one that changes the structure - it can add a brand-new element to your SVG, delete one, replace one, duplicate it, or reorder it. If you've ever wanted to script "delete every stray rect" or "add a red circle at the top-right" without opening a vector editor, this is your node.

    Operations

    • add_element - inserts a new element. You supply new_tag (rect, circle, path…) and attributes as a raw string like width="100" height="100" x="10" y="10" fill="red". content holds inner text or child markup if the tag needs it.
    • remove_element - deletes elements matching selector (a tag name like path), optionally limited by target_index.
    • replace_element - swaps matched elements for your new tag/attributes.
    • modify_element - edits attributes on matched elements.
    • duplicate_element - copies matched elements, duplicate_count times.
    • reorder_element - moves matched elements to reorder_position in the document. This one matters more than it sounds: in SVG, later elements draw on top, so z-order is document order. Reordering is how you fix "my shape is behind the thing it should be in front of."

    How it works

    It parses the SVG as XML (lxml under the hood), finds elements with the selector, performs the operation, and serializes back to a string. Because it's real XML parsing rather than regex, the output is structurally valid - no half-broken tags - but note that it re-serializes the whole document, so formatting from your original file (indentation, attribute order) will be normalized.

    target_index targets the Nth match (default 0), and reorder_position accepts negative values to count from the end. Output is a single STRING: the modified SVG.

    Install

    Pack standard:

    cd ComfyUI/custom_nodes
    git clone https://github.com/MushroomFleet/svg-suite
    pip install -r requirements.txt
    

    Restart ComfyUI, or via Manager under "svg-suite". Needs lxml from the requirements; if the node errors immediately, lxml is your first suspect.

    Where people get burned

    The attribute string is freeform, which is flexible and unforgiving - fill=red without quotes, or a stray > in an attribute value, and the XML parse can fail. Write attributes the way they'd appear in valid SVG.

    Second: when you remove_element, selectors match by tag name. Removing path nukes every path. If you need surgical removal of one specific element, pair this with an ID selector by setting the element's ID first (or use SVG Attribute Manipulation's match_index for attribute-level surgery). And remember the z-order insight above - "add a background rect" should be add_element with reorder_position 0 so it lands behind everything, because by default new elements append last, which is on top.

    Category💎TOSVG/XMLParser

    Inputs (9)

    NameTypeDefaultDescription
    svg_stringSTRING—
    operationCOMBOmodify_element6 options: add_element, remove_element, replace_element, modify_element, duplicate_element, reorder_element
    selectorSTRINGpath—
    new_tagoptSTRINGrect—
    attributesoptSTRINGwidth="100" height="100" x="10" y="10" fill="red"—
    contentoptSTRING—
    target_indexoptINT00–100—
    reorder_positionoptINT0-100–100—
    duplicate_countoptINT11–10—

    Outputs (1)

    NameTypeDescription
    STRINGSTRING—