Run Python script on "New Component"

Run Python script on "New Component"

isocam
Collaborator Collaborator
403 Views
2 Replies
Message 1 of 3

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
404 Views
2 Replies
Replies (2)
Message 2 of 3

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
Message 3 of 3

PinRudolf
Advocate
Advocate

 - woops, Kandennti beat me to it -