Maya MEL Scripting: Episode 02

Tip nr. 4

An easy way to fix the shape’s name of the selected objects.
This can be really useful if you got objects from incorrectly formatted alembic files.

{

    // Get the slected object's name
    string $sel[] = `ls -sl`;
    
    // A for cycle
    for ($item in $sel)
    {
        // Select the object
        select $item;
        
        // The pickWalk command is a simple way to move up and down (and with that select) in the hierarchy
        pickWalk -d down;
        
        // Get the name of the newly selected shape
        string $selShape[] = `ls -sl`;
        
        // The rename command is super simple to use: 
        // Fist part is the name of the node you want to rename the second is the new name.
        rename $selShape[0] ($item+"Shape");
    }
    
    // After the renaming process reselect the original selection. It's better this way. The user won't lost focus.
    select $sel;
}

Tip nr. 5

An easy way to reorder the selected nodes alphabetically in the Outliner.
This uses the built-in simple sorter (the order will be 1, 2, 21, 3, 35, 4 etc.).

{
    string $sel[] = `ls -sl`;
    // print $sel;

    string $afterSorting[] = sort($sel);
    // print $afterSorting;

    for ($each in $afterSorting)
    {
        reorder -back $each;
    }
}

Tip nr. 6

Change numbered suffix to Alphabetic one (simple version), using the order of the selection.

{
    string $ddAlphabet[] = stringToStringArray("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z", " ");
    string $sel[] = `ls -sl`;
    int $selSize =`size($sel)`;
    //print $selSize;
    
    for ($i=0; $i<$selSize; $i++)
    {
        string $tempName = `match ".*[^0-9]" $sel[$i]`;
        rename $sel[$i] ($tempName + $ddAlphabet[$i]);
    }
}

Tip nr. 7

Get the name of the user’s Maya Temp directory. You can use this folder to export/import temporary files.

{
string $tmpDir = `internalVar -userTmpDir`;
print $tmpDir;
}

For example:

Tip nr. 8

Export selection as obj and import back using the always existing userTmpDir folder. This is a widely used method to fix object issues in Maya.
Of course, it’s best to use the usual for() cycle to run this on every selected object one by one.
The basic file command is used to export and import the objects.
This code (as all 🙂 ) could be more efficient and elegant of course.

{
    string $sel[] = `ls -sl`;
    string $tmpDir = `internalVar -userTmpDir`;
    print $tmpDir;
    
    print $sel;
    for ($item in $sel)
    {
        // Select Object
        select $item;
        
        // Export Object
        file -force -options "groups=0;ptgroups=0;materials=0;smoothing=0;normals=1" -typ "OBJexport" -pr -es ( $tmpDir + "/tempOBJ.obj");
        
        // Import Temp OBJ file
        file -import 
        -type "OBJ"  
        -ignoreVersion 
        -ra true 
        -mergeNamespacesOnClash false 
        //-namespace "tempOBJ" 
        //-options "mo=1"  
        -pr  
        -importFrameRate false  
        -importTimeRange "override" 
        ( $tmpDir + "/tempOBJ.obj" );
        select tempOBJ_Mesh;
        rename ($item + "_fixed");
    }
}

Tip nr. 9

You can easily change the Selection Priority via scripts.
For example:

{
    selectPriority -light 100;
    selectPriority -locator 100;
    selectPriority -camera 95;
    selectPriority -polymesh 100;
    selectPriority -handle 100;
    selectPriority -cluster 150;
    selectPriority -curve 150;
    
    // Conponent priorites
    selectPriority -polymeshEdge 101;
    selectPriority -polymeshFace 102;
    selectPriority -polymeshUV 103;
    selectPriority -polymeshVertex 103;
    selectPriority -polymeshVtxFace 103;
}

Don’t worry. You can restore this to the default values at the Preferences window -> Selection section.

That’s it for today.

Cheers, D

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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.