Skip to content
Snippets Groups Projects
Commit 94df2c49 authored by Nicolas Pope's avatar Nicolas Pope
Browse files

Tidy blender python script

parent 01eb49b6
No related branches found
No related tags found
No related merge requests found
...@@ -140,34 +140,18 @@ def render(): ...@@ -140,34 +140,18 @@ def render():
rl = tree.nodes.new('CompositorNodeRLayers') rl = tree.nodes.new('CompositorNodeRLayers')
v = tree.nodes.new('CompositorNodeViewer') v = tree.nodes.new('CompositorNodeViewer')
v.use_alpha = False v.use_alpha = True
#setup the depthmap calculation using blender's mist function:
#bpy.context.scene.render.layers['RenderLayer'].use_pass_mist = True
#bpy.context.scene.view_layers["View Layer"].use_pass_mist = True
#the depthmap can be calculated as the distance between objects and camera ('LINEAR'), or square/inverse square of the distance ('QUADRATIC'/'INVERSEQUADRATIC'):
bpy.context.scene.world.mist_settings.falloff = 'LINEAR'
#minimum depth:
bpy.context.scene.world.mist_settings.intensity = 0.1
#maximum depth (can be changed depending on the scene geometry to normalize the depth map whatever the camera orientation and position is):
bpy.context.scene.world.mist_settings.depth = 8.0
links.new(rl.outputs['Depth'],v.inputs['Image'])
bpy.ops.render.render()
pixels = bpy.data.images['Viewer Node']
depthim = (np.array(pixels.pixels[:]).reshape((pixels.size[1], pixels.size[0], pixels.channels))[:,:,0]).astype(np.float32)
# set invalid depth values to 0.0
depthim[depthim >= _d_max] = 0.0
print(np.amax(depthim))
links.new(rl.outputs['Image'], v.inputs['Image']) links.new(rl.outputs['Image'], v.inputs['Image'])
# depth cannot be accessed in python; hack uses alpha to store z-values # depth cannot be accessed in python; hack uses alpha to store z-values
#links.new(rl.outputs['Depth'], v.inputs['Alpha']) links.new(rl.outputs['Depth'], v.inputs['Alpha'])
bpy.ops.render.render() bpy.ops.render.render()
pixels2 = bpy.data.images['Viewer Node'] pixels = bpy.data.images['Viewer Node']
im = np.array(pixels2.pixels[:]).reshape((pixels2.size[1], pixels2.size[0], pixels2.channels)) im = np.array(pixels.pixels[:]).reshape((pixels.size[1], pixels.size[0], pixels.channels))
depthim = (np.array(pixels.pixels[:]).reshape((pixels.size[1], pixels.size[0], pixels.channels))[:,:,3]).astype(np.float32)
# set invalid depth values to 0.0
depthim[depthim >= _d_max] = 0.0
return (im[:,:,0:3]*255.0).astype(np.uint8), depthim return (im[:,:,0:3]*255.0).astype(np.uint8), depthim
...@@ -181,7 +165,7 @@ def render_stereo(camera, baseline=0.15): ...@@ -181,7 +165,7 @@ def render_stereo(camera, baseline=0.15):
imR, depthR = render() imR, depthR = render()
camera_location = location_old camera.location = location_old
return StereoImage(np.array(K), pose, baseline, imL, depthL, imR, depthR) return StereoImage(np.array(K), pose, baseline, imL, depthL, imR, depthR)
...@@ -217,8 +201,8 @@ scale = bpy.context.scene.render.resolution_percentage / 100 ...@@ -217,8 +201,8 @@ scale = bpy.context.scene.render.resolution_percentage / 100
resolution_x_in_px = scale * bpy.context.scene.render.resolution_x resolution_x_in_px = scale * bpy.context.scene.render.resolution_x
resolution_y_in_px = scale * bpy.context.scene.render.resolution_y resolution_y_in_px = scale * bpy.context.scene.render.resolution_y
err = ftlIntrinsicsWriteLeft(c_void_p(stream), c_int(0), c_int(int(resolution_x_in_px)), c_int(int(resolution_y_in_px)), c_float(300.0), c_float(-resolution_x_in_px/2), c_float(-resolution_y_in_px/2), c_float(0.1), c_float(0.1), c_float(8.0)) err = ftlIntrinsicsWriteLeft(c_void_p(stream), c_int(0), c_int(int(resolution_x_in_px)), c_int(int(resolution_y_in_px)), c_float(300.0), c_float(-resolution_x_in_px/2), c_float(-resolution_y_in_px/2), c_float(0.1), c_float(0.1), c_float(16.0))
err = ftlIntrinsicsWriteRight(c_void_p(stream), c_int(0), c_int(int(resolution_x_in_px)), c_int(int(resolution_y_in_px)), c_float(300.0), c_float(-resolution_x_in_px/2), c_float(-resolution_y_in_px/2), c_float(0.1), c_float(0.1), c_float(8.0)) err = ftlIntrinsicsWriteRight(c_void_p(stream), c_int(0), c_int(int(resolution_x_in_px)), c_int(int(resolution_y_in_px)), c_float(300.0), c_float(-resolution_x_in_px/2), c_float(-resolution_y_in_px/2), c_float(0.1), c_float(0.1), c_float(16.0))
print(err) print(err)
err = ftlImageWrite(stream, 0, 0, 3, 0, image.imL.ctypes.data_as(c_void_p)) err = ftlImageWrite(stream, 0, 0, 3, 0, image.imL.ctypes.data_as(c_void_p))
print(err) print(err)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment