The following section
introduces some basic concepts that help you understand what’s going
on when you use scripting in Maya.
The command architecture
Maya has a command-based
architecture for handling all of its operations. The commands for
all of Maya’s functionality are accessed through scripting language
commands tied to the Maya user interface—menus, tools,
dialog boxes; in fact, just about anything you interact with in
Maya. As well, you can explicitly enter Maya commands in the scripting
languages that Maya supports.
Every Maya command has
multiple arguments that set the way in which the command executes.
Arguments and their implicit or explicit values are required for
a command to execute.
To set argument values,
the scripting languages in Maya use flags. Flags tell the scripting
language which argument of the Maya command is set with what value.
The value that is assigned to the argument follows the flag.
Flags have short and
long names in MEL and Python. Commands execute identically whether
or not you use the short or long name of the flag. Long names are
often descriptive and useful for new users; short names abbreviate flag
names for power users.
Scripting languages in
Maya supports two scripting
- MEL (Maya Embedded Language) is a proprietary
scripting language modeled after Unix shell script.
- Python is a recent addition to Maya that
provides the same access to Maya’s commands as MEL. Python is a
widely-used modern programming language.
Maya’s user interface
is built using scripts that execute (call) Maya commands. As Python
is a recent addition to Maya, the majority of the scripts accessed
through the user interface are written in MEL.
For example, when you
click the sphere icon on the Shelf or select an item from a menu,
Maya calls MEL commands to create a sphere or execute the command
associated with the menu item.
Entering commands in Maya
There are several ways
to explicitly enter scripting commands in Maya.
- To enter single line MEL and Python commands,
use the Command Line. To show the Command
Line if it is hidden, select
Display > UI Elements.
The Command Line is located near
the bottom edge of the Maya interface.
- To enter multi-line MEL and Python commands,
use the Script Editor. The Script
Editor displays a history of executed commands, as well
as the results and outputs of commands. To open the Script
> General Editors > Script Editor.
- All MEL commands are highlighted as you
enter them into the MEL tab in the Script Editor.
- As well, the Expression Editor uses
a subset of MEL commands to create animation in the scene. MEL commands
in the Expression Editor are evaluated
for every frame in the animation. The Expression Editor does
not support Python commands. To open the Expression Editor,
> Animation Editors > Expression Editor.
You can also load external
MEL and Python files and run them using the Script Editor. For more
Loading a script file.
What MEL looks like
Invoking commands in
MEL has the basic structure of a command followed by a combination
of flags and arguments. Flags are prefaced by the hyphen character
All MEL commands are
case-sensitive; SPHERE is not the
same as sphere (and
returns an error message).
If no flags are provided,
the command executes with default arguments. The command fails if
it requires arguments that it did not get.
MEL also has an alternate
syntax, which implements commands and flags in a method similar
to the C programming language. For more information, see
MEL for programmers
Getting help on MEL
You can get help with
MEL in three ways:
- To access help on a particular MEL command,
open the MEL Command Reference by selecting
Help > MEL Command Reference.
- Help can also be accessed from within
the Script Editor by using the Maya Command help.
For example, entering help
sphere shows a list of flags that can be used with the sphere command.
- More information on all aspects of using
MEL commands is also available in the MEL and Expressions guide,
part of the Maya Help. Open the Maya Help and select Using Maya
> General > MEL and Expressions.
Basics of Python
Python and MEL are scripting
languages with equal prominence and capabilities in Maya. Python
can access all the Maya commands that MEL does.
Python accesses Maya
commands through the Python module maya.cmds. Modules
in Python are sets of commands that add functionality to Python, and
must be imported before using any of the commands from a module.
Flags in Python are implemented
through Python’s named arguments. You specify which argument to
modify and use the assignment operator (=)
to assign a new value to the argument.
In order to run this
or any example command, you must have imported the Python module
first. Enter import maya.cmds before
starting to enter Python commands.
All Python commands are
case-sensitive; maya.cmds.SPHERE is
not the same as maya.cmds.sphere (and
returns an error message).
Getting help on Python
You can get help with
Python in several different ways:
- To access help on a particular Maya (Python)
command, open the Python Command Reference by
Help > Python Command Reference.
- You can also access help on Maya Python
commands from within the Script Editor by using the Maya Command help(). For example: typing maya.cmds.help("sphere") outputs a
list of possible flags that can be used with the sphere command.
- Help for native Python commands is accessed
from within the Script Editor by typing help("command") where command is the Python command you want
to access help for.
- More information on using Python commands
is also available in the Python guide, part of the Maya Help. Open
the Maya Help and select Using Maya > General > Python.
- All Python commands are highlighted as
you enter them into the Python tab in the Script Editor.
- There are existing reference materials
and documentation for Python on the web, as well as many downloadable
modules to add extra functionality to Python.