forked from sagniklp/doc3D-renderer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
render_dmap.py
99 lines (77 loc) · 2.73 KB
/
render_dmap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'''
Code for rendering the groundtruths of Doc3D dataset
https://www3.cs.stonybrook.edu/~cvl/projects/dewarpnet/storage/paper.pdf (ICCV 2019)
This code renders the depth maps using the .blend files
saved from render_mesh.py
Written by: Sagnik Das
Stony Brook University, New York
January 2019
'''
import sys
import csv
import os
import bpy
import bmesh
import random
import math
import string
from pathlib import Path
def select_object(ob):
bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active = None
ob.select_set(True)
bpy.context.view_layer.objects.active = ob
def render():
bpy.context.scene.camera = bpy.data.objects['Camera']
bpy.data.scenes['Scene'].render.image_settings.color_depth='8'
bpy.data.scenes['Scene'].render.image_settings.color_mode='RGB'
# bpy.data.scenes['Scene'].render.image_settings.file_format='OPEN_EXR'
bpy.data.scenes['Scene'].render.image_settings.compression=0
bpy.ops.render.render(write_still=False)
def prepare_no_env_render():
# Remove lamp
for lamp in bpy.data.lights:
bpy.data.lights.remove(lamp, do_unlink=True)
world=bpy.data.worlds['World']
world.use_nodes = True
links = world.node_tree.links
# clear default nodes
for l in links:
links.remove(l)
scene=bpy.data.scenes['Scene']
scene.cycles.samples=1
scene.cycles.use_square_samples=True
scene.view_settings.view_transform='Standard'
def get_depth_map(img_name):
# no normalization or inversion use true z
bpy.context.scene.camera = bpy.data.objects['Camera']
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
render_layers = tree.nodes.new('CompositorNodeRLayers')
composite_node = tree.nodes.new("CompositorNodeComposite")
file_output_node = tree.nodes.new("CompositorNodeOutputFile")
file_output_node.format.file_format = 'OPEN_EXR'
file_output_node.base_path = path_to_output_dmap
file_output_node.file_slots[0].path = img_name
links.new(render_layers.outputs['Depth'], file_output_node.inputs[0])
strt=int(sys.argv[-2])
end=int(sys.argv[-1])
rridx=sys.argv[-3]
path_to_output_dmap = os.path.abspath('./dmap/{}/'.format(rridx))
blend_list = './blendlists/blendlist{}.csv'.format(rridx)
if not os.path.exists(path_to_output_dmap):
os.makedirs(path_to_output_dmap)
with open(blend_list,'r') as b:
blendlist = list(csv.reader(b))
for bfile in blendlist[strt:end]:
bpy.ops.wm.read_factory_settings()
bfname=bfile[0]
fn=Path(bfname).stem
bpy.ops.wm.open_mainfile(filepath=bfname)
get_depth_map(fn)
render()