Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to use the Input Changed Event Handler correctly

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
joe_palmer
697 Views, 2 Replies

How to use the Input Changed Event Handler correctly

Apologies if this is a very basic question but I've read lots of forum posts and the API manual and still can't figure this out.

 

I'm trying to get my script to refresh/update each time the user makes a different selection from a drop-down list. I understand that I need to use the InputChangedEventHandler for this but I'm really struggling with logic and implementation.

 

Here is a very basic script that I have written to try and get to grips with this functionality. If I can get this example working then I'm sure I can implement this functionality in my actual script.

 

#Test to understand the InputChanged functionality


import adsk.core, adsk.fusion, adsk.cam, traceback, os #importing modules which contain functions that will need to be used later

_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)

# Global set of event handlers to keep them referenced for the duration of the command
_handlers = []


# Event handler for the inputChanged event.
class MyInputChangedHandler(adsk.core.InputChangedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):

        eventArgs = adsk.core.InputChangedEventArgs.cast(args)

        userselecteditem = dropdownInput3.selectedItem.index

        changedInput = eventArgs.input


# Event handler for the execute event.
class MyExecuteHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            eventArgs = adsk.core.InputChangedEventArgs.cast(args)         
        except:
            if _ui:
                _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


# Event handler for the destroy event.
class MyDestroyHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        adsk.terminate()
        

# Event handler for the commandCreated event.
class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:

            eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
            
            
                    
            #--------------------CREATE TABS------------------
            
            inputs = adsk.core.CommandInputs.cast(eventArgs.command.commandInputs)

            # Create a tab input.
            tabCmdInput1 = inputs.addTabCommandInput('tab_1', 'Selection')
            tab1ChildInputs = tabCmdInput1.children
            
            # multiple tabs to be created in the future.


            #--------------------CREATE TABLE WITH USER INSTRUCTIONS------------------       
            table = tab1ChildInputs.addTableCommandInput('instructionstable', 'Inputs', 1, '1') 
            table.minimumVisibleRows = 1
            table.maximumVisibleRows = 3
            table.columnSpacing = 1
            table.rowSpacing = 0
            table.tablePresentationStyle = adsk.core.TablePresentationStyles.itemBorderTablePresentationStyle
            table.hasGrid = False                    
            

            textboxInput = inputs.addTextBoxCommandInput('readonly_textBox', 'Text Box 1', '<div align="center">Make your selection below </div>', 2, True)
            textboxInput.isReadOnly = True
            textboxInput.numRows = 3
            table.addCommandInput(textboxInput, 0, 0, False, False)

            #--------------------------------------


            #--------------------USER MAKES SELECTION------------------
            
                       
            dropdownInput3 = tab1ChildInputs.addDropDownCommandInput('dropdown3', '             What is your selection?          ', adsk.core.DropDownStyles.LabeledIconDropDownStyle); 
            dropdown3Items = dropdownInput3.listItems
            dropdown3Items.add('1. Item 1', True, '')
            dropdown3Items.add('2. Item 2', False, '')
            dropdown3Items.add('3. Item 3', False, '')
            dropdown3Items.add('4. Item 4', False, '')
         

            userselecteditem = dropdownInput3.selectedItem.index

            #--------------------------------------

            #--------------------CREATE TABLE WSHOWING SELECTION------------------       
            table = tab1ChildInputs.addTableCommandInput('selectiontable', 'Inputs', 1, '1') 
            table.minimumVisibleRows = 3
            table.maximumVisibleRows = 3
            table.columnSpacing = 1
            table.rowSpacing = 10
            table.tablePresentationStyle = adsk.core.TablePresentationStyles.itemBorderTablePresentationStyle
            table.hasGrid = False                    
            
                        
            textboxInput = inputs.addTextBoxCommandInput('readonly_textBox', 'Text Box 1', 'your selection is '+str(userselecteditem), 2, True)
            textboxInput.isReadOnly = True
            textboxInput.numRows = 3
            table.addCommandInput(textboxInput, 0, 0, False, False)

            

            #-----------Subscribe to the various command events-------------

            # Connect to command execute.
            onExecute = MyExecuteHandler()
            eventArgs.command.execute.add(onExecute)
            _handlers.append(onExecute)

            # Connect to input changed.
            onInputChanged = MyInputChangedHandler()
            eventArgs.command.inputChanged.add(onInputChanged)
            _handlers.append(onInputChanged)
            
            # Connect to the command terminate.
            onDestroy = MyDestroyHandler()
            eventArgs.command.destroy.add(onDestroy)
            _handlers.append(onDestroy)   
                   
        except:
            if _ui:
                _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
        


def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui  = _app.userInterface
        
        # Create a command.
        cmd = _ui.commandDefinitions.itemById('tableTest')
        if cmd:
            cmd.deleteMe()
            
        cmd = _ui.commandDefinitions.addButtonDefinition('tableTest', 'User selection test', 'Table Test', '')
        
        # Connect to the command create event.
        onCommandCreated = MyCommandCreatedHandler()
        cmd.commandCreated.add(onCommandCreated)
        _handlers.append(onCommandCreated)
        
        # Execute the command.
        cmd.execute()
        
        # Set this so the script continues to run.
        adsk.autoTerminate(False)

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

 

 

The script should just tell the user what selection they have made from the drop-down list. Apologies for tagging members but @ekinsb I'm sure this would be very easy for you to solve.

 

Thanks in advance.

 

Tags (3)
Labels (1)
  • API
2 REPLIES 2
Message 2 of 3
BrianEkins
in reply to: joe_palmer

