Fusion Python Script - Create Multi Selection Panel

isocam
Collaborator

Fusion Python Script - Create Multi Selection Panel

isocam
Collaborator
Collaborator

Can anybody help?

 

Has anybody got a sample script showing how to create a multi select "drop down" panel with the corresponding code??

 

For example, 

I need to create a new panel (for testing purposes) with four possible selections

 

Selection1

Selection2

Selection3

Selection4

 

So if the user selects any of the above a corresponding sub routine is called that shows a message box Saying...

 

Selection1

Selection2

Selection3

Selection4

 

The sub-routine will be something like...

 

def Selection1():

      ui.messageBox("Selection1 Selected", 'Fusion 360', 0, 3)

 

Many thanks in advance!!!

 

Darren

0 Likes
Reply
315 Views
1 Reply
Reply (1)

tykapl.breuil
Advocate
Advocate

I wasn't sure if you were asking for just a normal dropdown or a dropdown with selection inputs, for the first the code is quite simple and works the way you intend; for the second, you have to use a GroupCommandInput to have a dropdown and you only get a messageBox when the selection is changed rather than when you switch focus between inputs.

Here is the sample code let me know if it helps.

import adsk.core, adsk.fusion, traceback, adsk.cam

#Affectations usuelles
handlers = []
app: adsk.core.Application = adsk.core.Application.get()
ui = app.userInterface

class CommandCreatedHandler(adsk.core.CommandCreatedEventHandler):    
    def __init__(self):
        super().__init__()        
    def notify(self, args: adsk.core.CommandCreatedEventArgs):
        try:
            cmd = args.command
            inputs = cmd.commandInputs
            onExecute = CommandExecuteHandler()
            cmd.execute.add(onExecute)
            handlers.append(onExecute)
            onInputChanged = MyInputChangedHandler()
            cmd.inputChanged.add(onInputChanged)
            handlers.append(onInputChanged)

            dropdown = inputs.addDropDownCommandInput('testDropdown', 'Test', 1)
            listItems = dropdown.listItems
            for dropdownId in ['dropdown' + str(i) for i in range(4)]:
                listItems.add(dropdownId, False)

            group = inputs.addGroupCommandInput('TestGroup', 'Group')
            group.children.addSelectionInput('select1', 'Select1', 'Select1')
            group.children.addSelectionInput('select2', 'Select2', 'Select2')
            group.children.addSelectionInput('select3', 'Select3', 'Select3')
            group.children.addSelectionInput('select4', 'Select4', 'Select4')

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

class CommandExecuteHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args: adsk.core.CommandEventArgs):
        try:
            pass
    
        except:
            if ui:
                ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class MyInputChangedHandler(adsk.core.InputChangedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            eventArgs = adsk.core.InputChangedEventArgs.cast(args)
            if eventArgs.input.id == 'testDropdown':
                ui.messageBox(eventArgs.input.selectedItem.name)
            else:
                ui.messageBox(eventArgs.input.id)
        
        except:
            if ui:
                ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def run(context):
    try:
        ui = app.userInterface
        cmdDefinitions = ui.commandDefinitions
        btnCmdDefinition = cmdDefinitions.itemById('testCommand')
        if btnCmdDefinition:
            btnCmdDefinition.deleteMe()
        btnCmdDefinition: adsk.core.CommandDefinition = cmdDefinitions.addButtonDefinition('testCommand', 'test', 'test')
        onCreated = CommandCreatedHandler()
        btnCmdDefinition.commandCreated.add(onCreated)
        handlers.append(onCreated)
        btnCmdDefinition.execute()

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

def stop(context): 
    # Il faut nettoyer l'interface si on ferme l'add-in
    try:
        cmdDef = ui.commandDefinitions.itemById('testCommand')
        if cmdDef:
            cmdDef.deleteMe()
        
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 In general, to see the possibilities of a command dialog, I'll advise you to check the command inputs sample addin here : https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-e5c4dbe8-ee48-11e4-9823-f8b156d7cd97

 

2 Likes