Practical Maya Programming with Python

Book description

Unleash the power of Python in Maya and unlock your creativity

In Detail

Autodesk Maya is a 3D computer graphics software. It offers a vast and flexible set of features utilizing Python. It is not difficult to get started using Python in Autodesk Maya, but it can be difficult to go from writing procedural, MEL-inspired tools to building the powerful yet simple systems that Python promises.

Practical Maya Programming with Python will help you master both Python and Maya. With thorough explanations, illustrative examples, and complete sample projects, you will learn how to use Python to charm Maya into obeying your every command. Practical Maya Programming with Python will teach you how PyMEL works as well as how to deal with errors and write composable code in Python.

What You Will Learn

  • Understand the architecture of Python objects, Maya nodes, and PyMEL
  • Write composable code that is modular and re-usable
  • Make Maya ""Pythonic"" through the use of decorators and context managers
  • Learn about different Maya UI options and libraries
  • Use PySide and PyQt to build Maya user interfaces
  • Turn Maya into a server that can be controlled remotely
  • Understand how to use Python with Maya's API
  • Conquer custom nodes using Python's dynamic type creation

Table of contents

  1. Practical Maya Programming with Python
    1. Table of Contents
    2. Practical Maya Programming with Python
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Introspecting Maya, Python, and PyMEL
      1. Creating your library
        1. Using the interpreter
        2. Finding a place for our library
        3. Choosing a development root
        4. Creating a function in your IDE
        5. Reloading code changes
      2. Exploring Maya and PyMEL
        1. Creating an introspection function
        2. Understanding Python and MEL types
        3. Using the method resolution order
        4. PyNodes all the way down
        5. Understanding PyMEL data and math types
        6. Leveraging the REPL
      3. Building the pmhelp function
        1. Creating a query string for a PyMEL object
        2. Creating more tests
        3. Adding support for modules
        4. Adding support for types
        5. Adding support for methods
        6. Adding support for functions
        7. Adding support for non-PyMEL objects
        8. Designing with EAFP versus LBYL
        9. Code is never complete
        10. Opening help in a web browser
      4. Summary
    9. 2. Writing Composable Code
      1. Defining composability
        1. Identifying anti-patterns of composability
        2. Avoiding the use of Boolean flags
        3. Evolving legacy code into composable code
        4. Rewriting code for composability
        5. Getting the first item in a sequence
        6. Writing head and tail functions
      2. Learning to use list comprehensions
        1. Implementing is_exact_type
        2. Saying goodbye to map and filter
      3. Writing a skeleton converter library
        1. Writing the docstring and pseudocode
        2. Understanding docstrings and reStructured Text
        3. Writing the first implementation
        4. Breaking the first implementation
        5. Understanding interface contracts
        6. Extracting the safe_setparent utility function
        7. Learning how to refactor
        8. Simplifying the node to joint conversion
        9. Learning how to use closures
        10. Dealing with node connections
        11. Dealing with namespaces
        12. Wrapping up the skeleton converter
      4. Writing a character creator
        1. Stubbing out the character creator
        2. Implementing convert_hierarchies_main
        3. Implementing convert_hierarchies
        4. Decomposing into composable functions
        5. Implementing convert_hierarchy
        6. Supporting inevitable modifications
      5. Improving the performance of PyMEL
        1. Defining performance
        2. Refactoring for performance
        3. Rewriting inner loops to use maya.cmds
      6. Summary
    10. 3. Dealing with Errors
      1. Understanding exceptions
        1. Introducing exception types
        2. Explaining try/catch/finally flow control
        3. Explaining traceback objects
        4. Explaining the exc_info tuple
      2. Living with unhandled exceptions
        1. Handling exceptions at the application level
      3. Golden rules of error handling
        1. Focus on the critical path
        2. Keep the end user in mind
        3. Only catch errors you can handle
        4. Avoid partial mutations
      4. Practical error handling in Maya
        1. Dealing with expensive and mutable state
        2. Leveraging undo blocks
        3. Dealing with Maya's poor exception design
        4. Leveraging the Maya application
        5. Dealing with the Maya application
        6. Leveraging Python, which is better than MEL
      5. Building a high-level error handler
        1. Understanding sys.excepthook
        2. Using sys.excepthook in Maya
        3. Creating an error handler
        4. Improving the error handler
        5. Inspecting Python code objects
        6. Adding filtering based on filename
        7. Assembling the contents of an error e-mail
        8. Sending the error e-mail
      6. Installing the error handler
        1. Obeying the What If Two Programs Did This rule
      7. Improving the error handler
        1. Adding a user interface
        2. Using a background thread to send the e-mail
        3. Moving beyond e-mail
        4. Capturing locals
        5. Attaching log files
      8. Summary
    11. 4. Leveraging Context Managers and Decorators in Maya
      1. Inverting the subroutine
      2. Introducing decorators
        1. Explaining decorators
        2. Wrapping an exporter with a decorator
      3. Introducing context managers
        1. Writing the undo_chunk context manager
        2. Writing the undo_on_error context manager
        3. Contrasting decorators and context managers
      4. Context managers for changing scene state
        1. Building the set_file_prompt context manager
        2. Building the at_time context manager
        3. Building the with_unit context manager
        4. Building the set_renderlayer_active context manager
        5. Building the set_namespace_active context manager
        6. Improving on future versions of Maya
      5. Creating the denormalized_skin context manager
        1. Safely swapping vertex influences
        2. Addressing performance concerns
      6. Creating a decorator to record metrics
        1. Getting a unique key
        2. Recording duration
        3. Reporting duration
        4. Handling errors
      7. Advanced decorator topics
        1. Defining decorators with arguments
        2. Decorating PyMEL attributes and methods
        3. Stacking decorators
        4. Using Python's decorator library
        5. Doing decorators the right way
      8. Summary
    12. 5. Building Graphical User Interfaces for Maya
      1. Introducing Qt, PyQt, and PySide
        1. Introducing Qt widgets
        2. Introducing Qt layouts
        3. Understanding Qt main windows and sorting
        4. Introducing Qt signals
      2. Establishing rules for crafting a GUI
        1. Prefer pure PySide GUIs where possible
        2. Use command-style UI building where necessary
        3. Avoid the use of .ui files
      3. Installing PySide
      4. Supporting PySide and PyQt
      5. Creating the hierarchy converter GUI
        1. Creating the window
        2. Running a Python file as a script
        3. Introducing the QApplication class
        4. Understanding the event loop
        5. Running your GUI
        6. Designing and building your GUI
        7. Defining control, container, and window widgets
        8. Adding the rest of the widgets
        9. Hooking up the application to be effected by the GUI
        10. Hooking up the GUI to be effected by the application
        11. Simulating application events
        12. Considering alternative implementations
      6. Integrating the tool GUI with Maya
        1. Opening the tool GUI from Maya
        2. Getting the main Maya window as a QMainWindow
        3. Making a Qt window the child of Maya's window
        4. Using Python's reload function with GUIs
        5. Emitting a signal from Maya
        6. Connecting Maya to a signal
        7. Verifying the hierarchy converter works
      7. Working with menus
        1. Creating a top-level menu
        2. Getting the Qt object from a Maya path
        3. Changing the font of a widget
        4. Marking menus as new
        5. Creating a test case
        6. Adding a persistence registry
        7. Verifying the new menu marker works
        8. Using alternative methods to style widgets
      8. Working with Maya shelves
      9. Summary
    13. 6. Automating Maya from the Outside
      1. Controlling Maya through request-reply
        1. Using a Python client and Maya server
        2. Controlling Python through exec and eval
        3. Handling problems with IPC
        4. Installing ZeroMQ
        5. Demonstrating request-reply with ZeroMQ
        6. Explaining connection strings, ports, bind, and connect
      2. Designing the automation system
        1. Pairing one client and one server
        2. Bootstrapping the server from the client
        3. The client-server handshake
        4. Defining the server loop
        5. Serializing requests and responses
        6. Choosing what the server does
        7. Handling exceptions between client and server
      3. Understanding the Maya startup routine
        1. Using batch mode versus GUI mode
        2. Choosing a startup configuration mechanism
        3. Using command line options
        4. Using environment variables
      4. Building the request-reply automation system
        1. Creating a Python package
        2. Launching Maya from Python
        3. Automatically killing the server
        4. Creating a basic Maya server
        5. Running code at Maya startup
        6. Understanding eval and exec
        7. Adding support for eval and exec
        8. Adding support for exception handling
        9. Adding support for timeouts
        10. Adding support for the client-server handshake
      5. Practical uses and improvements
        1. Batch processing using Maya
        2. Running a server in a Maya GUI session
        3. Running automated tests in Maya
        4. Adding support for logging
        5. Supporting multiple languages and applications
        6. Supporting control from a remote computer
        7. Designing an object-oriented system
        8. Evaluating other RPC frameworks
      6. Summary
    14. 7. Taming the Maya API
      1. Explaining types
        1. Dicts all the way down
        2. Using custom types to simplify code
        3. Introducing inheritance by drawing shapes
      2. Introducing Maya's API and architecture
        1. Understanding the OpenMaya bindings
        2. Navigating the Maya API Reference
        3. Understanding MObjects and function sets
      3. Learning the Maya Python API by example
        1. Converting a name to an MObject node
        2. Getting the name of an MObject
        3. Getting the hash of a node
        4. Building a mesh
        5. Setting mesh normals
        6. Using MScriptUtil to call a method
        7. Using OpenMaya for callbacks
        8. Comparing Maya Python API and PyMEL
      4. Creating a Maya Python plugin
        1. The life of a Python plugin
        2. Creating the sound player library
        3. Creating the plugin file
        4. Reloading plugins
        5. Adding a command flag
        6. Comparing the OpenMaya and scripting solutions
      5. Using PyMEL in a plugin that loads during startup
      6. Summary
    15. 8. Unleashing the Maya API through Python
      1. Understanding Dependency Graph plugins
        1. Building a simple node plugin
        2. Understanding plugin type IDs
        3. Defining inputs, outputs, and the initializer
        4. Creating the compute method
        5. Taming the non-Pythonic Maya API
      2. Demystifying Python metaprogramming
        1. Rethinking type creation
        2. Exploring the type function
        3. The importance of being declarative
      3. Designing the node factory
        1. Designing plugin nodes
        2. Designing the attribute specification
        3. Designing the node type specification
      4. Building the node factory
        1. Specifying attributes
        2. Creating attributes
        3. Specifying a node
        4. Using partial application to create attributes
        5. Creating a node
        6. Slaying the compute method
      5. Extending the node factory
        1. Supporting string and color attributes
        2. Supporting enum attributes
        3. Supporting transform nodes
        4. Overriding MPxNode methods
      6. Summary
    16. 9. Becoming a Part of the Python Community
      1. Understanding Open Source Software
        1. Differentiating OSS from script download sites
        2. Defining what a third-party module is
      2. Creating a site directory for third-party modules
        1. Explaining the site directory
        2. Creating a new site directory for Maya
        3. Establishing the site directory at startup
      3. Working with Python distributions in Maya
        1. Using the Python Package Index
        2. Adding a source distribution to Maya
        3. Adding an egg or wheel to Maya
        4. Using binary distributions on Windows
        5. Using pip to install third-party modules
      4. Contributing to the open source community
        1. Designing Maya Python code for open source
        2. Starting an open source project
        3. Distributing your project
      5. Engaging with the wider community
      6. Summary
    17. A. Python Best Practices
      1. The args and kwargs parameters
      2. String formatting
      3. String concatenation
      4. Raw strings and string literals
      5. Path building and manipulation
      6. Unicode strings
      7. Using the doctest module
      8. Adopting Test-Driven Development
      9. Using the GitHub repository for this book
    18. Index

Product information

  • Title: Practical Maya Programming with Python
  • Author(s): Robert Galanakis
  • Release date: July 2014
  • Publisher(s): Packt Publishing
  • ISBN: 9781849694728