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 First Save

1 REPLY 1
Reply
Message 1 of 2
isocam
141 Views, 1 Reply

Run Python Script On First Save

Can anybody help?

 

I have a Python script that runs every time the user presses the "Save" button.

 

This Python script works extremely well.

 

However,

 

If I create a brand new part and press the "Save" button, the script does not run. It only runs on the second press of the "Save" button.

 

My question is....

 

Is there a way of modifying the Python script file so that it runs on the very first save as well?

 

Many thanks in advance!

 

Darren

1 REPLY 1
Message 2 of 2
kandennti
in reply to: isocam

Hi @isocam .

 

I created an add-in that uses documentSaved Event for testing purposes.

# Fusion360API Python Addin
import adsk.core, adsk.fusion, traceback

_app = None
_ui  = None
_handlers = []

class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            global _app
            _app.get().log(f'FiringEventName : {args.firingEvent.name}')
            
            global _handlers
            cmd = adsk.core.Command.cast(args.command)
            
            onDestroy = MyCommandDestroyHandler()
            cmd.destroy.add(onDestroy)
            _handlers.append(onDestroy)

            onDocumentSaved = MyDocumentSavedHandler()
            _app.documentSaved.add(onDocumentSaved)
            _handlers.append(onDocumentSaved)
        except:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class MyDocumentSavedHandler(adsk.core.DocumentEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        _app.get().log(f'FiringEventName : {args.firingEvent.name}')

class MyCommandDestroyHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        _app.get().log(f'FiringEventName : {args.firingEvent.name}')

def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui = _app.userInterface
        
        cmdDef = _ui.commandDefinitions.itemById('Test')
        if not cmdDef:
            cmdDef = _ui.commandDefinitions.addButtonDefinition('Test', 'Test', 'Test')

        global _handlers
        onCommandCreated = MyCommandCreatedHandler()
        cmdDef.commandCreated.add(onCommandCreated)
        _handlers.append(onCommandCreated)
        
        cmdDef.execute()

        _app.get().log('-- Start addin --')
        adsk.autoTerminate(False)
    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def stop(context):
    try:
        _app.get().log('-- Stop addin --')

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

 

I ran the add-in and saved a new document. I got "DocumentSaved" in the log, so the first save is working correctly.

1.png

 

Without seeing what you have created, I don't think anyone can answer your question.

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