遮罩批次合成 (Ref)
Prep your SCAIL-2 reference images so the model stops arguing with its own masks
- images
- masks
- backgrounds
- images
- masks
SCAIL-2 has a specific appetite: it wants a reference image and a colored support mask that says which pixels are the character and which are background. Getting those two into agreement - same subject, same background logic, same ordering - is where most first-time SCAIL-2 workflows quietly go wrong. MaskCompositeRef is the pack's dedicated preprocessor for that, sitting between your reference images + masks and the Wan SCAIL To Video (Multi Ref) node.
It's not a general compositor. It's built for SCAIL-2's two generation strategies, switched by replacement_mode:
- Replacement mode (
replacement_mode = true) - for moving a character out of their old background. Your masks have a black background (RGB < 10/255 = background, colored = person). The node auto-drops any fully-black mask frames (and the matching source image), blackens the background of the remaining images, and ignores thebackgroundsinput entirely. Output: person cut loose from their surroundings, ready for replacement. - Animation / action-transfer mode (
replacement_mode = false, default) - for moving a character's motion onto a new background. Masks have a white background. The node composites the person onto the first image of yourbackgroundsbatch, and any extra background images get inserted into the sequence (between the second-to-last and last frames).process_first_frame/process_last_framecontrol whether the first/last source images participate.
In both modes remove_background is the kill switch for the compositing itself: off, and it only does frame-dropping/size alignment, passing the rest through.
What you set
images- the reference sequence, order must matchmasksone-to-one (the node does not validate ordering - that's on you).masks- the colored mask batch. Not connected → passes through, with an all-black (replacement) or all-white (animation) mask output.backgrounds- only meaningful in animation mode.- The four booleans above.
Outputs are images and masks, both sized to match, ready for Wan SCAIL To Video's reference_image and reference_image_mask inputs. The mask background rules line up exactly with the pack's Create SCAIL-2 Colored Mask node, so the two compose cleanly.
Install
Ships in user2318/ComfyUI-CustomNodeKit. ComfyUI Manager: search "CustomNodeKit". Or:
cd ComfyUI/custom_nodes
git clone https://github.com/user2318/ComfyUI-CustomNodeKit.git
cd ComfyUI-CustomNodeKit
pip install -r requirements.txt
then restart. No extra models - it's numpy/torch math and a LANCZOS resize.
The gotcha
The failure modes are all "garbage in, masks-out-of-sync." Wrong background color: feed a white-background mask into replacement mode and the node thinks the whole frame is background - this is the #1 reason a SCAIL-2 reference comes out as a floating smudge. Out-of-order pairs: since the node doesn't validate ordering, a shuffled images batch gives you a character wearing someone else's mask. And in replacement mode, if every frame is all-black the node returns an empty batch - which sounds like an error but is actually the pack's intended "skip this segment" signal, so a downstream conditional should handle it rather than panic. Match the mask background to the mode, keep the ordering identical, and this node does its one job beautifully.
Inputs (7)
| Name | Type | Default | Description |
|---|---|---|---|
| images | IMAGE | 原图批次 [B, H, W, C]。输入参考图序列,顺序需与 masks 一一对应(节点不校验排序一致性)。Source image batch [B, H, W, C]. The order must correspond one-to-one with masks (node does not validate ordering). | |
| replacement_mode | BOOLEAN | false | True=替换模式:遮罩为黑底彩色(检测纯黑区域为背景),忽略背景图输入,自动剔除遮罩全黑帧。False=动作迁移模式:遮罩为白底彩色(检测纯白区域为背景),与背景图合成。Replacement mode: True=mask has black bg, ignores backgrounds, auto-removes all-black mask frames; False=animation mode: mask has white bg, composites with backgrounds. |
| process_first_frame | BOOLEAN | false | 仅动作迁移模式生效。是否对第1张原图也进行遮罩合成。False时第1张保留不动。Only valid in animation mode. Whether to apply mask composite to the first image. When False, the first image is kept unchanged. |
| process_last_frame | BOOLEAN | true | 仅动作迁移模式生效。True时最后1张原图参与合成。Only valid in animation mode. Whether to apply mask composite to the last image. When True, the last image participates in compositing. |
| remove_background | BOOLEAN | true | 替换模式:关闭时仅做全黑帧剔除+透传,不做背景替换合成。动作迁移模式:关闭时仅做尺寸对齐和背景插入,不做遮罩合成。Whether to remove background. Replacement mode: when disabled, only removes all-black mask frames and passes through. Animation mode: when disabled, only aligns sizes and inserts backgrounds. |
| masksopt | IMAGE | 遮罩批次 [B, H, W, C],顺序必须与 images 一一对应(节点不校验排序一致性)。替换模式:黑底彩色,纯黑(RGB<10/255)区域为背景,全黑帧会被自动剔除。动作迁移模式:白底彩色,纯白(RGB>245/255)区域为背景。未连接时透传原图,遮罩输出根据模式为全黑(替换模式)或全白(动作迁移模式)。Mask batch [B, H, W, C]. Order must correspond to images one-to-one (node does not validate ordering). Replacement mode: black bg, black areas(RGB<10/255)=background, all-black frames auto-removed. Animation mode: white bg, white areas(RGB>245/255)=background. When disconnected, passes through original images with all-black(all-white in animation mode) mask output. | |
| backgroundsopt | IMAGE | 背景图批次 [B, H, W, C]。仅动作迁移模式有效,替换模式下此输入被忽略。第1张用于遮罩合成填充背景,其余插入到合成序列(倒数第2和倒数第1之间)。未连接时用纯黑背景(仅做剔除效果)。Background image batch [B, H, W, C]. Only valid in animation mode; ignored in replacement mode. First image used for compositing background, rest inserted before the last frame. When disconnected, uses pure black background (removal only). |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| images | IMAGE | — |
| masks | IMAGE | — |