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\Maxon\Autograph\Python
On macOS
~/Maxon/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.
Loose scripts in the directory are executed first, then the sub-folder init.py scripts. The order within each group is not guaranteed, so a script must not depend on another having run.
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() .
Each script has its own namespace
Every start-up script is executed as its own module, with its own global namespace. Two scripts that define a function or a global with the same name do not interfere with each other.
The module is named after the plugin: a sub-folder’s init.py takes the name of its folder, a loose script the name of its file, placed under an autograph_scripts package and adjusted to be a valid Python identifier.
Script
__name__
my-plugin/init.py
autograph_scripts.my_plugin
helper.py
autograph_scripts.helper
Note that __name__ is therefore not "__main__". Work guarded by if __name__ == "__main__": will not run in a start-up script.
Finding your own folder
A plugin in a sub-folder usually needs that folder in order to load the files next to its init.py — a .qml file, an icon, or its own Python modules. Use Autograph.Application.getScriptDirPath(), which returns the directory of the script currently executing:
import sys, Autograph
# Make the modules next to this init.py importable
sys.path.insert(0, Autograph.app.getScriptDirPath())
Do not confuse it with Autograph.Application.getPythonScriptsDirPath(), which returns the scripts directory itself. That is the right answer only for a loose script sitting directly in it; for a plugin in a sub-folder it points one level too high.
Note
__file__ is also set correctly in a start-up script, so os.path.dirname(os.path.abspath(__file__)) works as well. Autograph.Application.getScriptDirPath() is preferred because it is derived from the running code object, and so stays correct even in a module that rebinds __file__.
Callbacks are kept alive
Registering a callback — Autograph.Application.onProjectCreated(), Autograph.Application.onApplicationClosing() and the others — makes Autograph keep a reference to it, so a callable that nothing else refers to still works:
Autograph.app.onApplicationClosing(lambda: print("Goodbye"))
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 PySide6.QtQuick import *
from PySide6.QtQml import *
from PySide6.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.getScriptDirPath(), "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.
# getScriptDirPath(), not getPythonScriptsDirPath(): the QML file sits next to this
# script, which is the same folder only while this script is loose in the scripts
# directory. Resolving against the script's own directory keeps working once it moves
# into a sub-folder. It works here as well as at module level, because this method is
# defined in this file.
comp = QQmlComponent(Autograph.app.getQmlEngine(), Autograph.app.getScriptDirPath() + '/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)
Note
This example requires MyPanel.qml to be next to init.py
import QtQuick 2.14
import QtQuick.Controls 2.14 as QQC
import QtQuick.Layouts 1.14
import com.leftangle.UICore 1.0 as UICore
QQC.Control {
id: root
required property QtObject panel
ColumnLayout {
anchors.fill: parent
UICore.Text {
text: "My Python Panel"
}
UICore.SpinBox {
value: root.panel.value
onValueChanged: root.panel.value = value
}
}
Component.onCompleted: {
console.log("Completed MyPanel qml, panel = " + root.panel)
}
}