Maya API 2.0 get vertices/faces faster than converting selection?

Hey guys!

Could somebody help me?

There is a way to get vertices of a face in API, faster than converting selection with polyListComponentConversion?

Thanks a lot!

There might be, but it depends on your use-case.
Often for problems like this, you can pre-compute the whole list, but that may take some extra time. But once that list is computed, everything after that is lightning fast… but only for one mesh.

So if you’re doing lots of conversion for one mesh, you could do that pre-computation like this:

def buildFaceVertList(fnMesh):
    """ Group vertex indices by face

        fnMesh (maya.api.OpenMaya.MFnMesh): The Mesh function set to read the data from

        (list): A list of lists of vertex indices per face
    cursor, out = 0, []

    counts, idxs = fnMesh.getVertices()
    # `counts` contains the number of vertexes per face
    # `idxs` is a list of all indices for all faces

    # So if counts was [4, 3, 5], the first 4 indices in idxs
    # would be the verts for the first face. The next 3 indices
    # in idxs would be the verts for the second face. The next 5
    # would belong to the third face.

    for i, count in enumerate(counts):
        out.append(idxs[cursor: cursor + count])
        cursor += count
    return out

If that’s not your use-case, then we’ll need more information to help you.

Hi @tfox_TD!
thanks a lot for replying!
I will try your code in the task I need to do, so I get back to you to tell if it worked for me!

thanks a lot!!!

@jvvrf - Not sure if you want the triangle face vertex id’s - this is a potential approach using the .getTriangles MFnMesh function - as a polygon can be comprised of multiple triangles, we first get the triangle count for each polygon and then iterate the vertex id’s of each triangle updating the count as we go. Finally we look up into this list returning the vertex ids of the face indices we’re querying. As @tfox_TD mentioned - this should probably be split into a two functions: One that gets all the data and another that does the look ups. Much much faster to cache the data first then do multiple queries on it

def getFaceIds(mFunc, ids):

  """Get face ids.

  :param MfnMesh mFunc: Mesh function.
  :param list[int] ids: Face ids.
  :return: A list of vertex face vertex ids.
  :rtype: list[list]

  pFaces, vIds = MFnMesh.getTriangles()

  faces = []
  count = -1
  for i in pFaces:
    for j in range(i):
      vertices = []
      for k in range(3):
        count += 1
  return [faces[n] for n in ids]

Theres also MItMeshFaceVertex

If you have the faceID you can setIndex(faceID) -> iterate over the vertex of the face, extract the vertID and the position data I think.

1 Like

hey @chalk!
thanks a lot for these great informations, I understood your explanation, I will try your example, and I get back to you with the result!

thank u very much!

thanks for this tip @dive! I will look for that too!

cheers! :slight_smile:

Hey @tfox_TD,

It is really amazing, man. It tooks only 0.047 sec in 100K faces. Thank you very much, this is exactly what I need! :slight_smile:


hey @chalk!
I tried your code, it returned: <generator object at 0x0000023E78B8F3F0>
Not sure if I did something wrong because I couldn´t get the ids, but the suggestion from @tfox_TD fitted perfect for me.
Thanks a lot for your time and your help! :slight_smile: