-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrender.py
More file actions
260 lines (189 loc) · 8.61 KB
/
render.py
File metadata and controls
260 lines (189 loc) · 8.61 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import bpy
import csv
import sys
import os
import argparse
import numpy as np
#Default: violet stiff (healthy), orange soft (ill)
sys.path.append("/Users/Torsa_Legend/Desktop/ROBE TESI/Progr/scripts")
import celldiv
argv = sys.argv
if "--" not in argv:
print("ERROR: No arguments provided to script")
sys.exit(80)
else:
a = argv.index("--")
argv = argv[a + 1:]
helpString = """
Run as:
blender --background --python %s --
[options]
""" % __file__
parser = argparse.ArgumentParser(description=helpString)
parser.add_argument("trajPath", type=str,
help="Trajectory path. Absolute or relative.")
parser.add_argument("-s", "--smooth", action='store_true',
help="Do smoothing (really expensive and doesn't look as good)")
parser.add_argument("-k", "--skip", type=int, required=False,
help="Trajectory frame skip rate. E.g. SKIP=10 will only \
render every 10th frame.",
default=1)
parser.add_argument("-nc", "--noclear", type=bool, required=False,
help="specifying this will not clear the destination directory\
and restart rendering.",
default=False)
parser.add_argument("--min-cells", type=int, required=False,
help='Start rendering when system has at least this many cells',
default=1)
parser.add_argument("--inds", type=int, required=False, nargs='+',
help="Only render cells with these indices",
default=[])
parser.add_argument("-nf", "--num-frames", type=int, required=False,
help="Only render this many frames.",
default=sys.maxsize)
parser.add_argument("-r", "--res", type=int, default=1, required=False,
help='Renders images with resolution RES*1080p. RES>=1. \
Use 2 for 4k. A high number will devour your RAM.')
parser.add_argument("-cc", "--cell-color", type=int, nargs=3, required=False,
default=[72, 38, 153],
help="RGB values of cell color. From 0 to 255")
parser.add_argument("-sc", "--softcell-color", type=int, nargs=3, required=False,
default=[153, 72, 38],
help="RGB values of color of cells with different stiffness, if present. From 0 to 255")
parser.add_argument("-bc", "--background-color", type=int, nargs=3,
required=False, default=[255,255,255],
help="RGB values of cell color. From 0 to 255")
parser.add_argument("-si", "--specular-intensity", type=float, required=False,
default = 0.0,
help="Set specular-intensity (shininess). From 0.0 to 1.0")
args = parser.parse_args(argv)
imageindex = 0
firstfaces = []
bpy.data.worlds["World"].horizon_color=[ (1.0/255.0)*c for c in args.background_color]
bpy.data.scenes["Scene"].render.alpha_mode='SKY'
doSmooth = args.smooth
if doSmooth:
print("Doing smoothing. Consider avoiding this feature...")
if (args.res < 1):
print("ERROR: invalid resolution factor")
sys.exit()
bpy.data.scenes["Scene"].render.resolution_x*=args.res
bpy.data.scenes["Scene"].render.resolution_y*=args.res
with open('C180_pentahexa.csv', newline='') as g:
readerfaces = csv.reader(g, delimiter=',')
for row in readerfaces:
firstfaces.append([int(v) for v in row])
filename = os.path.realpath(args.trajPath)
basename = os.path.splitext(filename)[0] + "/images/CellDiv_"
nSkip = args.skip
if nSkip > 1:
print("Rendering every %dth" % nSkip, "frame...")
noClear = args.noclear
sPath = os.path.splitext(filename)[0] + "/images/"
if not noClear and os.path.exists(sPath):
for f in os.listdir(sPath):
os.remove(sPath+f)
cellInds = []
minInd = args.min_cells - 1
if len(args.inds) > 0:
minInd = max(minInd, min(args.inds))
stopAt = args.num_frames
bpy.context.scene.objects.active = bpy.data.objects['Cube']
bpy.ops.object.delete()
lamp = bpy.data.lamps['Lamp']
lamp.energy = 5 # 10 is the max value for energy
lamp.type = 'SUN' # in ['POINT', 'SUN', 'SPOT', 'HEMI', 'AREA']
lamp.distance = 100
#mio
math = bpy.data.materials.new("hm")
math.diffuse_color = [ (1.0/255.0)*c for c in args.cell_color] # it was (1.0/255.0)*c
math.specular_intensity = args.specular_intensity
mats = bpy.data.materials.new("sm")
mats.diffuse_color = [ (1.0/255.0)*c for c in args.softcell_color]
mats.specular_intensity = args.specular_intensity
with celldiv.TrajHandle(filename) as th:
frameCount = 1
try:
for i in range(int(th.maxFrames/nSkip)):
frameCount += 1
if frameCount > args.num_frames:
break
f = th.ReadFrame(inc=nSkip)
if len(f) < minInd+1:
print("Only ", len(f), "cells in frame ", th.currFrameNum,
" skipping...")
continue
if len(args.inds) > 0:
f = [f[a] for a in args.inds]
f0=[]
f1=[]
if sum(th.currTypes) > 0.1: #there is at least one cell with nonzero index
for cc in range(len(th.currTypes)):
if (th.currTypes[cc]):
f1.append(f[cc])
else:
f0.append(f[cc])
if len(f0) > 0:
f=f0
f = np.vstack(f)
faces = []
for mi in range(int(len(f)/192)):
for row in firstfaces:
faces.append([(v+mi*192) for v in row])
mesh = bpy.data.meshes.new('cellMesh')
ob0 = bpy.data.objects.new('cellObject', mesh)
mat0 = bpy.data.materials['hm']
ob0.data.materials.append(mat0)
bpy.context.scene.objects.link(ob0)
mesh.from_pydata(f, [], faces)
mesh.update()
#Soft cells
if len(f1) > 0:
f=f1
f = np.vstack(f)
faces = []
for mi in range(int(len(f)/192)):
for row in firstfaces:
faces.append([(v+mi*192) for v in row])
mesh1 = bpy.data.meshes.new('scellMesh')
ob1 = bpy.data.objects.new('scellObject', mesh1)
mat1 = bpy.data.materials['sm']
ob1.data.materials.append(mat1)
bpy.context.scene.objects.link(ob1)
mesh1.from_pydata(f, [], faces)
mesh1.update()
if doSmooth:
bpy.ops.object.select_by_type(type='MESH')
bpy.context.scene.objects.active = bpy.data.objects['cellObject']
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.normals_make_consistent(inside=False)
bpy.ops.object.editmode_toggle()
bpy.ops.object.shade_smooth()
bpy.ops.object.select_by_type(type='MESH')
bpy.context.scene.objects.active = bpy.data.objects['scellObject']
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.normals_make_consistent(inside=False)
bpy.ops.object.editmode_toggle()
bpy.ops.object.shade_smooth()
bpy.context.scene.objects.active = bpy.data.objects['cellObject']
bpy.ops.object.select_all(action='TOGGLE')
bpy.ops.object.modifier_add(type='SUBSURF')
bpy.context.scene.objects.active = bpy.data.objects['scellObject']
bpy.ops.object.select_all(action='TOGGLE')
bpy.ops.object.modifier_add(type='SUBSURF')
bpy.ops.object.select_by_type(type='MESH')
bpy.context.scene.objects.active = bpy.data.objects['cellObject']
bpy.context.scene.objects.active = bpy.data.objects['scellObject']
#mia
bpy.ops.view3d.camera_to_view_selected()
bpy.ops.object.select_all(action='TOGGLE')
imagename = basename + "%d.png" % frameCount
bpy.context.scene.render.filepath = imagename
bpy.ops.render.render(write_still=True) # render to file
bpy.ops.object.select_pattern(pattern='cellObject')
bpy.ops.object.delete() # delete mesh...
#roba mia
bpy.ops.object.select_pattern(pattern='scellObject')
bpy.ops.object.delete()
except celldiv.IncompleteTrajectoryError:
print ("Stopping...")