Question:
I attached the attributes to some selected faces of an assembly. When I iterate the faces of each component and try to find out those faces, they returned none attributes. My code is as below. It assumes a face has been selected in the assembly.
def main():
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
face = adsk.fusion.BRepFace.cast(ui.activeSelections.item(0).entity)
face.attributes.add('MyGroup', 'MyName', 'MyValue')
design = adsk.fusion.Design.cast(app.activeProduct)
for occ in design.rootComponent.occurrences:
comp = occ.component
body = comp.bRepBodies.item(0)
for face in body.faces:
attr = face.attributes.itemByName('MyGroup', 'MyName')
#always none
if attr != None:
ui.messageBox('Attribute is {}'.format(attr))
else:
print('Attribute is None')
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Solution:
The geometries we see in the assembly are actually the references from the native components. In Fusion 360, they are called Proxy objects. So, when you select a face in the context of an assembly, the face is actually a proxy face. That means, the attributes was added to the proxy face. While when you iterate the faces of each component, that will be the native face which does not have such attributed added for proxy face. So if you want to find back those information, you need to get the proxy face from the native one. e.g.
def main():
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
face = adsk.fusion.BRepFace.cast(ui.activeSelections.item(0).entity)
face.attributes.add('MyGroup', 'MyName', 'MyValue')
design = adsk.fusion.Design.cast(app.activeProduct)
for occ in design.rootComponent.occurrences:
comp = occ.component
body = comp.bRepBodies.item(0)
for face in body.faces:
#get proxy face in the assembly context, corresponding to the
# native face in this occurence
faceproxy= face.createForAssemblyContext(occ)
#get attribute from the proxy face
attr = faceproxy.attributes.itemByName('MyGroup', 'MyName')
#attr = face.attributes.itemByName('MyGroup', 'MyName')
if attr != None:
ui.messageBox('Attribute is {}'.format(attr))
else:
print('Attribute is None')
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))