1
$\begingroup$

I know how to do this manually but I want to know how to do this with a python script. I was trying to use nodes to connect the z buffer to the output. However, when I save this, the outputs are all 0s and 1s.

#switch on nodes
scene.use_nodes = True
tree = 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')    

# create defocus layer
d = tree.nodes.new('CompositorNodeDefocus')
d.use_zbuffer = True

# create output node
v = tree.nodes.new('CompositorNodeViewer')   
v.use_alpha = False

# Links
links.new(rl.outputs[0], d.inputs[0])  # link Image output to defocus Image
links.new(rl.outputs[2], d.inputs[1]) # link Z-Buffer output to defocus Z
links.new(d.outputs[0], v.inputs[0])

# render
bpy.ops.render.render()

# get viewer pixels
pixels = bpy.data.images['Viewer Node'].pixels
print(len(pixels)) # size is always width * height * 4 (rgba)

# copy buffer to numpy array for faster manipulation
arr = np.array(pixels[:])
fname = "depth.npz"
with open(fname, "wb") as f:
    np.savez(f, arr)
$\endgroup$
  • 1
    $\begingroup$ Well, when I try your code and load the resulting file I see a lot of values, many of which are neither zero nor 1. Maybe you're looking at the alpha channel of the RGBA which is mostly going to be zero (transparent) or 1 (opaque). I do not see any place where you tried to save the Z buffer. You didn't even link the Z output to the viewer's Z input. Maybe you want to render the image to an EXR (instead of PNG) including Z Buffer checkbox. Please clarify your question to address some of this confusion. $\endgroup$ – Mutant Bob Jul 1 '16 at 14:38
  • $\begingroup$ I thought the line: links.new(rl.outputs[2], d.inputs[1]) # link Z-Buffer output to defocus Z connects the z output to the defocus z. which is then combined to the output image which is connected to the viewer in: links.new(rl.outputs[2], d.inputs[1]) # link Z-Buffer output to defocus Z Am I not correctly linking the z output to the z input? $\endgroup$ – f34r51n Jul 11 '16 at 14:19
  • $\begingroup$ That links it to the defocus node, but it does not link the Z to the Z input on the viewer, and there seems to be no Composite node (which is the "true" output of the compositor for a regular file output). $\endgroup$ – Mutant Bob Jul 11 '16 at 17:02
3
$\begingroup$

Ok. I finally got the depth data by directly connecting the z data to the viewer node:

# create input render layer node
rl = tree.nodes.new('CompositorNodeRLayers')    

# create output node
v = tree.nodes.new('CompositorNodeViewer')   
v.use_alpha = False

# Links
links.new(rl.outputs['Z'], v.inputs[0]) # link Z to output

# render
bpy.ops.render.render()

And then I output the information into an npz file to be used within python

# get viewer pixels
pixels = bpy.data.images['Viewer Node'].pixels
print(len(pixels)) # size is always width * height * 4 (rgba)

# copy buffer to numpy array for faster manipulation
arr = np.array(pixels[:])
fname2 = "depth.npz" 
with open(fname2, "wb") as f:
    np.savez(f, arr)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.