I am trying to figure out how to build an array with faces that have a particular material assigned to it. I have found bit of info here and there, and this is what i came up with.
//need to make a selection of all the faces
string $matchingFaces[]; //this will be used later to select faces
ConvertSelectionToFaces; // just select all the faces of the mesh
string $allFaces[] = `ls -psh`; // Query all face component
//iterate through each face, and see if it materials name matches
string $fakeMat = "texB"; //eventually, will use proc to fetch a name of the material
for($i in $allFaces){
string $curMat[] = `hyperShade -smn`; //Check what material is currently assigned to the face
if($curMat[0] == $fakeMat){ //if the materials matches, then create an array for selection later on
print("we found faces that have the " + $fakeMat + "applied.");
$matchingFaces[size($matchingFaces)] = $i; //append the faces to an array so we can select them later
}
}
select -r $matchingFaces; //select the faces;
Replies
https://help.autodesk.com/cloudhelp/2017/ENU/Maya-Tech-Docs/Commands/hyperShade.html
for example this:
returns a string with everything that has $myMaterial applied to it.
import maya.cmds as mc
# select object or face
b = mc.ls(sl=1)[0].split("[")[0] # remember a token for comparison below
mc.hyperShade(smn=1) # that will select the shader
s = mc.ls(sl=1)[0] # remember the selected shader
sg = mc.listConnections(s+".oc", s=0, d=1)[0] # figure out the shading group
# select the faces of the same object with same shader attached
l = []
for o in mc.sets(sg, q=1):
if b not in o: continue
l.append(o)
mc.select(l)
Or do it the way oglu suggested ¯\_(ツ)_/¯ python has a lot more comprehensive ways to work with lists of things.