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!
- 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
- Start a new document in Wing
- 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
- Now we've called the os module, we can call it's function "listdir", giving our root directory as an argument
- 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):
Python12345678910111213import osfor file_name in os.listdir(r'I:\_TEMP'): # Note the "r" character!print file_name"""[evaluate list_dir.py]20170114_012641206_iOS.jpg20170114_012642023_iOS.jpg20170114_012642706_iOS.jpg20170114_012643422_iOS.jpg20170114_012655000_iOS.MOV"""
- This will print out the file name of every file in the directory
- 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:
Python1234567891011121314import osroot_dir = r'I:\_TEMP'for file_name in os.listdir(root_dir): # Note the "r" character!print os.path.join(root_dir, file_name)"""[evaluate evaluate list_dir.py]I:\_TEMP\20170114_012641206_iOS.jpgI:\_TEMP\20170114_012642023_iOS.jpgI:\_TEMP\20170114_012642706_iOS.jpgI:\_TEMP\20170114_012643422_iOS.jpgI:\_TEMP\20170114_012655000_iOS.MOV"""
|Next: Intro to Python - Part 2|