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 5

Object-Oriented Programming in Maya

Author

Seth Gibson

Project

Develop a basic class to explore OOP, create an LOD window with pymel

Example Files

human.py
lodwindow.py

Synopsis

This chapter shows one of the key advantages of Python over MEL by introducing object-oriented programming. The chapter briefly explains what objects are and what the constituent parts of Python classes are. It explores the distinctions between methods and data attributes, as well as class and instance attributes. We also discuss the difference between the @staticmethod and @classmethod decorators. Readers will walk through basic examples to learn how to define a class from scratch and how to inherit from custom classes. Readers will also be introduced to the pymel module as a practical implementation of OOP in Maya. In addition to describing where more information on PyMEL can be found, PyMEL’s underlying mechanics will be explained in order to compare and contrast the pymel module with the cmds module. The chapter concludes with an example tool created using PyMEL.

Resources

PyMEL
PyMEL Installation Instructions
A Guide to Python’s Magic Methods

Other Notes

On p. 151 the section on instantiation says that “each instance is a separate immutable object.” Strictly speaking (and according to the definition we use in Chapter 2), instances are immutable by default, as their value is their identity (and hence they are hashable). If you use a colloquial definition of mutability, however, instances can be mutated, since their attributes can be altered.

Errata for the First Edition

On p. 149 the section distinguishing OOP and procedural programming incorrectly states that “the term object refers to a unique occurrence of a class, an instance.” In fact, everything in Python—including a class itself—is an object of some kind. A class describes a type of a thing, while an instance is an occurrence of a thing of that type.
On pp. 161-162, the pseudocode example for static methods and class methods should include the def keyword before each method name in order to properly execute.


8 Responses to “Chapter 5”

  1. on page 154, to emphasis shouldn’t “this function” should be written as “current method”, in the second line ?

  2. Hello Seth,

    on page 155, why are you setting Human.bmi = bmi ?are you setting it as attribute of a class ? following step 6 here is what I have
    class Human(object):
    def __init__(self,*args,**kwargs):
    self.first_name=kwargs.setdefault('first')
    self.last_name=kwargs.setdefault('last')
    self.height = kwargs.setdefault('height')
    self.weight = kwargs.setdefault('weight')
    def bmi(self):
    return self.weight/float(self.height)**2

    Human.bmi = bmi

    but this gives me error that bmi doesnt exist which is obvious since its in the Human class, to my understanding Human.bmi = bmi will work if we also have bmi function at global level outside Human Class..
    executing step 7 i do get answer if i comment Human.bmi = bmi, what is the role of reassigning an attribute Human.bmi = bmi after bmi(self) methid ?

  3. the code indentation doesnt showed up properly so I pasted at http://pastebin.com/5WuseMda

  4. I’m at chapter 5 page 154 and this is the script I’m trying to run:

    import sys;
    class NewClass():
    # exitst in class definition
    data_attribute1 = 1;
    def_init_(self):
    # added to instance upon instantiaton
    self.data_attribute2 = 2;
    print(NewClass.data_attribute2);
    except AttributeError:
    print(sys.exc_info()[0]);
    instance = NewClass();
    print(instance.data_attribute1);
    print(instance.data_attribute2);

    But I only get # Error: SyntaxError: invalid syntax # when i do so, and this happens almost all the time. Except that I can’t find anything wrong on this one.

  5. Hi,
    on page 168 “Installing PyMEL”

    the 2 links you gave http://www.luma-pictures.com/tools/pymel/docs/1.0/index.html
    appears to be 404 error now?

    Could you please update the address of the documentation on install PyMel?

    Thanks a lot,
    Sty

Leave a Reply to styzhu Cancel 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.