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: 

materials.itemByName() should return "null" but it doesn't

1 REPLY 1
Reply
Message 1 of 2
JeromeBriot
240 Views, 1 Reply

materials.itemByName() should return "null" but it doesn't

Hello,

 

import adsk
import traceback

def run(context):

    ui = None

    try:
        app = adsk.core.Application.get()
        ui = app.userInterface

        matLib = 'Fusion 360 Material Library'
        matName = 'ABS Plastic'

        # Get the library.
        lib = app.materialLibraries.itemByName(matLib)
        if not lib:
            ui.messageBox('"{}" not found'.format(matLib))
            return

        material = lib.materials.itemByName(matName)
        if not material:
            ui.messageBox('"{}" not found'.format(matName))
            return

        ui.messageBox('Succeed')

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


When mispeling the material library name (matLib variable), the script above works as expected.
app.materialLibraries.itemByName() returns "null" and the first message box appears.

When mispeling the material name (matName variable), an exception occurs and the except clause is executed.
materials.itemByName() should return "null" but it doesn't. That's what the documentation says: Materials.itemByName Method.

A workaround:

import adsk
import traceback

def run(context):

    ui = None

    try:
        app = adsk.core.Application.get()
        ui = app.userInterface

        matLib = 'Fusion 360 Material Library'
        matName = 'ABS Plastic'

        lib = app.materialLibraries.itemByName(matLib)
        if not lib:
            ui.messageBox('"{}" not found'.format(matLib))
            return

        try:
            material = lib.materials.itemByName(matName)
        except:
            ui.messageBox('"{}" not found'.format(matName))
            return

        ui.messageBox('Succeed')

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Tags (2)
1 REPLY 1
Message 2 of 2
marshaltu
in reply to: JeromeBriot

Hello,

 

Thank you for reporting the issue to us. UP-36434 has been logged in our internal system to track it.

 

Thanks,

Marshal



Marshal Tu
Fusion 360 Developer
Autodesk, Inc.

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