The Python Node can be used to modify the data flowing through the tree via a Python script. Users can write scripts to perform functions such as creating or modifying cameras, trackers etc.
A script editor is available, along with a window showing output from the script and error messages.
We provide several example scripts which can be used as a starting point for customisation in the Python Node API documentation.
Any Python scripts placed in the user's
/Users/USERNAME/Documents/The Pixel Farm/PFTrack/nodes
directory will be picked up the application on startup. For each
Python script present, a new item in the Python node group is added to create such a node.
An empty Python Script node can be placed in the tree and the script written (or loaded from disk) and tested in-situ.
Python scripts can be set to run automatically whenever data is needed to pass down-stream. If this behaviour is required, make sure the Auto-Run option is switched on. When this option is disabled, the node stores the results when the script is ran manually, and will re-use that result until it is ran again.
The Python script node can only have a single input and a single output.
Python Script Structure
The API is provided by the
pfpy module, which must be imported by
the Python script as the first statement. Additional modules are
provided for Camera, Tracker, Lens (distortion), Mesh, Clip and Group (for
rigid object groups), Cloud and Dense (for both sparse and dense pointc clouds). Note that a module only needs to be imported if access to its API functions is required.
import pfpy from pfpy import Camera, Tracker, Lens, Mesh, Clip, Group, Cloud, Dense
An arbitrary, but unique, name for the Python Script node must be defined using:
def pfNodeName(): return 'name'
An entry of this name will appear in the application's Python node group to create an instance of this node type (or in the Python Scripts... submenu when using the Tree View's context menu to create nodes).
If you do not wish the user to be able to directly create an instance of this node you can "hide" it from appearing in the interface using:
def pfHiddenNode(): return 1
Note that hiding a node only prevents the user creating an instance of it - the node can still be created by, for example, a Compound Node that uses it.
The auto-run parameter can be defined using:
def pfAutoRun(): return True
Finally, the Python script for performing the actual manipulation of the data flowing through the tree work is defined in a "main" function thus:
def main(): # Python code goes here
The documents below contain API references for each module:
The Python Script node stores a set of user-defined key-value pair attributes. These attributes are typically used to pass information between the node and (for example) macros or compound nodes.
For example, the Python Script node stores in an attribute the number of selected user-features which makes that information available to a compound node which could alter the state of its GUI accordingly. The following functions access those attributes:
string pfpy.getAttribute(string key) -
returns the specified attribute.
void pfpy.setAttribute(string key, string value) - sets the specified attribute.
int pfpy.hasAttribute(string key) - returns
1 if the specified attribute exists, 0 otherwise.
void pfpy.clearAttribute(string key) -
removes the specified attribute.
void pfpy.clearAttributes() - removes all
The image coordinate system is measured from (0,0) at the bottom-left pixel of the image to (w-1, h-1) at the top-right pixel, where w and h are the width and height of the image.
Python scripts that require input from the user can display a popup window to request a piece of text or browse for a filename. To do this, simply call input() as follows:
def main(): filename= input("Enter filename: ") print("user entered filename ", filename)
Pressing the Enter key will close the popup window and return the text entered by the user.
Default Keyboard Shortcuts
Keyboard shortcuts can be customised in the Preferences.