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

Search

Categories

  • CG
  • cvxporter
  • Maya
  • Personal

Archives

  • July 2010
  • May 2010
  • April 2010
  • March 2010
  • December 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007

Rss

  • Main Entries RSS
  • Comments RSS

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

© 2010 Chad Vernon