Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

How can I run code when a body is selected outside of a command?

perryK67ZX
Contributor

How can I run code when a body is selected outside of a command?

perryK67ZX
Contributor
Contributor

I'd like to create a persistent display to show some numbers derived from the currently selected body's mass. How can I determine when something new is selected?

My approach thus far has been to try to add a handler to the select command, but I'm having trouble figuring out how to do this. This is my current attempt, added to my add-in's startup code:

    def hook_into_select_command(args: adsk.core.ApplicationCommandEventArgs):
        futil.log("command starting.")
        if args.commandId != "SelectCommand":
            futil.log(f"was {args.commandId}")
            return

        futil.log("Select command starting. Attempting to do weird stuff.")

        def tmp(args: adsk.core.CommandCreatedEventArgs):
            futil.log(f"Event: {args.firingEvent.name} Command: {args.command}")

            futil.add_handler(args.command.execute, do_things_on_select_change)

        futil.add_handler(
            args.commandDefinition.commandCreated, tmp, local_handlers=local_handlers
        )
        futil.log("set handler")

    futil.add_handler(ui.commandStarting, hook_into_select_command)

 

I can get the log messages to print when the select command is in use, but I haven't figured out how to get code to run when something is actually selected. My idea was to add a handler to the select command's execute event, but I can't figure out how to get access to the command itself. The inner `tmp` function I defined on line 9 never fires.

Any advice? Is there a better approach I could be taking?

0 Likes
Reply
Accepted solutions (1)
275 Views
2 Replies
Replies (2)

perryK67ZX
Contributor
Contributor
Accepted solution

Aha, looks like UserInterface.activeSelectionChanged may be what I'm looking for. Will investigate and report back.

In the meantime, are there any other options I should consider? Any pitfalls to be aware of?

EDIT: Yep, that did the trick!

0 Likes

BrianEkins
Mentor
Mentor

I was going to suggest the activeSelectionChanged event. That should be exactly what you were looking for.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
1 Like