Depth-of-Field Mask (MEC)
Turn a depth pass into a defocus mask — the DoF node that understands VFX terms
- depth
- coc_mask
- in_focus_mask
You've got a depth pass - from Depth Anything, a renderer, or any depth node - and you want to simulate depth of field. The compositor's move is to compute a circle of confusion: how blurry each pixel should be, based on how far it sits from the focus plane. DepthOfFieldMaskMEC converts your depth image into exactly that: a coc_mask you can drive a blur with, plus an in_focus_mask that says which pixels are sharp. It speaks the VFX vocabulary, and that's rare in a ComfyUI node.
It's one of the pack's Render suite nodes, sitting next to Merge Render Passes. If you render CG passes or generate a depth pass from a diffusion model and want the final shot to have a real lens feel - sharp subject, falling-off background - this gives you the mask to do it. Pair it with a blur node (or the pack's own post-processors) and you've got an end-to-end DoF rig.
How it works
The math is the definition of depth of field, simplified for masks:
coc = clamp(|depth - focus_distance| / aperture, 0, 1)
focus_distance is where the plane of focus sits in your depth range; aperture scales how fast defocus grows away from that plane. Pixels at the focus distance get a CoC of 0 (perfectly sharp); pixels further away ramp toward 1 (maximally blurry). The node outputs that raw CoC as coc_mask - the blur amount per pixel - and an in_focus_mask for sharpness (which can be inverted to give you the defocus region instead). Depth is read from a channel you choose (R, G, B, or luma), which matters when your depth pass is packed into an RGB image.
The inputs that matter
- depth - the depth
IMAGE. If 3-channel, you pick the channel; R is the default. - focus_distance (0.5) - where in normalized depth the focus sits.
- aperture (0.1) - how quickly defocus spreads. Bigger aperture = shallower apparent DoF.
- invert - flip the in-focus mask to defocus-only.
Outputs are coc_mask and in_focus_mask, both MASK.
Installing it
Part of Code2Collapse/ComfyUI-CustomNodePacks. ComfyUI Manager → search "CustomNodePacks", or:
cd ComfyUI/custom_nodes
git clone https://github.com/Code2Collapse/ComfyUI-CustomNodePacks.git
Restart ComfyUI. Pure tensor math - no models, no downloads. (Standard pack rule: don't run the root pip install -r requirements.txt over ComfyUI's bundled torch/numpy.)
Gotchas
The whole game is making focus_distance and aperture match your depth convention. If your depth is "near=bright" (white close, black far) and you set focus at 0.5 expecting the middle of the scene to be sharp, you'll find the focus lands somewhere entirely different - check whether your depth source is near-bright or far-bright before tuning. Also, this node produces the mask, not the blur - you still need to apply a spatially varying blur using the CoC. And a depth pass with a hard ramp (like a Depth Anything map with soft gradients) will give a soft, natural falloff, while a stepped depth pass gives stepped blur; that's the source's fault, not the node's. Get the convention right and it's a lovely little DoF rig.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| depth | IMAGE | — | |
| focus_distance | FLOAT | 0.5000–100 | — |
| aperture | FLOAT | 0.1000.001–100 | — |
| invertopt | BOOLEAN | false | Invert the focus mask (1 = in focus, 0 = defocus). |
| depth_channelopt | COMBO | R | 4 options: R, G, B, luma |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| coc_mask | MASK | — |
| in_focus_mask | MASK | — |