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.
Solved! Go to Solution.
Solved by ekinsb. Go to Solution.
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.
This will be fixed in our May 25 update!
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
Can't find what you're looking for? Ask the community or share your knowledge.