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 to handle hanlders with multiple selection inputs

1 REPLY 1
Reply
Message 1 of 2
sirois.christophe
185 Views, 1 Reply

How to handle hanlders with multiple selection inputs

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()))
1 REPLY 1
Message 2 of 2
nnikbin
in reply to: sirois.christophe

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

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