Home Technical Talk

MEL - selecting faces by material

polycounter lvl 11
Offline / Send Message
raul polycounter lvl 11
Hello, 

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

  • sprunghunt
    Options
    Offline / Send Message
    sprunghunt polycounter
    You don't need any of this - you can just use the hypershade command. 

    https://help.autodesk.com/cloudhelp/2017/ENU/Maya-Tech-Docs/Commands/hyperShade.html

    for example this:

    hyperShade -objects $myMaterial;


    returns a string with everything that has $myMaterial applied to it.

  • raul
    Options
    Offline / Send Message
    raul polycounter lvl 11
    isnt that going to select All the meshes in my scene that use the materials? I just want to have one mesh selected and be able to quickly select faces by material. Thats why i was going the long route. I dont want to have to select a lot other objects and deselct their faces. Just the one i have selected.
  • oglu
    Options
    Offline / Send Message
    oglu polycount lvl 666
    in python


    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)
  • sprunghunt
    Options
    Offline / Send Message
    sprunghunt polycounter
    raul said:
    isnt that going to select All the meshes in my scene that use the materials? I just want to have one mesh selected and be able to quickly select faces by material. Thats why i was going the long route. I dont want to have to select a lot other objects and deselct their faces. Just the one i have selected.
    That command returns a string of references - it doesn't select anything. You could then use Gmatch to get the references to your object. 

    Or do it the way oglu suggested ¯\_(ツ)_/¯ python has a lot more comprehensive ways to work with lists of things.
Sign In or Register to comment.