Announcements
Autodesk Community will be read-only between April 26 and April 27 as we complete essential maintenance. We will remove this banner once completed. Thanks for your understanding

Run Python script on "New Component"

isocam
Collaborator

Run Python script on "New Component"

isocam
Collaborator
Collaborator

Can anybody help?

 

Is is possible to run a Python script when creating a "New Component" in Fusion 360?

 

Many thanks in advance!!!

 

Darren

0 Likes
Reply
388 Views
2 Replies
Replies (2)

kandennti
Mentor
Mentor

@Hi @isocam .

 

If it is an add-in rather than a script, it is possible to monitor that the command to create the component is executed.

 

Try this by creating a new add-in.

#FusionAPI_python addin
import adsk.core, adsk.fusion, traceback

handlers = []


def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface

        onCommandTerminated = MyCommandTerminatedHandler()
        ui.commandTerminated.add(onCommandTerminated)
        handlers.append(onCommandTerminated)

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

def stop(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        ui.messageBox('Stop addin')

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

# Event handler for the commandTerminated event.
class MyCommandTerminatedHandler(adsk.core.ApplicationCommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        ui = None
        try:
            eventArgs = adsk.core.ApplicationCommandEventArgs.cast(args)
            app = adsk.core.Application.get()
            ui  = app.userInterface

            newCompCmds =[
                'FusionCreateNewComponentCommand',
                'FusionCreateNewCompCmdFromBody']

            if eventArgs.commandId in newCompCmds:
                # Add the process you want to perform
                ui.messageBox('Catch New Component')

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

 

0 Likes

PinRudolf
Advocate
Advocate

 - woops, Kandennti beat me to it -

1 Like