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 do I set the initial value and also an updated value for a Python slider control?

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
tim.collins29V9X
135 Views, 3 Replies

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

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?
 
 
3 REPLIES 3
Message 2 of 4

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

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.

Message 4 of 4

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?

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