Nodes/ComfyUI-MultiCutAndDrag/MapTrajectoriesToSegmentedMasks
ComfyUI Node

MapTrajectoriesToSegmentedMasks

Snap your drag path onto the mask's center

By Pablerdo·Created 2 years ago·Updated about a year ago· 4
MapTrajectoriesToSegmentedMasks
  • masks
  • MASKS
  • TRANSLATED_TRAJECTORIES
trajectories

The genuinely annoying part of MultiCutAndDragOnPath is the coordinate math. Its paths live in absolute pixel space, which means you have to know exactly where each mask sits in your image and hand-write coordinates that land on it. Do that for a handful of objects and you'll be in an image editor squinting at pixel readouts for a solid hour. MapTrajectoriesToSegmentedMasks exists to kill that hour. It computes the centroid of each mask and shifts your trajectory so the path's first point sits dead-center on the object it belongs to.

So instead of measuring, you sketch a rough path anywhere - say from {"x": 0, "y": 0} to {"x": 400, "y": 300} - and the node relocates it onto the actual object. From there MultiCutAndDragOnPath takes over and does the dragging. It's the ergonomics node of Pablerdo's ComfyUI-MultiCutAndDrag pack, and it's the one that makes multi-object animations feel achievable instead of masochistic.

How it works

For every mask in your batch, the node finds all pixels brighter than 0.5 and averages their x and y positions to get the centroid. Then it reads the corresponding trajectory, takes the offset from the path's first point to that centroid, and adds that same offset to every point in the path. The shape and relative spacing of your path are preserved - the whole thing just gets lifted and placed so its start lands on the mask's center. If a mask turns out to be empty, the centroid defaults to the image center so the node doesn't crash on you.

That's it. It's a translation, not a warp.

Inputs and outputs

Two inputs, two outputs, both simple:

  • masks - the MASK batch you're targeting. One mask per trajectory.
  • trajectories - a STRING of the same JSON path format MultiCutAndDragOnPath consumes: an array of arrays of {"x", "y"} objects.

Outputs:

  • MASKS - the input masks, passed through unchanged (handy if you're building a chain).
  • TRANSLATED_TRAJECTORIES - a JSON string, now aligned to the mask centroids. Wire this directly into MultiCutAndDragOnPath's coordinate_paths input and you're done.

Installation

Ships with ComfyUI-MultiCutAndDrag:

cd ComfyUI/custom_nodes
git clone https://github.com/Pablerdo/ComfyUI-MultiCutAndDrag

Restart ComfyUI (or install "MultiCutAndDrag" from ComfyUI Manager). Same modest dependency list as the rest of the pack - numpy, torch, scipy, Pillow, opencv-python - and no model downloads.

Common issues

  • Path count must equal mask count. The node raises a ValueError if they differ, so it fails loudly rather than silently.
  • Centroids are shape-weighted. The centroid is the mean of every bright pixel, so a mask with a hole or two disconnected blobs gets a center pulled toward the bigger mass. For a normal, compact object it's exactly what you want; for weird shapes, expect a slight offset.
  • It's a one-shot translation. The path's first point lands on the centroid. If your animation is meant to start from somewhere else, the whole path still moves - that's the point - but you can't ask it to anchor on the midpoint instead.
  • Coordinates stay in pixel space, same as the drag node expects.

If your "just sketch it and let it snap" instinct is strong, this node validates it. Few things in ComfyUI turn a fiddly pixel-measuring chore into a two-wire setup this cleanly.

CategoryPSNodes/masking

Inputs (2)

NameTypeDefaultDescription
masksMASK
trajectoriesSTRING

Outputs (2)

NameTypeDescription
MASKSMASK
TRANSLATED_TRAJECTORIESSTRING