How to handle hanlders with multiple selection inputs

How to handle hanlders with multiple selection inputs

Anonymous
Not applicable
364 Views
1 Reply
Message 1 of 2

How to handle hanlders with multiple selection inputs

Anonymous
Not applicable

I am trying to write a script with 2 selection inputs, but I am unable to figure out how to add the selections to my 2 different list with my handler. Is there a way to know which selection triggered the call to the handler?

import adsk.core, adsk.fusion, adsk.cam, traceback

ui = adsk.core.UserInterface.cast(None)
handlers = []
sourceBodies = []
targetBodies = []


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

    def notify(self, args):
        try:
            cmd = args.command
            cmd.isExecutedWhenPreEmpted = False
            inputs = cmd.commandInputs

            sourceInput = inputs.addSelectionInput(
                "SourceBodySelection", "Source", "Please select a source body"
            )
            sourceInput.addSelectionFilter(adsk.core.SelectionCommandInput.Bodies)
            sourceInput.setSelectionLimits(1)

            targetInput = inputs.addSelectionInput(
                "TargetBodySelection", "Target", "Please select a source body"
            )
            targetInput.addSelectionFilter(adsk.core.SelectionCommandInput.Bodies)
            targetInput.setSelectionLimits(1)

            onSelect = MySelectHandler()
            cmd.select.add(onSelect)
            handlers.append(onSelect)

            onUnSelect = MyUnSelectHandler()
            cmd.unselect.add(onUnSelect)
            handlers.append(onUnSelect)

            onExec = MyExecuteHandler()
            cmd.execute.add(onExec)
            handlers.append(onExec)
        except:
            if ui:
                ui.messageBox("Failed:\n{}".format(traceback.format_exc()))


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

    def notify(self, args):
        try:
            pass
        except:
            if ui:
                ui.messageBox("Failed:\n{}".format(traceback.format_exc()))


class MySelectHandler(adsk.core.SelectionEventHandler):
    def __init__(self):
        super().__init__()

    def notify(self, args):
        try:
            ## How can I know which list to add my selection?
            pass
        except:
            if ui:
                ui.messageBox("Failed:\n{}".format(traceback.format_exc()))


class MyUnSelectHandler(adsk.core.SelectionEventHandler):
    def __init__(self):
        super().__init__()

    def notify(self, args):
        try:
            pass
        except:
            if ui:
                ui.messageBox("Failed:\n{}".format(traceback.format_exc()))
0 Likes
365 Views
1 Reply
Reply (1)
Message 2 of 2

nnikbin
Collaborator
Collaborator

I was thinking args.activeInput.id could be used but it seems args.activeInput returns None. Perhaps it is a bug.

 

Instead of using SelectionEventHandler you can use InputChangedEventHandler and use InputChangedEventArgs object passed to its notify method. Then using InputChangedEventArgs.input.id you can distinguish between different SelectionCommandInput(s).

0 Likes