listItems refusing to reset selectedItem when cleared

listItems refusing to reset selectedItem when cleared

perryK67ZX
Contributor Contributor
242 Views
1 Reply
Message 1 of 2

listItems refusing to reset selectedItem when cleared

perryK67ZX
Contributor
Contributor

I have several inputs that use listItems (namely a Dropdown and a ButtonRow) in my plugin that need to update values depending on other inputs. Unfortunately, when I try to reset them, I can never get it to deselect the currently selected item. I've tried calling deleteMe(), setting isSelected=False, calling clear() on the parent listItems, and so forth, but the listItem.selectedItem is still there, and still valid too. Am I doing something wrong? Is this a known bug? What am I missing here?

0 Likes
243 Views
1 Reply
Reply (1)
Message 2 of 2

kandennti
Mentor
Mentor

Hi @perryK67ZX -San.

 

This has been my experience as well.

It's not a pretty solution, but when I set up ListItems, I first assigned an item that means unselected and made it selected.

# Fusion360API Python script

import traceback
import adsk
import adsk.core as core
import random

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

CMD_INFO = {
    'id': 'kantoku_test',
    'name': 'test',
    'tooltip': 'test'
}

_dropIpt: core.DropDownCommandInput = None
_buttonIpt: core.BoolValueCommandInput = None

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

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

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

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

            global _buttonIpt
            _buttonIpt = inputs.addBoolValueInput(
                "_buttonIptId",
                "Change Items",
                False,
            )

            global _dropIpt
            _dropIpt = inputs.addDropDownCommandInput(
                "_dropIptId",
                "Select Item",
                core.DropDownStyles.TextListDropDownStyle,
            )
            reset_items(_dropIpt)

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


class MyInputChangedHandler(core.InputChangedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args: core.InputChangedEventArgs):
        global _buttonIpt
        if args.input != _buttonIpt: return

        global _dropIpt
        reset_items(_dropIpt)


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

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


def reset_items(
        dropIpt: core.DropDownCommandInput
) -> None:
    listItems: core.ListItems = dropIpt.listItems
    listItems.clear()

    listItems.add("Plase Select Item", True)

    numbers = [random.randint(1, 100) for _ in range(5)]
    for num in numbers:
        listItems.add(str(num), False)


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

        cmdDef: core.CommandDefinition = _ui.commandDefinitions.itemById(
            CMD_INFO['id']
        )

        if not cmdDef:
            cmdDef = _ui.commandDefinitions.addButtonDefinition(
                CMD_INFO['id'],
                CMD_INFO['name'],
                CMD_INFO['tooltip']
            )

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


When I click on the "Change Items" button, the drop-down list changes and the "Plase Select Item" becomes selected.

1.png

0 Likes