Created
February 16, 2018 11:06
-
-
Save oleg-kachan/7b50a0b4199e3e56381aa89d457ac686 to your computer and use it in GitHub Desktop.
Blender render
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
run:
blender handler.py
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
config.json