-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimportXPZcurve.py
278 lines (194 loc) · 7.16 KB
/
importXPZcurve.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
print("Hello")
from mathutils import Vector
import bpy
import glob
import csv
import math
import struct
import os
import urllib.request
w=1
def clearAllCurves():
# gather list of items of interest.
candidate_list = [item.name for item in bpy.data.objects if item.type == "CURVE"]
# select them only.
for object_name in candidate_list:
bpy.data.objects[object_name].select = True
# remove all selected.
bpy.ops.object.delete()
# remove the meshes, they have no users anymore.
for item in bpy.data.meshes:
bpy.data.meshes.remove(item)
print('Cleared curves')
def clearAllMeshes():
# gather list of items of interest.
candidate_list = [item.name for item in bpy.data.objects if item.type == "MESH"]
# select them only.
for object_name in candidate_list:
bpy.data.objects[object_name].select = True
# remove all selected.
bpy.ops.object.delete()
# remove the meshes, they have no users anymore.
for item in bpy.data.meshes:
bpy.data.meshes.remove(item)
print('Cleared meshes')
def makeMaterial(name, diffuse, specular, alpha):
mat = bpy.data.materials.new(name)
mat.diffuse_color = diffuse
mat.diffuse_shader = 'LAMBERT'
mat.diffuse_intensity = 1.0
mat.specular_color = specular
mat.specular_shader = 'COOKTORR'
mat.specular_intensity = 0.5
mat.alpha = alpha
mat.ambient = 1
return mat
def setMaterial(ob, mat):
me = ob.data
me.materials.append(mat)
def printSummary():
for object in bpy.data.objects:
# print(object.type)
if object.type == 'CAMERA':
print('Camera location: ' + str(object.location))
if object.type == 'LAMP':
print('Sun location: ' + str(object.location))
def setSun():
rx = 0
ry = 180
rz = 180
pi = 3.14159265
sun = bpy.data.objects['Sun']
sun.location = [math.floor(133/2), math.floor(81/2), 0]
sun.rotation_mode = 'XYZ'
sun.rotation_euler[0] = rx*(pi/180.0)
sun.rotation_euler[1] = ry*(pi/180.0)
sun.rotation_euler[2] = rz*(pi/180.0)
sun.data.distance = 500
sun.data.falloff_type = 'CONSTANT'
def moveSag():
cam = bpy.data.objects['Camera']
cam.location = [math.floor(133/2), math.floor(81/2), -150]
rx = 0
ry = 180
rz = 180
pi = 3.14159265
cam.rotation_mode = 'XYZ'
cam.rotation_euler[0] = rx*(pi/180.0)
cam.rotation_euler[1] = ry*(pi/180.0)
cam.rotation_euler[2] = rz*(pi/180.0)
#cam.data.type = 'PERSP'
cam.data.type = 'ORTHO'
cam.data.ortho_scale = 250
def moveCoronal():
cam = bpy.data.objects['Camera']
cam.location = [-150, math.floor(81/2), math.floor(115/2)]
rx = 0
ry = 90
rz = 180
pi = 3.14159265
cam.rotation_mode = 'XYZ'
cam.rotation_euler[0] = rx*(pi/180.0)
cam.rotation_euler[1] = ry*(pi/180.0)
cam.rotation_euler[2] = rz*(pi/180.0)
#cam.data.type = 'PERSP'
cam.data.type = 'ORTHO'
cam.data.ortho_scale = 250
def moveAxial():
cam = bpy.data.objects['Camera']
cam.location = [math.floor(133/2), -115, math.floor(115/2)]
rx = 90
ry = 0
rz = 0
pi = 3.14159265
cam.rotation_mode = 'XYZ'
cam.rotation_euler[0] = rx*(pi/180.0)
cam.rotation_euler[1] = ry*(pi/180.0)
cam.rotation_euler[2] = rz*(pi/180.0)
#cam.data.type = 'PERSP'
cam.data.type = 'ORTHO'
cam.data.ortho_scale = 250
def addRootGroup(list_of_roots):
group_indexes = range(50,100)
print("there are %d roots" % len(list_of_roots))
#for rootgroup in list_of_roots[group_indexes]:
for group_in in group_indexes:
if group_in > len(list_of_roots):
break
rootgroup = list_of_roots[group_in].split('\\')[1]
urlstring = 'http://localhost:8888/series/%s' % rootgroup
url = urllib.request.urlopen(urlstring)
mybytes = url.read()
colorstring = mybytes.decode("utf8")
url.close()
print(colorstring)
csplit = colorstring.split(',')
r = float(csplit[0])
g = float(csplit[1])
b = float(csplit[2])
mtlname = rootgroup + '.mtl'
red = makeMaterial(mtlname, (r,g,b), (1,1,1), 1)
i = 0
print(rootgroup)
group_list = glob.glob(binary_location + '/' + rootgroup + '*')
for mes in group_list:
if i % 100 == 0: print(i)
vec_list = []
import os
f = open(mes,'rb')
filesize = os.fstat(f.fileno()).st_size
for k in range(0,int(filesize/12)):
vals = struct.unpack('fff', f.read(12))
vec = Vector(vals)
vec_list.append(vec)
def MakePolyLine(objname, curvename, cList):
curvedata = bpy.data.curves.new(name=curvename, type='CURVE')
curvedata.dimensions = '3D'
curvedata.bevel_depth = 0.025
objectdata = bpy.data.objects.new(objname, curvedata)
objectdata.location = (0,0,0) #object origin
bpy.context.scene.objects.link(objectdata)
polyline = curvedata.splines.new('POLY')
polyline.points.add(len(cList)-1)
for num in range(len(cList)):
x, y, z = cList[num]
polyline.points[num].co = (x, y, z, w)
MakePolyLine("%s-%04d" % (rootgroup,i), "%s-%04d" % (rootgroup,i), vec_list)
ob = bpy.data.objects.get("%s-%04d" % (rootgroup,i))
ob.select = True
i+=1
bpy.context.scene.objects.active = bpy.data.objects[("%s-0000" % (rootgroup))]
bpy.ops.object.join()
bpy.data.curves[("%s-0000" % (rootgroup))].bevel_depth = 0.025
setMaterial(bpy.context.active_object, red)
bpy.ops.object.select_all( action='DESELECT' )
add_cube = bpy.ops.mesh.primitive_cube_add
bpy.ops.object.select_all( action='DESELECT' )
binary_location = '/Users/Administrator/connectivity-blend/bindata'
raw_location = '/Users/Administrator/connectivity-blend/rawdata'
list_of_binmesh = glob.glob(binary_location + '/*')
list_of_roots = []
for mes in list_of_binmesh:
ms = mes.split('/')[-1].split('.')[0].split('-')[0]
if ms not in list_of_roots:
list_of_roots.append(ms)
print(list_of_roots[0])
#clearAllMeshes()
#clearAllCurves()
addRootGroup(list_of_roots)
layerList = [False]*20
layerList[0] = True
import math
shouldAddCube = 0
if shouldAddCube:
add_cube(location=(0, 0, 0,), layers=layerList)
ob = bpy.data.objects['Cube']
print(ob.location)
space = [133, 81, 115]
ob.scale = [133/2, 81/2, 115/2]
ob.location = [math.floor(133/2), math.floor(81/2), math.floor(115/2)]
printSummary()
#moveCoronal()
moveSag()
#moveAxial()
setSun()