Message 1 of 6
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
I have a float slider command input in a dialog (see code example below) that I am trying to update based on the user selection in a drop down box. When 'Material2' is selected it crashes the macro at line 34 where it is attempting to reset the valueOne property to a new value based on a list. Any help here would be greatly appreciated. Just run the attached code and change the drop drown to 'Material2' to throw the error.
Thanks,
Eric
#Author-Autodesk Inc.
#Description-Demo command input examples
import adsk.core, adsk.fusion, traceback
_app = None
_ui = None
_rowNumber = 0
matProps = None
# Global set of event handlers to keep them referenced for the duration of the command
_handlers = []
# Event handler that reacts to any changes the user makes to any of the command inputs.
class MyCommandInputChangedHandler(adsk.core.InputChangedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
eventArgs = adsk.core.InputChangedEventArgs.cast(args)
changedInputs = eventArgs.input
inputs = eventArgs.firingEvent.sender.commandInputs
if changedInputs.id == 'dropdown':
for i in range(1,len(matProps)):
#find material
mater_choice = inputs.itemById('dropdown')
if matProps[i][0] == mater_choice.selectedItem.name:
min = float(matProps[i][2])
max = float(matProps[i][3])
break
SS_Slider = inputs.itemById('floatSlider')
SS_Slider.minimumValue = min
SS_Slider.maximumValue = max
SS_Slider.valueOne = min
SS_Slider.valueTwo = max
print('')
except:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
# Event handler that reacts to when the command is destroyed. This terminates the script.
class MyCommandDestroyHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
# When the command is done, terminate the script
# This will release all globals which will remove all event handlers
adsk.terminate()
except:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
# Event handler that reacts when the command definitio is executed which
# results in the command being created and this event being fired.
class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
global matProps
try:
# Get the command that was created.
cmd = adsk.core.Command.cast(args.command)
# Connect to the command destroyed event.
onDestroy = MyCommandDestroyHandler()
cmd.destroy.add(onDestroy)
_handlers.append(onDestroy)
# Connect to the input changed event.
onInputChanged = MyCommandInputChangedHandler()
cmd.inputChanged.add(onInputChanged)
_handlers.append(onInputChanged)
# Get the CommandInputs collection associated with the command.
inputs = cmd.commandInputs
#material property list used to change slider min/maxs
matProps = [['Material1', '12', '1.05', '2'],['Material2', '12', '3', '5']]
dropdownInput = inputs.addDropDownCommandInput('dropdown', 'Drop Down', adsk.core.DropDownStyles.TextListDropDownStyle)
dropdownItems = dropdownInput.listItems
dropdownItems.add(matProps[0][0], True, '')
dropdownItems.add(matProps[1][0], False, '')
# Create float slider input with two sliders.
min = float(matProps[0][2])
max = float(matProps[0][3])
SS_Slider = inputs.addFloatSliderCommandInput('floatSlider', 'Float Slider', '', min , max , True)
SS_Slider.spinStep = 0.10
except:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def run(context):
try:
global _app, _ui
_app = adsk.core.Application.get()
_ui = _app.userInterface
# Get the existing command definition or create it if it doesn't already exist.
cmdDef = _ui.commandDefinitions.itemById('cmdInputsSample')
if not cmdDef:
cmdDef = _ui.commandDefinitions.addButtonDefinition('cmdInputsSample', 'Command Inputs Sample', 'Sample to demonstrate various command inputs.')
# Connect to the command created event.
onCommandCreated = MyCommandCreatedHandler()
cmdDef.commandCreated.add(onCommandCreated)
_handlers.append(onCommandCreated)
# Execute the command definition.
cmdDef.execute()
# Prevent this module from being terminated when the script returns, because we are waiting for event handlers to fire.
adsk.autoTerminate(False)
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Solved! Go to Solution.