Get vertex list and face list in maya

452 Views Asked by At

I am new to maya scripting. I have a .fbx being loaded and for each shape node I need to convert such shape into a .obj file.

Now I've found a way to exctract the vertex coordinates by doing something like

node_name = "testnodeshape"
vtxIndexList = cmds.getAttr(node_name +".vrts", multiIndices=True)
vtxWorldPosition = []
for i in vtxIndexList:
    curPointPosition = cmds.xform( node_name +".pnts["+str(i)+"]", query=True, translation=True, worldSpace=True)
    vtxWorldPosition.append( curPointPosition )

for entry in vtxWorldPosition:
    print('v ' + (' '.join([str(x) for x in entry])))

However I cannot find a way to extract the face list. I've tried to print all the attribute of a shape node to find such a list, there's something called 'edge' and 'face' but when I print it I only see consecutive numbers.

I am also not an expert in navigating the maya documentation and it doesn't seem to help. There's also OpenMaya but I struggle a bit to understand how to use it.

Can you suggest what to do (I need to extract the triplets of vertex index per face).

If there's a quicker way also to do this conversion that's also fine.

Update: I've also found this link https://forums.cgsociety.org/t/how-maya-vertex-indexes-indcies-work/1519043/6

But it doesn't cover how to get the triangles. From maya the documentation there's a getTriangles() method but it doesn't seem leading me to get an equivalent of an obj yet.

Along this line is what I've:

import maya.OpenMaya as om
import maya.cmds as c

meshIt = om.MItDag ( om.MItDag.kBreadthFirst , om.MFn.kMesh)
while not meshIt.isDone ():
    obj = meshIt.currentItem()
    mesh = om.MFnMesh(obj)
    floatArray = om.MFloatPointArray()
    intArrayVc = om.MIntArray()
    intArrayVl = om.MIntArray()
    mesh.getVertices(intArrayVc,intArrayVl)
    mesh.getPoints(floatArray)
    for vl in intArrayVl:
        print "v {} {} {}".format(floatArray[vl].x,floatArray[vl].y,floatArray[vl].z)
    intArrayFc = om.MIntArray( )
    intArrayFl = om.MIntArray( )
    mesh.getTriangles (intArrayFc, intArrayFl)
    numberOfIndices = len(intArrayFl)
    for fl in range(0,numberOfIndices,3):
        print "f {} {} {}".format(intArrayFl[fl],intArrayFl[fl+1],intArrayFl[fl+2])
    break
    meshIt.next()

Update 2 I've found the following lines of script that given a selected shape node can do the export to .obj

import maya.cmds as cmds

cmds.file("D:/TestMesh/tmp/something.obj",pr=1,typ="OBJexport",es=1,
   op="groups=0; ptgroups=0; materials=0; smoothing=0; normals=0")

However what this doesn't do is to convert quads to triangles... Is there a way to do that?

0

There are 0 best solutions below