How do I set the initial value and also an updated value for a Python slider control?

How do I set the initial value and also an updated value for a Python slider control?

tim.collins29V9X
Enthusiast Enthusiast
529 Views
3 Replies
Message 1 of 4

How do I set the initial value and also an updated value for a Python slider control?

tim.collins29V9X
Enthusiast
Enthusiast

In my Python add-in I have an integer value that can be between 15 and 32 and the initial value is 22.  I'd like to use a slider but it does not have an initial value.

   def addIntegerSliderCommandInput(self, id: str, name: str, min: int, max: int, hasTwoSliders: bool) -> IntegerSliderCommandInput:
 
A spinner does have the initial value:

    def addIntegerSpinnerCommandInput(self, id: str, name: str, min: int, max: int, spinStep: int, initialValue: int) -> IntegerSpinnerCommandInput:
 
How do I set the initial value for a slider? At present this works fine if and only if an acceptable initial value is 0, which in my case is never true. Very often the initial value will be computed based on some other dependent variables and I will need to update the "initial" value inside the change handler.  Is there some sample code with this in it?
 
 
0 Likes
Accepted solutions (1)
530 Views
3 Replies
Replies (3)
Message 2 of 4

kandennti
Mentor
Mentor
Accepted solution

Hi @tim.collins29V9X .

 

After creating an instance, the valueOne property can be set.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-0f49349e-5ad6-4386-9d78-91da5387e82e 

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core

_app: adsk.core.Application = None
_ui: adsk.core.UserInterface = None
_handlers = []

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

    def notify(self, args: adsk.core.CommandCreatedEventArgs):
        adsk.core.Application.get().log(args.firingEvent.name)
        try:
            global _handlers
            cmd: adsk.core.Command = adsk.core.Command.cast(args.command)
            inputs: adsk.core.CommandInputs = cmd.commandInputs

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

            sliderIpt: adsk.core.IntegerSliderCommandInput = inputs.addIntegerSliderCommandInput(
                'sliderId',
                'test',
                15,
                32,
                False
            )
            sliderIpt.valueOne = 22

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


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

    def notify(self, args: adsk.core.CommandEventArgs):
        adsk.core.Application.get().log(args.firingEvent.name)
        adsk.terminate()


def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui = _app.userInterface

        cmdDef: adsk.core.CommandDefinition = _ui.commandDefinitions.itemById(
            'test_cmd'
        )

        if not cmdDef:
            cmdDef = _ui.commandDefinitions.addButtonDefinition(
                'test_cmd',
                'Test',
                'Test'
            )

        global _handlers
        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()))

 

Message 3 of 4

tim.collins29V9X
Enthusiast
Enthusiast

Thanks. My experience as mainly a C++/Java/C# developer gets me in trouble with Python-style coding! This also applies to change events where it seems you have to test for the type of the event sender to know if .value or .valueOne is of intereset.

0 Likes
Message 4 of 4

tim.collins29V9X
Enthusiast
Enthusiast

To be more specific why is there both "CommandCreatedEventArgs" and also "CommandCreatedEvent"?  Why does the API talk about passing in the "args" object (which is how Python handles variable length arguments) when the event is always a single argument? Why does one ever "cast()" -- ever?  I have about a hundred questions along these lines but I think I need to stop here.   Anyone?

0 Likes