Object-Oriented Programming in Maya
Develop a basic class to explore OOP, create an LOD window with pymel
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.
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.