Difference between revisions of "Scripting Guide"

From Silhouette Wiki
Jump to navigation Jump to search
Line 46: Line 46:
|<tt>startupComplete</tt>||called after startup initialization finishes
|<tt>startupComplete</tt>||called after startup initialization finishes
|<tt>node_selected</tt>||called when a node is selected
|<tt>project_selected</tt>||called when then active project changes
|<tt>node_selected</tt>||called when the active node changes
|<tt>post_load</tt>||called after a Project is loaded
|<tt>post_load</tt>||called after a Project is loaded

Revision as of 21:43, 2 November 2012


Silhouette has an embedded Python interpreter that can be used to control key bindings and invoke Actions. When Silhouette starts up, it executes the startup.py script, which in turn runs the keybinds.py script. There is a search path when looking for these scripts, so users can modify their scripts without modifying the default scripts.

The Silhouette scripting functionality is implemented in the fx module.

Script Search Path

First, Silhouette looks in $(SFX_SCRIPT_PATH), then $(SFX_USER_PATH)/scripts (these can easily be overridden on a per-user basis). Then Silhouette looks in $(SFX_RESOURCE_PATH)/scripts, where SFX_RESOURCE_PATH is the path to the resources directory on Windows and Linux, and the Resources bundle folder on Macintosh OSX.

Platform Path
Linux /opt/SilhouetteFX/silhouette5/resources
OS X /Applications/SilhouetteFX/Silhouette5/Silhouette.app/Contents/Resources
Windows C:\Program Files\SilhouetteFX\Silhouette5\resources

In an environment where the stock Silhouette install lives on a server and cannot be written to by a normal user, the easiest way to support user-specific scripts is to set SFX_SCRIPT_PATH to some writable location in the user's home directory, such as $HOME/Silhouette/scripts.

Scripting Console

The Silhouette Console can be used to enter interactive scripts. This is useful for running quick code snippets or testing scripting commands. The fx module is imported automatically in the interactive console.

For example, load a Project and then open the Console tab. Press Enter until you see a >> prompt, and type the following command:

print activeProject().label

The current project name will be printed to the console.

User Interface Scripting

Key binds

Most of the UI functions in Silhouette can be controlled by custom keybinds, which are normally defined in keybinds.py. Keys can be configured to perform simple, global operations such as activating a control, or complex operations such as toggling states or cycling through modes each time a key is pressed. Binds can also examine the current node and tool to perform different operations depending on the current state.

The easiest way to get familiar with key binding is to examine the default keybinds file. More information on the bind functions can be found in the Scripting Reference.

Action Scripting

Actions Scripting


Hooks can be used to perform various operations when certain events happen. The available hooks are:

Hook name Description
startupComplete called after startup initialization finishes
project_selected called when then active project changes
node_selected called when the active node changes
post_load called after a Project is loaded
pre_save called just before a Project is saved
post_saved called just after a Project is saved
pre_render called just before rendering begins
post_render called just after rendering finishes
post_save_frame(frame, path, type) called just after a frame is saved during rendering

Hooks are assigned by editing the fx.hooks dictionary, usually in startup.py.

Hook Example: node_selected

# set the view mode to 'Foreground' each time a 'Roto' node is selected
def nodeSelected():
    node = activeNode()
    if node != None and node.type == "RotoNode":

fx.hooks["node_selected"] = nodeSelected

Hook Example: post_load

# parse the project name for a version field, and set an environment
# variable with the version number. Later, the variable can be used
# in the output format specification
def post_load():
    p = fx.activeProject()
    if p:
        name = p.path
        name = os.path.basename(name)
        name = os.path.splitext(name)[0]
        version = name.split('_')[-1]
        if version.startswith('v'):
            os.environ["PROJECT_VERSION"] = version

fx.hooks["post_load"] = post_load

Scripting Reference