-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_export_tip_carthesian_coordinates.py
215 lines (190 loc) · 7.24 KB
/
1_export_tip_carthesian_coordinates.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
#This script is used to export carthesian coordinates of the whisker tips
#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 give_tip ()
# 3. All rotation- and scale-transforms must be applied to the whisker (Cmd + A)
# 4. Insert the excel file path in lines 53 and 54
# 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 get_origin(curve_object):
#This function reads the objects origin
depsgraph = bpy.context.evaluated_depsgraph_get()
evaluated_curve = curve_object.evaluated_get(depsgraph)
origin = evaluated_curve.location
return origin
def get_tip (curve_object):
#This function reads the tip position of the whisker relative to its origin
curve = curve_object.data.splines[0]
curve_data = curve_object.data
spline = curve_data.splines[0]
num_points = len(spline.points)
p0 = curve.points[0].co
p1 = curve.points[num_points-1].co
whisker = (p1 - p0)
return whisker
def get_global(curve_object):
#This Function combines the previous two to return the whisker tip's global carthesian coordinates
whisker=get_tip(curve_object)
origin=get_origin(curve_object)
global_tip = (whisker[0] + origin[0], whisker[1] + origin[1], whisker[2] + origin[2])
return global_tip
def give_tip(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':
points = [(point.co.x, point.co.y, point.co.z) for point in curve_data.splines.active.points]
#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)
#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
tip = get_global(curve_obj)
x = tip[0]
y = tip[1]
z = tip[2]
#print to console
print(f"{whisker_name} X: {x} Y: {y} Z: {z}")
#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*4, value='X')
sheet.cell(row=2+(27*a), column=3+i*4, value='Y')
sheet.cell(row=2+(27*a), column=4+i*4, value='Y')
sheet.cell(row=n+2+(27*a), column=1, value=whisker_name)
sheet.cell(row=1+(27*a), column=1+i*4, value=animal)
sheet.cell(row=1+(27*a), column=2+i*4, value=Run)
# Write data & save
sheet.cell(row=n+2+(27*a), column=2+i*4, value=x)
sheet.cell(row=n+2+(27*a), column=3+i*4, value=y)
sheet.cell(row=n+2+(27*a), column=4+i*4, value=z)
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':
give_tip(curve)
else:
print("Please select at least one curve object.")
print("________finished________")