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 7

Basic Tools with Maya Commands

Author

Adam Mechtley

Project

Tool GUI Template, Polygon Primitives tool, Pose Manager tool

Example Files

optwin.py
polywin.py
posemgr.py
posemgr1.ma
posemgr2.ma

Synopsis

This chapter introduces the basics of creating a Maya GUI window. Readers will use the cmds module to create a base window class with a menu and buttons that they can extend for their own tools. We introduce additional GUI commands available in the cmds module to extend this base class and implement radio buttons, input fields, and color pickers in a simple polygon creation tool. Finally, we cover some advanced techniques for tool development including code organization, data serialization with the pickle module, and working with files in a Maya GUI. The final code example is a basic tool that can copy, paste, save, and load poses on characters.

Resources

functools Documentation
pickle Documentation
Reading and Writing Files

Other Notes

None

Errata for the First Edition

In the optwin.py module (AR_OptionsWindow class), the call to tabLayout should have set the childResizable flag. The file available for download reflects this change.


8 Responses to “Chapter 7”

  1. Hello,

    I am trying to complete the exercise on “Using functools Module” but I keep getting an error that reads:

    #Error: keyword cannot be an expression

    Any help here would be appreciated. I know its a small issue but it will bother me until I get it:)

    Thanks for the wonderful book.

    Michael

    • Hi Michael! Can you be a little more specific about precisely where you get the problem, what line, what your code looks like, etc.? I tried the example in the book again and get no problems.

  2. Hi, I actually posted a thread on CG talk with several issues I’m having in chapter 7.
    Thought to post it here just aswell if there could be the slightest chance you’d answer 😉

    http://forums.cgsociety.org/showthread.php?p=7642584#post7642584

    Thanks, and the book is great! Took me a while to get started for real though.

  3. Hi

    I want to start of by congratulating the authors to a fantastic book! I have read it front to back and feel that I master a lot of python programming by now.

    I am currently working on some simulations in Maya for which I need some graphical interfaces. By using your optwin class I was able to extend it to my own class and build a custom window, but now I have the problem that I want to open a second window when the first one is closed. And even though I put an identic call to a new window it only pops open for the fraction of a second before it is closed by it self. Any idea what is causing this?

    I call the window using:
    win = cellWin.AR_CellOptionsWindow.showUI(self)
    and then one more time at another place in my script.

    Further more, I want to start a loop when the window is closed where I basically iterate over the full timescale of the scen like

    for t in range(tmax):
    currentTime(t)
    # do something

    but even though this loop is called after the close button on the window is pressed, the loop starts before the window is closed and remains open until the end of the loop. Any clue why this is happening?

    Very grateful for any advice

    Best Regards
    Emil Ljungberg
    Lund University, Sweden

  4. Hi,

    Can you explain a little more about why we need to use

    mel.eval(`python(“import maya.cmds”);`)

    at the bottom of page 203? I just don’t get it. Why couldn’t we just do

    import maya.cmds

    ?

    thanks!

    • Hi John! As mentioned in the text, passing a string command as a callback executes the statements in the __main__ module. If you want to make sure maya.cmds exists in __main__, it needs to be imported from within __main__. You can either do that by including the import statement in your callback, or by using MEL’s python command to import it in __main__ in one place. If you were just executing this code from the Script Editor then you’d be fine, but the assumption here is that you’re working on this code in a module of your own.

  5. I am trying to follow from Extending GUI Classes, but when I execute AR_PolyOptionsWindow lines i get this:

    # Error: global name ‘cmds’ is not defined
    # Traceback (most recent call last):
    # File “”, line 8, in
    # File “D:\MayaPython\MayWorkspace\HelloPyDev\optwin.py”, line 7, in showUI
    # win.create()
    # File “D:\MayaPython\MayWorkspace\HelloPyDev\optwin.py”, line 24, in create
    # if cmds.window(self.window, exists=True):
    # NameError: global name ‘cmds’ is not defined #

    Why is that?

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.