🎨 SAM Segment Hair + Outfit (ComfyUI-tracked)
Two sprites, one node, and a checkpoint you have to supply
- image
- hair_sprite
Strip a character down to parts. That's what this node does: feed it a character image, get the hair and the outfit back as separate cut-outs on white. It's the front half of a mesh pipeline - segment the hair from the portrait, generate hair as its own 3D asset, same for the clothing, then reassemble the character. If you've been masking hair by hand to feed TRELLIS, this is the shortcut.
It uses Meta's Segment Anything (ViT-B) with prompt points placed by cheap geometry rather than by hand: the topmost foreground pixel is almost certainly hair, the torso at ~55% down the figure is almost certainly clothing, and the face at ~28% is a negative point so the hair mask doesn't eat the face.
How it works
The foreground is found without any model at all - sample the four image corners, average them into a background colour, threshold the per-pixel difference, and run a small morphological open/close to clean it up. That gives you a bounding box, and the box gives you the prompt points. Then SAM does the actual segmentation with multimask_output=True, keeping the highest-scoring mask, intersected with the foreground mask. Both results get composited on white.
The wrapping is the interesting engineering. SAM's SamPredictor isn't a torch.nn.Module, so ComfyUI's VRAM accounting couldn't see it and couldn't evict it. Rather than squat on a module-level global predictor forever, the pack wraps it in a small adapter that forwards .to(), .parameters(), .buffers() and .children() to the underlying model and hangs it off ComfyUI's current_loaded_models pipeline patcher. Net effect: ComfyUI knows how much VRAM SAM is using and can free it when something bigger shows up - instead of the usual outcome where a half-forgotten model squats in VRAM and your next sampler OOMs. That's a real quality-of-life win, and worth stealing for your own nodes.
The hair/clothing prompt heuristics are, by the author's own admission, application-specific - no upstream node knows what "hair" means. The source notes that if you want a general SAM loader, install Impact Pack or ComfyUI-Segment-Anything and wrap theirs instead.
Inputs and outputs
image- the character image, straight from aLoadImagenode.
One output: hair_sprite, a STRING path to the hair PNG written into ComfyUI's output/.
Read that again, because it surprises people. The node produces two PNGs - hair and outfit - and files them both, but only the hair path comes out of the socket. Both are reported in the UI's image list, so you can see and open the outfit sprite; to use it programmatically you have to reach into output/ by name. The outfit file is named sam_outfit_<timestamp>.png, the hair sam_hair_<timestamp>.png.
Install
cd /path/to/ComfyUI/custom_nodes
git clone https://github.com/JayDataEngineer/melite-poser-nodes.git
Restart, or install melite-poser-nodes from ComfyUI Manager. Now the part that will cost you twenty minutes if nobody warns you: the pack's requirements.txt does not include SAM's dependencies. You need, separately:
segment_anything- Meta's official package, not a reimplementation.pip install git+https://github.com/facebookresearch/segment-anything.git, or thesegment-anythingpip package.sam_vit_b_01ec64.pth- Meta's official ViT-B weights. The node checks, in order,$SAM_VIT_B_CHECKPOINT,/mnt/data/models/image-gen/comfyui/sams/sam_vit_b_01ec64.pth,/root/ComfyUI/models/sams/sam_vit_b_01ec64.pth, then/tmp/sam_vit_b.pth. If none exist it raises with the full candidate list, which is genuinely helpful.opencv-python(cv2) - used for the foreground/morphology pass and the colour conversion.
The easy setup is the environment variable:
export SAM_VIT_B_CHECKPOINT=/your/models/sams/sam_vit_b_01ec64.pth
Where people get burned
The prompt heuristics assume a plain background and a centred, front-on subject. The foreground detection samples the four corners and assumes they're background. A photo with a busy background, a vignette, a gradient, or a subject that touches a corner will give you a wrong bounding box, and every downstream prompt point inherits that error. Generated portraits on flat or transparent backgrounds are the intended input.
Uniform images fail loudly, which is nice. If the foreground detection finds nothing, you get no foreground detected - the source image may be uniform-colored or all-background rather than an empty sprite.
A hood, a hat, a helmet, or heavy shadow will confuse the hair prompt. The topmost pixel is hair when there's hair at the top. Swap it for a cap and the mask may find fabric, or find nothing. Same for clothing: a subject in a full-body cloak at 55% down will segment the cloak, which may or may not be what you wanted.
It's not SAM 3. ComfyUI's own nodes_sam3.py is for Wan-video-adjacent diffusion models, not static-image segmentation. The source audit is explicit that this node is the only thing in the tree importing segment_anything. So don't go looking for it under "SAM3" - that's a different tool for a different job.
The sprites are RGB on white, not masks. Useful for feeding image-to-3D, less useful if you wanted alpha. There is no alpha channel coming out of this node.
Inputs (1)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | Source character image — connect a LoadImage output here. |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| hair_sprite | STRING | — |