Nodes/comfyui-easy-sam3-tools/SAM3 Mask Range Selector
ComfyUI Node

SAM3 Mask Range Selector

SAM3 gives you one mask per object. Pick the ones you care about.

By SuLU-K·Created 9 months ago·Updated 7 months ago· 9
SAM3 Mask Range Selector
  • obj_masks
  • mask
take_start0
take_count1

Run SAM3 on a frame and you don't get one mask - you get a stack of them, one per detected object, in the order your prompt or detector produced them. If the frame has four people and you only want to inpaint one of them, feeding the whole stack downstream is asking for trouble. Sam3MaskRangeSelector is the node that says "just these objects." It takes that ordered stack of obj_masks, slices out the range you name, and merges the survivors into a single mask per frame - ready to drive a masked inpainting or editing pass.

In: obj_masks (MASK), take_start (INT, 0–4096, default 0), take_count (INT, −1 to 4096, default 1). Out: mask (MASK), one merged mask per input frame.

The mechanism, in plain terms

SAM3-centric packs tend to hand you masks as a 4D tensor shaped [B, N, H, W] - batch, number of objects, height, width. This node normalizes whatever it receives (the source code defensively handles raw tensors, lists of per-object masks, and lists of per-frame masks, because upstream nodes are famously inconsistent about which shape they emit), then does three things:

  1. Clamps take_start into range and computes the slice. Set take_count to -1 and it means "everything from take_start to the end" - that's the power move if you want "skip the first two objects, keep the rest."
  2. Sums the selected masks together and thresholds at zero, so overlapping objects still produce one clean union instead of double-darkened pixels.
  3. Returns a single MASK per frame.

If the slice comes out empty - start at the end, or a count of zero - you get an all-black mask rather than an error. That's arguably friendlier than a crash, but an all-black mask downstream will silently inpaint nothing, so keep an eye on your numbers.

The inputs that matter

  • obj_masks (MASK) - the ordered stack from your SAM3 node. Order matters: "object 2" means index 2 in this array, so get familiar with what your detector emits first.
  • take_start (INT, default 0) - first object to keep.
  • take_count (INT, default 1) - how many to keep from there. -1 = through the end.

Installing it

Same pack, same install:

  • ComfyUI Manager → search comfyui-easy-sam3-tools → Install → restart.
  • Or:
cd ComfyUI/custom_nodes
git clone https://github.com/SuLU-K/comfyui-easy-sam3-tools

Then restart ComfyUI. Pure mask math - no dependencies beyond ComfyUI's own torch, no model files. It shows up under Test Nodes/SAM3.

Troubleshooting

  • The output mask is empty. take_start landed past the last object, or take_count sliced to nothing. Dial take_count to -1 to confirm - that grabs everything remaining, and if that's still empty, your obj_masks input is the problem, not the selector.
  • You're getting the whole frame masked. That usually means the mask stack itself is one big mask (object 0 is everything), which is a SAM3-prompting issue upstream, not something this node can fix.
  • Wrong objects selected. Remember it's zero-indexed and order follows your upstream detector's ordering. There's no visual preview here - wire the output into a mask preview node (or Sam3DrawBBox if you have boxes) to confirm you grabbed the right object.

Once you've got the single merged mask, the natural next stop is a masked inpainting pass - the KB's core argument that masks still uniquely own "change exactly this, touch nothing else" is exactly the use case this node feeds. It's a small utility, but it's one of the two or three nodes in this pack you'll actually build a workflow around.

CategoryTest Nodes/SAM3

Inputs (3)

NameTypeDefaultDescription
obj_masksMASK
take_startINT00–4096
take_countINT1-1–4096

Outputs (1)

NameTypeDescription
maskMASK