Hi, developers. I am trying to generate depth (z) image for some research purpose, but when I adjust values in Compositor node, I cannot find the min and max depth in current scene in order to map z to 0-1 full range.
In code below, I marked two places that I don’t know where to get variables from context to: have rendered image lightest area to be the most closed to camera, and darkest to be the farthest for any model.
Here’s my section of code cited from post from Stack Overflow.
# Set up rendering of depth map:
bpy.context.scene.use_nodes = True
tree = bpy.context.scene.node_tree
links = tree.links
# clear default nodes
for n in tree.nodes:
tree.nodes.remove(n)
# create input render layer node
rl = tree.nodes.new('CompositorNodeRLayers')
map = tree.nodes.new(type="CompositorNodeMapRange")
# Size is chosen kind of arbitrarily, try out until you're satisfied with resulting depth map.
map.inputs[1].default_value = 0.6 ##### Min value here
map.inputs[2].default_value = 1.65 ##### Max value here
map.inputs[3].default_value = 0.0
map.inputs[4].default_value = 1.0
links.new(rl.outputs[2], map.inputs[0])
invert = tree.nodes.new(type="CompositorNodeInvert")
links.new(map.outputs[0], invert.inputs[1])
# The viewer can come in handy for inspecting the results in the GUI
depthViewer = tree.nodes.new(type="CompositorNodeViewer")
links.new(invert.outputs[0], depthViewer.inputs[0])
# Use alpha from input.
links.new(rl.outputs[1], depthViewer.inputs[1])
# create a file output node and set the path
fileOutput = tree.nodes.new(type="CompositorNodeOutputFile")
fileOutput.base_path = out_z_path
links.new(invert.outputs[0], fileOutput.inputs[0])
bpy.ops.render.render()