Message 1 of 2
materials.itemByName() should return "null" but it doesn't
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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()))
Jérôme Briot, Freelance engineer - Mechanical design and prototyping
3D Print Plus / Pro | IDF Import | GitHub To Fusion 360 | Tube Bending Data Exchanger | Slice Data Export
Memory Used | Basic Calculator | Check Computer Specifications | Import spline from any CSV file