Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Python API: Inconsistent behaviour amongst collections on `itemByName(_name_)` when `_name_` doesn't exist.

njdupreez
Participant

Python API: Inconsistent behaviour amongst collections on `itemByName(_name_)` when `_name_` doesn't exist.

njdupreez
Participant
Participant

Good day,

 

Using the `itemByName(_name_)` method on instances of `MirrorFeatures` and `StichFeatures` results in exceptions when there is no feature with a name as the given argument `_name_` in the collection.  The online API reference at itemByName, states that `null` i.e. `None` should be returned if `_name_` can't be found.  This is seemingly not the case for the two mentioned collections.

 

The script following further on replicates the problem and shows that other collections seemingly work, for non-existent names, as documented in the API reference.

 

Regards,

Nicolaas

import adsk.fusion, adsk.core, traceback

def replicate_problem():
    # Identify the root component
    app = adsk.core.Application.get()
    design = app.activeProduct
    root = design.rootComponent
    # Get the names of all collections on the root component
    collections = [c for c in dir(root.features) if c.lower().endswith('features') and not c.startswith('_')] 
    # Try out each collection to verify the behaviour
    results = []
    for c in collections:
        statement = f'root.features.{c}.itemByName("non-existent")'
        try:
            result = eval(statement)
            result_str = f'{statement} -> {result}'
        except:
            result_str = f'{statement} -> {result}: {traceback.format_exc()}'
        results.append(result_str)
    return results # list of results for all Feature collections

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

        results = replicate_problem()
        print('\n'.join(results))
        
        ui.messageBox('\n'.join(results))

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

 

0 Likes
Reply
Accepted solutions (1)
313 Views
1 Reply
Reply (1)

goyals
Autodesk
Autodesk
Accepted solution

This is bug in code. Created FUS-79396 ticket to track it. Thanks for posting it. 



Shyam Goyal
Sr. Software Dev. Manager
0 Likes