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: 

Unable to update value or expression of ValueCommandInput object

0 REPLIES 0
Reply
Message 1 of 1
JeromeBriot
231 Views, 0 Replies

Unable to update value or expression of ValueCommandInput object

Hello,

 

I can't modify the value displayed on a ValueCommandInput object.

 

Here is an example where I try to constrain user input to positive numbers only.

 

#Author-
#Description-

import adsk.core, adsk.fusion, traceback

_app = None
_ui  = 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)

            cmdInput = eventArgs.input

            if cmdInput.value < 0.0:

                _ui.messageBox('Value must be greater than 0.0')

                cmdInput.value = 0.0
#                cmdInput.expression = '{:.2f}'.format(0.0)

            adsk.doEvents()

        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):
        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

            inputs.addValueInput('test_scale_factor', 'Scale factor', '', adsk.core.ValueInput.createByReal(1.0))

        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('ScaleTest')
        if not cmdDef:
            cmdDef = _ui.commandDefinitions.addButtonDefinition('ScaleTest', 'Scale Test', '')

        # 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()))

Thank you.

0 REPLIES 0

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

Post to forums