Anything Crop (for Swap)
Segment an object and crop it for a swap pipeline
- image
- mask
- crop
- crop_mask
- swap_prompt
- source_masked
- stitch
- detected
Object swapping is a three-act play: cut the thing out, generate a replacement, stitch it back in so the seams don't show. Anything Crop (for Swap) is the first act - the node that finds the object, crops tightly around it, and packages everything downstream needs to undo the crop later. If you've ever hand-cropped a face into a paint editor, generated a patch, and spent an hour fighting the seam back into the original, this is the node that automates that ritual.
It's the first half of WorkflowX's Anything Swap bridge, and it's built on SAM3 - the "Segment Anything" successor that ComfyUI shipped native support for. The design philosophy is worth stating: the crop node emits an opaque SWAP_STITCH payload carrying the clean original and the exact geometry, so the stitch node can put things back pixel-perfectly even if the swap model returns a totally different resolution.
How it works
With use_sam3 on (the default), the node runs SAM3 internally: it loads the SAM3 checkpoint you pick, prompts it with a short concrete noun like "face" or "left hand", and gets back object masks. It then measures the bounding box, expands it per expand_factor (1.4 is a good default - 1.0 hugs the object so tight that your swap model has no context), snaps dimensions to the padding multiple, and crops.
The important contract: the stitch output carries the original image, the pre- and post-resize sizes, and the exact crop geometry. Anything Stitch reads that payload and composites the replacement back without ever asking you to re-align anything. detected (BOOLEAN) tells you whether SAM3 found anything - branch on it to skip downstream work when the mask comes back empty.
Inputs that matter
image(IMAGE) - single image, not a batch.use_sam3+sam3_prompt+sam3_checkpoint- the segmentation path. Turnuse_sam3off to feed your ownmaskinstead.expand_factor/expand_pixels- crop breathing room.force_squaresquares the crop (right for faces, wasteful for a bottle) and is required ifresize_modeistarget_size.swap_prompt(STRING) - the edit instruction, with{target} {width} {height} {caption}tokens substituted on output.
Outputs
crop (IMAGE), crop_mask (MASK), swap_prompt (STRING), source_masked (IMAGE - source with the object blanked, great for preview or an inpaint pass), stitch (SWAP_STITCH), and detected (BOOLEAN).
Install
Part of WorkflowX-Configurator - Manager search WorkflowX Configurator, or:
cd ComfyUI/custom_nodes
git clone https://github.com/haroonaslam/WorkflowX-Configurator
Restart, hard-refresh. The heavy dependency is the model, not the code: you need a SAM3.1 checkpoint in ComfyUI/models/checkpoints/ (e.g. sam3.1_multiplex_fp16.safetensors, from huggingface.co/Comfy-Org/sam3.1), plus a ComfyUI build that includes native SAM3 support (merged in Comfy-Org/ComfyUI#13408). The node will tell you when that's missing.
Common issues
- "SAM3_Detect not found" - your ComfyUI predates native SAM3. Update ComfyUI, or set
use_sam3off and wire your own mask. - Nothing detected / junk mask. Lower isn't always better -
thresholdat 0.5 is a sane floor; below that you find more and more garbage. Useselect_modeandobject_indexto pick among several matches. - Prompt too long. Short concrete nouns beat sentences for SAM3. "face", "shoe", "sunglasses".
One trap worth naming: source_masked blanks the object out, which is not what you want to feed the swap model - that wants the clean crop. Keep the two straight. And remember the Anything Swap approach is detection-crop-rerender-paste: the same loop the detailing ecosystem has run for years, just with a swap model in the middle instead of a face restorer.
Inputs (23)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | Source image. Single image only, not a batch. | |
| use_sam3 | BOOLEAN | true | On: segment internally from sam3_prompt. Off: use the mask input. |
| sam3_prompt | STRING | face | What to segment. Short, concrete nouns work best: 'face', 'left hand', 'shoe', 'sunglasses'. |
| sam3_checkpoint | COMBO | SAM3 checkpoint in models/checkpoints. It carries its own text encoder. | |
| threshold | FLOAT | 0.500–1 | Detection confidence floor. Lower finds more, and more junk. |
| refine_iterations | INT | 20–5 | SAM3 mask refinement passes. 0 is fastest, 2 is the core default. |
| keep_model_loaded | BOOLEAN | true | Keep SAM3 in memory between runs. Off reloads from disk each time. |
| select_mode | COMBO | Which object to crop when the prompt matches several. | |
| object_index | INT | 00–63 | Used only when select_mode is 'index'. |
| expand_factor | FLOAT | 1.401–4 | Grow the bounding box around its centre. 1.0 is a tight crop. |
| expand_pixels | INT | 00–512 | Extra pixels added per side after expand_factor. |
| force_square | BOOLEAN | false | Square the crop. Right for faces, wasteful for hands, shoes, bottles. Required if resize_mode is 'target_size'. |
| padding | COMBO | 16 | Round crop dimensions up to this multiple. |
| edge_handling | COMBO | shift: slide the box inside. pad_replicate: edge-pad the source. clamp: truncate. | |
| resize_mode | COMBO | none preserves native resolution and is lossless. | |
| target_size | INT | 102464–8192 | — |
| upscale_method | COMBO | 5 options: lanczos, bicubic, bilinear, area, nearest-exact | |
| downscale_method | COMBO | 4 options: area, lanczos, bicubic, bilinear | |
| mask_grow | INT | 0-256–256 | Dilate (+) or erode (-) before the bbox is measured. |
| mask_blur | INT | 30–256 | Softens only the mask handed downstream. Does not affect stitching. |
| swap_prompt | STRING | Replace the {target} with the reference. Match the original lighting direction, colour temperature, and perspective. Preserve everything outside the mask exactly. Blend the edges seamlessly with no visible seam or colour shift. | Passed to the output. Tokens: {target} {width} {height} {caption} |
| maskopt | MASK | Used when use_sam3 is off. | |
| captionopt | STRING | Optional VLM caption, substituted into {caption}. |
Outputs (6)
| Name | Type | Description |
|---|---|---|
| crop | IMAGE | The cropped region, ready for your swap model or API. |
| crop_mask | MASK | The object's mask inside the crop, blurred by mask_blur. |
| swap_prompt | STRING | swap_prompt with tokens substituted. |
| source_masked | IMAGE | Source with the object blanked. Preview, or feed an inpaint model. |
| stitch | SWAP_STITCH | Opaque payload for Anything Stitch. Carries the clean original and exact geometry. |
| detected | BOOLEAN | False when nothing was segmented. Branch on this to skip work. |