Nodes/ComfyUI_demucus/Path Relpath
ComfyUI Node

Path Relpath

Shrinking absolute paths down to something portable

By kale4eat·Created 2 years ago·Updated 2 years ago· 0
Path Relpath
    • relpath
    path

    Path Abspath makes paths absolute. Path Relpath is the reverse operation: it takes a path and strips it down to the shortest version relative to your current location. If you've ever stared at a full path like /home/you/ComfyUI/output/img_0001.png and wished you had just output/img_0001.png, this is that wish, as a node.

    Where it earns its keep is portability. Absolute paths are machine-specific - copy a workflow to another box and the full path points at the wrong place. Relative paths travel. If you're saving metadata or passing paths around in a way that needs to survive a move, shrinking them to relative first is the difference between "works here" and "works everywhere."

    How it works

    The node calls os.path.relpath(path) - note the single argument. Python's relpath takes an optional second argument for the base directory, and when you omit it, the base defaults to the process's current working directory. For ComfyUI that's the folder you launched the server from, normally the ComfyUI install root.

    That's the key thing to internalize: Path Relpath is not "relative to the workflow file" and not "relative to the output folder." It's relative to wherever the ComfyUI server was started. Same workflow, launched from a different directory, different result.

    Inputs and outputs

    • path (STRING, default empty) - the absolute (or partial) path to relativize.
    • relpath (STRING) - the path expressed relative to the working directory.

    Wire the output into any string input, or chain it with the rest of the pack - run a relativized path through Path Basename for the filename, or Path Dirname for the folder portion.

    Installing it

    Dependency-free like the whole pack - standard library only, no requirements.txt, no pip, no model downloads, nothing to collide with your other nodes.

    cd ComfyUI/custom_nodes
    git clone https://github.com/kale4eat/ComfyUI-path-util
    

    Restart ComfyUI. Or through Manager: search "path-util", install.

    Common issues

    The classic surprise is relativity anchoring. If you feed Relpath a path that isn't under the working directory, it walks back with .. segments - os.path.relpath will happily give you ../../other/stuff rather than failing. That's correct behavior, but it looks alarming the first time, and it means the output only makes sense if you know the anchor.

    The second gotcha is the same one haunting every node in this pack: an empty string in, and you get "" back - not a useful relative path. And on Windows, drive letters and backslashes can make "relative" behave in ways that don't transfer to a Linux machine. Relative paths are portable between machines with the same layout; they're not magic. For anything more complex, this node plus Path Abspath in the same pack are the two ends of the same stick.

    Categorypath-util

    Inputs (1)

    NameTypeDefaultDescription
    pathSTRING

    Outputs (1)

    NameTypeDescription
    relpathSTRING