Array manipulation in MEL – Part 2

As we saw in PART 1, stringArrayContains() and stringArrayInsertAtIndex() are two handy commands that can be used to manipulate arrays. In this Tutorial we will explore few more commands that can be used in the context of strings.

stringArrayRemoveDuplicates(), stringArrayCatenate(), stringArrayToString()

stringArrayRemoveDuplicates() can be used to remove duplicate entries from an array. The code block in PART1 can be simplified to something like this.

 for ($each in $transformList)
 if (stringArrayContains($each,$cleanList)!=1)

can be simplified to something like this

$cleanList = stringArrayRemoveDuplicates($transformList);

stringArrayCatenate() can be used to catenate two arrays together. Suppose we want to add a suffix “_geo” to all geometries in the scene, the following script can be made use of

 global proc ak_addSuffixAllGeo()
 string $polyList[],$nurbsList[];
 $polyList = `ls -type "mesh"`;
 $nurbsList = `ls -type "nurbsSurface"`;
 $allList = stringArrayCatenate($polyList,$nurbsList);
 $transformList = `listRelatives -p $allList`;
 $cleanList = stringArrayRemoveDuplicates($transformList);
 for ($each in $cleanList)
 $newName = $each + "_geo";
 rename $each $newName;

stringArrayToString() can be used to generate a string containing elements of the array separated using a separator string mentioned in the command.

 $myFolderTree = {"My Documents","maya","scripts"};
 $backSlash = "\";
 print (stringArrayToString($myFolderTree,$backSlash));

This would print My Documentsmayascripts” when executed in the Script Editor. This can be handy in cases where you create an UI from which the user selects a list of options which can include a scene name, a scene type (model, tex, rig), scene location etc and saving the file to particular location. You can build an array which includes all these individual user choices and encode it in to a OS recognizable string, which can be used for reading from or writing to the particular file location.


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s