-
Notifications
You must be signed in to change notification settings - Fork 0
/
4_export_zeta.py
233 lines (207 loc) · 8.18 KB
/
4_export_zeta.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
#This script calculates the torsional angle of selected whiskers
#Results are printed into the console and exported to a excel file
#To run this script, first check:
# 1. Object mode
# 2. Select the whisker-objects for export. Make sure the objects are named as required by the function export ()
# 3. All rotation-transforms must be applied to the whisker (Cmd + A)
# 4. Insert the excel file path in lines 74 and 75
# 5. When using an existing file: Make sure the sheet you want to export into is opened in the excel file and that all previous changes are saved
# 6. All curves, representing whiskers must be in base > tip direction
import bpy
import openpyxl
import os
import math
from mathutils import Vector
def give_phi_theta(curve_obj):
#This function calculates the emergence angles based on the initial segment of the curve
curve = curve_obj.data.splines[0]
curve_data = curve_obj.data
spline = curve_data.splines[0]
p0 = curve.points[0].co
p1 = curve.points[3].co
direction = (p1 - p0).normalized()
directionxy = math.sqrt(direction.y**2 + direction.x**2)
angle_phi = math.atan2(directionxy, -direction.z)
if direction.x == 0:
angle_theta = (math.pi / 2 if direction.y > 0 else -math.pi / 2)+math.pi
else:
angle_theta = (math.atan2(direction.y, direction.x))+math.pi
emergence_theta = math.degrees(angle_theta)
emergence_phi = math.degrees(angle_phi)
return emergence_phi, emergence_theta
def orient_whisker(curve_obj, ori_phi, ori_theta):
#This function orients the whiskers origin parallel to the x-axis, so that torsion can be measured
curve_obj.rotation_euler.order = 'ZXY'
curve_obj.rotation_euler = (0, -ori_phi+(math.pi/2), -ori_theta)
bpy.ops.object.transform_apply(location=False, rotation=True, scale=False)
#print(f"whisker {curve_obj.name} oriented")
def give_zeta(curve_obj):
#This function calculates the torsion after the whisker has been oriented
curve = curve_obj.data.splines[0]
curve_data = curve_obj.data
spline = curve_data.splines[0]
num_points = len(spline.points)
p0 = curve.points[0].co
p1 = curve.points[num_points-1].co
direction = (p1 - p0).normalized()
angle_zeta = math.atan2(-direction.y, -direction.z)
return angle_zeta
def reorient_whisker(curve_obj, ori_phi, ori_theta):
#This function resets the whisker orientation after the torsion has been measured
curve_obj.rotation_euler.order = 'YXZ'
curve_obj.rotation_euler = (0, -(-ori_phi+(math.pi/2)), ori_theta)
bpy.ops.object.transform_apply(location=False, rotation=True, scale=False)
#print(f"whisker {curve_obj.name} REoriented")
def export(curve_obj):
#This function exports the coordinates to excel and the console
#The variables for finding column and row are based on the names of the whisker object
#This version is used for exporting all whiskers of all 5 animals from one position
#They can easily be adapted as desired
curve_data = curve_obj.data
if curve_data.splines.active.type == 'POLY':
#define file name and directory
file_name = f"file_name.xlsx"
directory = f"/directory/"
# Join the directory path with the file name
file_path = os.path.join(directory, file_name)
points = [(point.co.x, point.co.y, point.co.z) for point in curve_data.splines.active.points]
#The indentation i can be used if more positions of the same animal are exported
#remove the comment and add the positions to the whisker object name and script
i = 0
Run = ""
# if "Position name 1" in curve_obj.name.lower():
# Run ="Position name 1"
# i=0
# if "Position name 2" in curve_obj.name.lower():
# Run ="Position name 2"
# i=1
#expand if needed for more positions and indentations
#Ectract the animal-ID (a)
a=0
if "007" in curve_obj.name.lower():
animal="Animal 1"
a=0
if "008" in curve_obj.name.lower():
animal="Animal 2"
a=1
if "009" in curve_obj.name.lower():
animal="Animal 3"
a=2
if "010" in curve_obj.name.lower():
animal="Animal 4"
a=3
if "011" in curve_obj.name.lower():
animal="Animal 5"
a=4
#Extract the names of the whisker (n)
if "alpha" in curve_obj.name.lower():
whisker_name="Alpha"
n=1
if "a1" in curve_obj.name.lower():
whisker_name="A1"
n=2
if "a2" in curve_obj.name.lower():
whisker_name="A2"
n=3
if "a3" in curve_obj.name.lower():
whisker_name="A3"
n=4
if "a4" in curve_obj.name.lower():
whisker_name="A4"
n=5
if "beta" in curve_obj.name.lower():
whisker_name="Beta"
n=6
if "b1" in curve_obj.name.lower():
whisker_name="B1"
n=7
if "b2" in curve_obj.name.lower():
whisker_name="B2"
n=8
if "b3" in curve_obj.name.lower():
whisker_name="B3"
n=9
if "b4" in curve_obj.name.lower():
whisker_name="B4"
n=10
if "gamma" in curve_obj.name.lower():
whisker_name="Gamma"
n=11
if "c1" in curve_obj.name.lower():
whisker_name="C1"
n=12
if "c2" in curve_obj.name.lower():
whisker_name="C2"
n=13
if "c3" in curve_obj.name.lower():
whisker_name="C3"
n=14
if "c4" in curve_obj.name.lower():
whisker_name="C4"
n=15
if "delta" in curve_obj.name.lower():
whisker_name="Delta"
n=16
if "d1" in curve_obj.name.lower():
whisker_name="D1"
n=17
if "d2" in curve_obj.name.lower():
whisker_name="D2"
n=18
if "d3" in curve_obj.name.lower():
whisker_name="D3"
n=19
if "d4" in curve_obj.name.lower():
whisker_name="D4"
n=20
if "e1" in curve_obj.name.lower():
whisker_name="E1"
n=21
if "e2" in curve_obj.name.lower():
whisker_name="E2"
n=22
if "e3" in curve_obj.name.lower():
whisker_name="E3"
n=23
if "e4" in curve_obj.name.lower():
whisker_name="E4"
n=24
#calculate zeta
emergence = give_phi_theta(curve)
phi = math.radians(emergence[0])
theta = math.radians(emergence[1])
orient_whisker(curve, phi, theta)
zeta = math.degrees(give_zeta(curve))
reorient_whisker(curve, phi, theta)
#print to console
print(f"{whisker_name} torsion {zeta}°")
#open or create the excel file
if os.path.exists(file_path):
workbook = openpyxl.load_workbook(file_path)
else:
workbook = openpyxl.Workbook()
sheet = workbook.active
# Write headers
sheet.cell(row=2+(27*a), column=2+i*2, value='torsion')
sheet.cell(row=n+2+(27*a), column=1, value=whisker_name)
sheet.cell(row=1+(27*a), column=1+i*2, value=animal)
sheet.cell(row=1+(27*a), column=2+i*2, value=Run)
# Write data & save
sheet.cell(row=n+2+(27*a), column=2+i*3, value=zeta)
workbook.save(file_path)
print(f"wrote {whisker_name}")
else: print(f"curve {whisker_name} is of the wrong type")
selected_objects = bpy.context.selected_objects
list = []
if any(obj.type == 'CURVE' for obj in selected_objects):
for curve in selected_objects:
if curve.type == 'CURVE':
list.append(curve)
print(f"selected {len(list)} curves: {list}")
if any(obj.type == 'CURVE' for obj in list):
for curve in selected_objects:
if curve.type == 'CURVE':
export(curve)
else:
print("Please select at least one curve object.")
print("________finished________")