-
Notifications
You must be signed in to change notification settings - Fork 1
/
do_blender.py
239 lines (207 loc) · 9.54 KB
/
do_blender.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# This stub runs a python script relative to the currently open
# blend file, useful when editing scripts externally.
import bpy
import sys # for stderr
import math # for log2
import time # for time.time()
sys.path.append('/Users/david/epic_gs_data')
import trace_lineage
start_time = time.time()
#e_end_pnts = [
#"Ealaa", "Ealap", "Ealpa", "Ealpp", "Earaa", "Earap", "Earpa", "Earpp",
#"Eplaa", "Eplap", "Eplpa", "Eplpp", "Epraa", "Eprap", "Eprpa", "Eprpp" ]
end_pnts = trace_lineage.ALL_END_PNTS
n_stages = int( math.log2( len( end_pnts ) ) )
parents = {}
# since get_parent has to go backwards, reverse the sequence from 0-n_stages
stages_low_high = list(range(n_stages))[::-1]
for stage in stages_low_high:
parents[ stage ] = {}
for end_pnt in end_pnts:
parents[ stage ][ trace_lineage.get_parent( end_pnt ) ] = ''
def scale_value(inval):
MINV = 20000
MAXV = 90000
margin = MAXV - MINV
percent_max = (inval - MINV) / margin
return percent_max
NTICKS = 4
#def do_thing(metaball, end_cell, filename):
def do_thing(metaball, end_cell, min_time, max_time, big_data):
last_cell = ''
#ticker = 0
#for timepnt, row, found_cell in trace_lineage.parse_gs_epic_file([end_cell], filename):
timer = time.time()
for timepnt, row, found_cell in trace_lineage.search_gs_epic_file([end_cell], min_time, max_time, big_data):
#if ticker:
#if ticker - 1 == 0:
#print("at", str(timepnt) + ",", NTICKS, "post cell division", file=sys.stderr)
#ticker -= 1
if found_cell != last_cell:
print("at cell division", str(timepnt) + ",", last_cell, "==>", found_cell, "in %.4f seconds" % (time.time() - timer), file=sys.stderr)
ticker = NTICKS
timer = time.time()
last_cell = found_cell
if not row:
continue
frame = timepnt *12
bpy.context.scene.frame_current = frame
bpy.context.object.location = (row['x']/100,row['y']/100,row['z']/10)
size = row['size']/200
bpy.context.object.scale = (size,size,size)
#bpy.ops.anim.keyframe_insert_menu(type='BUILTIN_KSI_LocScale')
#bpy.ops.anim.keyframe_insert(type='BUILTIN_KSI_LocScale')
bpy.context.object.keyframe_insert('scale')
bpy.context.object.keyframe_insert('location')
# do something with the material
curMat = bpy.context.object.active_material
#if 'ecdf' in row:
if False:
curMat.node_tree.nodes['ColorRamp'].inputs[0].default_value = row['ecdf']
else:
curMat.node_tree.nodes['ColorRamp'].inputs[0].default_value = scale_value(row['value'])
curMat.node_tree.nodes['ColorRamp'].inputs[0].keyframe_insert("default_value", frame = frame )
bpy.context.scene.render.engine = 'CYCLES'
###############
## profiling ##
###############
import cProfile, pstats, io
#from pstats import SortKey
pr = cProfile.Profile()
pr.enable()
#big_data = trace_lineage.get_big_data('mml-1_5_CD20070804.csv')
#infilename = 'pha4_b2_CD20060629.csv'
infilename = 'lin-13_6B12_1_L1_CD20080814.csv'
#infilename = 'pha4_b2_CD20060629_w_q.csv'
#do_thing(obj, celltype, 'elt7_c2a_CD20070310.csv')
#infilename = 'mml-1_5_CD20070804.csv'
#infilename = 'elt7_c2a_CD20070310.csv'
min_time, max_time, big_data = trace_lineage.load_gs_epic_file(infilename)
#base_mat = bpy.data.materials.get('epic_color_ramp')
base_mat = bpy.data.materials.get('ab_emission')
#ab_mat = bpy.data.materials.get('outer_cells')
ab_mat = bpy.data.materials.get('ab_emission')
current_cell_type_char = ''
profilers = {}
active_profiler = None
for celltype in end_pnts:
cell_start_time = time.time()
cell_type_char = celltype[0]
# run a different profiler for each cell type
if current_cell_type_char != cell_type_char:
if active_profiler:
active_profiler.disable()
current_cell_type_char = cell_type_char
active_profiler = cProfile.Profile()
active_profiler.enable()
profilers[current_cell_type_char] = active_profiler
print("Doing", celltype, file=sys.stderr)
print("==============", file=sys.stderr)
#bpy.ops.object.metaball_add(type='BALL', radius=1)
if celltype.startswith('C'):
bpy.ops.mesh.primitive_cone_add(
radius1=1, radius2=0, depth=2, view_align=False, enter_editmode=False, location=(0, 0, 0),
layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))
bpy.context.scene.layers[0] = True
elif celltype.startswith('D'):
bpy.ops.mesh.primitive_cylinder_add(radius=1, depth=3, view_align=False, enter_editmode=False, location=(0, 0, 0),
layers=(False, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))
bpy.context.scene.layers[1] = True
elif celltype.startswith('E'):
#bpy.ops.mesh.primitive_torus_add(location=(0, 0, 0), view_align=False, rotation=(0, 1.5708, 0),
#layers=(False, False, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False),
#mode='MAJOR_MINOR', major_radius=0.5, minor_radius=0.25, abso_major_rad=0.75, abso_minor_rad=0.25)
bpy.ops.mesh.primitive_ico_sphere_add(
subdivisions=2,
size=1,
view_align=False,
enter_editmode=False,
location=(0,0,0),
layers=(False, False, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))
bpy.context.scene.layers[2] = True
elif celltype.startswith('MS'):
bpy.ops.mesh.primitive_cube_add(radius=1, view_align=False, enter_editmode=False, location=(0, 0, 0),
layers=(False, False, False, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))
bpy.context.scene.layers[3] = True
elif celltype.startswith('Z'):
#bpy.ops.mesh.primitive_monkey_add(radius=1, view_align=False, enter_editmode=False, location=(0, 0, 0),
#layers=(False, False, False, False, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))
bpy.ops.mesh.primitive_round_cube_add(layers=(False, False, False, False, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False), rotation=(0, 0, 0), location=(0,0,0), view_align=False)
bpy.context.scene.layers[4] = True
### subdivide AB cells into different layers
elif celltype.startswith('ABpl'):
bpy.ops.mesh.primitive_ico_sphere_add(
subdivisions=2,
size=1,
view_align=False,
enter_editmode=False,
location=(0,0,0),
layers=(False, False, False, False, False, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False))
bpy.context.scene.layers[5] = True
elif celltype.startswith('ABpr'):
bpy.ops.mesh.primitive_ico_sphere_add(
subdivisions=2,
size=1,
view_align=False,
enter_editmode=False,
location=(0,0,0),
layers=(False, False, False, False, False, False, True, False, False, False, False, False, False, False, False, False, False, False, False, False))
bpy.context.scene.layers[6] = True
elif celltype.startswith('ABal'):
bpy.ops.mesh.primitive_ico_sphere_add(
subdivisions=2,
size=1,
view_align=False,
enter_editmode=False,
location=(0,0,0),
layers=(False, False, False, False, False, False, False, True, False, False, False, False, False, False, False, False, False, False, False, False))
bpy.context.scene.layers[7] = True
else:
bpy.ops.mesh.primitive_ico_sphere_add(
subdivisions=2,
size=1,
view_align=False,
enter_editmode=False,
location=(0,0,0),
layers=(False, False, False, False, False, False, False, False, True, False, False, False, False, False, False, False, False, False, False, False))
bpy.context.scene.layers[8] = True
# create the material
if not celltype.startswith('MS') and not celltype.startswith('AB'):
bpy.ops.object.modifier_add(type='SUBSURF')
bpy.context.object.modifiers["Subsurf"].levels = 2
if celltype.startswith('AB'):
new_mat = ab_mat.copy()
else:
new_mat = base_mat.copy()
new_mat.name = celltype
obj = bpy.context.object
obj.data.materials.append(new_mat)
obj.name = celltype
scn = bpy.context.scene
scn.frame_start = 1
scn.frame_end = 212*12
do_thing(obj, celltype, min_time, max_time, big_data)
cell_end_time = time.time()
print("Finished cell in %.4f seconds" % (cell_end_time - cell_start_time), file=sys.stderr)
## final cell type profiler
if active_profiler:
active_profiler.disable()
end_time = time.time()
print("Done thing in %d seconds" % int(end_time - start_time), file=sys.stderr)
###################
## end profiling ##
###################
pr.disable()
s = io.StringIO()
#sortby = SortKey.CUMULATIVE
sortby = 'cumtime'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print(s.getvalue(), file=sys.stderr)
for cell_symbol,pr in profilers.items():
print("PROFILER FOR", cell_symbol, file=sys.stderr)
s = io.StringIO()
sortby = 'cumtime'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print(s.getvalue(), file=sys.stderr)