Use the "hyperShade -shaderNetworksSelectMaterialNodes" to select all selected objects materials, if they have any. Now get the selected shader names with "ls -sl". Quick and dirty.
But you presumably would have your script a bit more comfortable.. So lets deduce one.
- What types of objetcs you want to know their connected shaders? If you do some research in your HypershadeWindow you will see that only the shapes of shadeable objects are connected to the shadingEngines, so first you would have to convert the input object list into their contained shapes.
// Get the objects with "ls". Or put a predefined object list into this string array. And declare an empty string array for the result.
string $theObjects[] = ls -sl
;
string $theMaterials[] = {};
// Now lets get the shapes. We have to assume that the object list will already contain shapes as well
// as transforms/groups. To go for sure and to get all shapes, we use the "ls" command again and modify
// it with some flags.
string $shapeList[] = ls -long -allPaths -dagObjects -shapes $theObjects
;
// now having all shapes, we want to find all the plugged upstream dependency nodes and sort the shadingEngines out.
for($shape in $shapeList)
{
string $depNodes[] = listConnections -destination 1 -plugs 0 $shape
;
// because the result array may contain a single dependency node multiple times we remove all duplicates
$depNodes = stringArrayRemoveDuplicates($depNodes);
// now iterate through all items and check if they are of type shadingEngine
for($node in $depNodes)
{
if(nodeType $node
== "shadingEngine")
{
// if so we now get the downstram connection to the materail node, just as connections showed in the
// HypershadeWindow suggest it and put the result into $theMatrials
string $material[] = listConnections -source 1 -plugs 0 ($node + ".surfaceShader")
;
$theMaterials[size $theMaterials
] = $material[0];
};
};
};
print $theMaterials
// This script isn't very handy, so we should compress it a bit and make a procedure out of it.
proc string[] getAssignedMaterials(string $theObjects[])
{
string $theMaterials[] = {};
for($shape in ls -long -allPaths -dagObjects -shapes $theObjects
)
{
string $depNodes[] = stringArrayRemoveDuplicates(listConnections -destination 1 -plugs 0 $shape
);
for($SG in ls -type "shadingEngine" $depNodes
)
{
string $material[] = listConnections -source 1 -plugs 0 ($SG + ".surfaceShader")
;
$theMatrials[size $theMaterials
] = $material[0];
};
};
return $theMaterials;
};
// example:
string $mats[] = getAssignedMaterials(ls -sl
);