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

Preface

When Autodesk added support for Python into Maya 8.5, few people understood the implications. It was a decision that has fundamentally changed the way 3D art gets done. Now, years later, we stand on the edge of realizing its promise.

The promise of Python in Maya goes beyond just a familiar language with a great syntax. Any language could have been chosen to bind to Maya; and most would have been more familiar, and with a better syntax than MEL, and easier to use than C++. So, why Python?

The promise goes beyond a powerful language with lots of built-in features. Python is said to have batteries included, but so do other languages, and Autodesk certainly has lots of batteries in Maya that now also exist in Python. So, again, why Python?

The promise goes beyond having a single language for scripting, API use, and plugins. It goes beyond the endless third-party libraries maintained by a large community. It goes beyond having powerful development tools.

The promise of Python in Maya is all of these things and more. You can learn how to use the language by leveraging a wide variety of resources that have nothing to do with Maya. You can easily translate what you know of MEL and the C++ API and use it in Python, but with an improved development velocity and maintainability of code. You can use your favorite standard Python editor and tools. You can learn about the language from a technical and design perspective and apply that to improve your programming in Maya. You can be part of a large, vibrant, diverse community of developers on the forefront of multiple areas of technology.

Join me as we explore topics that will allow you to unleash the power of Maya through Python. Together, we'll learn how Python works both under the hood and over it, how Maya integrates with Python, and how the elegant PyMEL builds on that integration. We will drill down into what makes Python code beautiful and idiomatic, and how we can use these concepts and Python's language features to make our Maya Python code expressive and elegant. We will leverage third-party solutions for networking and user interfaces, to compliment and extend what is included with Maya and Python. We will decouple Python code from Maya dependencies, making our work go smoother and faster

This book is not a reference. It is not a cookbook, and it is not a comprehensive guide to Maya's Python API. It is a book that will teach you how to write better Python code for use inside of Maya. It will unearth interesting ways of using Maya and Python to create amazing things that wouldn't be possible otherwise. While there is plenty of code in this book that I encourage you to copy and adapt, this book is not about providing recipes. It is a book to teach skills and enable.

This is a book which, I hope, helps realize the promise of Python in Maya.

What this book covers

Chapter 1, Introspecting Maya, Python, and PyMEL, explores how Maya and Python work individually and together to create a powerful programming and scripting environment. It covers some of the key technical underpinnings for the rest of the book.

Chapter 2, Writing Composable Code, introduces the practice of writing code that can be reused in many places. Composable code is a fundamental concept for the rest of the skills taught in this book.

Chapter 3, Dealing with Errors, teaches you all about exceptions and errors in Maya and Python. We explore several strategies for handling them effectively.

Chapter 4, Leveraging Context Managers and Decorators in Maya, covers context managers and decorators, which are two powerful features of Python, and how they can be used to simplify your code.

Chapter 5, Building Graphical User Interfaces for Maya, demonstrates the PySide and PyQt frameworks, how to abstract your user interface code from underlying logic, and a strategy of building GUIs to maximize maintainability and productivity.

Chapter 6, Automating Maya from the Outside, shows how Maya can be controlled from another process, explains how request-reply systems work, and builds a fully featured automation system on these principles.

Chapter 7, Taming the Maya API, introduces the Maya Python API and how types and classes work in Python and Maya. It contains a number of examples to demonstrate the API, as well as a Maya command plugin.

Chapter 8, Unleashing the Maya API through Python, covers the creation of a library to easily create Maya plugin nodes, demonstrating how to map the Maya API onto Pythonic idioms using metaprogramming.

Chapter 9, Becoming a Part of the Python Community, goes over the concepts behind open source software, demonstrates how to find and use third-party modules, explains how to create your own open source project, and tours the Python and Maya programming communities.

Appendix, Python Best Practices, explains in detail various Python language features and miscellaneous, but very relevant, topics.

What you need for this book

You will need a copy of Autodesk Maya 2013 for this book. Newer versions are fine, and older versions that use Python 2.6 (2011, 2012) should be acceptable as well. Any operating system capable of running Maya (Windows, OS X, Linux) should work, though you will need to translate things such as file paths to what is appropriate on your system.

I would also suggest having an install of Python 2.6 or 2.7 outside Maya for exploring and running some of the samples that can be run from the interactive interpreter prompt. You can download Python 2.6 or 2.7 from http://www.python.org/download, or it may be installed on your Mac or Linux OS already!

Finally, I strongly suggest installing both a powerful text editor and an Integrated Development Environment (IDE). Python is a real programming language, and you should use the powerful tools available. If you are an experienced Python user already happy with vim, I don't expect to convert you. But if you are a converted MEL scripter playing around in Notepad, it is time to embrace your good fortune!

For a text editor, Sublime Text (http://www.sublimetext.com) is popular, cross-platform, and free to use on an unlimited trial. Notepad++ (http://notepad-plus-plus.org) is excellent if you are on Windows and prefer free and open source. There are dozens of other good text editors, and if the two editors mentioned here do not tickle your fancy, you should keep trying until you find one that does.

Finally, the choice of IDE is usually a contentious topic. For Python, however, I can confidently say PyCharm (http://www.jetbrains.com/pycharm/) by JetBrains is the premiere IDE, and my personal favorite. It has a free and quite powerful Community Edition as well. Other popular options are Wing IDE (http://www.wingware.com) and Eclipse with PyDev (http://pydev.org). Experiment with a few different programs, but whatever you do, move past IDLE, the IDE bundled with Python!

Who this book is for

  • Do you currently use Python with Maya and ask yourself: "Can I do better?"

  • Are you a MEL scripter who has started using Python and want to know what all the fuss is about?

  • Are you a Python programmer who is starting with Maya and believes there must be a better way?

  • Have you been using Python in Maya for a while but work hard to continuously improve?

Some basic experience with Python and Maya is expected. This book does not cover those most primitive topics that are inevitably learned through introductory Maya and Python use.

Even more than experience, this book requires a willingness to learn. Some of the more advanced or unfamiliar topics may feel a bit like learning how to ride a bicycle, but keep peddling and you'll get the hang of things in no time.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "We can include other modules through the use of the import statement."

A block of code is set as follows:

def spam():
    return 'spam!'

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

def more_spam():
    spams = ' '.join([spam()] * 5)
    return spams

Any command line input or output is written as follows:

> mayapy --version
Python 2.6.4

Code meant to be entered by the Python interactive interpreter uses its familiar conventions. Input lines are prefixed with ">>>". Continuations for multiline statements are prefixed with "...". Output from the interpreter has no prefix:

>>> 'This is input'.replace('in', 'out')
'This is output'
>>> if True:
...     print 'Hello!'
Hello

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Clicking on the Next button moves you to the next screen".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

The code for this book is also available on GitHub, at https://github.com/rgalanakis/practicalmayapython. See the Appendix, Python Best Practices, for more information about the GitHub repository.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.

You can also contact the author, Robert Galanakis, at .