Nodes/ComfyUI-NormalsToDepth/Depth Map Math Combiner
ComfyUI Node

Depth Map Math Combiner

Do actual math on depth maps with a text expression

By EMkrtchyan·Created 9 months ago·Updated 7 months ago· 1
Depth Map Math Combiner
  • depth_map_x
  • depth_map_y
  • combined_depth
expressiona * X + b * Y
a1.00
b1.00

Most depth nodes do one rigid thing. "Depth Map Math Combiner" hands you a text box and a tiny math language, and lets you write the operation yourself. It's the glue node of the ComfyUI-NormalsToDepth pack: take two depth maps, combine or remap them however you like, get one depth map out.

What it's for

Depth maps come from different estimators with different personalities. Depth Anything is the community's default daily driver; Marigold trades speed for maximum sharpness (both get their due in the knowledge base's model rundown). When you want strengths of both, you blend - and a weighted average is the obvious move. Or you want to remap a single map: boost contrast, log-scale it, soften it. This node is that, with an expression instead of a preset dropdown.

How it works

The two depth maps are bound to the names X and Y; the float inputs a and b are also in scope. The expression string is evaluated against a small sandboxed namespace - torch plus a whitelist of log, exp, sqrt, sin, cos, abs, max, min, pow - and the result is clamped to [0,1] so it stays a valid image.

The default a * X + b * Y is a weighted blend: a=0.5, b=0.5 is a 50/50 mix, a=0.7, b=0.3 leans on one source. A few that actually come up:

a * X + (1 - a) * Y      # crossfade between the two maps
torch.log(Y + 0.001)     # log remap - pulls detail out of the dark end
torch.pow(X, 2)          # square - pushes midtones down, more contrast

Inputs and outputs

  • depth_map_x, depth_map_y - IMAGE. The two sources. The code doesn't resize or validate them, so feed matching dimensions or you're in broadcasting roulette.
  • expression - STRING, default a * X + b * Y. This is where you live. Uppercase X/Y - lowercase won't resolve.
  • a, b - FLOAT, -100 to 100, step 0.01. The coefficients the default expression uses.

Output: combined_depth, an IMAGE, clamped to 0–1.

Installing

Same pack, same drill - ComfyUI Manager → search "ComfyUI-NormalsToDepth", or:

cd ComfyUI/custom_nodes
git clone https://github.com/EMkrtchyan/ComfyUI-NormalsToDepth

Restart. Only deps are scipy, numpy, torch; nothing to download, no models to fetch.

Where people get burned

  • Silent failure. If your expression errors, the node prints [DepthMapMathCombiner] Error evaluating expression: ... to the console and quietly returns X unchanged. You get a "combined" output that is just the first input - no red node, no popup. Watch the console when results look wrong.
  • Don't get clever. The namespace strips __builtins__ and only exposes those torch functions, so a lot of normal Python won't parse - and when it fails, see the bullet above. Keep expressions to one line of arithmetic.
  • No shape checking. Mismatched map sizes aren't caught, so you get a broadcasting error (or silent weirdness) instead of a resize. Normalize resolutions before the node, not after.

It's a small, unceremonious pack - the README is one screenshot - but this node is genuinely handy as a depth mixer, and it's a surprisingly fun way to learn what an estimator's maps actually contain by doing math on them.

CategoryDepthSolvers

Inputs (5)

NameTypeDefaultDescription
depth_map_xIMAGE
depth_map_yIMAGE
expressionSTRINGa * X + b * Y
aFLOAT1.00-100–100
bFLOAT1.00-100–100

Outputs (1)

NameTypeDescription
combined_depthIMAGE