How does Maya calculate face center?

How does Maya calculate face center?

jicc
Explorer Explorer
891 Views
6 Replies
Message 1 of 7

How does Maya calculate face center?

jicc
Explorer
Explorer

Hello team,

 

Could you please tell me how Maya calculates the face center of a polygon?

Its in-center, centroid or something else. Thanks.

 

best,

892 Views
6 Replies
Replies (6)
Message 2 of 7

Christoph_Schaedl
Mentor
Mentor

Here is a Bifrost compound doing that. You could dig in to find out.
https://discord.com/channels/872260298508222534/876728994404368446/1010066336212324373

----------------------------------------------------------------
https://linktr.ee/cg_oglu
Message 3 of 7

FirespriteNate
Advocate
Advocate

do you actually need to know mathematically how they are calculating it internally so you can replicate it elsewhere? or do you just need to query that position? The API MItMeshPolygon class has a method to query that, here's some example code to query a single given face:

import maya.api.OpenMaya as om

sList = om.MSelectionList()
sList.add('pPlane1.f[0]')

sIter = om.MItSelectionList(sList, om.MFn.kMeshPolygonComponent)
dagPath, component = sIter.getComponent()
pIter = om.MItMeshPolygon(dagPath, component)
while not pIter.isDone():
    c = pIter.center(space=om.MSpace.kWorld)
    print('Center:', c)
    pIter.next()
0 Likes
Message 4 of 7

jicc
Explorer
Explorer

Thanks for the Bifrost compound. But it is still not the 100% Maya face center. I want to know the algorithm of Maya one. 

0 Likes
Message 5 of 7

jicc
Explorer
Explorer

Thanks. But I want to know the algorithm of Maya native face center.

0 Likes
Message 6 of 7

jmreinhart
Advisor
Advisor

I was able to find the answer on an old Maya Station post. Apparently it does a weighted average of the vertex positions based on the length of adjacent edges. I wrote a snippet of code that does the same calculation:

 

import maya.api.OpenMaya as om2

def getFaceCenter(mesh, polyId):
    # Get the mesh as an MObject
    sel = om2.MSelectionList()
    sel.add(mesh)
    mMesh = sel.getDependNode(0)
    
    # Create some iterators so we can convert
    # from faces/edges/vertices
    mitFace = om2.MItMeshPolygon(mMesh)
    mitVertex = om2.MItMeshVertex(mMesh)
    mitEdge = om2.MItMeshEdge(mMesh)
    
    # Set the iterator to operate on the face we want
    mitFace.setIndex(polyId)
    # Get the vertices of that face
    faceVertices = mitFace.getVertices()
    # Get the edges of that face
    faceEdges = mitFace.getEdges()
    
    # Prepare to record the position of each vertex
    vertexPnts = []
    # and the length of the adjacent edges
    vertexEdgeLengths = []
    
    # For each vertex of the face
    for vertex in faceVertices:
        # Get the position of the vertex
        mitVertex.setIndex(vertex)
        pnt = mitVertex.position()
        # Get the connected edges that border the face
        edges = mitVertex.getConnectedEdges()
        edges = [x for x in edges if x in faceEdges]
        # Sum the length of those edges
        edgeLengths = 0
        for edge in edges:
            mitEdge.setIndex(edge)
            edgeLengths += mitEdge.length()
        
        # Record that info
        vertexPnts.append(pnt)
        vertexEdgeLengths.append(edgeLengths)
                    
    # Convert lengths to weights
    # length of adjacent edges / length of all edges
    sumLengths = sum(vertexEdgeLengths)
    weights = [x/sumLengths for x in vertexEdgeLengths]
    
    centerPoint = om2.MVector()
    for w,p in zip(weights,vertexPnts):
        centerPoint += w*om2.MVector(p)
        
    return centerPoint
Message 7 of 7

jicc
Explorer
Explorer

Hi jmreinhart,

 

Yeah, there is an old post. Thanks. 🙂

0 Likes