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 can I run code when a body is selected outside of a command?

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
perryK67ZX
142 Views, 2 Replies

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

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?

2 REPLIES 2
Message 2 of 3
perryK67ZX
in reply to: perryK67ZX

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!

Message 3 of 3
BrianEkins
in reply to: perryK67ZX

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report