Pymel

This aims to serve as a quick reference guide to common tasks and workflows when using PyMEL

PyMEL technical documentation


The Basics

We'll start by quickly outlining some key concepts of programming
Throughout, we'll be conforming to "PEP8" coding standards, which is the preferred style for python

Read more about PEP8 standards

Variables

As we write our code and scripts, we will need to move data around and manipulate it. We do this by storing data in variables. Variables can be "assigned" and "edited".

Variables are all written using "snake case". This means when you write them, they should:

  • be all lower case
  • have each word separated by an underscore

They should also be appropriately named to make your code easy to read. Avoid simplistic variables like "x" or "y" unless they make sense (like iterating through texture pixels)

Integer

A counter of objects would use an "integer" - this is a whole number (e.g 1,2,3,4 etc)

Float

A "float" is any number that contains a decimal point (it's short for "floating point")

Strings and string slicing

String variables are basically any value that contains letters, such as words, node names, file paths, URLs etc

Lists

Lists (or arrays) are a collection of other variables. You can have a list of integers, a list of strings or, in python, you can mix and match (although it can get messy so tread carefully)

We can query the list in a few ways

FOR loops

IF Statements

An IF statement only executes the code block if the condition meets the criteria given

IF statements use logical operators to determine criteria

Lets look at this is action

Functions

A function is a small collection of code that is

Functions take parameters

A comment defining what the function does is called a docstring and has a particular syntax

When written like this, the docstring can be queried in code:

Methods

A method is syntactically very similar to a function (in that it can take arguments, return a value etc) but it must be called on an object

Here we make a simple method called "create" in our "Car" class

And we call the method by first instantiating an instance of the class

Parameters vs Arguments

Most people tend to use the terms parameter and argument interchangeably. Which is almost OK because in conversation it's known what you meant, but technically that's incorrect.

A parameter is the value defined in your function or method. Here, "valueA" and "valueB" are parameters:

However, the arguments are the data that is passed in to the function when it's called. Here, the arguments are "r'C:\renders'" and "0.05":

We can think of these as almost being opposites of each other, like a throw and catch. The argument is "thrown" to the function and "caught" in the parameter"

It's unlikely you will ever get called out on it (like if you mixed up using "imply" and "infer"), but it's worth pointing out that they ARE different.

Classes

Classes form the foundation of Object Oriented Programming (OOP). They bucket together related methods and variables (also called "fields" in the context of a class).

You create an Instance of a class, and each instance can have unique properties.

As an example, think about constructing a "Car" class. This would need:
Fields/Properties

  • Make
  • Model
  • Color
  • FuelLevel
    Methods
  • Start()
  • Stop()
  • Refuel

String Formatting

We can manipulate the printing of strings to make the output more readable

Generators

Importing Other Packages

The real power of Python comes into play when importing other packages. There are thousands (according to the Python Package Index, current count is 173,474 python packages available), but some example are:

Built-in packages (these ship with Python as standard):

  • os : this lets you query and manipulate file paths and data on disk
  • random : random number module
  • shutil : shell utilities, letting you manipulate the file system in a nicer way than native os can
  • xml : xml parsing and writing
  • re : regular expressions, letting you run string based pattern matching
  • subprocess : lets you run operations through the command shell

Available for install (using managers such as "pip"):

  • PIL : Python Image Library, a fullt fledged image manipulation package (other options are Imagemagick and OpenImageIO)
  • moviepy : wraps the core functionality of ffmpeg and adds some cool features, letting you read, manipulate and writing video files
  • pygame : make fun games using Python
  • pywin32 : access to the win32 API, letting you read and manpulate items such as Word docs and Excel spreadsheets
  • beautifulsoup : web-scraping package, letting you get data from web pages with minimal effort
  • numpy : a collection of high level mathematical operations
  • tensorflow : machine learning package that has fast become the standard for learning how ML works

Trawling Directories Using os.walk

The "walk" function in the os.package allows us to recursively propagate directories and get the paths of all files found


Using PyMEL

Importing and Testing PyMEL

Creating Maya nodes

Using the "Quick Help" in the Maya Script Editor

When using the Maya Script Editor, you can double click on any blue highlighted word, then right click and go to "Quick Help".
This will open a shortcut panel on the right that will list all the keyword arguments for than command, and the data type

This lets you see how you control the creation of Maya nodes. For example:

Moving Objects


Useful Functions and Samples

Simple Browser UI Using PyMEL and Contexts

This is an example of simple UI written in PyMEL, that uses the "with" contexts to make the code structure more readable
In this simple but functional UI, we can browse to a directory on disk, and then run actions using this path

Example usage:

Scan Assets in a Directory

This will recursively get every obj and fbx file in a given directory tree, load them and get some basic data, which is printed out to a CSV file.

This csv file can then be loaded in Excel

Example Usage:

Find Materials Used on Selected Meshes

Example Usage:

Find Meshes Using Material

Example Usage:

Create a Material Using a Supplied Color Image

Arnold Render Wrapper

Example Usage:

Print Number of Vertices Split by Normal

This is useful for estimating the cost of a mesh when exporting to a game engine

Example Usage:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.