Package autodesk_toxik
[frames] | no frames]

Package autodesk_toxik

Autodesk Composite Python scripting package.

Scripting in Composite

Scripts can be powerful tools for eliminating the need to perform repetitive, tedious, and time-consuming tasks manually, thereby improving the efficiency of a facility. For example you can use scripts to manage projects, import media, perform rendering, etc.

Composite ships with a number of sample scripts for performing common tasks. These sample scripts are located in the resources/scripts/examples subfolder of the Composite installation directory.

Note: Example Python scripts are provided with no guarantee of effectiveness or efficiency.

There are also scripts that Composite itself invokes to perform some of its tasks (archiving, rendering, pre-comp import, etc) in the resources/scripts subfolder of the Composite installation directory. You may find these scripts helpful both as examples and as starting points for your own scripts.

In some cases the script command that Composite launches to accomplish a given task is specified in the project preferences. For example, the "Render Actions" tab in the project preferences contains the "Render Executable" setting that specifies the Python command line executed each time you render a composition in Composite. By default, that command line launches the "render.py" script. You can edit the command line to launch a different script or change the "render.py" script (located in "resources/scripts") to modify the default rendering behavior; like performing post-render tasks for example.

How To Execute a Composite Python Script

To successfully run a Python script that makes use of the Composite Python API (or more specifically, a script that imports Composite Python modules), it is important to run it in the right environment, for the Python interpreter to find the Composite Python modules and libraries, and for the Composite code to execute properly afterwards.

The Composite Python modules are located in the following directory (where <INSTALL_DIR> refers to the path of the Composite installation directory):

On Windows:

 <INSTALL_DIR>/python/lib/site-packages/autodesk_toxik

On Linux:

 <INSTALL_DIR>/lib/python2.6/site-packages/autodesk_toxik

On Mac OS:

 <INSTALL_DIR>/Contents/Frameworks/Python.framework/Versions/Current/lib/python2.6/site-packages/autodesk_toxik

There are two contexts in which a script can be run. First, in the Composite embedded Python interpreter, i.e. within the Composite process ("composite.exe" executable). And secondly, in an external Python interpreter, i.e. within a Python process, typically from a shell ("python.exe" executable).

Setting Up The Environment

Embedded

In the case of the embedded interpreter, the environment is already set up by Composite for a Python script to run properly.

External

Running a Script

Embedded

To run a Python script in the Composite embedded Python interpreter do the following steps:

  1. Locate the script in a Composite "File Browser" view ("Ctrl-I" to bring up a floating browser for example). Note that in order for Composite to recognize a file as a Python script, it must have the ".py" extension.
  2. Right-click on the script.
  3. To run the script with no parameter choose "Run".
  4. To run the script with parameters choose "Run with Parameters" and type in the script arguments as you would in a shell.

External

To run a Composite Python script, it is strongly recommended to use the Python distribution shipped with Composite (deployed under the Composite installation directory). Running a Composite Python script with any other Python distribution might not work properly and is very likely to result in a crash.

On Windows:

 <INSTALL_DIR>/program/python.exe myScript.py

On Linux:

 <INSTALL_DIR>/bin/python myScript.py

On Mac OS:

 <INSTALL_DIR>/Contents/Frameworks/Python.framework/Versions/Current/bin/python myScript.py

Scripted Actions

Custom actions can be implemented as Python scripts that can be invoked from the UI in the same way as native Composite actions.

Action scripts can be located anywhere on the local file system or on the network. Two environment variables (and toxik.ini options) control the loading and searching of action scripts:

Simple Action Script

The simplest action script is a normal Python script with no special API. Such an action script will only appear in the Scripts top toolbar menu and is named after the Python file without its extension. Also, such a scripted action will be available to the user regardless of the current state of the Composite interactive application.

It is important however that the action script does not execute its action on import, otherwise the action will be executed upon registration of the script. Also, it is recommended that the code ran on import be a light as possible. In particular, heavy Python modules on which the script depends should only be imported when they are actually needed.

In practice, the action execution code and module imports should only be run if the name of the interpreter's main module is equal to "__main__" as described in the Python documentation:

Consequently, the basic structure of an action script should look like:

 if __name__ == "__main__":
   import someModules
   excuteAction()

Advanced Action Script

To get control over the context in which a scripted action should be available to the user and where it should appear in the Composite UI, the script must implement a special API that Composite will query upon its registration.

First of all, the script should define a "getActions" global function which takes no parameter and returns a list of arbitrary Python objects each defining an action that the script implements. This allows for a single Python script to define many actions (most likely a set of related actions that may share some base code).

Secondly, each action object returned by "getActions" must support the following interface; that is, define the following methods, some of which are optional.

The following example is an action script that adds a sphere layer to a Reaction tool node and sets its radius to a value entered by the user in a pop-up dialog. Since the isOnNode method returns True and the isApplicable method returns True only when the target node is a Reaction node, the "Add Sphere" action will only show up in the schematic right-click menu if the selected node is a Reaction tool.

Note that the class name "AddSphereAction" is totally arbitrary; the only hard requirements are for the method names and return types to respect the interface expected from an object returned by the getActions global function:

 class AddSphereAction(object):

   def getName(self):
     return "Add Sphere"

   def isOnNode(self):
     return True

   def isApplicable(self):
     from autodesk_toxik import ui, graph
     node = ui.Workspace.getTargetNode()
     return (node is not None and
             isinstance(node, graph.GroupNode) and
             node.getTool().getName() == "Reaction")

   def perform(self):
     from autodesk_toxik import ui, reaction
     values = ui.showSimpleDialog(
       "Sphere", [("TextField","rad","Radius","100")], 150)
     try:
       radius = int(values["rad"])
     except:
       radius = 100
     sLayer, sMat, sGeom = reaction.addLayer(
       ui.Workspace.getTargetNode(), "My Sphere", geomType="Sphere")
     sGeom["Radius"].setValue(radius)

 def getActions():
   return [AddSphereAction()]

For an example of a scripted action that acts on files selected in the Composite browser, take a look the script that implements the Import All Channels action, which imports all channels of an OpenEXR file sequence into multiple import nodes aggregated together by a group node with an output for each channel view: resources/scripts/actions/multiChannelImport.py under the Composite installation directory.

Submodules
  • admin: Administration module.
  • debug: Debugging utility module.
  • graph: Graph module.
  • init: Initialization module.
  • libs
  • media: Media module.
  • pimgr: Plug-in manager module.
  • precomp: Module for precomp import related functionality.
  • precompmodule: Defines a wrapper around a precomp module: the PrecompModule class.
  • reaction: This module provides convenient functions to modify nodes inside a Reaction node.
  • relocationbrowser: Relocation browser module.
  • tools: The tools module is the placeholder for classes that give access to the Composite tools, i.e.
  • txxml: Xml module.
  • ui: User Interface module.
  • utils
    • utils.interp: The interp module contains utility functions related to the Python interpreter itself: starting a script in an existing interpreter, loading a specific file as a module, frame hack utilities, that sort of things.
    • utils.utils: Common utilities.
  • wiretap: Wiretap module.