Maya 2020 fundamentals - modelling the real world
Get halfway through a model and find it's an unworkable mess? Can't add edge loops where you need them? Can't subdivide a mesh properly? If any of this sounds familiar check this course out.
# 1 15-03-2013 , 09:48 AM
Falott's Avatar
Registered User
Join Date: Jan 2005
Location: vienna
Posts: 1,095

apply shader to all object instances

this might be a simple task but I didn´t find any working solution yet - please help!

task: select lots of objects and all their instances via script. then apply a shader.

what I have so far:

Code:
if (objExists("geo_test" + "*"))
    {
    select -hi ("geo_test" + "*");      
    string $sel[] = `ls -sl -ap`;
        for ($item in $sel)
            {
            sets -e -forceElement testSG;
            }
    select -cl  ;
    };

unfortunately I can´t figure out how to select all the instances too. only the parents get shaded :/


everything starts and ends in the right place at the right time.
# 2 18-03-2013 , 06:14 PM
NextDesign's Avatar
Technical Director
Join Date: Feb 2004
Posts: 2,988
Can you post a quick test file?


Imagination is more important than knowledge.
# 3 19-03-2013 , 08:16 AM
Falott's Avatar
Registered User
Join Date: Jan 2005
Location: vienna
Posts: 1,095
hi mate! thx for your interest.

you´ll find an empty readME groupNode included with some lines of mel code in the Notes section.

Attached Files
File Type: zip instanceShade.zip (21.9 KB, 170 views)

everything starts and ends in the right place at the right time.
# 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.
# 5 26-03-2013 , 07:16 AM
Falott's Avatar
Registered User
Join Date: Jan 2005
Location: vienna
Posts: 1,095
Wahnsinn! Es funktioniert user added image

Thank you, thank you, thank you.

I have to look at the code real closely right now and learn from it.


everything starts and ends in the right place at the right time.
Posting Rules Forum Rules
You may not post new threads | You may not post replies | You may not post attachments | You may not edit your posts | BB code is On | Smilies are On | [IMG] code is On | HTML code is Off

Similar Threads