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: 

Run Python script on "New Component"

2 REPLIES 2
Reply
Message 1 of 3
isocam
218 Views, 2 Replies

Run Python script on "New Component"

Can anybody help?

 

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

 

Many thanks in advance!!!

 

Darren

2 REPLIES 2
Message 2 of 3
kandennti
in reply to: isocam

@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()))

 

Message 3 of 3
PinRudolf
in reply to: isocam

 - woops, Kandennti beat me to it -

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