Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Practical Maya Programming with Python
Practical Maya Programming with Python

Practical Maya Programming with Python: Unleash the power of Python in Maya and unlock your creativity

Arrow left icon
Profile Icon Galanakis
Arrow right icon
Mex$722.99 Mex$803.99
Full star icon Full star icon Full star icon Full star icon Empty star icon 4 (12 Ratings)
eBook Jul 2014 352 pages 1st Edition
eBook
Mex$722.99 Mex$803.99
Paperback
Mex$1004.99
Subscription
Free Trial
Arrow left icon
Profile Icon Galanakis
Arrow right icon
Mex$722.99 Mex$803.99
Full star icon Full star icon Full star icon Full star icon Empty star icon 4 (12 Ratings)
eBook Jul 2014 352 pages 1st Edition
eBook
Mex$722.99 Mex$803.99
Paperback
Mex$1004.99
Subscription
Free Trial
eBook
Mex$722.99 Mex$803.99
Paperback
Mex$1004.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Practical Maya Programming with Python

Chapter 2. Writing Composable Code

In this chapter, we'll explore the concept of composable code. We start by defining the term. We then examine composable and non-composable examples to better understand what composability is, and improve code by refactoring. We will learn about important techniques to maximize composability, such as predicates, list comprehensions, contracts, closures, and docstrings. We put these techniques to work by building a library to convert a hierarchy of transforms into joints. After that, we compose this library into a configurable higher-level tool for creating characters. Finally, we will look at some issues and solutions surrounding PyMEL, composability, and performance.

Defining composability

The idea of composability is to write code as small and well-defined units that can be combined with other units in flexible and predictable ways. Composable code is decoupled and cohesive. That is, the code has few dependencies and is responsible for a single responsibility. Writing composable code is the key to creating maintainable systems and libraries you'll use for years.

Tip

A unit is a function, class, or method. It is generally the smallest piece of independently useful code, and should be smaller than a module or group of classes (system). There is no black and white definition, so prefer smaller and simpler to larger and more complex.

Maya's utility nodes, such as the MultiplyDivide node, are examples of composable units. They are very clear in their inputs, outputs, and descriptions of what they do. The input to a MultiplyDivide node can be a number from any source, such as a Maya transform, another utility node, or a constant. The MultiplyDivide...

Learning to use list comprehensions

Let's look at list comprehensions in depth now that we've used simpler versions several times. It's a powerful-yet-simple syntax that has been in Python since version 2.0. List comprehensions are described very succinctly and with very clear examples in PEP 202 at http://www.python.org/dev/peps/pep-0202/.

Tip

A Python Enhancement Proposal (PEP) is a design document for a Python feature (or similar). Often it can be very long and technical. PEP 202 is a very straightforward PEP that is well worth reading, consisting of three paragraphs and one example block. Some other notable PEPs will be mentioned in this book and in the appendices.

Oh, and PEP 1 describes PEPs and the PEP process, of course.

A list comprehension has the following form:

[<map> for <variable> in <selection> | if <predicate>]

The map is the item that ends up in the list. It can be the same as variable if no transformation is to take place. It's commonly...

Writing a skeleton converter library

Let's put composability to work for us. We'll implement a true rite of passage for programming in Maya—a routine to automatically convert a hierarchy of nodes into a hierarchy of joints (a skeleton). This task allows us to focus on building small, composable pieces of code, and string them together so that the character creator tool we build later in the chapter can be very simple. These composable pieces won't just serve our character creator. They'll serve us all throughout the book and our coding lives.

Writing the docstring and pseudocode

Before we start coding, we need a place to put the code. Create the skeletonutils.py file inside the development root you chose in Chapter 1, Introspecting Maya, Python, and PyMEL. This book's examples use C:\mayapybook\pylib.

After creating skeletonutils.py, open it in your IDE. Add the following code. The code defines a function, a docstring that explains precisely what it will do...

Writing a character creator

So far, we've written the code to convert a hierarchy of transforms into joints. Now we must write something that we can hook up to a menu and a workflow. An overly simplified solution is to convert the current selection by assigning the following expression into a shelf button:

map(skeletonutils.convert_to_skeleton, pmc.selection())

But that's a pretty bad high-level function. What about error handling, user feedback, and the high-level decisions that we put off while writing the converter?

What we really want is something like the following:

charcreator.convert_hierarchies_main()

This can be hooked up to a menu button, which provides a better experience for the user and a place to make those decisions that we kept out of the skeletonutils module.

Stubbing out the character creator

Create a charcreator.py file next to the skeletonutils.py file in your development root, and open it up in your favorite IDE. Go ahead and type the following code, which will stub...

Improving the performance of PyMEL

I hope at this point, PyMEL's superiority over maya.cmds has been thoroughly demonstrated. If you have any doubt, feel free to rewrite the examples in this chapter to use maya.cmds and see how much cleaner the PyMEL versions are.

Having said that, PyMEL can be slower than maya.cmds. The performance difference can usually be made up by applying one of the following three improvements.

Defining performance

For most scripts, the performance difference is too miniscule to matter. Fast and slow are relative terms. The important metric is fast enough. If your code is fast enough, performance improvements won't really matter. Most of the tools you will write fall under this category. For example, making a pose mirroring tool go 500% faster doesn't matter if it only takes a tenth of a second in the first place.

Refactoring for performance

In most cases where PyMEL code actually needs to be sped up, rewriting small parts can make huge gains. Is your code...

Defining composability


