Python Tools - Session 1A

Introduction to Python and Maya Scripting

January 2020

Session Navigation

Overview

Welcome to CS115 — Introduction to Scripting and Programming. This first session covers why scripting matters for technical artists, the fundamentals of what a computer program is, and your very first steps writing code in Maya's Script Editor. No prior programming experience is required.

Why Learn Scripting?

  • Make your own tools — automate repetitive tasks and build custom workflows
  • Understand engineering — bridge the gap between art and code to communicate better with programmers
  • Job opportunities — technical artists who can script are in high demand across the industry

What's a Computer Program?

At its core, a computer program is simply a set of instructions. These instructions are made up of statements (commands the computer executes) and data (the information those commands operate on). Every script you write — no matter how complex — is built from these two building blocks.

Interpreted vs Compiled Languages

Programming languages generally fall into two categories:

  • Compiled languages (C, C++) — code is translated into machine code before running. Faster execution, but requires a compilation step.
  • Interpreted languages (Python, MEL) — code is executed line-by-line at runtime. Easier to write and test, but generally slower.

Python is an interpreted language, which makes it ideal for rapid prototyping and tool development.

Scripting in Maya

Maya supports multiple scripting approaches, each with different trade-offs:

  • MEL — Maya's original scripting language
  • Python (maya.cmds) — native Python commands wrapping MEL functionality
  • PyMEL — an object-oriented wrapper around maya.cmds, more Pythonic
  • Maya Python API — direct access to Maya's C++ internals via Python
Speed Comparison

For the same operation, the performance difference is dramatic:

  • Native Python (maya.cmds): 20.9 seconds
  • PyMEL: 16.6 seconds
  • Maya Python API: 0.15 seconds

We'll start with PyMEL for its readability, and explore the API later when performance matters.

Your First Code in the Script Editor

Open Maya's Script Editor and try the following:

a = 1 + 2
print a

This creates a variable a, assigns the result of 1 + 2 to it, then prints the value. You should see 3 in the output.

Strings vs Variables

A key distinction for beginners:

  • 'Hello' — this is a string (text data, wrapped in quotes)
  • Hello — this is a variable name (Python will look for something stored with that name)

Forgetting the quotes is one of the most common beginner errors and will produce a NameError.

Error Messages and Debugging

Errors are a natural part of programming. When you see an error in the Script Editor, read it carefully — Python error messages tell you the line number and type of error. Common early errors include:

  • NameError — you referenced a variable that doesn't exist (typo or missing quotes)
  • SyntaxError — the code structure is wrong (missing colon, mismatched parentheses)
  • IndentationError — Python uses whitespace to define code blocks

Code Standards: PEP8

Python has an official style guide called PEP8. Key conventions we'll follow:

  • Use snake_case for variable and function names: tile_size, image_path
  • Use 4 spaces for indentation (not tabs)
  • Keep lines under 79 characters where practical
  • Use descriptive names — width_count is better than wc

Converting MEL to Python

Many Maya tutorials use MEL. Here's how a common command translates:

# MEL:
# polySphere -r 5 -sx 20 -sy 20 -name "mySphere";

# Python (maya.cmds):
import maya.cmds as cmds
cmds.polySphere(r=5, sx=20, sy=20, name="mySphere")

# PyMEL:
import pymel.core as pm
pm.polySphere(r=5, sx=20, sy=20, name="mySphere")

Notice how MEL uses flags with dashes (-r), while Python uses keyword arguments (r=5). The Maya documentation provides both MEL and Python syntax for every command.

Next: Session 1B →