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 in subdirectories based on functionality.

Figure 11 - A Sample Package Organization

To create a package, create a folder in one of you PYTHONPATH directories like your Maya script directory or the scripts directory of your Maya module (not to be confused with a Python module), 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 mymayatools.rigging.ikleg as ikleg
ikleg.create('L_leg_joint')

Any code you put in the __init__.py file of a package gets executed with the package. However, it is usually good practice not to execute any complex code when the user imports your module or package.

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. Some commonly used useful modules include:

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 json        # Tools to deal with JSON data
import xmllib      # XML parser module
import math        # Contains many useful math operations
import random      # Random number module
import re          # Regular expressions module
import argparse    # Command-line option parser
import logging     # Customizable logging functionality
import webbrowser  # Open a web browser to a certain page

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.

comments powered by Disqus