« Some sketches from the archive | Main | Animation Reading »

MEL Scripting Tip #1: Always use an objects full path name

[Okay. Wow. The format of this post looks terrible... I'll see what I can do in the next day or so to get it fixed...]
[Better, but some of the code is broken across lines. Oh well.]
[Correcting the code fragment. It had my fix in it as originally posted.]

I just spent about twenty minutes debugging a problem with someone else's MEL script.

The script I'm trying to use has the following code snippet in it:

{
  string $currentSelXform[] = `ls -sl`;
  string $currentSelShape[] = `listRelatives -shapes $currentSelXform[0]`;
  if ($currentSelXform[1] != "")
  {
    warning "No base mesh loaded. Please select only 1 polygonal mesh to load.\n";
  } else {
    if (`objectType $currentSelShape[0]` != "mesh")
    {
      warning "Base mesh must be a polygonal mesh. NURBS and SUBD surfaces are not supported.\n";
    } else {
      textField -edit -tx $currentSelXform[0] currentBaseMeshTextField;
      print ($currentSelXform[0] + " was loaded as the base mesh.\n");
    }
  }

I select the menu item that calls this, and nothing happens. Now if you look at the script, every path prints either a warning or a message indicating success. Why was this doing nothing when I call it?

Turns out that the script was bailing out on the call to the objectType MEL command. After nosing around, I find the shape node for the object I was selecting is called 'subdTessShape1'. As it turns out, I've duplicated this shape many times. Guess what the shape node for all those duplicates is called? Yep, 'subdTessShape1' (this surface started out as a subd surface and is now a polygonal mesh).

This line:
  string $currentSelShape[] = `listRelatives -shapes $currentSelXform[0];
returns the shape name in $currentSelShape[] as 'subdMeshShape1'. When this is passed to the objectType command, it just bails because (apparently) it doesn't know which one.

listRelatives should be called like so:
  string $currentSelShape[] = `listRelatives -shapes -pa $currentSelXform[0]`;

So, MEL scripting Tip #1:
When passing object names around, always use full path names. When getting object names from listRelatives, use the -pa flag to get the full path.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)