Array manipulation in MEL – Part 1

Here, in this tutorial we will see some of the array manipulation commands in MEL that can be put to use while creating your own scripts.
Problem:
Getting a List of objects that are of a particular type, based on user input.
Solution:

ak_listObjectsOfType(“$type”)

where $type can be mesh, nurbsCurve, ikHandle, joint, multiplyDivide….

 global proc ak_listObjectsOfType(string $type)
 {
 string $transformList[],$cleanList[],$conflictList[];
 string $newName,$each;
 $choiceList = `ls -type $type`;
 if ($type == "nurbsCurve" || $type == "mesh")
 {
 $transformList = `listRelatives -parent $choiceList`;
 }
 else
 {
 $transformList = $choiceList;
 }
 int $i = 0;
 for ($each in $transformList)
 {
 if (stringArrayContains($each,$cleanList)!=1)
 {
 stringArrayInsertAtIndex($i,$cleanList,$each);
 $i++;
 }
 }
 print $cleanList;
 }
 

Commands:
ls, listRelatives, stringArrayContains, stringArrayInsertAtIndex
Discussion:
The result can be easily achieved using the command ls. Using the -type flag followed by the object type will give a list of objects of that specific type.
For eg: ls -type “ikHandle” will return a list of all ik handles in the scene
But specifying mesh or nurbsCurve will give the shape names instead of the transform names. By running a listRelatives -parent on the returning list will give the transform nodes, but this will include duplicate entries if the mesh is having a deformer attached to it since ls -type “mesh” return a list of all the intermediate objects too. So we will be using the script listed above to print out objects that matches the user entry type.

The pseudocode for the script is as follows.


global procedure ak_listObjectsOfType($type of type string )
 {
 declare String Arrays $transformLis,$cleanList and $conflictList
 declare String $newName and $each;
 store the value of ls command of node type $type in $choiceList
 if ($type  is "nurbsCurve" or   "mesh")
 {
 store the parents of $choiceList in $transformList;
 }
 else
 {
 Assign  the value of $choiceList itself to $transformList
 }
 initialize index number $i  to zero;
 for (each element  in $transformList)
 {
 if (string Array $cleanList doesn't contain $each)
 {
 insert $each at position $i of $cleanList
 increment the index value $i by 1
 }
 }
 print the $cleanList
 }

The command stringArrayContains() checks a string exists in a string Array or not and returns true of false. stringArrayInsertAtIndex() will insert a string in to the index position position in the string Array. Both the command names are self-explanatory.

Advertisements

5 thoughts on “Array manipulation in MEL – Part 1

  1. Ekalavya

    Sorry for asking something silly.
    After running the procedure, if I want to call it with joints, how should it be done?
    I tried : ak_listObjectsOfType(joint).
    But I got
    Error: Invalid use of Maya object “joint”.

    Reply
    1. anoop ak Post author

      ak_listObjectsOfType(“joint”). There is nothing silly when it comes to learning things. Solid foundation on the basics is what matters in the long run. Feel free to come up with your queries

      Reply
  2. anoop ak

    ak_listObjectsOfType(“joint”).You got an error since the argument is of type string.Typing just joint without quotes make maya treat in like an object.
    There is nothing silly when it comes to learning things. Solid foundation on the basics is what matters in the long run. Feel free to come up with your queries

    Reply
  3. Pingback: Array manipulation in MEL – Part 2 | CODE-CG BLOG

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s