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: 

Accessing The Drop Down List selected Item from Input Changed Handler

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
Anonymous
1785 Views, 3 Replies

Accessing The Drop Down List selected Item from Input Changed Handler

I'm trying to access a selected item from a Text Drop down text box and use that as a key to a dictionary that will influence the input types I fill a second tab with.

However no item seems to be selected even though there was a change that activated the handler. How do I access the selected item in this list? Is it possible or are there work arounds?

Here are the handlers in m

class InputChangedHandler(adsk.core.InputChangedEventHandler):
            def __init__(self):
                super().__init__()
            def notify(self, args):
                try:
                    command = args.firingEvent.sender
                    cmdInput = args.input
                    if cmdInput.id == 'ObjectTypeSelection':
                        ui.messageBox('Input: {} changed event triggered for Object Selection'.format(command.parentCommandDefinition.id))
                        inputs = command.commandInputs
                        ui.messageBox("Input is valid: "+str(cmdInput.isValid))
                        objectItems = cmdInput.listItems
                        selection = None
                        for item in objectItems:
                            ui.messageBox(str(item.name)+"selected = "+str(item.isSelected))
                            if item.isSelected:
                                selection = item
                                break
                        ui.messageBox("selection: "+str(selection))
                        tabCmdInput2 = inputs.addTabCommandInput(commandIdOnPanel + '_tab_2', 'Define your ')
#                        tab1ChildInputs = tabCmdInput2.children
                except:
                    if ui:
                        ui.messageBox('Input changed event failed: {}'.format(traceback.format_exc()))

        class CommandExecuteHandler(adsk.core.CommandEventHandler):
            def __init__(self):
                super().__init__()
            def notify(self, args):
                try:
                    command = args.firingEvent.sender
                    ui.messageBox('command: {} executed successfully'.format(command.parentCommandDefinition.id))
                except:
                    if ui:
                        ui.messageBox('command executed failed: {}'.format(traceback.format_exc()))

        class CommandCreatedEventHandlerPanel(adsk.core.CommandCreatedEventHandler):
            def __init__(self):
                super().__init__() 
            def notify(self, args):
                try:
                    cmd = args.command
                    onExecute = CommandExecuteHandler()
                    cmd.execute.add(onExecute)

                    onInputChanged = InputChangedHandler()
                    cmd.inputChanged.add(onInputChanged)
                    # keep the handler referenced beyond this function
                    handlers.append(onExecute)
                    handlers.append(onInputChanged)
                    # Keep the handler referenced beyond this function
                    inputs = cmd.commandInputs
        
                    # Create tab input 1
                    tabCmdInput1 = inputs.addTabCommandInput(commandIdOnPanel + '_tab_1', 'Object Type Selection')
                    tab1ChildInputs = tabCmdInput1.children
                    dropDownCommandInput = tab1ChildInputs.addDropDownCommandInput('ObjectTypeSelection', 'Select an Object Definition', adsk.core.DropDownStyles.TextListDropDownStyle)
                    dropDownItems = dropDownCommandInput.listItems
                    for objType in {"Bolt":"stuff"}:
                        dropDownItems.add(objType, False)                    
                    ui.messageBox('Panel command created successfully')
                except:
                    if ui:
                        ui.messageBox('Panel command created failed: {}'.format(traceback.format_exc()))

y code. Bolt should be shown as selected.

3 REPLIES 3
Message 2 of 4
ekinsb
in reply to: Anonymous

It took a little while, but I've discovered the problem.  There is a bug in the API, but there is also a simple workaround.  The problem occurs when you create a drop down without any of the items being selected.  The first time an item is selected from the drop down Fusion doesn't correctly return the selected item.  However, if there is an item already selected then everything works as expected.  If you don't want to have an item selected because the act of selecting one causes an action, you can add an additional item that is something like "None" or even have an item that is an empty string.  You can put the empty string item at the bottom of the list so it's not as obvious that it's there. 

 

I don't think any of the Fusion commands use a drop down in this way so it's possibly wasn't designed to support not having any of the items selected.  I'll log a bug but it's not obvious right now what the "correct" answer will be to fix it.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 3 of 4
keqingsong
in reply to: Anonymous

This will be fixed in our May 25 update! 


Keqing Song
Autodesk Fusion Community Manager
Portland, Oregon, USA

Become an Autodesk Fusion Insider



Message 4 of 4

In the original example you could simplify the code:

                        objectItems = cmdInput.listItems
                        selection = None
                        for item in objectItems:
                            ui.messageBox(str(item.name)+"selected = "+str(item.isSelected))
                            if item.isSelected:
                                selection = item
                                break

to be:

objectItems = cmdInput.selectedItem

 This is from the reference here:

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-d48e4975-f1c0-494f-b2aa-d41c20532009


Luke Edwards
Consulting Services Manager

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report