cannot get material of component

cannot get material of component

tin.pecirep
Explorer Explorer
1,207 Views
6 Replies
Message 1 of 7

cannot get material of component

tin.pecirep
Explorer
Explorer

I'm trying to access the material of a component that has both subcomponents with different materials and its own bodies, but I'm getting errors that there is no material attribute associated with it. there's a picture of my structure at the bottom, Z Idler A is the component I'm trying to access the material of. This is my code:

        root = design.rootComponent
        occs = root.allOccurrences
        
        # Gather information about each unique component
        componentsToExport = []
        for occ in occs:
            comp = occ.component
            
            if not hasattr(comp, 'material'): continue
            if not hasattr(comp.material, 'name'): continue
            if comp.material.name != 'ABS Plastic': continue


Fusion360_ZlJ4JXEvbd.png

0 Likes
Accepted solutions (1)
1,208 Views
6 Replies
Replies (6)
Message 2 of 7

BrianEkins
Mentor
Mentor

Can you attach your model?  The Component object should _always_ have the material property.  I created a simple assembly that is close to yours in structure and I couldn't reproduce the problem.

 

What you've built is something that doesn't have a logical equivalent in the real world.  Fusion lets you create a component that has both bodies and an occurrence.  In the real-world you have either an assembly or a part where an assembly is a grouping a parts and a part is a single piece of geometry.  Fusion lets you easily mix the two but it's better if you keep components clean by only having them represent an assembly (would only contain occurrences) or a part (would only contain a body).

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 7

kandennti
Mentor
Mentor

Hi @tin.pecirep .

 

If there is more than one material, component.material returns None.

Isn't the error occurring after the presented code?

0 Likes
Message 4 of 7

tin.pecirep
Explorer
Explorer

thank you, I can post the model later today hopefully, but I will probably just go with the best practice you described. Thank you for the explanation, that makes sense of course.

0 Likes
Message 5 of 7

tin.pecirep
Explorer
Explorer

that's a bummer. and yes, I didn't actually describe my code properly. this piece of code already checks for the material attribute because I was getting errors further down where I'm comparing material names. After I did this I noticed that components were missing from the end result, because they were being skipped over in this step.
So there is no way around this then? I can't get the material of bodies in assembly components?

0 Likes
Message 6 of 7

kandennti
Mentor
Mentor
Accepted solution

Probably you have to get all body materials.

 

#Fusion360API Python Script

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        des = adsk.fusion.Design.cast(app.activeProduct)
        root = des.rootComponent

        # all bRepBodies
        bodies = [bdy for bdy in root.bRepBodies]

        occ :adsk.fusion.Occurrence.cast(None)
        for occ in root.allOccurrences:
            lst = [bdy for bdy in occ.bRepBodies]
            bodies.extend(lst)

        # get Material Info
        infos = [getMaterialInfo(bdy) for bdy in bodies]

        # finish
        msg :str = '** BodyName : Material : ComponentName **\n'
        msg += '\n'.join(infos)

        print(msg)
        ui.messageBox(msg)

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def getMaterialInfo(
    bdy :adsk.fusion.BRepBody) -> str:

    return "{} : {} : {}".format(
        bdy.name,
        bdy.material.name,
        bdy.parentComponent.name)
Message 7 of 7

tin.pecirep
Explorer
Explorer

this worked out perfectly, thank you very much!

0 Likes