very new to API and Add ins, Im trying to add drop down menu items in an add in, that depending on what is selected, would change a user parameter. I have the menu working and the "else" writing to parameters. But I cant get the IF statement to work, mostly because I have no idea what i'm doing.
dropDownCommandInput_ = commandInputs_.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) if dropDownItems_ == 'ALREC00229 0.5in_1in': userParams.itemByName('stockX').expression = '.5' userParams.itemByName('stockY').expression = '1' else: userParams.itemByName('stockX').expression = '5' userParams.itemByName('stockY').expression = '5'
Solved! Go to Solution.
Solved by JeromeBriot. Go to Solution.
Hello,
Try this :
if dropDownCommandInput_.selectedItem.name == 'ALREC00229 0.5in_1in':
Jérôme Briot, Freelance engineer - Mechanical design and prototyping
3D Print Plus / Pro | IDF Import | GitHub To Fusion 360 | Tube Bending Data Exchanger | Slice Data Export
Memory Used | Basic Calculator | Check Computer Specifications | Import spline from any CSV file
perfect! that works!, but now is there a way to make that selection false so another one can be true?
dropDownItems_.add(_('Select'), True) dropDownItems_.add(_('ALREC00229 0.5in_1in'), False) dropDownItems_.add(_('ALREC00174 1in_2.5in'), False) dropDownItems_.add(_('ALREC00290 1in_3in'), False) if dropDownCommandInput_.selectedItem.name == 'Select': userParams.itemByName('stockZ').expression = '1' userParams.itemByName('stockX').expression = '1' if dropDownCommandInput_.selectedItem.name == 'ALREC00229 0.5in_1in': userParams.itemByName('stockZ').expression = '.5' userParams.itemByName('stockX').expression = '1'
what I meant to say was... That seem to work great for the first one, but doesn't seem to work for any other selection
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
Jérôme Briot, Freelance engineer - Mechanical design and prototyping
3D Print Plus / Pro | IDF Import | GitHub To Fusion 360 | Tube Bending Data Exchanger | Slice Data Export
Memory Used | Basic Calculator | Check Computer Specifications | Import spline from any CSV file
Can't find what you're looking for? Ask the community or share your knowledge.