-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.py
330 lines (256 loc) · 12.1 KB
/
parser.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
'''
module to parse fusion file
'''
import adsk, adsk.core, adsk.fusion
from . import transforms
from . import parts
class Hierarchy:
''' hierarchy of the design space '''
def __init__(self, component) -> None:
self.children = []
self.component = component
self.name = component.name
self._parent = None
def add_child(self, c):
self.children.append(c)
c.parent = self
def get_children(self):
return self.children
def get_all_children(self):
''' get all children and sub children '''
child_map = {}
parent_stack = set()
parent_stack.update(self.get_children())
while len(parent_stack) != 0:
# Pop an element form the stack (order shouldn't matter)
tmp = parent_stack.pop()
# Add this child to the map
# use the entity token, more accurate than the name of the component (since there are multiple)
child_map[tmp.component.entityToken] = tmp
# Check if this child has children
if len(tmp.get_children())> 0:
# add them to the parent_stack
parent_stack.update(tmp.get_children())
if 'Neck' in tmp.name:
print(f' Has children: {[ x.name for x in tmp.get_children()]} with entityToken ids {[x.component.entityToken for x in tmp.get_children()]}')
return child_map
def get_all_parents(self):
''' get all the parents of this node '''
child_stack = set()
child_stack.add(self)
parent_map = []
while len(child_stack) != 0:
tmp = child_stack.pop()
if tmp.parent is None:
return parent_map
parent_map.append(tmp.parent.component.entityToken)
child_stack.add(tmp.parent)
return parent_map
@property
def parent(self):
if self._parent is None:
return None
return self._parent
@parent.setter
def parent(self,v):
self._parent = v
@staticmethod
def traverse(occurrences, parent=None):
'''
Based on the fusion 360 API docs
'''
for i in range(0, occurrences.count):
occ = occurrences.item(i)
cur = Hierarchy(occ)
if parent is None:
pass
else:
parent.add_child(cur)
if occ.childOccurrences:
node = Hierarchy.traverse(occ.childOccurrences, parent=cur)
return cur
class Configurator:
joint_type_list = [ 'fixed', 'revolute', 'prismatic', 'Cylinderical',
'PinSlot', 'Planner', 'Ball'] # these are the names in urdf
def __init__(self, root) -> None:
# Export top-level occurrences
self.root = root
self.occ = root.occurrences.asList
self.inertial_dict = {}
self.inertia_accuracy = adsk.fusion.CalculationAccuracy.LowCalculationAccuracy
self.links_xyz_dict = {} # needed ?
self.joints_dict = {}
self.links = {} # Link class
self.joints = {} # Joint class for writing to file
self.scale = 100.0 # Units to convert to meters (or whatever simulator takes)
self.inertia_scale = 10000.0 # units to convert mass
self.base_links= set()
def get_scene_configuration(self):
'''
Build the graph of how the scene components are related
'''
root_node = Hierarchy(self.root)
Hierarchy.traverse(self.root.occurrences.asList, root_node)
self.component_map = root_node.get_all_children()
def get_joint_preview(self):
''' Get the scenes joint relationships without calculating links '''
self._joints()
return self.joints_dict
def parse(self):
''' parse the scene '''
self._inertia()
self._joints()
self._base()
self._build_links()
self._build_joints()
@property
def name(self):
''' Name of the root component '''
return self.root.name.split()[0]
def _base(self):
''' Get the base link(s) '''
for oc in self.occ:
if oc.isGrounded:
self.base_links.add(oc.name)
def _inertia(self):
'''
Define inertia values
'''
for oc in self.occ:
occs_dict = {}
prop = oc.getPhysicalProperties(self.inertia_accuracy)
occs_dict['name'] = oc.name
mass = prop.mass # kg
occs_dict['mass'] = mass
center_of_mass = [_/self.scale for _ in prop.centerOfMass.asArray()] ## cm to m
occs_dict['center_of_mass'] = center_of_mass
# https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-ce341ee6-4490-11e5-b25b-f8b156d7cd97
(_, xx, yy, zz, xy, yz, xz) = prop.getXYZMomentsOfInertia()
moment_inertia_world = [_ / self.inertia_scale for _ in [xx, yy, zz, xy, yz, xz] ] ## kg / cm^2 -> kg/m^2
occs_dict['inertia'] = transforms.origin2center_of_mass(moment_inertia_world, center_of_mass, mass)
self.inertial_dict[oc.name] = occs_dict
def _joints(self):
for joint in self.root.joints:
joint_dict = {}
joint_type = Configurator.joint_type_list[joint.jointMotion.jointType]
joint_dict['type'] = joint_type
# switch by the type of the joint
joint_dict['axis'] = [0, 0, 0]
joint_dict['upper_limit'] = 0.0
joint_dict['lower_limit'] = 0.0
occ_one = joint.occurrenceOne
occ_two = joint.occurrenceTwo
geom_one_origin = joint.geometryOrOriginOne.origin.asArray()
geom_one_primary = joint.geometryOrOriginOne.primaryAxisVector.asArray()
geom_one_secondary = joint.geometryOrOriginOne.secondaryAxisVector.asArray()
geom_one_third = joint.geometryOrOriginOne.thirdAxisVector.asArray()
print('')
print(joint.name)
print(f'Occurence One: {occ_one.name}')
print(occ_one.entityToken)
print(occ_one.fullPathName)
print(f'Origin one {geom_one_origin}')
print(f'geom_one_primary {geom_one_primary}')
print(f'geom_one_secondary {geom_one_secondary}')
print(f'geom_one_third {geom_one_third}')
# Check if this is already top level
# Check if the parent_list only contains one entity
parent_list = self.component_map[occ_one.entityToken].get_all_parents()
if len(parent_list) == 1:
print(f' Exists Occurence ONE: {occ_one.entityToken}')
# If it is not, get the mapping and trace it back up
else:
# the expectation is there is at least two items, the last is the full body
# the second to last should be the next top-most component
print(f' PARENT: {parent_list}')
# reset occurrence one
occ_one = self.component_map[parent_list[-2]].component
print(f'Occurence One New: {occ_one.name}')
print(f'Occurence Two: {occ_two.name}')
print(f'Occurence Two entity: {occ_two.entityToken}')
print(f'Occurence Two full path: {occ_two.fullPathName}')
parent_list = self.component_map[occ_two.entityToken].get_all_parents()
if len(parent_list) == 1:
print(f' Exists Occurence TWO: {occ_two.entityToken}')
else:
# the expectation is there is at least two items, the last is the full body
# the second to last should be the next top-most component
print(f' PARENT: {parent_list}')
# reset occurrence two
occ_two = self.component_map[parent_list[-2]].component
print(f'Occurence Two New: {occ_two.name}')
if occ_one.isGrounded:
print('--------OCCURRENCE ONE IS GROUNDED -----')
if occ_two.isGrounded:
print('--------OCCURRENCE TWO IS GROUNDED -----')
############ JOINT #####################
geom_two_origin = joint.geometryOrOriginTwo.origin.asArray()
geom_two_primary = joint.geometryOrOriginTwo.primaryAxisVector.asArray()
geom_two_secondary = joint.geometryOrOriginTwo.secondaryAxisVector.asArray()
geom_two_third = joint.geometryOrOriginTwo.thirdAxisVector.asArray()
print(f'Origin two {geom_two_origin}')
print(f'geom_two_primary {geom_two_primary}')
print(f'geom_two_secondary {geom_two_secondary}')
print(f'geom_two_third {geom_two_third}')
joint_type = joint.jointMotion.objectType # string
print(f'joint_type {joint_type}')
# Only Revolute joints have rotation axis
if 'RigidJointMotion' in joint_type:
pass
else:
joint_vector = joint.jointMotion.rotationAxisVector.asArray()
joint_rot_val = joint.jointMotion.rotationValue
joint_limit_max = joint.jointMotion.rotationLimits.maximumValue
joint_limit_min = joint.jointMotion.rotationLimits.minimumValue
if abs(joint_limit_max - joint_limit_min) == 0:
joint_limit_min = -180.0
joint_limit_max = 180.0
joint_angle = joint.angle.value
print(f'joint_vector {joint_vector}')
print(f'joint_rot_val {joint_rot_val}')
print(f'joint_limit_max {joint_limit_max}')
print(f'joint_limit_min {joint_limit_min}')
print(f'joint_angle {joint_angle}')
joint_dict['axis'] = joint_vector
joint_dict['upper_limit'] = joint_limit_max
joint_dict['lower_limit'] = joint_limit_min
# Reverses which is parent and child
joint_dict['child'] = occ_one.name
joint_dict['parent'] = occ_two.name
joint_dict['xyz'] = [ x/self.scale for x in geom_one_origin]
self.joints_dict[joint.name] = joint_dict
def _build_links(self):
''' create links '''
mesh_folder = 'meshes/'
base_link = self.base_links.pop()
center_of_mass = self.inertial_dict[base_link]['center_of_mass']
link = parts.Link(name=base_link,
xyz=[0,0,0],
center_of_mass=center_of_mass,
sub_folder=mesh_folder,
mass=self.inertial_dict[base_link]['mass'],
inertia_tensor=self.inertial_dict[base_link]['inertia'])
self.links_xyz_dict[link.name] = link.xyz
self.links[link.name] = link
for k, joint in self.joints_dict.items():
name = joint['child']
center_of_mass = [ i-j for i, j in zip(self.inertial_dict[name]['center_of_mass'], joint['xyz'])]
link = parts.Link(name=name,
xyz=(joint['xyz'][0], joint['xyz'][1], joint['xyz'][2]),
center_of_mass=center_of_mass,
sub_folder=mesh_folder,
mass=self.inertial_dict[name]['mass'],
inertia_tensor=self.inertial_dict[name]['inertia'])
self.links_xyz_dict[link.name] = (link.xyz[0], link.xyz[1], link.xyz[2])
self.links[link.name] = link
def _build_joints(self):
''' create joints '''
for k, j in self.joints_dict.items():
xyz = []
for p,c in zip(self.links_xyz_dict[j['parent']], self.links_xyz_dict[j['child']]):
xyz.append(p-c)
joint = parts.Joint(name=k , joint_type=j['type'],
xyz=xyz, axis=j['axis'],
parent=j['parent'], child=j['child'],
upper_limit=j['upper_limit'], lower_limit=j['lower_limit'])
self.joints[k] = joint