NoneType error when using SelectionCommandInput

NoneType error when using SelectionCommandInput

Anonymous
1,165 Views
2 Replies
Message 1 of 3

NoneType error when using SelectionCommandInput

Anonymous
Not applicable

Hello,

 

I'm pretty new to scripting within Fusion and as such am sure there is a simple explanation for the error I am receiving.  I am attempting to, utilizing a Command, get a user to select a component within a given design. I then want to use this component selection and its associated attributes (name) within the script to do some additional work (transformations, etc.).  I am currently using the InputChangedEventHandler to handle this selection based event and have everything working up until the point where I attempt to get the selected components name -- at which point I get an AttributeError: 'NoneType' object has no attribute 'name' error.  The component I am selecting is not the root component of the design, but I am unsure if this matters in this scenario.

 

My InputChangedHandler is as follows:

 

class MyCommandInputChangedHandler(adsk.core.InputChangedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            global fileButtonPressed
            
            command = args.firingEvent.sender
            cmdInput = args.input
            inputs = command.commandInputs    
                              
            if cmdInput.id == inputs.itemById(commandId + '_fileButton').id:
                fileButtonPressed = True
            elif cmdInput.id == inputs.itemById(commandId + '_bodySelector').id:
                selectionIn = adsk.core.SelectionCommandInput.cast(cmdInput)
                if selectionIn.selectionCount == 1:
                    bodySelected = adsk.fusion.Occurrence.cast(selectionIn.selection(0).entity)
                    # simple check to see if the appropriate body is selected
                    ui.messageBox(bodySelected.name)   
                    
        except:
            if ui:
                ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

I should note that everything executes as expected up until I attempt to retrieve the name attribute of the selected component

 

Thanks in advance for any help,

 

Dylan

0 Likes
Accepted solutions (1)
1,166 Views
2 Replies
Replies (2)
Message 2 of 3

ekinsb
Alumni
Alumni
Accepted solution

It's difficult to say what the problem is with the code you've posted here.  I would suggest debugging your add-in and putting a break point on the messageBox line.  Once you've hit that break point using the Console window and enter "bodySelected".  It will display the type of the object you have.  Actually it will probably return nothing indicating that it's None which most likely means that the cast failed.  In the Console window enter "selectionIn.selection(0).entity" to see what the entity type really is.  I'm guessing that the settings you have for the selection input aren't set correctly to pick and Occurrence.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 3 of 3

Anonymous
Not applicable

Hello Brian,

 

Thanks for the very quick reply, I wasn't certain exactly what portions of my code to post so I went with a minimal set -- fortunately for me you solved my problem with your guess that I had set an inappropriate selection filter and as such the cast wasn't working -- I have fixed this and it now works as expected.

 

Thanks again,

 

Dylan

0 Likes