You are here: User Guide > Nuke > The Script Editor and Python > Automating Procedures

Automating Procedures

Okay, so you know how to use the Script Editor to type in a sequence of Python statements that take care of a procedure. But so far, you’ve still sat by your computer typing the statements in. It’s time to automate the procedure. All you need to do is save your statements, and when you want to use them again later, import them into the Script Editor.

Saving Statements in a Python Module

To save statements in a Python module:

1.   On the top of the Script Editor, click the Save a script button .
2.   Save the script with the extension .py (for example firstmodule.py) in a directory that is contained in the sys.path variable. (To see these directories, enter print sys.path in the Script Editor. To add a directory to the sys.path variable, enter sys.path.append ("directory") where directory represents the directory you want to add.)

You have now created your first Python module.

Opening a Python Script in the Script Editor

To open a Python script in the Script Editor:

1.   Click the Load a script button . on top of the Script Editor. The Script to open dialog opens.
2.   Navigate to the Python module that contains the script you want to open and click Open.

Nuke opens the script in the input pane of the Script Editor, but does not execute it.

Importing and Executing a Python Script

To import and execute a Python script:

1.   On top of the Script Editor, click the Source a script button . The Script to open dialog opens.
2.   Navigate to the Python module that contains the script you want to import and click Open.

OR

In the input pane, enter:

import module

Where module represents the name of your Python module without the file extension, for example:

import firstmodule

Nuke imports the Python module and performs the procedure defined in the module.

NOTE:  Importing the module is done according to Python’s default rules. During the import, the module is searched in the following locations and order:
1. In the current directory.
2. In the directories contained in the PYTHONPATH environment variable, if this has been defined. (To view these directories, enter echo $PYTHONPATH in a command shell.)
3. In an installation-dependent default directory.
During the search, the variable sys.path is initialized from these directories. Modules are then searched in the directories listed by the sys.path variable. To see these directories, execute the statement print sys.path in the Script Editor.