Due to a miscommunication between Elsevier Press and the Python Software Foundation, the first printing of this title used an unauthorized modification of the trademarked Python logo. We apologize to the PSF for this, and they have been understanding of our in-press status; in the 2nd and subsequent printing of this title, we shall use a cover design that has been approved as non-dilutive by the Python Software Foundation.

Help make the world a better place and make a secure donation to the Python Software Foundation today!

 

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.


22 Responses to “Chapter 4”

  1. 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

    • Hi Taebong,
      1. The polyPrimitive command, like many others in Maya, returns its results in a list. Ordinarily, commands like polySphere or polyCube are returning the names of both a shape node and a transform node within the list, which is why the pattern exists. Although the polyPrimitive 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 to polyPrimitive is only an intermediate step to create and get the name of a new object (see point 1).
      Hope this clears things up!

  2. 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

    • 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

      • 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.

  3. 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 🙂

    • 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.

  4. 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.

  5. 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.

  6. Just tried the download versions and they work!??
    Why do mine not work……Hmmmm?.
    Will super dooper scruitinize. hahahaha.

  7. 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”

  8. 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?

  9. 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 ^^

  10. 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.

  11. 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!

  12. 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?

    • 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.

  13. hi
    i have need to tutorial installing pyqt4 for maya

  14. Love the Uncharted reference on pg 117! Great book!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.