Here's a modified version of your script.  I fixed some small issues.  I also made the command inputs global variables.  This isn't required because you can get later at any time from the command object but it saves having to get them again and again.  I then use them in the input changed event.

 

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

_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)

# Global set of event handlers to keep them referenced for the duration of the command
_handlers = []
_instructionTable = adsk.core.SelectionCommandInput.cast(None) 
_dropdownInput3 = adsk.core.DropDownCommandInput.cast(None)
_selectionTable = adsk.core.SelectionCommandInput.cast(None)
_textBoxInput1 = adsk.core.TextBoxCommandInput.cast(None)
_textBoxInput2 = adsk.core.TextBoxCommandInput.cast(None)


# Event handler for the inputChanged event.
class MyInputChangedHandler(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 == 'dropdown3':
                _textBoxInput2.text = 'Your selection is ' + _dropdownInput3.selectedItem.name
        except:
            if _ui:
                _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


# Event handler for the execute event.
class MyExecuteHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            eventArgs = adsk.core.InputChangedEventArgs.cast(args)         
        except:
            if _ui:
                _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


# Event handler for the destroy event.
class MyDestroyHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        adsk.terminate()
        

# Event handler for the commandCreated event.
class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:

            eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
                    
            #--------------------CREATE TABS------------------
            
            inputs = adsk.core.CommandInputs.cast(eventArgs.command.commandInputs)

            # Create a tab input.
            tabCmdInput1 = inputs.addTabCommandInput('tab_1', 'Selection')
            tab1ChildInputs = tabCmdInput1.children
            
            # multiple tabs to be created in the future.


            #--------------------CREATE TABLE WITH USER INSTRUCTIONS------------------    
            global _instructionTable   
            _instructionTable = tab1ChildInputs.addTableCommandInput('instructionstable', 'Inputs', 1, '1') 
            _instructionTable.minimumVisibleRows = 1
            _instructionTable.maximumVisibleRows = 3
            _instructionTable.columnSpacing = 1
            _instructionTable.rowSpacing = 0
            _instructionTable.tablePresentationStyle = adsk.core.TablePresentationStyles.itemBorderTablePresentationStyle
            _instructionTable.hasGrid = False                    
            
            global _textboxInput1
            _textboxInput1 = inputs.addTextBoxCommandInput('readonly_textBox', 'Text Box 1', '<div align="center">Make your selection below </div>', 2, True)
            _textboxInput1.isReadOnly = True
            _textboxInput1.numRows = 3
            _instructionTable.addCommandInput(_textboxInput1, 0, 0, False, False)

            #--------------------------------------


            #--------------------USER MAKES SELECTION------------------
            
            global _dropdownInput3     
            _dropdownInput3 = tab1ChildInputs.addDropDownCommandInput('dropdown3', '             What is your selection?          ', adsk.core.DropDownStyles.LabeledIconDropDownStyle)
            _dropdownInput3.listItems.add('Item 1', True, '')
            _dropdownInput3.listItems.add('Item 2', False, '')
            _dropdownInput3.listItems.add('Item 3', False, '')
            _dropdownInput3.listItems.add('Item 4', False, '')

            #--------------------------------------

            #--------------------CREATE TABLE WSHOWING SELECTION------------------   
            global _selectionTable    
            _selectionTable = tab1ChildInputs.addTableCommandInput('selectiontable', 'Inputs', 1, '1') 
            _selectionTable.minimumVisibleRows = 3
            _selectionTable.maximumVisibleRows = 3
            _selectionTable.columnSpacing = 1
            _selectionTable.rowSpacing = 10
            _selectionTable.tablePresentationStyle = adsk.core.TablePresentationStyles.itemBorderTablePresentationStyle
            _selectionTable.hasGrid = False                    
            
            
            global _textBoxInput2
            selectedItem = _dropdownInput3.selectedItem.name
            _textBoxInput2 = inputs.addTextBoxCommandInput('result_textBox', 'Text Box 2', 'Your selection is ' + selectedItem, 2, True)
            _textBoxInput2.isReadOnly = True
            _textBoxInput2.numRows = 3
            _selectionTable.addCommandInput(_textBoxInput2, 0, 0, False, False)

            #-----------Subscribe to the various command events-------------

            # Connect to command execute.
            onExecute = MyExecuteHandler()
            eventArgs.command.execute.add(onExecute)
            _handlers.append(onExecute)

            # Connect to input changed.
            onInputChanged = MyInputChangedHandler()
            eventArgs.command.inputChanged.add(onInputChanged)
            _handlers.append(onInputChanged)
            
            # Connect to the command terminate.
            onDestroy = MyDestroyHandler()
            eventArgs.command.destroy.add(onDestroy)
            _handlers.append(onDestroy)      
        except:
            if _ui:
                _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
        

def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui  = _app.userInterface
        
        # Create a command.
        cmd = _ui.commandDefinitions.itemById('tableTest')
        if cmd:
            cmd.deleteMe()
            
        cmd = _ui.commandDefinitions.addButtonDefinition('tableTest', 'User selection test', 'Table Test', '')
        
        # Connect to the command create event.
        onCommandCreated = MyCommandCreatedHandler()
        cmd.commandCreated.add(onCommandCreated)
        _handlers.append(onCommandCreated)
        
        # Execute the command.
        cmd.execute()
        
        # Set this so the script continues to run.
        adsk.autoTerminate(False)
    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 3
joe_palmer
in reply to: BrianEkins

Thanks so much for this Brian, this has helped me immensely!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report