Start-up scripts

On start-up, Autograph executes Python scripts to setup the application. You can add more scripts to be executed in the Python scripts directory, which is located

  • On Windows

    C:\Users\<user>\Documents\LeftAngle\Autograph\Python

  • On Linux and macOS

    ~/LeftAngle/Autograph/Python

Any script in this directory (not recursively) will be executed. In Addition, foreach sub-folder that contains a init.py file, the script will be executed.

These initialization scripts are executed very early in the application, before the main project is created. This is useful to register classes (e.g: Autograph.Application.registerPanelClass()) and other types. At this stage, since no Project is yet created, you can only register callbacks to be executed later, such as Autograph.Application.onProjectCreated() .

Command-line scripts

Autograph can also execute Python scripts provided from the command-line with the --exe_script <script> switch (which can be provided multiple times). The scripts provided on the command-line are executed after creating (or loading if a project file was supplied on the command-line) the main project. This is useful to tweak the project before starting a render.

Example

init.py

import os, Autograph
from PySide2.QtQuick import *
from PySide2.QtQml import *
from PySide2.QtCore import Property, Signal, QObject

# Callback invoked when a project is created
def onProjectCreated(project):
    print("Python: Project created!")

Autograph.app.onProjectCreated(onProjectCreated)

# A custom menu action in the menubar
def myMenuAction():
    print("Python: Custom menu action")

Autograph.app.registerMenuItem(myMenuAction, "Python/My Action", "", "Ctrl+Shift+L")

print("Python: starting %s/init.py script. Autograph is background ? %s" % (Autograph.app.getPythonScriptsDirPath(), "Yes" if Autograph.app.isBackground() else "No"))

# A custom QObject to hold properties that we need to interact with QML
# Unfortunately, you cannot (yet) inherit both from QObject and Autograph.CustomPanel
# so we encapsulate every Property/Signal in a separate class add a member to the custom panel class
# See https://qmlbook.github.io/ch20-python/python.html for basics of how to use Qt for Python
class MyPanelQObject(QObject):
    def __init__(self):
        super().__init__()
        self._value=5.4
    def getValue(self):
        return self._value
    def setValue(self, v):
        if (self._value != v):
            self._value = v
            self.valueChanged.emit(self._value)
    valueChanged = Signal(float)
    value = Property(float, getValue, setValue, notify=valueChanged)


# Custom Autograph Panel class
class MyPanel(Autograph.CustomPanel):
    def __init__(self):
        super().__init__()
        self._obj = MyPanelQObject()

    # Must be implemented to create the main QQuickItem
    def createMainItem(self):
        # Create a QQmlComponent from the QML file
        comp = QQmlComponent(Autograph.app.getQmlEngine(), Autograph.app.getPythonScriptsDirPath() + '/MyPanel.qml')
        # Create an instance of the compoonent, passing it the QObject as property
        return comp.createWithInitialProperties({"panel": self._obj})

    # Returns a dict with the serialization to be saved in the workspace
    def saveInWorkspace(self):
        return {'value':self._obj.getValue()}

    # Load what was saved in saveInWorkspace()
    def loadFromWorkspace(self, container):
        try:
            self._obj.setValue(container["value"])
        except KeyError as e:
            pass

# Factory function for MyPanel
def makeMyPanel():
    return MyPanel()

# Register the panel class to Autograph
Autograph.app.registerPanelClass(makeMyPanel, "com.leftangle.python.MyPanel", "My Panel", "", True)