The idea of composability is to write code as small and well-defined units that can be combined with other units in flexible and predictable ways. Composable code is decoupled and cohesive. That is, the code has few dependencies and is responsible for a single responsibility. Writing composable code is the key to creating maintainable systems and libraries you'll use for years.

Tip

A unit is a function, class, or method. It is generally the smallest piece of independently useful code, and should be smaller than a module or group of classes (system). There is no black and white definition, so prefer smaller and simpler to larger and more complex.

Maya's utility nodes, such as the MultiplyDivide node, are examples of composable units. They are very clear in their inputs, outputs, and descriptions of what they do. The input to a MultiplyDivide node can be a number from any source, such as a Maya transform, another utility node, or a constant. The MultiplyDivide node does...

Left arrow icon Right arrow icon

Description

Practical Maya Programming with Python is a practical tutorial packed with plenty of examples and sample projects which guides you through building reusable, independent modules and handling unexpected errors. If you are a developer looking to build a powerful system using Python and Maya's capabilities, then this book is for you. Practical Maya Programming with Python is perfect for intermediate users with basic experience in Python and Maya who want to better their knowledge and skills.

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jul 25, 2014
Length: 352 pages
Edition : 1st
Language : English
ISBN-13 : 9781849694735
Vendor :
Autodesk
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Jul 25, 2014
Length: 352 pages
Edition : 1st
Language : English
ISBN-13 : 9781849694735
Vendor :
Autodesk
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just Mex$85 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just Mex$85 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total Mex$ 3,138.97
Maya Programming with Python Cookbook
Mex$1004.99
Unity Character Animation with Mecanim
Mex$1128.99
Practical Maya Programming with Python
Mex$1004.99
Total Mex$ 3,138.97 Stars icon

Table of Contents

11 Chapters
1. Introspecting Maya, Python, and PyMEL Chevron down icon Chevron up icon
2. Writing Composable Code Chevron down icon Chevron up icon
3. Dealing with Errors Chevron down icon Chevron up icon
4. Leveraging Context Managers and Decorators in Maya Chevron down icon Chevron up icon
5. Building Graphical User Interfaces for Maya Chevron down icon Chevron up icon
6. Automating Maya from the Outside Chevron down icon Chevron up icon
7. Taming the Maya API Chevron down icon Chevron up icon
8. Unleashing the Maya API through Python Chevron down icon Chevron up icon
9. Becoming a Part of the Python Community Chevron down icon Chevron up icon
A. Python Best Practices Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
(12 Ratings)
5 star 50%
4 star 25%
3 star 8.3%
2 star 8.3%
1 star 8.3%
Filter icon Filter
Top Reviews

Filter reviews by




Amazon Customer Oct 09, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
An extremely well thought out and informative book, a must have for any tech artist or anyone who wants to improve their Maya workflow. This is definitely one of the best resources I've come across for scripting in any 3d package
Amazon Verified review Amazon
Carleton Bray May 09, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I can't really say anything that hasn't already been said by the other glowing reviews, but they are indeed well-deserved. The depth and breadth of topics covered is impressive, if not a bit daunting at first. I've only read through it to get an idea of the content covered, and now I find myself following along and learning a ton. PyMEL is without a doubt the way that Python should have been implemented into Maya to begin with. Python is indeed my favorite (and only) language that I "program" with (I'm originally an animator, trying to transition to becoming a tech-artist). You will NOT find a better book on programming specifically inside of Maya with Python (Maya Programming for Games and Film is good, but is more geared towards beginners). Even if you're not a Maya user, a LARGE portion of the concepts in the book can theoretically be software-agnostic. I'm finding it very difficult to be nit-picky about this book.I eagerly await a second edition, where Mr. Galanakis goes into even deeper topics. If you're trying to level up in your technical skills, accept no substitute.
Amazon Verified review Amazon
SuJo Sep 15, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Practical Maya Programming with PythonPublisher Link: https://www.packtpub.com/hardware-and-creative/practical-maya-programming-pythonThis book was amazing, I'm by no means a Python expert and I'm just getting started, and this book exceeded my expectations. Right away I was put into creating a development area and getting familiar with the interpreter, and the author wasted little time with getting everything setup and going. PyMEL was new to me but this book made it approachable and easy to understand. I'm a huge fan of writing reusable code, it saves time, makes life easy, and once you gain enough experience it will show in your coding techniques. I'm very pleased the author spoke on refactoring code, again it's something that comes with experience and time. Learning to look over your code and going "Ah, I can totally do it better this time around." is a really awesome experience.From performance to error handling this book kept me reading and wanting to do more, not only did I feel the examples were great, they gave me an excellent starting point and provided me with a solid foundation with Maya and PyMEL. The chapter on error handling is full of so much great information you'll have to read it a few times, unless you're a super computer capable of remembering everything. Overall this book is highly recommended from beginner to expert.
Amazon Verified review Amazon
VytautasVincevicius Nov 23, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
good book
Amazon Verified review Amazon
Ronnie L. Ashlock Jul 12, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I'm fairly early into my journey with this book and I can already say it is worth every penny. There's a lot here still way above my head, but the information is presented in a very clear and logical format, and more importantly, the book is transformative. It's a deep dive, but Rob keeps the waters clear. Some knowledge of Maya and Python is expected, so there isn't a lot of hand-holding, but that's not really a problem. It's a better use of the book to get right down to business - and you can easily find supplementary introductions to both Maya and Python online or here on Amazon.Overall, this book is highly recommended. I like the Rob's writing style and his knowledge and experience are invaluable. Required reading for anyone even thinking about learning PyMEL.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.

Modal Close icon
Modal Close icon