Hello,
Here is a basic example:
import adsk.core, adsk.fusion, adsk.cam, traceback
# Global variables to keep everything in scope
handlers = []
app = adsk.core.Application.get()
ui = app.userInterface
# Event handler for the commandCreated event.
class CommandCreatedEventHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
cmd = eventArgs.command
inputs = cmd.commandInputs
dropDownCommandInput = inputs.addDropDownCommandInput('dropdownCommandInput', 'Material', adsk.core.DropDownStyles.LabeledIconDropDownStyle)
dropDownItems = dropDownCommandInput.listItems
dropDownItems.add('ALREC00229 0.5in_1in', True)
dropDownItems.add('ALREC00174 1in_2.5in', False)
dropDownItems.add('ALREC00290 1in_3in', False)
#Connect handler to inputChanged event
onInputChanged = CommandInputChangedHandler()
cmd.inputChanged.add(onInputChanged)
handlers.append(onInputChanged)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
# Event handler for the inputChanged event.
class CommandInputChangedHandler(adsk.core.InputChangedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
eventArgs = adsk.core.InputChangedEventArgs.cast(args)
changedInput = eventArgs.input
if changedInput.id == 'dropdownCommandInput':
ui.messageBox(changedInput.selectedItem.name)
if changedInput.selectedItem.name == 'ALREC00229 0.5in_1in':
stockZ = .5
stockX = .5
# userParams.itemByName('stockZ').expression = '.5'
# userParams.itemByName('stockX').expression = '1'
else:
stockX = 5
stockY = 5
# userParams.itemByName('stockX').expression = '5'
# userParams.itemByName('stockY').expression = '5'
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def run(context):
try:
# Get the CommandDefinitions collection.
cmdDefs = ui.commandDefinitions
# Create a button command definition.
buttonDefinition = cmdDefs.addButtonDefinition('TestDropdownId',
'Test Dropdown',
'',
'resources')
# Get the ADD-INS panel in the model workspace.
addInsPanel = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel')
# Add the button to the bottom of the panel.
buttonCommand = addInsPanel.controls.addCommand(buttonDefinition)
buttonCommand.isPromoted = True
buttonCommand.isPromotedByDefault = True
# Connect to the command created event.
commandCreated = CommandCreatedEventHandler()
buttonDefinition.commandCreated.add(commandCreated)
handlers.append(commandCreated)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def stop(context):
try:
# Clean up the UI.
cmdDef = ui.commandDefinitions.itemById('TestDropdownId')
if cmdDef:
cmdDef.deleteMe()
addinsPanel = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel')
cntrl = addinsPanel.controls.itemById('TestDropdownId')
if cntrl:
cntrl.deleteMe()
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
You should read this carefully: Creating Custom Fusion 360 Commands