View Single Post
# 4 19-03-2013 , 03:55 PM
NextDesign's Avatar
Technical Director
Join Date: Feb 2004
Posts: 2,988
Here you go user added image

Code:
// expandSelection found here: https://www.mayzie.net/maya-scripts/apply-shaders-to-all-maya-instances
global proc string[] expandSelection() 
{
    string $selection[] = `ls -sl`;
    string $hl[] = `ls -hl`;
    string $ret[];
    $hl = `listRelatives -shapes $hl`;
    
    $hl = listRelatives("-parent", "-f", `ls -ap $hl`);
    
    for ($item in $selection)
    {
        $match = `match "\.[^.]*$" $item`;
        
        for ($item in $hl)
            $ret[size($ret)]=($item+$match);
    }
  
    return $ret;
}

global proc assignShaderToInstance(string $nameFilter, string $SGName)
{
    if (objExists($nameFilter))
    {
        select -hi ($nameFilter);      
        string $sel[] = `ls -sl -g`;
        
        for ($obj in $sel)
        {
            int $nFaces[] = `polyEvaluate -f`;
            
            changeSelectMode -component;
            
            select -r ($obj + ".f[0:" + $nFaces[0] + "]");
    
            select -r (expandSelection());
            
            sets -e -forceElement $SGName;
            
            changeSelectMode -object;
        }
        
        select -cl;
    }
}

assignShaderToInstance("geo_Cube*", "SG_Cube");
assignShaderToInstance("geo_Sphere*", "SG_Sphere");
assignShaderToInstance("geo_Cone*", "SG_Cube");
assignShaderToInstance("geo_Cylinder*", "SG_Cylinder");


Imagination is more important than knowledge.