Nodes/WorkflowX Configurator/Anything Crop (for Swap)
ComfyUI Node

Anything Crop (for Swap)

Segment an object and crop it for a swap pipeline

By haroonaslam·Created 4 months ago·Updated 7 days ago· 10
Anything Crop (for Swap)
  • image
  • mask
  • crop
  • crop_mask
  • swap_prompt
  • source_masked
  • stitch
  • detected
use_sam3true
sam3_promptface
sam3_checkpoint
threshold0.50
refine_iterations2
keep_model_loadedtrue
select_mode
object_index0
expand_factor1.40
expand_pixels0
force_squarefalse
padding16
edge_handling
resize_mode
target_size1024
upscale_method
downscale_method
mask_grow0
mask_blur3
swap_promptReplace 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.
caption

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. Turn use_sam3 off to feed your own mask instead.
  • expand_factor / expand_pixels - crop breathing room. force_square squares the crop (right for faces, wasteful for a bottle) and is required if resize_mode is target_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_sam3 off and wire your own mask.
  • Nothing detected / junk mask. Lower isn't always better - threshold at 0.5 is a sane floor; below that you find more and more garbage. Use select_mode and object_index to 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.

CategoryWorkflowX/Anything Swap

Inputs (23)

NameTypeDefaultDescription
imageIMAGESource image. Single image only, not a batch.
use_sam3BOOLEANtrueOn: segment internally from sam3_prompt. Off: use the mask input.
sam3_promptSTRINGfaceWhat to segment. Short, concrete nouns work best: 'face', 'left hand', 'shoe', 'sunglasses'.
sam3_checkpointCOMBOSAM3 checkpoint in models/checkpoints. It carries its own text encoder.
thresholdFLOAT0.500–1Detection confidence floor. Lower finds more, and more junk.
refine_iterationsINT20–5SAM3 mask refinement passes. 0 is fastest, 2 is the core default.
keep_model_loadedBOOLEANtrueKeep SAM3 in memory between runs. Off reloads from disk each time.
select_modeCOMBOWhich object to crop when the prompt matches several.
object_indexINT00–63Used only when select_mode is 'index'.
expand_factorFLOAT1.401–4Grow the bounding box around its centre. 1.0 is a tight crop.
expand_pixelsINT00–512Extra pixels added per side after expand_factor.
force_squareBOOLEANfalseSquare the crop. Right for faces, wasteful for hands, shoes, bottles. Required if resize_mode is 'target_size'.
paddingCOMBO16Round crop dimensions up to this multiple.
edge_handlingCOMBOshift: slide the box inside. pad_replicate: edge-pad the source. clamp: truncate.
resize_modeCOMBOnone preserves native resolution and is lossless.
target_sizeINT102464–8192
upscale_methodCOMBO5 options: lanczos, bicubic, bilinear, area, nearest-exact
downscale_methodCOMBO4 options: area, lanczos, bicubic, bilinear
mask_growINT0-256–256Dilate (+) or erode (-) before the bbox is measured.
mask_blurINT30–256Softens only the mask handed downstream. Does not affect stitching.
swap_promptSTRINGReplace 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}
maskoptMASKUsed when use_sam3 is off.
captionoptSTRINGOptional VLM caption, substituted into {caption}.

Outputs (6)

NameTypeDescription
cropIMAGEThe cropped region, ready for your swap model or API.
crop_maskMASKThe object's mask inside the crop, blurred by mask_blur.
swap_promptSTRINGswap_prompt with tokens substituted.
source_maskedIMAGESource with the object blanked. Preview, or feed an inpaint model.
stitchSWAP_STITCHOpaque payload for Anything Stitch. Carries the clean original and exact geometry.
detectedBOOLEANFalse when nothing was segmented. Branch on this to skip work.