Book Image

Practical Maya Programming with Python

By : Robert Galanakis
Book Image

Practical Maya Programming with Python

By: Robert Galanakis

Overview of this book

Table of Contents (17 chapters)
Practical Maya Programming with Python
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Inverting the subroutine


The idea of taking code that is similar between two blocks and factoring it out into a common function (also known as a subroutine) is well known. We've done it several places in this book. For example, the following block of code sets up objects for left and right feet, which are just mirrored. Nearly all of the code, except for the name of the sphere and its x translation, is duplicated.

>>> leftfoot = pmc.polySphere(name='left_foot')[0]
>>> leftfoot.translate.set(5, 0, 0)
>>> # ...other code that changes left_foot
>>> rightfoot = pmc.polySphere(name='right_foot')[0]
>>> rightfoot.translate.set(-5, 0, 0)
>>> # ...same code, but for right_foot

We can refactor the duplicated code into the makefoot function.

>>> def makefoot(prefix, x=1):
...     foot = pmc.polySphere(name=prefix + '_foot')[0]
...     foot.translate.set(5 * x, 0, 0)
...     # ...other code that changes foot
...     return foot
>>&gt...