From f77a7932a644f7acaee93176ae21bdd87c13e765 Mon Sep 17 00:00:00 2001 From: panmari Date: Tue, 3 Apr 2018 22:18:39 +0200 Subject: [PATCH] Adding flag for output format. Exporting output as OPEN_EXR preserves the full range of values, which is especially important for depth. --- README.md | 7 +++++-- render_blender.py | 20 +++++++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 06613a1..1c4440f 100644 --- a/README.md +++ b/README.md @@ -2,16 +2,19 @@ A little helper script to render .obj files (such as from the stanford shapenet database) with Blender. -Tested on Linux, but should also work for other operating systems. -By default, this scripts generates 30 images by rotating the camera around the object. +Tested on Linux, but should also work for other operating systems. +By default, this scripts generates 30 images by rotating the camera around the object. Additionally, depth, albedo and normal maps are dumped for every image. +Tested with Blender 2.79. + ## Example invocation To render a single `.obj` file, run blender --background --python render_blender.py -- --output_folder /tmp path_to_model.obj +To get raw values that are easiest for further use, use `--format OPEN_EXR`. ## Batch rendering diff --git a/render_blender.py b/render_blender.py index 4bcd2db..89cf96d 100644 --- a/render_blender.py +++ b/render_blender.py @@ -21,9 +21,11 @@ parser.add_argument('--edge_split', type=bool, default=True, help='Adds edge split filter.') parser.add_argument('--depth_scale', type=float, default=1.4, - help='Scaling that is applied to depth. Depends on size of mesh. Try out various values until you get a good result.') + help='Scaling that is applied to depth. Depends on size of mesh. Try out various values until you get a good result. Ignored if format is OPEN_EXR.') parser.add_argument('--color_depth', type=str, default='8', help='Number of bit per channel used for output. Either 8 or 16.') +parser.add_argument('--format', type=str, default='PNG', + help='Format of files generated. Either PNG or OPEN_EXR') argv = sys.argv[sys.argv.index("--") + 1:] args = parser.parse_args(argv) @@ -38,6 +40,7 @@ # Add passes for additionally dumping albedo and normals. bpy.context.scene.render.layers["RenderLayer"].use_pass_normal = True bpy.context.scene.render.layers["RenderLayer"].use_pass_color = True +bpy.context.scene.render.image_settings.file_format = args.format bpy.context.scene.render.image_settings.color_depth = args.color_depth # Clear default nodes @@ -47,10 +50,21 @@ # Create input render layer node. render_layers = tree.nodes.new('CompositorNodeRLayers') -# Create a file output node and set the path. depth_file_output = tree.nodes.new(type="CompositorNodeOutputFile") depth_file_output.label = 'Depth Output' -links.new(render_layers.outputs['Depth'], depth_file_output.inputs[0]) +if args.format == 'OPEN_EXR': + links.new(render_layers.outputs['Depth'], depth_file_output.inputs[0]) +else: + # Remap as other types can not represent the full range of depth. + map = tree.nodes.new(type="CompositorNodeMapValue") + # Size is chosen kind of arbitrarily, try out until you're satisfied with resulting depth map. + map.offset = [-0.7] + map.size = [args.depth_scale] + map.use_min = True + map.min = [0] + links.new(render_layers.outputs['Depth'], map.inputs[0]) + + links.new(map.outputs[0], depth_file_output.inputs[0]) scale_normal = tree.nodes.new(type="CompositorNodeMixRGB") scale_normal.blend_type = 'MULTIPLY'