Adding an input to a command dynamically

Adding an input to a command dynamically

nraynaud
Enthusiast Enthusiast
1,574 Views
4 Replies
Message 1 of 5

Adding an input to a command dynamically

nraynaud
Enthusiast
Enthusiast

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.

0 Likes
1,575 Views
4 Replies
Replies (4)
Message 2 of 5

marshaltu
Autodesk
Autodesk

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 Developer
>
0 Likes
Message 3 of 5

Anonymous
Not applicable

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.

0 Likes
Message 4 of 5

Anonymous
Not applicable

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.

0 Likes
Message 5 of 5

Anonymous
Not applicable

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

0 Likes