Nodes/ComfyUi-MpiNodes/Mpi Mask Bbox
ComfyUI Node

Mpi Mask Bbox

Where the mask is, how big it is, and the mask itself, on one node

By MadPonyInteractive·Created 11 months ago·Updated 4 days ago· 3
Mpi Mask Bbox
  • mask
  • mask
  • mpi_box
  • width
  • height
padding0
divisible_by1

Crop-edit-stitch is the pattern that quietly won inpainting in 2026. You cut a region out of the plate, run the expensive model on that crop alone, and composite the result back through the mask. The unmasked pixels are never touched, not even by a VAE round trip, which is the property people spent years of discipline trying to fake with denoise settings. It's why the whole thing works so well with edit models like Qwen-Image-Edit, whose output is slightly misaligned even at the "correct" resolution.

The missing piece is almost always the same: you have a mask, and now you need the rectangle around it so something can crop.

MpiMaskBbox is that conversation, in one node. You feed it a MASK, it gives you the tight rectangular bounding box as an MPI_BOX plus width and height - and it hands the mask straight back, untouched.

That last bit is the reason to pick this over the pack's other box node. MpiMaskSquareBbox squares the box (w == h, whatever the subject) and returns a filled block where the mask was. Both of those are correct for what it was written for - a still, square reference crop - and both are wrong when the box is only supposed to say where to crop. Here, the same node gives the box to MpiBoxCrop and the real mask shape to whatever composites the result back, so you don't have to fork the mask upstream and remember which branch was the measured one.

Inputs and outputs

mask - the region. Anything that emits a MASK works: a SAM/SAM3 detect node, a grounded-box convert, a hand-painted mask.

padding - pixels added around the tight box, before divisible_by runs. Default 0. This is worth more attention than it gets: grounding boxes from a VLM are tighter than you expect - they wrap the cup and not the saucer - and on a face mask you want a bit of forehead and chin in the crop for the model to have context. Set it to 0 and your crop is exactly the mask's extents and nothing more.

divisible_by - rounds width and height up to a multiple, then re-centres the grown box on the tight one so the extra pixels are shared by both edges rather than pushed onto the right and bottom. 1 is off. Use 8 or 16 when a downstream node needs its dimensions on a grid.

Outputs: mpi_box (an MPI_BOX) for the box-aware nodes - MpiBoxCrop, MpiBoxMask, and MpiFromBox if you need raw integers back out; width and height as plain INTs, which is what a resize or an empty-latent actually consumes; and mask, unchanged. There are deliberately no x/y outputs, because MpiFromBox already unpacks an MPI_BOX and duplicating that would give you two ways to spell the same wire.

The two behaviours that surprise people

On a batch - a video mask - the box is the union of every frame, and one box comes back for the whole clip. That is not laziness, it's the point: a per-frame box crawls, and a crawling crop seam is far more visible in motion than a fixed one. You get a stable crop window and lay the per-frame mask over it.

An empty mask returns a zero box rather than raising. No crash, no exception - width and height come out 0 and the box is (0, 0, 0, 0). Safe, and also easy to miss: if a crop node starts behaving oddly, check whether the detector actually found anything before you debug the crop.

Edge cases are handled honestly rather than cleverly. Where the grown size overruns the image, the largest multiple that still fits wins; where the image is itself smaller than divisible_by, the image dimension is returned rather than a zero-sized crop that fails somewhere far away from the cause.

Install

Ships in MadPonyInteractive/ComfyUi-MpiNodes - ComfyUI Manager → search ComfyUi-MpiNodes (registry publisher mad-pony-interactive), or:

cd ComfyUI/custom_nodes
git clone https://github.com/MadPonyInteractive/ComfyUi-MpiNodes

Restart, then search "Mpi Mask Bbox" (it lives under MpiNodes/ImgOps). No requirements.txt, no models, nothing to pip - numpy, scipy and Pillow are already ComfyUI dependencies. AGPL-3.0 from v1.2.7 (MIT up to v1.2.6). If you need raw integers for a core node, MpiBox builds an MPI_BOX from width/height/x/y and MpiFromBox reverses it.

Where people get burned

Wiring mpi_box into a core node. MPI_BOX is this pack's own type. Core nodes want four integers; go through MpiFromBox.

Setting divisible_by on a small region. It only ever rounds up, so a 45-pixel-tall box with divisible_by = 64 becomes 64 and gets re-centred - you may have grown the crop past what you wanted (and past the image, which is why the clamp exists).

Expecting a per-frame box. If you wanted the crop to follow the subject through a clip, this isn't the node - it holds still by design. That's usually the right answer, just not always the one people came for.

CategoryMpiNodes/ImgOps

Inputs (3)

NameTypeDefaultDescription
maskMASK
paddingINT00–4096Pixels added around the tight mask box, before divisible_by.
divisible_byINT11–512Round width and height UP to a multiple of this. 1 = off.

Outputs (4)

NameTypeDescription
maskMASK
mpi_boxMPI_BOX
widthINT
heightINT