Intro to Python: Part 1

What is Python?

Python is a powerful object-oriented language, that has a lot of the versatility of more complex languages but the simplicity of authoring that simpler languages provide. For this reason, it’s pretty much the standard language for any professional art-based pipeline; DCC packages such as Maya, Photoshop, Houdini and Blender all support Python.

You can easily extend the functionality of your tool by downloading additional packages or modules. There is pretty much a module available for everything you want to do; if anything there’s occasionally too much choice!

Example packages:

  • win32com – lets you create and edit many applications, including Excel and Word documents
  • PIL – image processing library allowing you to do practically anything with an image
  • moviepy – read, write and edit video clips
  • django – web development framework

How Do I Get Started?

There are tons of tutorials online, but here is a quickstart guide to get you to running your first Python script.

Installing an IDE

The first step is to install an Integrated Development Environment (IDE). An IDE is a piece of software that you install, that lets you type the script and compile (run) it in the same place. There are a few to choose from, but the standard in every place I’ve worked has been Wing. They have a free version that lets you start learning.

Go to http://wingware.com/downloads/wingide-101 and download and install the free (non-commercial!) version.

First Script : Listing Files in a Directory
  1. Start a new document in Wing
  2. The first step in nearly all Python scripts is to “import” the package you want. As we’re listing the contents of a directory, we’ll import the “os” module
  3. Now we’ve called the os module, we can call it’s function “listdir”, giving our root directory as an argument
  4. When writing a string path, and using backslashes, you must use the r character to tell Python NOT to evaluate the slashes as special characters (for example, \n \t are common):
  5. This will print out the file name of every file in the directory
  6. To get the full file path, you need to add the original directory to the beginning (obviously…). The best way to join directory paths together is to use the “join” junction of the “path” class in the “os” module, e.g. “os.path.join”. To make things easier, we’ll turn our initial directory path into a string, because we need to use it twice but don’t want to write it every time:

Further Reading

ActiveState Code Python Recipes

Next: Intro to Python – Part 2