Category Archives: MEL

Take A Snapshot In Maya

// keep the old format
int    $format    =    `getAttr "defaultRenderGlobals.imageFormat"`;
// set to render to .png image
setAttr    "defaultRenderGlobals.imageFormat" 32;
// playblast one frame to a specific file
playblast    -frame 1    -format    "image"    -cf    "D:/foo.png" -v 0    -wh    240    175    -p 100;
// restore the old format
setAttr    "defaultRenderGlobals.imageFormat" $format;

Above is a chunk of MEL will take a snapshot of what’s inside active view port and save to a .png image file to disk. Could be useful when you want to save a thumbnail of the scene.


Leave a comment

Filed under MEL

Calculate Angle of View

float $aperture = `getAttr perspShape.hfa`;
float $fl = `getAttr perspShape.focalLength`;
float $fov = $aperture * 0.5 / ( $fl * 0.03937 );
$fov = 2.0*atan($fov) / 3.14159 * 180.0;
print(" angle of view in degrees: " + $fov);

This piece calculates angle-of-view of a camera from its Focal Length and Horizontal Film Aperture.

1 Comment

Filed under MEL

Maya Get Current Camera

Still a bit of maybe useful piece of MEL to find out which camera you are working with. Focus on a model panel, in which you can have a camera to tumble/pan/zoom, and run getCurrentCamera, you will have the transform name of the camera. If you are in some other type of panel, say outliner, empty string will be returned.

global proc string getCurrentCamera()
  string $camera;

  // First get the panel that currently has focus
  string $panel = `getPanel -wf`;

  // Compare the panel's type to confirm it is a "modelPanel"
  if ( "modelPanel" == `getPanel -to $panel` )
    // Get the name of the camera using the ‘modelEditor’ command
    $camera = `modelEditor -q -camera $panel`;
  if($camera == "")
      return "";
  if(`nodeType $camera` == "camera")
       string $rels[] = `listRelatives -p $camera`;
       $camera = $rels[0];
  return $camera;


Filed under MEL

Maya Get Date and Time

proc string getDateAndTime()
        python("import datetime");
        python("d =");
        return python("d.strftime('%Y-%m-%d-%Hh%Mm%Ss')");
// Result: 2011-08-03-20h24m56s //


Just a quick note about how to get date and time via MEL and Python

Leave a comment

Filed under MEL, Python

Get Maya Version And Bin Location

Sometimes it is important to deduce the version and bin location of the Maya your are working with. Getting the version number is easy:


should be sufficient. But there seems no built-in MEL command to get where the Maya application is installed. I tried to write a procedural to do that trick:

proc string getApplicationLocation()
string $scpt =
"import sys, os\n\
def where_maya_app_resides(path):\n\
    appName = '/Maya'\n\
    if sys.platform == 'win32':\n\
        appName = '/Maya.exe'\n\
    for apath in path:\n\
        if os.path.exists(apath+appName):\n\
            return apath\n\
    return 'maya not found in any path'\n";

        return python("where_maya_app_resides(sys.path)");

Actually, it is done in Python. Calling sys.path in Maya Python will returns a number of module search directories. Usually the first one will be Maya/bin. To make sure it returns the bin directory, I check every directory to find whether Maya(.exe) does exist inside it. On Mac, it returns: /Applications/Autodesk/maya2008/

1 Comment

Filed under MEL, Python

File Reference Basic

This document is about how to create and remove file referencing in Maya.

Say there is a file named bhdt.mb like above. New a file and run:

file -r -ns "cc" -typ "mayaBinary"  "bhdt.mb";

Will bring a bunch of Read-Only nodes that referred to what’s inside bhdt.mb

“cc” is the namespace, so everything in this reference will be named as “cc:*”

Can create more references to the same file by:

file -r -ns "dd" -typ "mayaBinary"  "bhdt.mb";
file -r -ns "ee" -typ "mayaBinary"  "bhdt.mb";
file -r -ns "ff" -typ "mayaBinary"  "bhdt.mb";
file -r -ns "gg" -typ "mayaBinary"  "bhdt.mb";

Each with its own namespace.

Put one of them under a group, then try to delete group1. This will fail because it has Read-Only children. To remove the reference, select the group1, and run:

file -rr "bhdt.mb";

dd:glb and everything under it will disappear. After the reference is gone, group1 can be deleted.


file -r command actually returns the file name it referred to. Those names are unique. In case there are multiple references to a same file, second reference will return “bhdt.mb{1}”, and third is “bhdt.mb{2}”, and so on. These names are useful to determine individual reference when calling file -rr command.

Leave a comment

Filed under MEL