-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMeshExporter.py
More file actions
399 lines (355 loc) · 15.9 KB
/
MeshExporter.py
File metadata and controls
399 lines (355 loc) · 15.9 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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# Creates seperate meshes from joint info
from pickle import TRUE
import adsk.core, adsk.fusion
from . import UnityExporter
rootComp = None
app = None
ui = adsk.core.UserInterface.cast(None)
jointOccs = {}
newTransform = None
inverseTransform = None
# Recursively finds grounded comps
def findGroundOccs(occurrences):
groundedOccs = []
for i in range(0, occurrences.count):
occ = occurrences[i]
if occ.isGrounded:
groundedOccs.append(occ)
if occ.childOccurrences:
groundedOccs.extend(findGroundOccs(occ.childOccurrences))
return groundedOccs
# Recursively travels through assemblies and stores all assemblies with joint info
def findJointAssemblies(occurrences):
assemblyOcc = []
for i in range(0, occurrences.count):
occ = occurrences.item(i)
if occ.name.startswith("component_"):
occ.component.name = "_" + occ.component.name
# Breaks Links to be able to edit comps freely
if occ.isReferencedComponent:
occ.breakLink()
if occ.childOccurrences:
# If has Joints, add
if occ.component.joints.count > 0 or occ.component.rigidGroups.count > 0:
assemblyOcc.append(occ)
# Locks Joints
for joint in occ.component.joints:
if joint.jointMotion.jointType != 0:
joint.isLocked = True
# Nested Search
newOcc = findJointAssemblies(occ.childOccurrences)
for o in newOcc:
assemblyOcc.append(o)
return assemblyOcc
# Recursively travels through assemblies deleting small bodies
def removeSmallInAssembly(baseSize, occurrences):
for occ in occurrences:
volume = occ.component.physicalProperties.volume
if volume < 1.5 and volume < baseSize / 2:
occ.isLightBulbOn = False
elif occ.childOccurrences:
removeSmallInAssembly(baseSize, occ.childOccurrences)
# Return Origin of Joint in World Space
def jointOriginWorldSpace(jointObj):
jointsOcc = jointObj.occurrenceTwo
if not jointsOcc:
jointsOcc = rootComp
# Sets Correct Origin Based on Occurrence not Component (Also only 1 origin for offset purposes)
try:
joint = jointObj.geometryOrOriginTwo
except: # May not have geometryTwo
jointsOcc = jointObj.occurrenceOne
joint = jointObj.geometryOrOriginOne
app.activeViewport.refresh()
if joint.objectType == adsk.fusion.JointOrigin.classType():
joint = joint.geometry
jointsOrigin = joint.origin
try:
if joint.entityOne.objectType == adsk.fusion.ConstructionPoint.classType():
baseComp = joint.entityOne.component
elif joint.entityOne.objectType == adsk.fusion.SketchPoint.classType():
baseComp = rootComp
else:
baseComp = joint.entityOne.body.parentComponent
except:
ui.messageBox("Whoops! It seems Joint: \"" + jointObj.name + "\" is connected to a currently not supported piece of Geometry! In a future update this may be fixed.")
joint.entityOne.body.parentComponent
baseComp = rootComp.allOccurrencesByComponent(baseComp).item(0)
if baseComp and not hasattr(jointObj, 'storedJoint'):
transform = baseComp.transform2
transform.invert()
transform.transformBy(jointsOcc.transform2)
jointsOrigin.transformBy(transform)
return jointsOrigin
# Returns vector with only one +/- 1 value
def returnNormalVector(point):
if abs(point.x) > abs(point.y) and abs(point.x) > abs(point.z):
returnVec = adsk.core.Vector3D.create(1, 0, 0)
returnVec.scaleBy(point.x / abs(point.x))
return returnVec
if abs(point.y) > abs(point.x) and abs(point.y) > abs(point.z):
returnVec = adsk.core.Vector3D.create(0, 1, 0)
returnVec.scaleBy(point.y / abs(point.y))
return returnVec
if abs(point.z) > abs(point.x) and abs(point.z) > abs(point.y):
returnVec = adsk.core.Vector3D.create(0, 0, 1)
returnVec.scaleBy(point.z / abs(point.z))
return returnVec
# Rigid Combination
def rigidOccs(occs, assembledComps, _assembledComps, groundedComps, exportComp):
# New Component per Rigid Joint or absorb into other Group
newCompNames = []
grounded = False
for occRG in occs:
if occRG in groundedComps:
grounded = True
for i in range(len(assembledComps)):
# If Comp Already in Link, Absorb Previous Linked Components into One
if occRG.fullPathName in assembledComps[i]:
if i == 0:
grounded = True
break
for compName in assembledComps[i]:
newCompNames.append(compName)
assembledComps[i] = []
_assembledComps[i].deleteMe()
break
# Else, Create Link for New Component
else:
newCompNames.append(occRG.fullPathName)
# Stores Linked info into Arrays
if grounded:
assembledComps[0].extend(newCompNames)
else:
newComp = exportComp.occurrences.addNewComponent(inverseTransform)
newComp.component.name = "component_" + str(len(assembledComps))
assembledComps.append(newCompNames)
_assembledComps.append(newComp)
return assembledComps, _assembledComps
# Main Function
def runMesh(wheelNames):
global app, ui
app = adsk.core.Application.get()
ui = app.userInterface
# Get the root component of the active design
product = app.activeProduct
design = adsk.fusion.Design.cast(product)
global rootComp
rootComp = design.rootComponent
app.activeViewport.refresh()
# Creates Progress Bar
progressBar = ui.createProgressDialog()
# Leave this here in case I need to debug again
# Create a graphics group on the root component.
#graphics = rootComp.customGraphicsGroups.add()
# Adds Point Graphic
#coord = adsk.fusion.CustomGraphicsCoordinates.create(point.asArray())
#point = graphics.addPointSet(coord, [0], adsk.fusion.CustomGraphicsPointTypes.UserDefinedCustomGraphicsPointType,
# 'SelectJoint.png')
# Checks upwards and forward directions
progressBar.show("Converting Robot File", "Starting Process...", 0, 1, 0)
progressBar.progressValue = 1
app.activeViewport.refresh()
adsk.doEvents()
originalCam = app.activeViewport.camera
# Move to Up
cam = app.activeViewport.camera
cam.viewOrientation = 10
cam.isSmoothTransition = False
app.activeViewport.camera = cam
app.activeViewport.fit()
# Capture Up
cam = app.activeViewport.camera
yVector = returnNormalVector(cam.eye)
# Move to Forward
cam.viewOrientation = 3
cam.isSmoothTransition = False
app.activeViewport.camera = cam
app.activeViewport.fit()
# Capture Forward
cam = app.activeViewport.camera
zVector = returnNormalVector(cam.eye)
# Reset
app.activeViewport.camera = originalCam
app.activeViewport.fit()
# Gets Transform to apply to occurrences
global newTransform, inverseTransform
newTransform = adsk.core.Matrix3D.create()
origin = adsk.core.Point3D.create(0, 0, 0)
newTransform.setToAlignCoordinateSystems(origin, yVector.crossProduct(zVector), yVector, zVector, origin,
adsk.core.Vector3D.create(-1, 0, 0), adsk.core.Vector3D.create(0, 1, 0), adsk.core.Vector3D.create(0, 0, -1))
# Adds on origin as well
minPoint = rootComp.boundingBox.minPoint.asVector()
maxPoint = rootComp.boundingBox.maxPoint.asVector()
minPoint.transformBy(newTransform)
maxPoint.transformBy(newTransform)
floorPoint = min(minPoint.y, maxPoint.y)
minPoint.add(maxPoint)
minPoint.scaleBy(-.5)
minPoint.y = -floorPoint #Ground Plane is offset weirdly so base it instead off of wheel sizes in importer
newTransform.setToAlignCoordinateSystems(origin, yVector.crossProduct(zVector), yVector, zVector, minPoint.asPoint(),
adsk.core.Vector3D.create(-1, 0, 0), adsk.core.Vector3D.create(0, 1, 0), adsk.core.Vector3D.create(0, 0, -1))
# Checks if any grounded comps
groundedComps = findGroundOccs(rootComp.occurrences)
# Creates List of assembly components that contain (locked) joints
assemblyOcc = [rootComp]
for joint in rootComp.joints:
if joint.jointMotion.jointType != 0:
joint.isLocked = True
for occ in findJointAssemblies(rootComp.occurrences):
assemblyOcc.append(occ)
# -Loops through Joints in Assemblies-
# Puts all in exportcomp to be able to re-orient unitycomps
exportComp = rootComp.occurrences.addNewComponent(adsk.core.Matrix3D.create()).component
exportComp.name = "exportcomps"
# (First Assembled Group is Base Group)
assembledComps = [[]]
inverseTransform = newTransform.copy() # This is needed as some comps need inverse transform applied
inverseTransform.invert()
_assembledComps = [exportComp.occurrences.addNewComponent(inverseTransform)]
_assembledComps[0].component.name = "chassis"
if progressBar.wasCancelled:
return False
progressBar.show("Converting Robot File", "Step 1: Analyzing Joint Data...", 0, len(assemblyOcc) * 4, 0)
progressBar.progressValue = 0
# Does Rigid Groups First
for occs in assemblyOcc:
occ = occs if occs == rootComp else occs.component
for rg in occ.rigidGroups:
if rg.isSuppressed:
continue
if occs != rootComp:
rg = rg.createForAssemblyContext(occs)
# Calls to modify assembledComps based off new occs
assembledComps, _assembledComps = rigidOccs(rg.occurrences, assembledComps, _assembledComps, groundedComps, exportComp)
if progressBar.wasCancelled:
return False
progressBar.progressValue += 1
# Does Rigid Joints Next
for occs in assemblyOcc:
occ = occs if occs == rootComp else occs.component
for rigidJoint in occ.joints:
if rigidJoint.isSuppressed:
continue
# Joins Rigid Joint Components together
if rigidJoint.jointMotion.jointType == 0:
if occs != rootComp:
rigidJoint = rigidJoint.createForAssemblyContext(occs)
jointsOcc = [rigidJoint.occurrenceOne, rigidJoint.occurrenceTwo]
# Calls to modify assembledComps based off new occs
assembledComps, _assembledComps = rigidOccs(jointsOcc, assembledComps, _assembledComps, groundedComps, exportComp)
if progressBar.wasCancelled:
return False
progressBar.progressValue += 1
# Does Revolve/Slide Joints Last
childGroups = []
jointPairs = []
jntXMLS = []
jointCount = 0
for occs in assemblyOcc:
occ = occs if occs == rootComp else occs.component
for jointObj in occ.joints:
if jointObj.isSuppressed:
continue
# Breaks up Joint Components into seperate assembledComps
if jointObj.jointMotion.jointType == 1 or jointObj.jointMotion.jointType == 2:
if occs != rootComp:
jointObj = jointObj.createForAssemblyContext(occs)
# Creates an Unlinked Component for the Revolute Part
occurrences = [jointObj.occurrenceOne, jointObj.occurrenceTwo]
if not occurrences[1]:
parentGroup = 0
for jntOcc in range(2 if occurrences[1] else 1):
if occurrences[jntOcc] == rootComp or occurrences[jntOcc] in groundedComps:
if jntOcc == 0:
childGroups.append(0)
else:
parentGroup = 0
continue
for i in range(len(assembledComps)):
if occurrences[jntOcc].fullPathName in assembledComps[i]:
if jntOcc == 0:
childGroups.append(i)
else:
parentGroup = i
break
else:
# Not a part of a section
if jntOcc == 0:
childGroups.append(len(assembledComps))
else:
parentGroup = len(assembledComps)
newOcc = exportComp.occurrences.addNewComponent(inverseTransform)
newOcc.component.name = "component_" + str(len(assembledComps))
assembledComps.append([occurrences[jntOcc].fullPathName])
_assembledComps.append(newOcc)
# Setup Joint XML
jntXMLS.append(UnityExporter.createJntXML(jointObj, parentGroup, childGroups[-1], jointCount))
jointPairs.append([parentGroup, childGroups[-1]])
jointCount += 1
if progressBar.wasCancelled:
return False
progressBar.progressValue += 2
# Need to do basejoint check if no grounded comp present
groundedJnts = [0]
# Check if already part of a grounded connection
if len(groundedComps) > 0:
i = 0
while i < len(groundedJnts):
for pair in jointPairs:
if pair[0] == groundedJnts[i] and pair[1] not in groundedJnts:
groundedJnts.append(pair[1])
elif pair[1] == groundedJnts[i] and pair[0] not in groundedJnts:
groundedJnts.append(pair[0])
i += 1
# For all groups not part of a grounded connection, ground loose parents of joints
for i in range(1, len(assembledComps)):
if i not in groundedComps and _assembledComps[i].isValid and i not in childGroups:
assembledComps[i] = []
_assembledComps[i].deleteMe()
app.activeViewport.refresh()
# Meshing Code
progressBar.show("Converting Robot File", "Step 2: Combining Occurrences...", 0, len(assembledComps), 0)
progressBar.progressValue = 0
for i in range(1, len(assembledComps)):
for c in range(len(assembledComps[i])):
childOccs = rootComp.occurrences
location = assembledComps[i][c].split('+')
for occName in location:
newOcc = childOccs.itemByName(occName)
if not newOcc:
break
childOccs = newOcc.childOccurrences
if newOcc:
# --WIP TEST--
# If has children comps, don't also link them
#if newOcc.childOccurrences:
# newComp = _assembledComps[i].component.occurrences.addNewComponent(adsk.core.Matrix3D.create())
# for body in newOcc.bRepBodies:
# body.moveToComponent(newComp)
# Link connected comp to assembledComp
#else:
newOcc.moveToComponent(_assembledComps[i])
if progressBar.wasCancelled:
return False
progressBar.progressValue += 1
while rootComp.occurrences.item(0).name != "exportcomps:1":
rootComp.occurrences.item(0).moveToComponent(_assembledComps[0])
progressBar.progressValue += 1
# Removes Wheel Comps
for wheel in wheelNames:
for i in range(1, len(assembledComps)):
if wheel in assembledComps[i]:
_assembledComps[i].isLightBulbOn = False
break
# Remove Small Bodies
progressBar.show("Converting Robot File", "Step 3: Removing Small Bodies...", 0, len(assembledComps), 0)
progressBar.progressValue = 0
for occ in rootComp.occurrences[0].childOccurrences: # _assembledComps couldnt tell if LightBulbOn
if occ.isLightBulbOn:
removeSmallInAssembly(occ.physicalProperties.volume, occ.childOccurrences)
if progressBar.wasCancelled:
return False
progressBar.progressValue += 1
progressBar.hide()
return jntXMLS