This repository has been archived by the owner on Apr 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
/
nerf_render_ori.py
214 lines (175 loc) · 6.95 KB
/
nerf_render_ori.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import sys, os, argparse
import json
import bpy
import mathutils
from mathutils import Vector
import numpy as np
np.random.seed(2) # fixed seed
DEBUG = False
VOXEL_NUMS = 512
VIEWS = 200
RESOLUTION = 800
RESULTS_PATH = 'rgb'
DEPTH_SCALE = 1.4
COLOR_DEPTH = 8
FORMAT = 'PNG'
RANDOM_VIEWS = True
UPPER_VIEWS = True
CIRCLE_FIXED_START = (.3,0,0)
parser = argparse.ArgumentParser(description='Renders given obj file by rotation a camera around it.')
parser.add_argument('output', type=str, help='path where files will be saved')
argv = sys.argv
argv = argv[argv.index("--") + 1:]
args = parser.parse_args(argv)
homedir = args.output
fp = bpy.path.abspath(f"{homedir}/{RESULTS_PATH}")
def listify_matrix(matrix):
matrix_list = []
for row in matrix:
matrix_list.append(list(row))
return matrix_list
if not os.path.exists(fp):
os.makedirs(fp)
if not os.path.exists(os.path.join(homedir, "pose")):
os.mkdir(os.path.join(homedir, "pose"))
# Data to store in JSON file
out_data = {
'camera_angle_x': bpy.data.objects['Camera'].data.angle_x,
}
# Render Optimizations
bpy.context.scene.render.use_persistent_data = True
# Set up rendering of depth map.
bpy.context.scene.use_nodes = True
tree = bpy.context.scene.node_tree
links = tree.links
# Add passes for additionally dumping albedo and normals.
#bpy.context.scene.view_layers["RenderLayer"].use_pass_normal = True
bpy.context.scene.render.image_settings.file_format = str(FORMAT)
bpy.context.scene.render.image_settings.color_depth = str(COLOR_DEPTH)
if not DEBUG:
# Create input render layer node.
render_layers = tree.nodes.new('CompositorNodeRLayers')
depth_file_output = tree.nodes.new(type="CompositorNodeOutputFile")
depth_file_output.label = 'Depth Output'
if 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 = [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])
normal_file_output = tree.nodes.new(type="CompositorNodeOutputFile")
normal_file_output.label = 'Normal Output'
links.new(render_layers.outputs['Normal'], normal_file_output.inputs[0])
# Background
bpy.context.scene.render.dither_intensity = 0.0
bpy.context.scene.render.film_transparent = True
# Create collection for objects not to render with background
objs = [ob for ob in bpy.context.scene.objects if ob.type in ('EMPTY') and 'Empty' in ob.name]
bpy.ops.object.delete({"selected_objects": objs})
# bounding box
for obj in bpy.context.scene.objects:
if 'Camera' not in obj.name:
bbox = [obj.matrix_world @ Vector(corner) for corner in obj.bound_box]
bbox = [min([bb[i] for bb in bbox]) for i in range(3)] + \
[max([bb[i] for bb in bbox]) for i in range(3)]
voxel_size = ((bbox[3]-bbox[0]) * (bbox[4]-bbox[1]) * (bbox[5]-bbox[2]) / VOXEL_NUMS) ** (1/3)
print(" ".join(['{:.5f}'.format(f) for f in bbox + [voxel_size]]),
file=open(os.path.join(homedir, 'bbox.txt'), 'w'))
def parent_obj_to_camera(b_camera):
origin = (0, 0, 0)
b_empty = bpy.data.objects.new("Empty", None)
b_empty.location = origin
b_camera.parent = b_empty # setup parenting
scn = bpy.context.scene
scn.collection.objects.link(b_empty)
bpy.context.view_layer.objects.active = b_empty
# scn.objects.active = b_empty
return b_empty
scene = bpy.context.scene
scene.render.resolution_x = RESOLUTION
scene.render.resolution_y = RESOLUTION
scene.render.resolution_percentage = 100
cam = scene.objects['Camera']
cam.location = (4, -4, 4)
cam_constraint = cam.constraints.new(type='TRACK_TO')
cam_constraint.track_axis = 'TRACK_NEGATIVE_Z'
cam_constraint.up_axis = 'UP_Y'
b_empty = parent_obj_to_camera(cam)
cam_constraint.target = b_empty
scene.render.image_settings.file_format = 'PNG' # set output format to .png
from math import radians
stepsize = 360.0 / VIEWS
rotation_mode = 'XYZ'
if not DEBUG:
for output_node in [depth_file_output, normal_file_output]:
output_node.base_path = ''
out_data['frames'] = []
if not RANDOM_VIEWS:
b_empty.rotation_euler = CIRCLE_FIXED_START
for i in range(0, VIEWS):
if DEBUG:
i = np.random.randint(0,VIEWS)
b_empty.rotation_euler[2] += radians(stepsize*i)
if RANDOM_VIEWS:
scene.render.filepath = os.path.join(fp, '{:04d}'.format(i))
if UPPER_VIEWS:
rot = np.random.uniform(0, 1, size=3) * (1,0,2*np.pi)
rot[0] = np.abs(np.arccos(1 - 2 * rot[0]) - np.pi/2)
b_empty.rotation_euler = rot
else:
b_empty.rotation_euler = np.random.uniform(0, 2*np.pi, size=3)
else:
print("Rotation {}, {}".format((stepsize * i), radians(stepsize * i)))
scene.render.filepath = os.path.join(fp, '{:04d}'.format(i))
# depth_file_output.file_slots[0].path = scene.render.filepath + "_depth_"
# normal_file_output.file_slots[0].path = scene.render.filepath + "_normal_"
print('BEFORE RENDER')
if DEBUG:
break
else:
bpy.ops.render.render(write_still=True) # render still
print('AFTER RENDER')
frame_data = {
'file_path': scene.render.filepath,
'rotation': radians(stepsize),
'transform_matrix': listify_matrix(cam.matrix_world)
}
with open(os.path.join(homedir, "pose", '{:04d}.txt'.format(i)), 'w') as fo:
for ii, pose in enumerate(frame_data['transform_matrix']):
print(" ".join([str(-p) if (((j == 2) | (j == 1)) and (ii < 3)) else str(p)
for j, p in enumerate(pose)]),
file=fo)
out_data['frames'].append(frame_data)
if RANDOM_VIEWS:
if UPPER_VIEWS:
rot = np.random.uniform(0, 1, size=3) * (1,0,2*np.pi)
rot[0] = np.abs(np.arccos(1 - 2 * rot[0]) - np.pi/2)
b_empty.rotation_euler = rot
else:
b_empty.rotation_euler = np.random.uniform(0, 2*np.pi, size=3)
else:
b_empty.rotation_euler[2] += radians(stepsize)
if not DEBUG:
with open(os.path.join(homedir, 'transforms.json'), 'w') as out_file:
json.dump(out_data, out_file, indent=4)
# save camera data
H, W = RESOLUTION, RESOLUTION
f = .5 * W /np.tan(.5 * float(out_data['camera_angle_x']))
cx = cy = W // 2
# write intrinsics
with open(os.path.join(homedir, 'intrinsics.txt'), 'w') as fi:
print("{} {} {} 0.".format(f, cx, cy), file=fi)
print("0. 0. 0.", file=fi)
print("0.", file=fi)
print("1.", file=fi)
print("{} {}".format(H, W), file=fi)