You are previewing MEL Scripting for Maya Animators, Second Edition.
O'Reilly logo
MEL Scripting for Maya Animators, Second Edition

Book Description

Trying to learn Maya programming from the documentation can be daunting whether or not you are a programmer. The first edition of MEL Scripting for Maya Animators earned the reputation as the best introductory book on MEL, Maya's scripting language. Now fully revised and updated, the second edition also includes new features, such as a discussion of global procedures, new chapters on fixing programming bottlenecks, advanced user interface techniques, and optimizing character rigs. New chapters on utility nodes and Maya's Web Panel feature provide new ideas on how to use MEL in applications. This new edition has kept the popular style of the first edition that offered very clear explanations of programming concepts to those without programming experience. A generous collection of code examples and Maya scene files is included on the companion Web site. This is a book for animators, artists, game developers, visual effects developers, and technical directors who want to learn the fundamentals of Maya, how to automate tasks, personalize user interfaces, build custom tools, and solve problems with MEL.

Table of Contents

  1. Copyright
    1. Dedication
  2. MEL Scripting for Maya Animators, 2e
  3. The Morgan Kaufmann Series in Computer Graphics and Geometric Modeling
  4. About the Authors
  5. Preface
    1. Acknowledgments
    2. Special Acknowledgment
  6. 1. Maya Under the Hood
    1. In this chapter you will learn
    2. Why Look Under the Hood?
    3. The Dependency Graph, Attributes, and Connections
    4. Example 1: Using the Hypergraph to Explore the Dependency Graph
    5. Transform Hierarchy and Parent/Child Relationships
    6. Examining the Hierarchy
    7. Transform and Shape Nodes
    8. Example 2: Exploring Transform and Shape Nodes, Instancing, and History
    9. MEL and Maya’s User Interface
    10. What to Remember About How Maya Works Behind the Scenes
  7. 2. The Basics of MEL Commands
    1. In this chapter you will learn
    2. Can I Use MEL Without Scripting?
    3. Command Line and Command Feedback Line
    4. Command Shell
    5. Script Editor
    6. Script Editor Versus Command Shell
    7. Script Editor’s Messages as MEL Code
    8. Making a Shelf Button for a MEL Script
    9. Saving a MEL Script
    10. Seductive Dangers of the Status Message Area
    11. The whatIs Command
    12. Basic Structure of MEL Commands
    13. Where to Find Information About Maya and MEL on the Internet
      1. Web Sites
    14. Newsgroups
    15. How to Use MEL Scripts Found on the Internet
    16. What to Remember About How to Use MEL Without Writing Scripts
  8. 3. Using Expressions
    1. In this chapter you will learn
    2. What Is an Expression?
    3. How Does an Expression Work?
    4. Equals Sign: Equality and Assignment
    5. How Maya Implements Expressions
    6. Is Maya’s Expression Language the Same as MEL?
    7. When (and When Not) to Use an Expression
    8. Defining Relationships Between Attributes
    9. What Is Operator Precedence?
    10. Walkthrough of Maya’s Expression Language
    11. Definitions of Variables
    12. Computing the Values of Attributes
    13. Assigning Computed Values
    14. Example 1: Eyes
    15. Analyzing the Problem
    16. Planning the Eyes’ Animation Controls
    17. Writing the Expression
    18. What to Remember About Using Expressions
  9. 4. Controlling Particles with Expressions
    1. In this chapter you will learn
    2. Two Kinds of Particle Object Attributes: Per Object and Per Particle
    3. All About Vectors
      1. Creating a Vector Variable
      2. Manipulating Vector Components
      3. Adding and Subtracting Vectors
      4. Other Useful Vector Operations
        1. unit
        2. mag
        3. angle
        4. cross
        5. dot
        6. rot
    4. Two Kinds of Expressions: Ordinary and Particle
      1. Using Ordinary Expressions to Control Particle Behavior
    5. Example 1: Ordinary Expressions and a Newton Field
      1. How this expression works
      2. When to Use a Particle Expression
    6. Example 2: A Simple Particle Expression
      1. How This Expression Works
    7. A Few Hints for Efficient Particle Expressions
    8. Example 3: Helical Particles Around a Curve
      1. Brainstorming Possible Solutions
      2. A First Attempt at the Particle Expression
        1. How this expression works
      3. Refining the Solution
    9. What to Remember About Particle Expressions in Maya
  10. 5. Problem Solving with MEL Scripting
    1. In this chapter you will learn
    2. MEL’s Role in Maya: Building Scenes
    3. Strategies for Planning MEL Applications
    4. The Simplest User Interface
    5. Creating, Editing, and Querying Nodes in MEL
    6. Adding, Setting, and Getting Values of Attributes in MEL
    7. Connecting Attributes in MEL
    8. Creating and Connecting Expression Nodes in MEL
    9. Example 1: Using MEL to Automate Setup for Spiral Particles
      1. How It Works
    10. What to Remember About Writing MEL Scripts
  11. 6. Variables and Data Types
    1. In this chapter you will learn
    2. Declaring Variables (and Not Declaring Them)
      1. Local and Global Variables
      2. Global Variables, Attributes, and Option Variables
    3. Environment Variables
    4. MEL Statements and Type Checking
    5. Simple and Aggregate Data Types
      1. Integers
        1. Definition
        2. How to declare an integer variable
        3. Integer arithmetic operations
        4. Combining arithmetic and assignments
      2. Floating-Point Numbers
        1. Definition
        2. How to declare an integer variable
        3. Useful floating-point operations
        4. Cautionary note about floating-point comparisons
      3. Conversions Between Integers and Floating-Point Numbers
      4. Vectors
        1. Definition
        2. How to declare a vector variable
        3. Working with vectors
        4. Other common vector operations
      5. Strings
        1. Definition
        2. How to declare a string variable
      6. Arrays
        1. Definition
        2. How to declare an array
        3. Working with array contents
        4. Array size property and its implications
      7. Choosing Between Arrays and Vectors
    6. What to Remember About Variables and Data Types in MEL
  12. 7. Using MEL Commands
    1. In this chapter you will learn
    2. What Is a MEL Command?
      1. Where to Find the MEL Command You Need
        1. Ask an experienced MEL programmer
        2. Read related scripts from the Internet or elsewhere
        3. Read the documentation
    3. Structure of a MEL Command
      1. Query, Create, and Edit Modes
      2. Other Command Flags
      3. Command Arguments
    4. Using MEL Commands in MEL Scripts
      1. Using MEL Commands as Statements
      2. Using Backticks
      3. Using eval
    5. Avoid Using MEL Commands in Expressions
    6. What to Remember About Using MEL Commands
  13. 8. Manipulating Nodes in MEL
    1. In this chapter you will learn
    2. Using ls Command to Find Nodes by Name or Other Properties
      1. Finding Objects with a Particular Name or Partial Name
      2. Getting Names of Selected Objects
      3. Getting One or More Objects’ Types
      4. Finding Objects of a Particular Type
      5. Finding List of Objects Under a Certain Node in Transform Hierarchy
      6. Listing Full Path Names
    3. Using Select Command to Manage Object Selection
      1. Clearing the Selection
      2. Selecting a List of Objects
      3. Adding to an Existing Selection
      4. Removing Objects from an Existing Selection
      5. Selecting All Nodes in a Scene
    4. Creating Nodes in a Maya Scene
      1. Using Commands Specific to a Node Type
      2. Using Utility Commands that Support the -ch on Flag
      3. Using the createNode Command
    5. Finding a Node’s Parents and Children
    6. Finding Information on Node Connections
      1. Using listConnections
      2. Using connectionInfo
      3. Using isConnected
    7. About Maya’s Node Types and the Node and Attribute Reference
    8. What to Remember About Managing Nodes in MEL
  14. 9. Controlling the Flow of Execution
    1. In this chapter you will learn
    2. Controlling the Flow of Script Execution
    3. Basic Conditional Operations: if-else and switch
      1. If-else Statements
      2. Conditional Tests
        1. The not operator
        2. The and operator
        3. The or operator
      3. Switch Statements
    4. Loops
      1. While Loops
      2. For-in Loops
      3. For Loops
    5. What to Remember About Controlling the Flow of Execution in MEL
  15. 10. Procedures and Functions
    1. In this chapter you will learn
    2. Top-Down Design
    3. Example 1: A Trip to the Grocery Store
    4. What Are Procedures and Functions?
      1. Scope and Global Procedures
      2. Defining and Using Procedures and Functions
    5. Example 2: Geometry-Constrained Locators
      1. Recursion
    6. Example 3: Recursive Antenna
    7. What to Remember About Procedures, Functions, and Top-Down Design in MEL
  16. 11. Naming Nodes, Scripts, and Variables
    1. In this chapter you will learn
    2. Why Naming Conventions Are Important
    3. Naming Scripts
      1. Script, Procedure, and Function Names
      2. Identify Data and Purpose
      3. Identifying the Script Developer in the Script Name
    4. Naming Variables
      1. Valid and Invalid Names
      2. Identify Data Type and Purpose
      3. Global Variables: Identifying Developer
    5. Naming Nodes
      1. Valid Names
      2. Transform Paths
      3. Identify Type and Purpose
      4. Unique Names and Writing Scripts
    6. Example 1: Adding a Name Prefix to Objects in a Hierarchy
      1. How the Script Works
    7. Example 2: Changing Name Prefixes in a Hierarchy
      1. How the Script Works
    8. What Are Namespaces?
      1. Finding Information About Namespaces
      2. Creating, Setting, and Deleting Namespaces
    9. Strategies for Using Namespaces
    10. What to Remember About Naming Scripts, Variables, and Nodes
  17. 12. Designing MEL User Interfaces
    1. In this chapter you will learn
    2. What Is a User Interface?
    3. What Maya Users Expect to See from a MEL Script
      1. Approach 1: Operate on the Selection
      2. Approach 2: Using a Dialog Box
      3. Approach 3: The Persistent Dialog Box
      4. Approach 4: Something Else
    4. Questions to Answer Before Designing a User Interface
    5. Designing and Testing a User Interface
      1. Hints for Planning a Good User Interface
        1. Do the repetition for the user
        2. One input window is better than many
        3. Put effort into most-used interfaces
        4. Show choices rather than ask questions
        5. Minimize number of choices presented to the user
        6. Build simple tools rather than large systems
    6. Structure of a Dialog Box
    7. What to Remember About Designing User Interfaces in MEL
  18. 13. Simple MEL User Interfaces
    1. In this chapter you will learn
    2. Collecting Information from Users
    3. Validating User Input: When and Why
    4. Asking for Confirmation with confirmDialog
    5. Asking User for Text String with promptDialog
    6. Asking User to Pick File or Directory with fileDialog
    7. Handling Warnings and Errors with Warning and Error Commands
    8. Using Regular Expressions and match to Validate Data
    9. How Regular Expressions Work
    10. Validating Integers
    11. Validating Floating-Point Numbers
    12. Validating Object Names (Without Namespaces)
    13. Example 1: Simple Dialogs and Input Validation
      1. How It Works
    14. What to Remember About Simple MEL User Interfaces and Input Validation
  19. 14. Custom Dialog Boxes
    1. In this chapter you will learn
    2. How to Structure a Script That Uses a Custom Dialog Box for Input
    3. Dialog Boxes and Their Contents
      1. Layouts
      2. Groups
      3. Collections
      4. Controls
    4. Example 1: Making the Example Dialog Box
      1. How It Works
    5. Common Types of Controls
      1. button
      2. checkBox
      3. floatFieldGrp
      4. floatSliderGrp
      5. intFieldGrp
      6. intSliderGrp
      7. textFieldButtonGrp
      8. textFieldGrp
      9. radioButtonGrp
    6. Common Types of Layouts
      1. columnLayout
      2. formLayout
      3. frameLayout
      4. rowColumnLayout
      5. rowLayout
    7. Example 2: Dialog Box for Making Geometric Primitives
      1. Creating the Main Script
      2. Building the Dialog Box
      3. Defining the Button Action Procedure
    8. What to Remember About Building Custom Dialog Boxes in MEL
  20. 15. Making Advanced Dialog Boxes with formLayout
    1. In this chapter you will learn
    2. Why Use formLayout?
    3. Planning a Dialog Box for formLayout
    4. Using formLayout: Overview
      1. How It Works
    5. Using formLayout: Defining Placement Rules for UI Objects
      1. -attachForm
      2. -attachOppositeForm
      3. -attachControl
      4. -attachOppositeControl
      5. -attachPosition
      6. -attachNone
    6. Example: Implementing a Dialog Box with formLayout
      1. How It Works
    7. What to Remember About Making Dialog Boxes with formLayout
  21. 16. Making Advanced Dialog Boxes with Web Panels
    1. In this chapter you will learn
    2. What You Need to Know Before You Proceed
    3. What Are Web Panels?
    4. Learning Web Authoring
    5. How a Dialog Box Built with Web Panels Works
    6. Planning a Dialog Box for Web Panels
    7. Creating a Web-Based Dialog Box for Maya
    8. Launching a Web-Based Dialog Box from MEL
      1. How It Works
    9. Example 1: Implementing a Dialog Box using JavaScript
      1. How It Works
    10. Ideas for Dialog Boxes Built with Web Panels
    11. What to Remember about Making Dialog Boxes with Web Panels
  22. 17. Improving Performance with Utility Nodes
    1. In this chapter you will learn
    2. What Is a Utility Node?
    3. When Should You Consider Using a Utility Node?
      1. Use utility nodes in custom shading networks
      2. Use utility nodes in place of expressions when many expressions will need to be evaluated in the scene
    4. How to Create and Connect a Utility Node
    5. Example 1: Using the plusMinusAverage Node to Find the Midpoint Between Two Locators
    6. Common Utility Nodes
      1. plusMinusAverage
      2. multiplyDivide
      3. vectorProduct
      4. condition
      5. setRange
    7. What to Remember About Improving Performance With Utility Nodes
  23. 18. Installing MEL Scripts
    1. In this chapter you will learn
    2. Installing a Script to Make It Available in All Scenes
    3. Installing a Script to Run When Maya Starts
    4. Installing a Script into a Script Node in a Scene
    5. Installing Custom Menus
    6. Managing Button Shelves and Creating Custom Shelf Icons
    7. What to Remember About Installing MEL Scripts
  24. 19. Examples Using MEL with Particle Dynamics
    1. Example 1: Introduction to Particle Goals
      1. Adding Color and Radius
      2. Adding Velocity
    2. Example 2: Particle Goals on a Surface
      1. Per Particle Arrays
      2. Initial States
      3. Expressions and the Expression Editor
      4. Watching Values of Particles
      5. Using a Lifespan Per Particle
      6. Changing U and V with Math Functions
    3. Example 3: Using Goals on Multiple Surfaces
      1. Example 2 Sin Function Revisited
      2. Adding a Custom Attribute
      3. Extra
    4. Example 4: Using Goals on Surfaces, Part 2
      1. Overview
      2. Load the Scene
      3. Add Particle Ball
      4. Adding Goals for Particle and Planes Using String Variables
      5. Adding Creation Expressions to Particle Ball
      6. Add Runtime Expression
  25. 20. Examples Using MEL with Solid Body Dynamics
    1. Example 1: Particle Collisions
      1. Create the Scene
      2. Collide the Particles
      3. Setting Controls for Collisions
      4. Using Other Controls in geoConnector Node
      5. Finish Scene Using MEL
    2. Example 2: Collision Events
      1. Overview
      2. Add Emitter and Particles
      3. Dynamic Relationships
      4. Limit Number of Particles and Add Gravity
      5. Add Other Collisions
      6. Events
      7. Expression for Buckets
      8. Edit Settings to Fix Problems
      9. Speed
    3. Example 3: Collisions Between Objects in Solid Dynamics
      1. Write Small Duplicate and Position Script
      2. Common Mistakes in Assembling String Variables
      3. Create Collision Box
      4. Flip Normals on collisionBox
      5. Active and Passive Rigid Bodies
      6. Make Each Grid a Passive Collision Object
      7. Turn on Collision Data Options
      8. Change Color of Grids with Collisions
    4. Example 4: Solid Dynamics and Particles
      1. Create Interface Window for Aim Control Using MEL
      2. Add New Window Control to Shelf
      3. Convert Panel to Polygons and Break Up into Pieces
      4. Add Dynamics and Expressions to Pieces
      5. Create Full MEL Script
      6. Load in Scene and Run Script
      7. Pass a Float into panelBreakup
  26. 21. Example of a Simple Crowd System
    1. Example 1: Creating a Vehicle
      1. Make Vehicle a Solid Dynamic Object
      2. Changing the Vehicle’s Direction
      3. Change Motion of vehicle_1 with Expression
      4. Create Wander Motion
      5. Orient vehicle_1’s Direction with Its Motion
    2. Example 2: Vehicle Interaction
      1. Create Script to Automate Creating Vehicle in Example 1
      2. Add Radial Field to vehicle_1 to Repel Other Vehicles
      3. Add Another Vehicle
      4. Add Simple MEL Interface
      5. Save and Source Script
      6. Create Wrapper Script and Place on Shelf
    3. Example 3: Vehicle Environment
      1. Launch crowdSystem.mel
      2. Add Boundary Obstacles
      3. Create More Obstacles
      4. Convert New Obstacles into Passive Rigid Bodies
      5. Add collectObstacles to crowdSystem.mel
      6. Create Global Forces
      7. Create Matrix Variable to Use for Placement
      8. Create Control Slider Interface
      9. Add Global Forces Procedure to crowdSystem.mel
    4. Example 4: Fine Tuning and Completing the Script
      1. Add New Features to User Interface
      2. Add Checkbox Options
      3. Add Cancel Button
      4. Window Standards
      5. Add More Vehicles
      6. Add Integer Pass from One Procedure to Another
      7. Connect Radials for Both Leaders and Followers Together with “Nested” Loops
      8. Add Global Forces Function to Main Script
      9. Test Global Forces Option
      10. Add Main Launch Procedure
      11. Add collectObstacles to Script
      12. Add Objects to Scene and Run Script
    5. Full Script Reference: crowdSystem.mel
  27. 22. Examples Using MEL in Character Rigging
    1. Example 1: Character Controls
      1. Load Scene
      2. Overview of Scene
      3. Overview of mrBlah’s Controls
      4. Locking Attributes
      5. Arm Controls
      6. Set Up mrBlah’s Backbone Controls
      7. Create Sway Effect When Picking Up Feet
    2. Example 2: Building a Character User Interface
      1. Load Saved mrBlah Scene
      2. Organize Controls for Users
      3. Create mrBlahControls.mel
      4. Create Sliders to Control spinCtrl Attributes
      5. Create Layout for Window Controls
      6. Test Window
      7. Display Only What You Want User to See
      8. Create Script Node for Window
      9. Create Shelf Icon to Reopen Window When Closed
    3. Full Text of mrBlahControls.mel