Chad Vernon
  • Home
  • Reel/Resume
  • Work
  • Resources
  • About
  • Contact
sideBar

Search

Categories

  • CG
  • cvxporter
  • Maya
    • API
    • Plug-ins
  • Personal

Archives

  • September 2011
  • June 2011
  • March 2011
  • December 2010
  • November 2010
  • September 2010
  • August 2010
  • July 2010
  • May 2010
  • April 2010
  • March 2010
  • December 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007

Rss

  • Main Entries RSS
  • Comments RSS
Home » Resources » Python Scripting for Maya Artists » Introduction to Python

Introduction to Python

What is Python?

Python is a general purpose scripting language used in many different industries. It is a relatively easy to use and easy to learn language. Python is used in internet services, hardware testing, game development, animation production, interface development, database programming, and many other domains.

The Python Interpreter

How does the computer run a Python program? How can you get the computer to understand the Python commands you write in a text file? The Python Interpreter is the software package that takes your code and translates it into a form that the computer can understand in order to execute the commands. This translated form is called byte code.

The Python Interpreter can be downloaded and installed for free from the Python website (http://www.python.org/download/). Linux, Unix, and Mac platforms usually ship with a Python Interpreter already installed. Windows users will need to download and install the interpreter if they want to use Python outside of programs such as the ones listed above. Maya 8.5 and later has a Python Interpreter built in so you could learn to use Python inside the script editor of Maya.

There are different versions of the Python Interpreter. At the time of this writing, the latest version is 3.0. Maya 8.5 uses Python 2.4. Maya 2008 uses Python 2.5. Linux and Mac users could have older versions shipped with their machines so they’ll need to upgrade if they want access to newer Python functionality.
Python is available in two download types: installer and source code. The installer (an .msi file or .dmg file) is what most people will download to get Python on their machine. Source code is used by Linux users and super dorks. Python is a scripting language that was created with another programming language and the source code contains the files necessary to build and install it on Linux machines. Ignore the source download and just get the installer for now.

What is a Python Script?

A Python script is simply a bunch of commands written in a file with a .py extension. This text file is also called a Python module. This .py file can be written in any text editor like Note Pad, Word Pad, vi, emacs, Scite, etc. However, you do not always have to write your code in a .py file. For quick tests and scripts, you can run code directly from the Maya script editor or from the interactive prompt.

The Interactive Prompt

Interactive prompts allow you to execute code on the fly and get an immediate result. This is a good way to experiment with chunks of code when you are just learning Python. When you download and install Python, you will see that Python ships with its own editor and interactive prompt called IDLE. IDLE is a convenient editor and prompt with syntax highlighting and is all you really need in order to learn Python when just starting out. The following 3 figures show different ways of accessing an interactive prompt.

IDLE

Figure 1 - IDLE

Figure 2 - Script Editor

Figure 2 - Script Editor

Figure 3 - Command Prompt

Figure 3 - Command Prompt

Running Python Scripts From a Command Prompt

Most of the code you write will be in external .py files like the one shown below.

Figure 4 - A Simple Python Script

Figure 4 - A Simple Python Script

To run this script from a command prompt or terminal window, you call the script with the python command (which is on your system once you install Python).

D:\>C:\Python26\python myFirstPythonScript.py
You are running a python script.
8
You can add text together.

If you have the PATH environment variable (Google search “path environment variable”) set to include Python, you don’t need to specify the path to the Python executable.

D:\>python myFirstPythonScript.py
You are running a python script.
8
You can add text together.

You can also route the output of your script to a file to save it for later use:

D:\>python myFirstPythonScript.py > output.txt

Another way to run a Python script is to open it in IDLE and run it from within the editor as shown below. This is a great way to quickly experiment with Python code as you learn and write new scripts and applications.

Figure 5 - Running a Script from IDLE

Figure 5 - Running a Script from IDLE

Running Scripts in Maya and within a Python Session

When we are in Maya, we cannot call a script with “python myScript.py”. The python command starts up the Python interpreter and runs the given script with the interpreter. When we are in Maya, the Python interpreter is already running. To call a script from inside Maya and other scripts, you need to import it.

Figure 6 - Calling a Script from Maya

Figure 6 - Calling a Script from Maya

When you import a Python module, you leave off the .py extension. How does Python know where to find the script? Python uses an environment variable called PYTHONPATH to find scripts. The PYTHONPATH variable contains all the directories Python should search in order to find an imported module. By default, inside Maya your scripts directories are added to the PYTHONPATH. An easy way to add directories to your Maya scripts directories is to create a maya.env file. The maya.env file is a file that modifies your Maya environment each time you open Maya. Place the maya.env file in your “My Documents\maya” folder.

Figure 7 - maya.env File

Figure 7 - maya.env File

Consult the Maya documentation for all the other variables you can set in the maya.env file. If you have multiple scripts with the same name in different directories, Python will use the first one it finds.
Notice when I import the module again, the script does not run:

Figure 8 - Re-importing a Module

Figure 8 - Re-importing a Module

Importing a module only works once per Python session. This is because when you import a module, Python searches for the file, compiles it to byte code, then runs the code, which is an expensive process to execute multiple times. Once a module is imported, it is stored in memory. To run a script again or if you’ve updated a script and wish to have access to the updates, you need to reload it:

Figure 9 - Reloading a Module

Figure 9 - Reloading a Module

Notice that when I reload the Python module, the result states it read the module from a .pyc file. A .pyc file is a compiled Python file. When you import a Python module, Python compiles the code and generates a .pyc file. You could distribute these .pyc files if you do not want people looking at your code. Import statements will work with .pyc files.

Python Modules

As you can see, Python modules are simply Python scripts that contain specific functionality. Since Python is so widely used, you can find thousands of free Python modules on the internet that implement various tasks such as networking, image manipulation, file handling, scientific computing, etc. To interface with Maya, you import the maya.cmds module, which is the module that ships with Maya to implement all the Maya commands.

2 Responses to “Introduction to Python”

Subscribes to this topic Comment RSS or TrackBack URL

Hey Chad,

I’m a complete novice and find this introduction pure gold in my faltering steps towards python fluency.

I really like the script above but am struggling to understand how to define which directory it will work on.

Should I be extending the script, importing sys and using argv to pass in a path argument when I run the program? Thanks in advance
Hazy

Hazy on June 13th, 2011 at 4:45 am

Sorry Chad,
I meant to post the above comment on the sample script page.
Hazy

Hazy on June 13th, 2011 at 4:48 am

Leave A Reply

Allowed tag : <blockquote>, <p>, <code>, <em>, <small>, <ul>, <li>, <ol>, <a href=>..

 Username

 Email Address

 Website

Sticky: Always double check your comment before posting Please Note: Comment Moderation Maybe Active So There Is No Need To Resubmit Your Comments
Home » Resources » Python Scripting for Maya Artists » Introduction to Python

© 2011 Chad Vernon