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
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.
on page 154, to emphasis shouldn’t “this function” should be written as “current method”, in the second line ?
Sanjeev Kumar said this on November 18, 2012 at 1:11 am |
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 ?
Sanjeev Kumar said this on November 18, 2012 at 3:34 am |
the code indentation doesnt showed up properly so I pasted at http://pastebin.com/5WuseMda
Sanjeev Kumar said this on November 18, 2012 at 3:37 am |
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.
Gaute said this on January 11, 2013 at 12:16 pm |
Hi! You have an “except” block with no corresponding “try” block.
Adam said this on January 11, 2013 at 5:30 pm |
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
styzhu said this on August 9, 2014 at 12:29 am |
pymel installed by following
http://download.autodesk.com/global/docs/maya2014/en_us/PyMel/install.html
and add pymel to PYTHONPATH as environment variable
styzhu said this on August 9, 2014 at 6:18 pm |
Thanks! I have updated the links on this page.
Adam said this on August 9, 2014 at 9:04 pm |