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

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

JeromeBriot
Mentor Mentor
399 Views
1 Reply
Message 1 of 2

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

JeromeBriot
Mentor
Mentor

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()))
0 Likes
400 Views
1 Reply
Reply (1)
Message 2 of 2

marshaltu
Autodesk
Autodesk

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 Developer
>
0 Likes