Filter selection by material type (lambert or phong)


#1

Hello, im new in Maya Mel programming.
I have a question that i have not found the answer after wandering around.

Let say i have a lot of object, each has it’s own material with different names and types(lambert, phong, redshift)
i need to select some of the object and get only the lambert materials

i’ve tried some code from this forum like:

string $shaders[] = stringArrayRemoveDuplicates(ls("-mat" ,listConnections(listConnections("-type","shadingEngine",`ls -sl -o -dag -s`))));
print $shaders;

but, the problem with this code is that, it includes all the material type (lambert, phong) from the selection(i only need the lambert type).
what im looking for is a code like this but only from my selection of objects;:

string $shaders[] = `ls -type "lambert"`;
print $shaders;

(above code get all the lambert material from the scene)
i dont know how to filter it based on my selection, so can u guys help me?
thanks


#2

Finally i found the answer,

//select all material from object selection
string $shaders[] = stringArrayRemoveDuplicates(ls("-mat" ,listConnections(listConnections("-type","shadingEngine",`ls -sl -o -dag -s`))));
string $lamberts[];

//Filter only to Lambert based on selection
for ($i =0; $i < size ($shaders); $i++) {
    if ( `objectType $shaders[$i]` == "lambert"){
         print "Lambert code goes here";  
    }
    else{
        print "Else goes here";       
    }
    
}