Skip to content

Instantly share code, notes, and snippets.

@oleg-kachan
Created February 16, 2018 11:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oleg-kachan/7b50a0b4199e3e56381aa89d457ac686 to your computer and use it in GitHub Desktop.
Save oleg-kachan/7b50a0b4199e3e56381aa89d457ac686 to your computer and use it in GitHub Desktop.
Blender render
import bpy
import json
import os
class DataCombiner(object):
def __init__(self, path_to_renders, path_to_groundtruth):
self.path_to_groundtruth = path_to_groundtruth
bpy.data.scenes['Scene'].render.filepath = path_to_renders
if os.path.exists(path_to_groundtruth):
os.remove(path_to_groundtruth)
def _form_output(self, frame_num, vector, quat):
output = ' '.join([str(frame_num)] + list(map(str, list(vector) + list(quat))))
return output
def _my_handler(self, scene):
with open(self.path_to_groundtruth, 'a') as w:
print(self._form_output(scene.frame_current, scene.camera.matrix_world.to_translation(), scene.camera.matrix_world.to_quaternion()), file=w)
def apply_handler(self):
handlers = bpy.app.handlers.render_pre
handlers.clear()
handlers.append(self._my_handler)
# // "bpy.context.scene.cycles.samples":1000,
def main():
with open('config.json') as config_file:
config = json.load(config_file)
bpy.ops.wm.open_mainfile(filepath=config['model'])
D = DataCombiner(config['path_to_renders'], config['path_to_groundtruth'])
D.apply_handler()
for param in config['parameters']:
exec("%s = config['parameters'][param]"%param)
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="CompositorNodeMapValue")
# Size is chosen kind of arbitrarily, try out until you're satisfied with resulting depth map.
map.size = [0.15]
map.use_min = True
map.min = [0]
map.use_max = True
map.max = [255]
links.new(rl.outputs[2], map.inputs[0])
invert = tree.nodes.new(type="CompositorNodeInvert")
links.new(map.outputs[0], invert.inputs[1])
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])
fileOutput = tree.nodes.new(type="CompositorNodeOutputFile")
fileOutput.base_path = "depth_maps/"
links.new(invert.outputs[0], fileOutput.inputs[0])
bpy.ops.render.render(animation=True)
if __name__ == '__main__':
main()
@oleg-kachan
Copy link
Author

oleg-kachan commented Jun 5, 2018

config.json

{
	"model":"/Users/Documents/test1.blend",
	"path_to_renders":"imgs/img",
	"path_to_groundtruth":"groundtruth.txt",
	"parameters": {
		"bpy.context.scene.render.engine":"CYCLES",
		"bpy.context.scene.render.antialiasing_samples":"8",
		"bpy.context.scene.render.use_bake_antialiasing":false,
		"bpy.context.scene.cycles.samples":1000,
		"bpy.context.scene.render.resolution_x":256,
		"bpy.context.scene.render.resolution_y":256,
		"bpy.context.scene.render.resolution_percentage":100
	}
}

@oleg-kachan
Copy link
Author

run:
blender handler.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment