Nodes/ComfyUI-CustomNodePacks/Depth-of-Field Mask (MEC)
ComfyUI Node

Depth-of-Field Mask (MEC)

Turn a depth pass into a defocus mask — the DoF node that understands VFX terms

By Code2Collapse·Created 6 months ago·Updated a day ago· 52
Depth-of-Field Mask (MEC)
  • depth
  • coc_mask
  • in_focus_mask
focus_distance0.500
aperture0.100
invertfalse
depth_channelR

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.

CategoryMaskEditControl/Render

Inputs (5)

NameTypeDefaultDescription
depthIMAGE
focus_distanceFLOAT0.5000–100
apertureFLOAT0.1000.001–100
invertoptBOOLEANfalseInvert the focus mask (1 = in focus, 0 = defocus).
depth_channeloptCOMBOR4 options: R, G, B, luma

Outputs (2)

NameTypeDescription
coc_maskMASK
in_focus_maskMASK