forked from dyf/blenderspin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spinning_camera.py
219 lines (175 loc) · 7.89 KB
/
spinning_camera.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
215
216
217
218
import math
import os
import copy as cp
import bpy
def reset_blend():
#bpy.ops.wm.read_factory_settings()
for scene in bpy.data.scenes:
for obj in scene.objects:
scene.objects.unlink(obj)
# only worry about data in the startup scene
for bpy_data_iter in (
bpy.data.objects,
bpy.data.meshes,
bpy.data.lamps,
bpy.data.cameras,
bpy.data.curves,
):
for id_data in bpy_data_iter:
bpy_data_iter.remove(id_data)
def add_camera(scale):
bpy.ops.object.camera_add()
scene = bpy.data.scenes[0]
camera = bpy.data.objects["Camera"]
camera.data.type = 'ORTHO'
camera.data.ortho_scale = scale
camera.location = (0,0,0)
bpy.context.scene.camera = camera
#camera.rotation_euler.x = math.pi*0.5
return camera
def add_tracker(z):
bpy.ops.mesh.primitive_cube_add()
cube = bpy.data.objects['Cube']
cube.location = (0,0,z)
cube.scale = (.1,.1,.1)
cube.hide_render = True
return cube
def add_camera_track(steps, camera, tracker, z):
bpy.ops.curve.primitive_bezier_circle_add()
circle = bpy.data.curves['BezierCircle']
circle.path_duration = steps
circle = bpy.data.objects['BezierCircle']
circle.location = (0,0,z)
circle.scale = (10.0,10.0,10.0)
camera.select = True
bpy.context.scene.objects.active = camera
camera.constraints.new('FOLLOW_PATH')
constraint = camera.constraints['Follow Path']
constraint.target = circle
override={'constraint':camera.constraints["Follow Path"]}
bpy.ops.constraint.followpath_path_animate(override,constraint='Follow Path')
bpy.ops.object.select_all(action = "DESELECT")
camera.select = True
tracker.select = True
bpy.context.scene.objects.active = tracker
bpy.ops.object.track_set(type = "TRACKTO")
def setup_animation(num_frames, directory):
scn = bpy.context.scene
scn.frame_end = num_frames
scn.render.filepath = directory
def add_light(tracker):
bpy.ops.object.lamp_add(type='AREA')
light = bpy.data.objects['Area']
light.location = (10,-5,10)
bpy.ops.object.select_all(action = "DESELECT")
light.select = True
tracker.select = True
bpy.context.scene.objects.active = tracker
bpy.ops.object.track_set(type = "TRACKTO")
light = bpy.data.lamps['Area']
light.node_tree.nodes['Emission'].inputs['Strength'].default_value = 5000
def add_ply(path, vertex_colors=True):
bpy.ops.import_mesh.ply(filepath=path)
obj = bpy.context.scene.objects.active
obj.location = (0,0,0)
bpy.ops.object.shade_smooth()
mat = bpy.data.materials.new("Material")
mat.use_nodes = True
if vertex_colors:
nodes = mat.node_tree.nodes
links = mat.node_tree.links
att = nodes.new('ShaderNodeAttribute')
att.attribute_name = 'Col'
diff = nodes.get('Diffuse BSDF')
links.new(att.outputs['Color'], diff.inputs['Color'])
obj.data.materials.append(mat)
def spin_render(num_frames, out_dir, scale, z, dry_run=False):
tracker = add_tracker(z)
camera = add_camera(scale)
add_camera_track(num_frames, camera, tracker, z)
add_light(tracker)
setup_animation(num_frames, out_dir)
if not dry_run:
bpy.ops.render.render(animation=True)
def setup_world(resolution_x=5940, resolution_y=3600, transparent_background=True, resolution_percentage=100):
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.render.resolution_x = resolution_x
bpy.context.scene.render.resolution_y = resolution_y
bpy.context.scene.render.resolution_percentage = resolution_percentage
bpy.context.scene.cycles.film_transparent = transparent_background
bpy.data.worlds['World'].light_settings.use_ambient_occlusion = True
bpy.data.worlds['World'].light_settings.ao_factor = 0.4
bpy.data.worlds['World'].horizon_color = (0,0,0)
def main():
base_config = {
'scale': 1.88,
'z': -0.0185,
'steps': 300
}
ply_filename = 'recon.ply'
base_dir = '.'
for dir_name in os.listdir(base_dir):
ply_file = os.path.join(base_dir, dir_name, ply_filename)
if not os.path.exists(ply_file):
continue
config = cp.deepcopy(base_config)
config['ply'] = ply_file
config['outdir'] = os.path.join(base_dir, dir_name)
reset_blend()
setup_world(resolution_percentage=20)
add_ply(config['ply'])
spin_render(config['steps'], config['outdir'], config['scale'], config['z'], dry_run=False)
def main_manual():
configs = [
dict(ply = "/allen/aibs/technology/mousecelltypes/artwork/human_press_release/H17.06.003.11.06.01_589259963_m.ply",
outdir = "/allen/aibs/technology/mousecelltypes/artwork/human_press_release/H17.06.003.11.06.01_589259963_m/",
scale = 1.88,
z = -.0185,
steps = 300),
dict(ply = "/allen/aibs/technology/mousecelltypes/artwork/human_press_release/H17.06.006.11.09.05_601947568_m.ply",
outdir = "/allen/aibs/technology/mousecelltypes/artwork/human_press_release/H17.06.006.11.09.05_601947568_m/",
scale = 1.88,
z = 0.376,
steps = 300),
dict(ply = "/allen/aibs/technology/mousecelltypes/artwork/human_press_release/H16.06.010.01.03.04.01_566350716_m.ply",
outdir = "/allen/aibs/technology/mousecelltypes/artwork/human_press_release/H16.06.010.01.03.04.01_566350716_m/",
scale = 1.48,
z = -.085,
steps = 300),
dict(ply = "/allen/aibs/technology/mousecelltypes/artwork/human_press_release/H16.03.007.01.01.08.01_564395300_m.ply",
outdir = "/allen/aibs/technology/mousecelltypes/artwork/human_press_release/H16.03.007.01.01.08.01_564395300_m/",
scale = 2.0,
z = .163,
steps = 300),
dict(ply = "/allen/aibs/technology/mousecelltypes/artwork/human_press_release/H16.06.004.01.04.05_556380170_m.ply",
outdir = "/allen/aibs/technology/mousecelltypes/artwork/human_press_release/H16.06.004.01.04.05_556380170_m/",
scale = 4.8,
z = .3,
steps = 300),
dict(ply = "/allen/aibs/technology/mousecelltypes/artwork/human_press_release/H16.06.010.01.03.05.02_599474744_m.ply",
outdir = "/allen/aibs/technology/mousecelltypes/artwork/human_press_release/H16.06.010.01.03.05.02_599474744_m/",
scale = 4.8,
z = .3,
steps = 300),
dict(ply = "/allen/aibs/technology/mousecelltypes/artwork/human_press_release/H16.06.010.01.03.14.02_599474317_m.ply",
outdir = "/allen/aibs/technology/mousecelltypes/artwork/human_press_release/H16.06.010.01.03.14.02_599474317_m/",
scale = 2.1,
z = .24,
steps = 300),
dict(ply = "/allen/aibs/technology/mousecelltypes/artwork/human_press_release/H16.06.007.01.05.02_550397440_p_DendriteAxon_aligned.ply",
outdir = "/allen/aibs/technology/mousecelltypes/artwork/human_press_release/H16.06.007.01.05.02_550397440_p_DendriteAxon_aligned/",
scale = 28.6,
z = -4,
steps = 300),
dict(ply = "/allen/aibs/technology/mousecelltypes/artwork/human_press_release/H16.06.010.01.03.14.02_548268538_p_dendriteaxon_aligned.ply",
outdir = "/allen/aibs/technology/mousecelltypes/artwork/human_press_release/H16.06.010.01.03.14.02_548268538_p_dendriteaxon_aligned/",
scale = 18,
z = 2,
steps = 300)
]
for config in configs:
reset_blend()
setup_world()
add_ply(config['ply'])
spin_render(config['steps'], config['outdir'], config['scale'], config['z'], dry_run=False)
if __name__ == "__main__": main()