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: 

Fusion 360 Python Script - Set Material Name

1 REPLY 1
SOLVED
Reply
Message 1 of 2
isocam
158 Views, 1 Reply

Fusion 360 Python Script - Set Material Name

Can anybody help?

 

Does anybody know how to set the material name using a Python script?

 

For example only, I want to set the material to "Stainless Steel, Brushed".

 

Can this be done???

 

Many thanks in advance!!!!

 

Darren

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

Hi @isocam .

 

We have created a sample that applies the material 'Stainless Steel, Brushed'.

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core


def run(context):
    ui = adsk.core.UserInterface.cast(None)
    try:
        app: adsk.fusion.Application = adsk.core.Application.get()
        ui = app.userInterface

        # select body
        msg: str = 'Select Body'
        selFiltter: str = 'Bodies'
        sel: adsk.core.Selection = selectEnt(msg, selFiltter)
        if not sel:
            return
        body: adsk.fusion.BRepBody = sel.entity

        # get Material
        materialLibs: adsk.core.MaterialLibraries = app.materialLibraries

        mateLib: adsk.core.MaterialLibrary = materialLibs.itemByName(
            'Fusion 360 Material Library')
        if not mateLib:
            return

        mate: adsk.core.Material = mateLib.materials.itemByName(
            'Stainless Steel, Brushed')
        if not mate:
            return

        # set Material
        body.material = mate

        ui.messageBox('Done')

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


def selectEnt(
        msg: str,
        filtterStr: str) -> adsk.core.Selection:

    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        sel = ui.selectEntity(msg, filtterStr)
        return sel
    except:
        return None

 

This will output a list of materials available to you.

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core


def run(context):
    ui = adsk.core.UserInterface.cast(None)
    try:
        app: adsk.fusion.Application = adsk.core.Application.get()
        ui = app.userInterface

        materialLibs: adsk.core.MaterialLibraries = app.materialLibraries
        mateLib: adsk.core.MaterialLibrary
        mate: adsk.core.Material
        for mateLib in materialLibs:
            print(f'-- {mateLib.name} : {mateLib.id} --')
            for mate in mateLib.materials:
                print(f'  {mate.name} : {mate.id}')

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

 

 

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