ComfyUI Node

Pt Scatter

Write values into a tensor at exact positions

By HowToSD·Created about a year ago·Updated about a year ago· 7
Pt Scatter
  • tens
  • TENSOR
dim0
index
src

Pt Scatter is the most powerful and most fiddly node in this batch, so let's start with what it's for. It takes a tensor and overwrites specific positions in it with values from another tensor, guided by an index tensor. Think "selective edit": your target tensor, plus a list of positions, plus the values to drop in. It's the inverse of gather - where gather pulls values out at indexed positions, scatter pushes values in. You'll reach for it when you need to inject values into a tensor at computed locations, like zeroing out or replacing specific rows of a matrix in a data pipeline.

What it actually does

It's a wrapper around torch.scatter. You give it:

  • tens - the target tensor whose values get replaced.
  • dim - which dimension the indexing happens along. Default 0, range -10 to 10.
  • index - a multiline string holding the index tensor, written as a Python list, e.g. [[1, 0], [0, 1]]. These are long-integer positions.
  • src - a multiline string holding the source values, e.g. [[9, 8], [7, 6]], the values that actually get written in.

The operation is tens.clone().scatter(dim, index, src). It works on a clone, so your original input tensor is untouched and you get a new TENSOR back - good, that's what you'd want in a graph.

The rule that bites: index and src must have identical shapes. The node checks this and throws a ValueError if they don't match. And for each element, index tells you which position along dim the corresponding src value goes into.

How to install it

It's part of the ComfyUI-Pt-Wrapper pack. In ComfyUI Manager, search ComfyUI-Pt-Wrapper, install, restart. Or clone:

cd ComfyUI/custom_nodes
git clone https://github.com/HowToSD/ComfyUI-Pt-Wrapper

Restart after. The pack's requirements.txt is heavy (transformers, datasets, accelerate, peft, gensim, sentencepiece, pandas, scikit-learn, scipy) - first install is slow and version conflicts with other nodes are possible.

Gotchas

This is the node where the string-parsing inputs earn their keep. index and src both go through ast.literal_eval, so they must be valid Python list literals - brackets included. And they must be the same shape, which is the error people hit most.

The subtle one: index values must stay in bounds. torch.scatter throws an "index is out of bounds for dimension" runtime error if any index exceeds the size of that dimension. No graceful clamp, just a hard failure in the middle of your run.

One more: src is parsed to a tensor matching the target's dtype, so if your target is float32 and your src list has ints, it converts silently. Usually fine, but if you're doing precision-sensitive work, keep the types in your head.

CategoryData Analysis

Inputs (4)

NameTypeDefaultDescription
tensTENSOR
dimINT0-10–10
indexSTRING
srcSTRING

Outputs (1)

NameTypeDescription
TENSORTENSOR