Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

User parameters and sliders are connected, but are always reset

542002010815
Observer

User parameters and sliders are connected, but are always reset

542002010815
Observer
Observer

I would like to link the slider to a user parameter so that the value of the slider can control the user parameter. But every time after modifying or creating a user parameter, the interface goes back to the very beginning, as if it hasn't changed, which it does, just for a moment. You can watch the attached video。

 

import adsk.core, adsk.fusion, traceback

_app = None
_ui = None
_handlers = []
_doc = None
_design = None
slider_value = 0.0
length_param = None

def create_or_update_length_parameter():
    try:
        global length_param
 
        length_param = _design.userParameters.itemByName("Length")
       
        if length_param:
 
            _design.userParameters.itemByName("Length").expression = str(slider_value)  
 
            _ui.messageBox('Length parameter updated to: {} cm'.format(length_param.value))
            if not _doc.isSaved:
 
                    _doc.saveAs("ceshi", "This is the first save description.")
            else:
             
                _doc.save("This is an update description.")

           
        else:
           
            length_value = 10.0  
            lengthValue = adsk.core.ValueInput.createByReal(length_value)
            lengthparam = _design.userParameters.add("Length", lengthValue, "cm", " ")
           
            _ui.messageBox('Length parameter created with value: {} cm'.format(lengthparam.value))

    except Exception as e:
        _ui.messageBox('Failed to create or update Length parameter:\n{}'.format(e))

class MyCommandDestroyHandler(adsk.core.CommandEventHandler😞
    def __init__(self😞
        super().__init__()

    def notify(self, args😞
        try:
            adsk.terminate()
        except:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class MyInputChangedHandler(adsk.core.InputChangedEventHandler😞
    def __init__(self😞
        super().__init__()

    def notify(self, args😞
        try:
           
            if args.input.id == "slider_controller":
                
                global slider_value
                slider_input = adsk.core.FloatSliderCommandInput.cast(args.input)
                slider_value = slider_input.valueOne 
                _ui.messageBox('Slider value changed to: {}'.format(slider_value))
                create_or_update_length_parameter() 
               
                _ui.messageBox('Length parameter value updated to:')

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

class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler😞
    def __init__(self😞
        super().__init__()

    def notify(self, args😞
        try:
            cmd = adsk.core.Command.cast(args.command)

            
            onDestroy = MyCommandDestroyHandler()
            cmd.destroy.add(onDestroy)
            _handlers.append(onDestroy)

            
            inputs = cmd.commandInputs

            
            slider_input = inputs.addFloatSliderCommandInput("slider_controller", "Slider", "cm", 0, 10.0)
            slider_input.valueOne = slider_value 

            
            onInputChanged = MyInputChangedHandler()
            cmd.inputChanged.add(onInputChanged)
            _handlers.append(onInputChanged)

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

def run(context😞
    try:
        global _app, _ui, _design, slider_value, length_param,_doc

        slider_value = 5.0  
       
        _app = adsk.core.Application.get()
        _ui = _app.userInterface
        _design = _app.activeProduct  

        _doc = _app.activeDocument
       
        cmdDef = _ui.commandDefinitions.itemById('sliderCommandSample')
        if not cmdDef:
            cmdDef = _ui.commandDefinitions.addButtonDefinition('sliderCommandSample', 'Slider Command Sample', 'Sample to demonstrate slider input.')

        onCommandCreated = MyCommandCreatedHandler()
        cmdDef.commandCreated.add(onCommandCreated)
        _handlers.append(onCommandCreated)

        cmdDef.execute()

        adsk.autoTerminate(False)

    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
0 Likes
Reply
Accepted solutions (1)
161 Views
2 Replies
Replies (2)

Jorge_Jaramillo
Collaborator
Collaborator
Accepted solution

Hi,

 

InputChanged event is mainly used to update command inputs inside the command being executed, not to make changes to the model.

You might need to use ExecutePreview event instead of InputChanged event in order to keep the changes to the model while the command is being executed. 

This topic from the API documentation explain the use of the ExecutePreview and also take especial consideration of using the isValidResult property from the CommandEventArgs passed in to the event:

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-3922697A-7BF1-4799-9A5B-C8539DF57051#Execute...

 

Regards,

Jorge Jaramillo

 

2 Likes

542002010815
Observer
Observer
Thank You!!
0 Likes