• Home
  • Reel/Resume
  • Work
  • Downloads
  • Resources
  • About
  • Contact
Home » Resources » Python Scripting for Maya Artists » Modules

Modules

We learned in the beginning that Python modules are simply .py files full of Python code. These modules can be full of functions, variables, classes (more on classes later), and other statements. We also learned that to load a Python module from within Maya or another interactive prompt, we need to import the module. When we import a module, we gain access to all the functionality of that module.

# myMathModule.py:
def add(x, y):
return x + y

def subtract(x, y):
return x – y

Inside Maya, we can access this functionality as follows:

import myMathModule
myMathModule.add(1, 2)

Or

import myMathModule as mm
mm.add(1,2)

Or

from myMathModule import add
add(1, 2)

Or

from myMathModule import *
add(1, 2)
subtract(1, 2)

Remember, we can only import a module once per Python session. If we were to update the code in myMathModule.py, we wouldn’t have access to the updates until we reload the module.

reload(myMathModule)

We can also import modules into other modules. If we have one module full of some really useful functions, we can import that module into other scripts we write (which are also modules) in order to gain access to those functions in our current module.

Module Packages

Packages allow us to organize our Python modules into organized directory structures. Instead of placing all of our modules into one flat directory, we can group our modules based on functionality.

Figure 11 - A Sample Package Organization

Figure 11 - A Sample Package Organization

To create a package, create a folder in one of you PYTHONPATH directories like your Maya script directory, then create a file called __init__.py inside of that new folder. The __init__.py can be empty. You can then place your modules into that package and import them as follows.

>>> import packageName.moduleName

You can have packages inside of packages.

>>> import cvtools.rigging.createIkLeg as createIkLeg
>>> createIkLeg('L_leg_joint')

Any code you put in the __init__.py file of a package gets executed with the package. For example, in the above sample package hierarchy, I could have code in scripts/cvtools/__init__.py to create a Maya menu when the package is imported.

>>> import cvtools     # creates a Maya menu

Built-In and Third Party Modules

Python ships with many built-in modules that give you access to really useful functionality. There is a module with many useful math functions, a module to copy files, a module to generate random numbers, etc.

import sys         # System commands
import os          # Generic module to the operating system
import shutil      # Contains file copying functions
import struct      # Deals with binary data
import xmllib      # XML parser module
import math        # Contains many useful math operations
import random      # Random number module
import re          # Regular expressions module
import optparse    # Command-line option parser

There are also hundreds of third-party modules available online. For example, the pil module contains many functions that deal with image manipulation. To find out what functions are in a module, run the help command or view online documentation.

Home » Resources » Python Scripting for Maya Artists » Modules

© 2009 Chad Vernon