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

eBook
$25.99 $28.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
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

Shipping Address

Billing Address

Shipping Methods
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.
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

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 : 9781849694728
Vendor :
Autodesk
Languages :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
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

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Publication date : Jul 25, 2014
Length: 352 pages
Edition : 1st
Language : English
ISBN-13 : 9781849694728
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 $5 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 $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 152.97
Maya Programming with Python Cookbook
$48.99
Unity Character Animation with Mecanim
$54.99
Practical Maya Programming with Python
$48.99
Total $ 152.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

What is the digital copy I get with my Print order? Chevron down icon Chevron up icon

When you buy any Print edition of our Books, you can redeem (for free) the eBook edition of the Print Book you’ve purchased. This gives you instant access to your book when you make an order via PDF, EPUB or our online Reader experience.

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
Modal Close icon
Modal Close icon