LayerMask: SAM2 Ultra V2(Advance)
SAM2 segmentation from a bounding box, with the model shared across nodes
- sam2_model
- image
- bboxes
- image
- mask
SAM2UltraV2 takes bounding boxes - from any detector in the pack: Gemini, GroundingDINO+SAM, Florence2, YOLO, or a hand-painted mask via ObjectDetectorMask - and turns them into precise segmentation masks using Meta's SAM2. It's a fix on top of the original SAM2Ultra node, and the fix is specifically about efficiency: V1 loaded its SAM2 model inline every time, which means reloading from scratch for every node in a graph that used it. V2 changes sam2_model to an external input, loaded once by a separate LoadSAM2Model node and shared across as many SAM2UltraV2 calls as you need. If your graph runs multiple segmentation passes, that's the difference between one model load and several.
The inputs and outputs that matter
sam2_model- required, typeLS_SAM2_MODEL, from LoadSAM2Model (which handles model selection and precision separately).image/bboxes- required. Boxes from any bbox-producing node in the pack.bbox_select-all,first, orby_index, same pattern as elsewhere in this pack, for when you've got multiple boxes but only want SAM2 to act on some of them.select_index(default"0,") - relevant only withby_index; comma-separated list, 0-indexed.detail_method-VITMatte,VITMatte(local),vitmatte-base-composition-1k,PyMatting, orGuidedFilter, the same edge-refinement pass as the pack's other Ultra nodes.detail_erode(default 6) /detail_dilate(default 4),black_point(0.15) /white_point(0.99),process_detail(default on),max_megapixels(default 2) - standard refinement controls.
Outputs are image and mask.
The thing to know before you build a batch workflow
SAM2 Ultra only processes a single image per call. If you're trying to run this over a batch, the README is explicit: convert the batch to an image list first, not just an image batch tensor - otherwise you won't get the per-frame handling you're expecting.
Installing it
ComfyUI Manager: search "ComfyUI Layer Style Advance". Manual:
cd ComfyUI/custom_nodes
git clone https://github.com/chflame163/ComfyUI_LayerStyle_Advance.git
Run install_requirements.bat (portable) or install requirements.txt yourself, then restart.
SAM2 model files are a separate download, into ComfyUI/models/sam2 - grab them from huggingface.co/Kijai/sam2-safetensors or the linked BaiduNetdisk mirror. This node builds on kijai/ComfyUI-segment-anything-2, and the model repackaging credit goes there too.
Common issues
"Works on one image, breaks or produces wrong output on a batch." This is the single-image limitation above - convert to an image list before feeding a batch through, don't assume it handles a batch tensor the way most other nodes do.
Model reloads every single node execution despite having multiple SAM2UltraV2 nodes in the graph. Check you're actually feeding the same LoadSAM2Model output into all of them, rather than instantiating a separate LoadSAM2Model per SAM2UltraV2 node - the sharing only helps if the model is genuinely one shared object in the graph.
ValueError: Trimap did not contain foreground values. Same known LayerStyle Advance error as elsewhere in the pack, tied to PyMatting as the detail method when the mask area is too large or too small. Adjust detail_erode/detail_dilate or switch detail_method.
Mask is off relative to where the object actually is. Confirm bboxes came from a detection run on this exact image, not a resized or cropped version - bbox coordinates are only meaningful relative to the image they were computed against, the same caveat that applies to DrawBBoxMask.
Inputs (12)
| Name | Type | Default | Description |
|---|---|---|---|
| sam2_model | LS_SAM2_MODEL | — | |
| image | IMAGE | — | |
| bboxes | BBOXES | — | |
| bbox_select | COMBO | 3 options: all, first, by_index | |
| select_index | STRING | 0, | — |
| detail_method | COMBO | 5 options: VITMatte, VITMatte(local), vitmatte-base-composition-1k, PyMatting, GuidedFilter | |
| detail_erode | INT | 61–255 | — |
| detail_dilate | INT | 41–255 | — |
| black_point | FLOAT | 0.150.01–0.98 | — |
| white_point | FLOAT | 0.990.02–0.99 | — |
| process_detail | BOOLEAN | true | — |
| max_megapixels | FLOAT | 2.01–999 | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| image | IMAGE | — |
| mask | MASK | — |