Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Change Part Material, Using a Python Script, Without Selecting The Part First?

1 REPLY 1
Reply
Message 1 of 2
isocam
322 Views, 1 Reply

Change Part Material, Using a Python Script, Without Selecting The Part First?

Can anybody tell me if the following is possible?

 

Is it possible to change a parts "physical" material, using a Fusion 360 API Python Script, without selecting the part first?

 

For example, run the script and the parts material changes without any other input from the user. Say, for testing purposes only, change the parts material to "Steel, Mild"

 

Many thanks

 

Darren

1 REPLY 1
Message 2 of 2
BrianEkins
in reply to: isocam

It's certainly possible to assign a material using the API. The question is what do you want to assign it to?  You said you want to assign it to a "part", but a material can be assigned to a component and it can also be assigned to individual bodies.  If assigned to a component, all of the bodies in that component will inherit that material unless that body has a material specifically assigned to it.

 

The code below assigns the material "gold" to the component associated with the first occurrence in a design. You'll have to define how you'll know which part you want to set the material for.

 

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

        # Get the first occurrence in the assembly.
        occ = root.occurrences.item(0)

        # Get the component associated with the occurrence.
        comp = occ.component

        # Get the material library and the "Gold" material from it.
        lib = app.materialLibraries.itemByName('Fusion 360 Material Library')
        gold = lib.materials.itemByName('Gold')
        
        # Assign the material to the component.
        comp.material = gold
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report