Maya ID Pass MEL Script

I just wrote a MEL script to quickly assign ID Passes to a selection of 8 objects.
Just select the objects in your scene and run the script!.

The script creates the ID Pass surfaceShaders and names them accordingly. This technique supports up to a maximum of 8 objects at a time because it uses the standard ID Pass colours – Red Green Blue Magenta Yellow Cyan Black White. If you do not wish to use the black and white colours, you may choose to select only 6 objects at once to run the script. Hope some may find this useful! cheers!

//==============
//ID Pass Assign
//==============
//  Function
//    1. Creates and assigns ID Pass Surface Shaders to selected objects
//  How to use
//    1. Select objects and run the script
//  Log Date / Author
//    1. Released 23 FEB 2015 / Ronald Fong
//==============
global proc rf_idPassAssign()
{
    //Declare variables
    $obj = `ls -sl`;
    $objCount = size($obj);
    
    //Create ID Pass Shaders
    if ( !`objExists idPass1B_MAT`) {
        shadingNode -asShader surfaceShader -n idPass1B_MAT;
        setAttr "idPass1B_MAT.outColor" -type double3 0 0 1 ;
        shadingNode -asShader surfaceShader -n idPass2R_MAT;
        setAttr "idPass2R_MAT.outColor" -type double3 1 0 0 ;
        shadingNode -asShader surfaceShader -n idPass3G_MAT;
        setAttr "idPass3G_MAT.outColor" -type double3 0 1 0 ;
        shadingNode -asShader surfaceShader -n idPass4Y_MAT;
        setAttr "idPass4Y_MAT.outColor" -type double3 1 1 0 ;
        shadingNode -asShader surfaceShader -n idPass5M_MAT;
        setAttr "idPass5M_MAT.outColor" -type double3 1 0 1 ;
        shadingNode -asShader surfaceShader -n idPass6C_MAT;
        setAttr "idPass6C_MAT.outColor" -type double3 0 1 1 ;
        shadingNode -asShader surfaceShader -n idPass7BLK_MAT;
        setAttr "idPass7BLK_MAT.outColor" -type double3 0 0 0 ;
        shadingNode -asShader surfaceShader -n idPass8WHT_MAT;
        setAttr "idPass8WHT_MAT.outColor" -type double3 1 1 1 ;
    }
    
    else
        print "\nID Pass Shaders exists!";
        
    //Assign Shaders to Selection
    string $id[] = `ls "idPass*_MAT"`;
    if ($objCount > 8 || $objCount == 0) {
        print " Please select a minimum of 1 object or a maximum of 8 objects at once!\n";
        }
        
    else  {  
        for ( $i=0; $i<$objCount; ++$i ){
            select -r $obj[$i];
            hyperShade -assign $id[$i];
        }    
        select -r $obj;
        print "\n8 ID Pass Colors assigned to selection!\n";
    }
}
rf_idPassAssign();

Featured on TeeDojo

I was interviewed and featured on TeeDojo. I shared my views on growing as an animator/artist in Singapore.
View the full article here: http://teedojo.com/blog/magical-world-art-animation-with-ronald-fong/

Here’s a shoutout to TeeDojo!
TeeDojo gives you a free platform to raise money and awareness through selling apparel.

Ronald Fong featured on TeeDojo