Chapter 4
All About Modules
Author
Ryan Trowbridge & Adam Mechtley
Project
Create modules, explore some built-in modules, configure PYTHONPATH, install an external IDE
Example Files
lightplane.py
spike.py
primitives package
Synopsis
While readers will have been using some modules up to this point (most notably maya.cmds), this chapter discusses exactly what modules are, how they work, and how import and reload() differ from the MEL source directive. While using some important built-in modules like math, os, and sys, this chapter also introduces readers to a variety of ways they can configure their Python environment. Finally, this chapter concludes with some brief information about two popular Python IDEs, WingIDE and Eclipse.
Resources
inspect module
Using WingIDE with Maya
Using Eclipse as a Maya IDE
Setting Up Autocompletion in Eclipse Using PyMEL
Other Notes
There are plenty of great IDEs out there that are not covered in the book. My personal favorite is PyCharm, which now also has a free version.
Errata for the First Edition
On p. 131 the section on userSetup scripts incorrectly states that “you may only have one userSetup script and it must be either a Python or a MEL script—you cannot have both a userSetup.mel and a userSetup.py script.” You may in fact have several userSetup.py scripts, and can use both .py and .mel scripts in conjunction with each other under some circumstances.
On p. 140 the section on setting up a systemwide environment variable for OS X advocates doing so in the launchd.conf file. Unfortunately, as of OS X 10.10, this method no longer works. See here for alternative suggestions that will achieve the same result.
in spike module, I have several question about the code of ‘addSpikes(maya.cmds.polyPrimitive(ch=0)[0])’.
1. why does the [0] added?
2. python’s function is the first-class. therefore, can pass a function into another function. but, in ‘addSpikes’ function, maya.cmds.polyPrimitive function don’t be called
taebong said this on April 23, 2012 at 8:20 am |
Hi Taebong,
1. The
polyPrimitive
command, like many others in Maya, returns its results in a list. Ordinarily, commands likepolySphere
orpolyCube
are returning the names of both a shape node and a transform node within the list, which is why the pattern exists. Although thepolyPrimitive
command only returns the name of the transform node, it is still inside of a list, and so must be indexed to pull it out from the result.2. In this case, the
addSpikes
function expects the name of an object to add spikes to, not a function to execute to create an object and then add spikes to it. As such, the call topolyPrimitive
is only an intermediate step to create and get the name of a new object (see point 1).Hope this clears things up!
Adam said this on April 23, 2012 at 10:24 am |
Having bizarre issues with Packages. I simply cannot get the example package to work. I keep getting this error when trying to import primitives:
# Error: IndentationError: file C:/Users/…/Documents/maya/2012-x64/scripts\primitives\__init__.py line 1: unexpected indent #
when all that exists in my __init__.py is:
import create, mesh
Tim said this on June 20, 2012 at 3:00 pm |
I downloaded and tried the available example files from this website. For some reason they worked when the contents of the files are exactly identical as far as I can tell
Tim said this on June 20, 2012 at 3:02 pm |
Never mind, discovered the problem. On the Kindle version the create.py and math.py has the contents beyond the import maya.cmds indented when they shouldn’t be.
Tim said this on June 20, 2012 at 3:34 pm |
Dear Adam and Ryan:
You have no idea how much I like this book! You gays are awesome! The book is so elaborate and understandable that even me with little comprehending about python and even English can easily understand.
But I have some confusion about the first example of the part of Static Methods and Class Methods in chapter 4.I just can not execute it in script editor of Maya2011 without an error of syntax. I hope it was my fault, But I add a keyword “def” before the definition of classmethod and staticmethod ,then it works! And I can go on with it to the rest part.
That’s all. Thank you for writing such a wonderful book to us! Looking for your reply 🙂
Brain said this on August 15, 2012 at 9:58 pm |
Thanks for letting us know! It looks like this problem is actually in Chapter 5, and I have made note of it on its page.
Adam said this on August 16, 2012 at 5:00 pm |
Dont know why when I import the module, my maya crashes, but i noticed it does printed the globals, guess its occuring after for loop, I do not understand where the problem lies, coz i tried it in maya 2010 and it went fine and i got spikes objects.
Sanjeev Kumar said this on November 17, 2012 at 1:24 am |
I typed out the code for the create.py, math.py and the __init__.py and stored them in the location folder. I imported the primitives package which did not produce an error and I used the help(primitives) to check the contents and it said the package contents were there (create and math) but when I tried to implement it with the cyl = primitives.create.cylinder(r=0.25); it says
#Error: AttributeError file line 3: ‘module’ has no attribute ‘create’ #
Why is this? Have I done something wrong. I made sure to use correct indents and syntax, although I noticed the download files are missing the semi colons that are in the book.
Great book though, really enjoying understanding the way it works under the hood.
Malcom Armstrong said this on June 29, 2013 at 9:25 am |
Just tried the download versions and they work!??
Why do mine not work……Hmmmm?.
Will super dooper scruitinize. hahahaha.
Malcom Armstrong said this on June 29, 2013 at 9:36 am |
Hot Damn. Found it. Sneaky little semi colon trying to disguise himself as a bigger character. Coffee, bagel and squinting saves the day. Hurrah. “Victory dance around the chair”
Malcom Armstrong said this on June 29, 2013 at 9:45 am |
On page 115 in chapter 4 Modules, it recommends that we consult section 9.2 of the Python Tutorial online for more info on names, but I cannot find the only tutorial section.
What is the link for the online Python Tutorial that this referring to?
Mike Chinn said this on October 10, 2013 at 1:28 pm |
http://docs.python.org/2/tutorial/classes.html#python-scopes-and-namespaces
Adam said this on October 10, 2013 at 9:34 pm |
The ‘Setting Up Autocompletion in Eclipse Using PyMEL’ link seems to have died, any idea where we can get this info from now? btw book is great, still working throug it ^^
Bobby said this on March 31, 2014 at 1:20 am |
Thanks for the heads up! The link has been fixed.
Adam said this on March 31, 2014 at 9:25 am |
Hi,
Brilliant book, I got about 130 pages in, no hicups so far! Best book Ive read about scripting. I taught my self python and API but this really makes me not just do but also understand the whys and hows.
I have one question, since I must have missed something. in chapter 4 it says> “for more information on packages, consult section 6.4 in the python tutorial online”
// but where is the 6.4 section? I might just have missed it. The packages explanation in the book makes totally sense but if there is more to know lying somewhere Id gladly look it up if anyone can point to me where to find it.
thanks for the great book! Tons of example on how to practise solid scripting.
Kasper said this on June 3, 2014 at 6:29 pm |
Hi Kasper,
Glad to hear the book has been a help! It is referring to this page: https://docs.python.org/2/tutorial/modules.html#packages
Adam said this on June 3, 2014 at 8:59 pm |
Oh I see – I thought it would here on this site I should find the python explanation! Got it now, great with the fast reply!
Cheers and yeah really enjoying it mate!
thanks!
Kasper said this on June 4, 2014 at 12:26 pm |
Hi,
great book, I am going through it while learning other python tutorials and it really gives me a good foundation. I have a question about creating Environment variables in Mac OS X. I can set the path to the file easily enough so it is added to the sys.path but I am having trouble making it a variable. The tutorial I am following uses Windows, which has a method in the system preferences, Mac doesn’t, I have followed the books example by creating a launchd.conf file, but I still cannot call the __init__.py file or the script I am trying to start with in that path, is there another way to set a variable for a custom path?
David Clabaugh said this on March 12, 2015 at 6:31 am |
Thanks for getting in touch, David! If you are using Yosemite or newer, you will have to use a different method. I had blogged about this on my personal site, but forgot to copy the information here. I have made a note in the errata section above, which has a link with a few options.
Adam said this on March 12, 2015 at 11:29 am |
hi
i have need to tutorial installing pyqt4 for maya
brsvt said this on June 26, 2015 at 6:04 pm |
Love the Uncharted reference on pg 117! Great book!
Maura said this on January 1, 2019 at 5:47 pm |