Mel Scripting

copyright (c) Rob Bateman - 2005/2006

Last Updated: 30/11/2006

Thanks to Janne Ojala for suggesting a
few improvements to these notes.

 

 

Disclaimer

 
I do my best, but i often make silly mistakes! You may find one or two errors within the examples presented here, and I therefore accept no responsibility if anything does go wrong!
 
 

 

 

Intro
Declaring Data
Control Structures
Good Scripting Techniques
Accessing Maya Data
Graphical User Interfaces (GUI)
Problems with data Storage
Expressions
Handling Files
Node Type Specific
Maya API Specific
Larger Examples
Common mel errors

 

 

 

 

Intro

 

Hopefully this document should provide a fairly good resource to answer most questions that commonly arise from anyone first learning mel.

DG, DAG, History, and Undo

Script Editor Basics

10 lines of script to access the world

 

 

 

 

 

Declaring Data

 
The first stage of any script is to decide what data we need to store and manipulate.
 

Basic Data Types and Variable Scope

Arrays

Vectors & Matrices

 

 

 

 

 

Control Structures

 
So we have our data stored, now it's time to start asking questions and repeating actions.
 

for loop

for in loop

while loop

do while loop

if / else statement

switch statement

 

 

 

 

 

Good Scripting Techniques

 
When developing mel scripts you will often want ways of simplifying the task at hand. Often you may use a procedure or seperate script files to improve the readability and to simplify the code. You may also want to run external programs or other scripts. This section deals with all of that....
 

declaring mel procedures [API:1|2]

eval [API]

Script Jobs [API]

Sourcing Files [API]

system

Script Nodes

Script Contexts [API]

 

 

 

 

 

Accessing Maya Data

 
The main reason to have a scripting language in Maya is so that we can control our scenes in a flexible way. This obviously means that we must be able to find out what objects are present in our scene, and be able to set and get attributes on those objects.
 

Listing and Selecting Objects [API:listing|selecting 1|2]

Getting / Setting Attributes [API]

Getting Attribute Connections [API]

Getting parenting information [API]

 

 

 

 

 

Graphical User Interfaces (GUI)

 
The best part about mel is the ease with which you can create User Interfaces. Generally speaking it's always nice to provide a graphical means of driving your mel scripts. Often this involves responding to a user input event by calling a pre-defined mel procedure.
 

Windows & A quick GUI overview

Layouts

Controls

Menus

Attribute Controls

Common Dialogs

 

 

 

 

 

Expressions

 

Expressions within Maya allow you to create small scripts that can be attached to a specific attribute to control it's value. These are known as Expressions. You can either use the Expression Editor to create them, or you can use the expression mel command.

The syntax of an expression is not exactly the same as that of mel, so you should bear that in mind whenever working with expressions.

 

 

The Expression Editor

Differences between mel & Expressions

Using the expression command

 

 

 

 

 

Problems with data Storage

 

During the course of developing a script you will always have a problem of persistant data storage. Often values are stored in global variables, however the problem is that the values stored will vanish when either the script is run again, or maya is re-started. Loosely speaking, persistant data falls into 2 catagories:

1. Custom Scene data required by a script or expression

2. Custom tool settings you want to access next time Maya is run

 

Nodes for storage and Adding Attributes

Using Script Nodes for Storage

Saving User Preferences with optionVar

 

 

 

 

 

Handling Files

 
Being able to load and save files from within mel allows you many possibilities such as, data caching, data import export etc.
 

writing ascii data

reading ascii data

writing binary data

reading binary data

getting directory listings

file dialogs

 

 

 

 

 

Node Type Specific

 

These sets of notes are designed to demonstrate methods to access specific data types within mel. They do very little other than showing you the basics of how to create the node types, and also how to access the data they contain.

I've directly cross-referenced the mel notes here with the sections from the Maya Exporter Factfile. Basically, all [API] links demostrate accessing the same data from the Maya API. (just thought it might be useful for someone).

 

Surface Properties

Textures [API]

Materials [API]

Bump Maps [API]

Environment Maps [API]

Shapes

Polygonal Meshes [API]

Nurbs Surfaces [API]

Nurbs Curves [API]

Lights [API]

Cameras [API]

Locator Nodes [API]

Volume Primitives [API]

Transforms & Animation

Transform Data [API]

Sampled Animation Data [API]

Animation Curves [API]

IK Chains [API]

Constraints [API]

Deformers

Blend Shapes [API]

Joint Clusters [API]

Skinning Data [API]

Lattice Deformers [API]

Dynamics

Particle Systems [API]

Dynamics Fields [API]

Misc

Display Layers [API]

Render Layers [API]

 

 

 

 

 

Maya API Specific

 
During the course of developing a script you will always have a problem of persistant data storage. Often values are stored in global variables, however the problem is that the values stored will vanish when either the script is run again, or maya is re-started. Loosely speaking, persistant data falls into 2 catagories.
 

The Attribute Editor Templates

 

 

 

 

 

Larger Examples

lorenzAttractor.mel - creates a lorenze attractor out of spheres

rosslerAttractor.mel - creates a rossler attractor out of spheres

unsort.mel - unsorts an array

bvhExport.mel - exports animation as a bvh motion capture file

mesh_exporter.mel - exports polygonal meshes to a file

nurbExport.mel - exports nurb surface data from Maya

polyWeightExport.mel - exports skin weighting info to a text file

polyWeightImport.mel - imports skin weighting info from a text file

transformExport.mel - exports transform and animation data

ExporterIntermediate.mel - exports polys, meshes, transforms, materials

 

 

 

 

 

Common mel errors

General Debugging / Working tips

Error: cannot convert string[] to string