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

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.

7 thoughts on “Modules”

Andrea August 18, 2011 at 3:58 am

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?

Chad August 18, 2011 at 8:57 am

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

josh November 14, 2016 at 3:47 am

Hi Chad
I would like to use the numpy module in maya…but for some reason when I type import numpy in maya python script editor , it says module not found

I have pasted the numpy module in (I do not have access to paste it into the default python folder)
C:/Users/jf/Documents/maya/2016.5/numpy/numpy;

then I edited the maya.env file to
PYTHONPATH = C:/Users/jf/Documents/maya/2016.5/numpy/numpy;

but when I type import numpy …it says module not found
is there something I am missing ? how to get to make numpy work in maya pthon ?

awaiting your reply
Thanks
Josh

Juan December 28, 2016 at 3:31 am

Hi,

I want to apply materials to the object in the scene. However when I do it from the batch file it gives me an error “TypeError: No object matches name: BlueDiamondSG”
It seems like it doesn’t load the library where the shading groups are.
The material to apply is “BlueDiamond” and it’s connected to the shading group “BlueDiamondSG”

Thanks for help

Chad December 29, 2016 at 1:28 pm

Hi Juan, what command are you using to assign materials? You should be using the hyperShade command.

Chad December 29, 2016 at 1:30 pm

Hi Josh, numpy is the package name and should have the __init__.py file in the directory. You’ve add one directory too deep to the PYTHONPATH. It should be PYTHONPATH = C:/Users/jf/Documents/maya/2016.5/numpy. Or you could just copy the numpy directory into your maya/2016.5.scripts directory.

Leave A Reply

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

You may use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

<code> For inline code.
[sourcecode] For multiline code.