Skip to content

Add max and min operations to MaskComposite - #15335

Open
NubeBuster wants to merge 1 commit into
Comfy-Org:masterfrom
NubeBuster:maskcomposite-max-min
Open

Add max and min operations to MaskComposite#15335
NubeBuster wants to merge 1 commit into
Comfy-Org:masterfrom
NubeBuster:maskcomposite-max-min

Conversation

@NubeBuster

@NubeBuster NubeBuster commented Aug 5, 2026

Copy link
Copy Markdown

Problem

MaskComposite's and, or and xor operations round both masks before combining:

https://github.com/comfyanonymous/ComfyUI/blob/master/comfy_extras/nodes_mask.py#L290-L294

torch.bitwise_or(destination_portion.round().bool(), source_portion.round().bool()).float()

MASK is a float tensor in [0, 1], and much of the ecosystem exists to produce intermediate values (blur, feather, grow, segmentation confidence). But there is currently no operation on this node that takes the union or intersection of two soft masks without destroying that feathering. multiply gives a product-style intersection, and add an approximate union, but neither is the standard union/intersection, and the three ops that are named like set operations all binarize.

The failure is silent — the output is still a valid MASK, so nothing complains, and the flattened feather only becomes visible much further downstream (e.g. as a hard seam in an inpaint composite).

Change

Adds max and min to the operation list — the standard fuzzy-set union and intersection, which preserve intermediate values:

elif operation == "max":
    output[:, top:bottom, left:right] = torch.max(destination_portion, source_portion)
elif operation == "min":
    output[:, top:bottom, left:right] = torch.min(destination_portion, source_portion)

Also adds a tooltip to the operation widget noting which operations are arithmetic and which binarize, since that distinction is not visible from the names alone.

Compatibility

Non-breaking. Existing operations are untouched, and the new options are appended to the end of the list. Combo values are serialized by name, so saved workflows are unaffected.

Example

destination [0.0, 0.25, 0.75, 1.0], source [0.5, 0.5, 0.5, 0.5]:

operation result
max [0.5, 0.5, 0.75, 1.0]
min [0.0, 0.25, 0.5, 0.5]
or [0.0, 0.0, 1.0, 1.0]

Tests

Adds tests-unit/comfy_extras_test/nodes_mask_test.py covering union/intersection semantics, preservation of intermediate values, commutativity and identity cases, plus one test documenting the existing binarizing behaviour of or. 9 tests, all passing.

🤖 Generated with Claude Code


PR Reviewed for being correct - Mark

@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

✅ All contributors have signed the CLA. Thank you! This PR is ready to be merged.
Posted by the CLA Assistant Lite bot.

@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

MaskComposite now supports documented max and min operations. These operations combine overlapping masks with elementwise tensor maxima or minima and preserve soft-mask values. Unit tests cover intermediate values, commutativity, identity cases, and existing binary or behavior.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly and concisely describes the main change: adding max and min operations to MaskComposite.
Description check ✅ Passed The description explains the new operations, compatibility, tooltip, and unit tests, all of which match the changeset.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@NubeBuster

Copy link
Copy Markdown
Author

I have read and agree to the Contributor License Agreement

comfy-legal added a commit to Comfy-Org/comfy-cla that referenced this pull request Aug 5, 2026
@NubeBuster
NubeBuster force-pushed the maskcomposite-max-min branch from 09252bd to 2a2969a Compare August 8, 2026 05:23
The and/or/xor operations round both masks to 0 or 1 before combining,
so they discard feathering. There was previously no way to take the
union or intersection of two soft masks without losing intermediate
values. max and min are the standard fuzzy-set equivalents and preserve
them.

Also documents on the operation widget which ops are arithmetic and
which binarize, since the distinction is not visible from the names.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@NubeBuster
NubeBuster force-pushed the maskcomposite-max-min branch from 2a2969a to dfaaca1 Compare August 9, 2026 00:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant