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!
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
Args:
fnMesh (maya.api.OpenMaya.MFnMesh): The Mesh function set to read the data from
Returns:
(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
vertices.append(
vIds[count])
faces.append(vertices)
return [faces[n] for n in ids]
Theres also MItMeshFaceVertex
https://help.autodesk.com/view/MAYAUL/2020/ENU/?guid=__py_ref_class_open_maya_1_1_m_it_mesh_face_vertex_html
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.
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!
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!
cheers!!