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: 

Adding an input to a command dynamically

4 REPLIES 4
Reply
Message 1 of 5
nraynaud
478 Views, 4 Replies

Adding an input to a command dynamically

Hi all, 

 

I am trying to make a command with an add/remove pair of button whose effects would make new inputs appear in the command panel.

I work on a Mac.

I can't get the new control to appear in the panel, here is the code:

 

 

from adsk.core import *
import traceback

SETTINGS_COMMAND_KEY = 'TestCmd'

keepHandlers = []
deleters = []


def handle(handler, clazz):
    class Handler(clazz):
        def notify(self, args):
            try:
                handler(args)
            except:
                Application.get().userInterface.messageBox('Failed:\n{}'.format(traceback.format_exc()))

    handler_instance = Handler()
    keepHandlers.append(handler_instance)
    return handler_instance


def delete_control(controls, command_key):
    control = controls.itemById(command_key)
    if control:
        control.deleteMe()


def replace_existing_control(controls, new_command):
    delete_control(controls, new_command.id)
    if new_command:
        control = controls.addCommand(new_command)
        return lambda: control.deleteMe()
    return lambda: None


def create_setting_panel(args):
    app = Application.get()
    ui = app.userInterface
    cmd = Command.cast(args.command)

    inputs = cmd.commandInputs

    def on_input_change(args):
        ui.messageBox('click')
        new_input = inputs.addBoolValueInput('sqf', 'Add sqf', False, '', True)
        new_input.isVisible = True

    inputs.addBoolValueInput('addExport', 'Add Export', False, '', True)
    cmd.inputChanged.add(handle(on_input_change, InputChangedEventHandler))


def run(context):
    app = Application.get()
    ui = app.userInterface
    try:
        cmd_defs = ui.commandDefinitions
        settings_command = ui.commandDefinitions.itemById(SETTINGS_COMMAND_KEY)
        if settings_command:
            settings_command.deleteMe()
        settings_command = cmd_defs.addButtonDefinition(SETTINGS_COMMAND_KEY, 'Test', 'Test', '')
        settings_command.commandCreated.add(handle(create_setting_panel, CommandCreatedEventHandler))
        deleters.append(
            replace_existing_control(ui.allToolbarPanels.itemById('SolidMakePanel').controls, settings_command))
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def stop(context):
    ui = None
    try:
        app = Application.get()
        ui = app.userInterface
        global keepHandlers
        keepHandlers = []
        for obj in deleters:
            obj()
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

I see the messagebox appearing with the "click", but the new button doesn't appear in the box. 

 

Is there a time window to add inputs to a command?

The UI example with the TableComandInput is a bit confusing and, I suspect, buggy (some stray controls appear under the table when I run it, and they don't disappear when I remove the row).

I can't use directly use a table system anyways since they don't accept SelectionInputs.

 

Thanks all for your help.

 

Nicolas.

4 REPLIES 4
Message 2 of 5
marshaltu
in reply to: nraynaud

Hello,

 

Unfortunately we don't support to create an input dynamically. The best practice would be to add all inputs in command created event handler.

 

Thanks,

Marshal



Marshal Tu
Fusion 360 Developer
Autodesk, Inc.

Message 3 of 5
ross.korsky
in reply to: nraynaud

If you are unable to create new inputs you could fake it by pre-creating them and hiding/showing as needed - assuming you don't need a huge number of them.

Message 4 of 5
ross.korsky
in reply to: ross.korsky

Actually, inputs can be created dynamically - see the Command Inputs API Sample

 

Frankly this sample makes a mess as the added inputs get put in the table AND on the main form - smells like a bug - but none the less, it's code that's adding inputs dynamically.

Message 5 of 5
ross.korsky
in reply to: ross.korsky

If I modify the sample code to NOT insert the controls into the table it appears that they are not dynamically added to the form (but they are), you can trick it to regenerate by toggling the viability of any single input.

 

valueInput.isVisible = False

valueInput.isVisible = True

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