This book is a guide to using the Python scripting language to automate tasks in, and build tools for, Maya. The first two chapters provide an overview of Python scripting in Maya and the basics of UI creation. From there, chapters three through seven each cover a different problem domain, in roughly the same order that they are encountered in real-world projects. We start with modeling, moving through texturing, rigging, animation, and rendering. The last three chapters cover topics needed to flesh out scripts into full tool pipelines, including file input and output and communicating with the world outside of Maya via the Web. In the last chapter, we'll cover a few more advanced topics, such as script nodes and script jobs.
Each chapter provides several examples of how to achieve specific tasks in that domain, will full code listing and explanation. Each example stands on its own, and the reader should feel free to move around within the book as needed.
Chapter 1, Getting Started with Maya, covers the basics of using Maya's script editor and getting set up to write scripts of your own. It also covers the differences between Maya Embedded Language (MEL) and Python. The basic parts of Maya's built-in commands are covered, along with the different modes (create, query, and edit) in which they can be invoked.
Chapter 2, Creating User Interfaces, introduces the reader to creating user interfaces for their scripts. It covers creating windows, adding layouts, and filling them with controls. Nested layouts and custom menus are also explained.
Chapter 3, Working with Geometry, covers using Python to work with geometric data. It starts with how to retrieve information about models, both polygonal and NURBS. It also covers creating new curves and new faces, as well as manipulating existing data to deform a model.
Chapter 4, Giving Things a Coat of Paint – UVs and Materials, deals with getting models ready for rendering, both working with UV data as well as creating and applying shading networks.
Chapter 5, Adding Controls – Scripting for Rigging, covers topics related to using scripting for rigging, including how to create bones and edit their properties. It also covers how to create set-driven key relationships and how to set up inverse kinematics (IK).
Chapter 6, Making Things Move – Scripting for Animation, deals with querying and setting keyframe data to create and modify animation. It also covers copying keyframes from one object to another and using code to create custom expressions.
Chapter 7, Scripting for Rendering, covers topics related to actually generating frames. The examples show how to create lights and cameras, and how to render images. It also covers using the Python Imaging Library (PIL) to combine images after they've been rendered.
Chapter 8, Working with File Input/Output, covers the topics needed to build larger toolchains by importing and exporting custom data. Reading and writing both text-based and binary formats is covered.
Chapter 9, Communicating with the Web, covers how to retrieve information from the Web for use in Maya. The parsing of both XML and JSON data is covered, as well as sending POST data to a website.
Chapter 10, Advanced Topics, covers several more advanced topics, including script jobs and script nodes. It also covers how to create a custom context to make scripts that work more like Maya's built-in tools.
To get the most out of this book, you'll need a copy of Maya and a programmer-friendly text editor. There are a great number of text editors out there, and many people have strong opinions on which they prefer. At a minimum, you'll want an editor that saves in plain text and offers the option to display line numbers. You'll also likely want an editor that offers syntax highlighting for Python.
All of the code for this book was written using Sublime Text (https://www.sublimetext.com/), an excellent and low-cost text editor that is great for Python, as well as many other tasks. You don't have to use that though; any text editor that allows you to edit plain text will work just fine.
Almost all of the examples in the book rely on nothing more than Python and Maya, but there is one example that uses PIL. To install PIL, you'll likely want to use PIP, a package manager that makes installing Python packages easy. You can get that at https://pip.pypa.io/en/stable/.
This book is for anyone that wants to use Python to get more out of Maya. It's expected that you have a decent familiarity with Maya's interface and toolset. Knowledge of Python or other programming languages is helpful, but not required.
In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).
To give clear instructions on how to complete a recipe, we use these sections as follows:
This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.
This section usually consists of a detailed explanation of what happened in the previous section.
This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We can include other contexts through the use of the include
directive."
A block of code is set as follows:
import maya.cmds as cmds print("Imported the script!") def makeObject(): cmds.polyCube() print("Made a cube!")
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Open the script editor by going to Windows | General Editors | Script Editor."
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>
, and mention the book's title in 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 at www.packtpub.com/authors.
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.
You can download the example code files for this book 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.
You can download the code files by following these steps:
Log in or register to our website using your e-mail address and password.
Hover the mouse pointer on the SUPPORT tab at the top.
Click on Code Downloads & Errata.
Enter the name of the book in the Search box.
Select the book for which you're looking to download the code files.
Choose from the drop-down menu where you purchased this book from.
Click on Code Download.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR / 7-Zip for Windows
Zipeg / iZip / UnRarX for Mac
7-Zip / PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Maya-Programming-with-Python-Cookbook. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from http://www.packtpub.com/sites/default/files/downloads/MayaProgrammingwithPythonCookbook_ColorImages.pdf.
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 could 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 to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted 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 <[email protected]>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at <[email protected]>
, and we will do our best to address the problem.