Class 2 - Week 1 - Mel Scripts

Mel Scripts are probably one of my favorite ways of speeding up my
workflow. They can range from simple scripts that change the color
of a keyframe to complex monstrosities that can automate just about
anything you can imagine. Plus the nerd in me just loves to see all
the code and syntax.

One of the things I love about Maya is you don't even need to know how
to write code in order to create a Mel script. All you need to do is
open the script editor (either by hitting the button in the bottom
right corner of maya, or going to Window> General Editors > Script
Editor) then use the command or series of commands you want to make a
shortcut for and the script will show up in the top pane of Script
Editor, where you can copy and paste them.


Here is the workflow I used to created a script that would allow me to
quickly frame up on the current playback range in the graph editor.

  • Open both the Script Editor (Window > General Editors > Script Editor)
    and the Graph Editor (Window > Animation Editors > Graph Editor).
  • In the Script Editor, make sure "History > Echo All Commands" is
    checked.(Echo All Commands isn't needed most of the time, but if you
    find that the command you want isn't showing up in the Script Editor,
    try turning on Echo All Commands and it should show up.)
  • Now go to the Graph Editor and hit "View > Frame Playback Range"
  • There should now be a few new lines in the top pane of the
    Script Editor. The one we're interested in is
    animView -startTime (`playbackOptions -query -minTime` - 1) -endTime (`playbackOptions -query -maxTime` + 1) graphEditor1GraphEd;
  • Finally, select the code and hit Ctrl + C to copy it.
That was simple enough! But now that we have our script, we have to
figure out what we can do with it.

The first way you can use a script is to paste the script into either
the MEL Command Line (which is at the bottom of Maya by default) or in
the bottom pane of the Script Editor, then hit enter and the script
should run. But this is only good for a one time use, which kind of
defeats the purpose of going through all the trouble of getting a
script.

The second thing we can do is save the script to the shelf, creating a
new button and making it easier to use the script later on.

Adding a script to the shelf is a very simple process as well. Just
select the script in either the Command Line or the Script Editor then
middle mouse drag it up to the shelf and voila! A new shelf button is
born. I would suggest not creating any new buttons in the arAnimTools
shelf. If the Anim Tools updates or refreshes it will wipe out all the
custom scripts you have in the arAnimTools shelf. Instead, create them
in a custom shelf tab. This becomes even more useful when creating
selection sets. For example, you could create a button on the shelf
that selects all of Stewie's spine controls, one to select all his
controls at once, or even one that selects all his controls and then
keys them for you.

Thirdly, if you go to "Window > Settings/Preferences > Hotkey Editor"
we can use the script we've just created to make a new Maya hotkey,
increasing it's usefulness even more!


  • To add a new hotkey, hit the New button in the Hotkey Editor.
  • Type in a name and, if you want, a description for the new hotkey.
  • Select the category you want the hotkey to be listed under. I
    personally like to add all my custom hotkeys to the "User" category,
    making it easier to find them later on.
  • Copy and paste the Mel script into the Command area and hit Accept.
You now have a new command but it's still not assigned to any hotkeys.
To do that, select your newly created command and in the "Assign New
Hotkey Section", pick the key you would like to use, as well as any
modifiers, and hit Query. This will just double check to make sure the
hotkey you wanted isn't already used by something else. Once you've
found hotkey combination you want to use, just hit Assign and you're
all set!

I found that setting the "Frame to Current Time" script I mentioned
earlier to "Ctrl + Alt + f" really helped my productivity.

These are a few mel scripts from Kevin Freeman that I found very
useful for marking which keys are breakdowns and inbetweens in the
timeline.

////change all key colors green////
keyframe -tds on `ls -sl`;

////change key colors green on frame 12////
keyframe -tds on -t "12:12" `ls -sl`;

////change key colors green on current frame////
$now=`currentTime -q`;
keyframe -tds on -t ($now + ":" + $now) `ls -sl`;


////set key and change key colors green on current frame////
$now=`currentTime -q`;
setKeyframe;
keyframe -tds on -t ($now + ":" + $now) `ls -sl`;


Again, here's the script that will allow you to frame up on the
current frame range in the graph editor.
animView -startTime (`playbackOptions -query -minTime` - 1) -endTime (`playbackOptions -query -maxTime` + 1) graphEditor1GraphEd;

I hope this helps you and gives you a foundation with which to build
your own awesome MEL scripts. If anything was unclear or you would
like more info on how to create a custom hotkey, feel free to contact
Cameron or me. We'd love to help out in any way we can. Have a
fantastic new term and enjoy the continuation of your AM adventure!

1 comment:

  1. really great mel, please can u show how to change blue and red thank you

    ReplyDelete