Chad Vernon
  • Home
  • Reel/Resume
  • Work
  • Resources
  • About
  • Contact
sideBar

Search

Categories

  • CG
  • cvxporter
  • Maya
    • API
    • Plug-ins
  • Personal

Archives

  • May 2012
  • March 2012
  • September 2011
  • June 2011
  • March 2011
  • December 2010
  • November 2010
  • September 2010
  • August 2010
  • 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
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.

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.

2 Responses to “Modules”

Subscribes to this topic Comment RSS or TrackBack URL

Hi,
I really like this posts, but I can’t figure it out one thing:
When I load a python plugin in Maya I start a python session, right?
Ok, if that’s right, I can’t understand how can I reload one module.
For example, I have my python plugin in pugin folder, but because the code is extremely long and edit that code is a mess, I have splitted the code into some files. Those files are inserted in one folder and in main plugin file I import what I need. But after I start maya, if I edit some files, changes will not propagate in my plugin (even if I reload plugin lot of times). So.. can you help me?

Andrea on August 18th, 2011 at 3:58 am

You use the reload command.
import myModule
reload(myModule)

Chad on August 18th, 2011 at 8:57 am

Leave A Reply

Allowed tag : <blockquote>, <p>, <code>, <em>, <small>, <ul>, <li>, <ol>, <a href=>..

 Username

 Email Address

 Website

Sticky: Always double check your comment before posting Please Note: Comment Moderation Maybe Active So There Is No Need To Resubmit Your Comments
Home » Resources » Python Scripting for Maya Artists » Modules

© 2011 Chad Vernon