为maya创建自己的.obj导出器

时间:2015-06-03 11:07:05

标签: python mesh maya pymel

我正在为maya创建我自己的.obj导出器。

当我只导出一个网格时,我的代码工作得很好,但是当导出多个网格/对象时,它无法创建完整的网格。

我很确定问题出在我得到的时候 face.getVertices(),face.getUVIndex()和face.normalIndex()并将它们打印到文件中。正如我所说,第一个网格工作正常,但当它到达第二个网格时,编码完全错误,它们连接到错误的三角形。 如果有人对如何以不同方式循环它们或将值更改为正确值有任何想法,那将永远是伟大的。非常感谢帮助!

这是一个关于多目标网格如何结束的示例。 http://postimg.org/image/rr0fvs0v7/

import pymel.core as pm
import pymel.core.nodetypes as nt

planes = pm.ls(sl=True)

def meshFile():    
    def myRound(n):
        return round(n, 6)

    file = open("C:/Users/Blondiegirls/Desktop/test2.obj", "wb")
    file.write("mtllib test2.mtl\r\n")
    for p in planes[:]:
        #pm.polyTriangulate(planes[0])
        file.write("\r\ng default")

        # Printa world kordinater
        for index, point in enumerate(p.vtx):
            temp = index,map(myRound, point.getPosition(space='world'))

            file.write("\r\nv ")
            file.write(str(' '.join(map(str, temp[1]))))

        # Printa texture kordinater
        mesh = pm.ls(g=True)[0]
        U,V = mesh.getUVs()
        UVs = zip(U,V)
        for uv in UVs:
            file.write("\r\nvt ")
            file.write(str(uv[0])+" "+str(uv[1]))

        #printa normals
        for n in p.getNormals():
            file.write("\r\nvn ")
            file.write(str(n[0])+" "+str(n[1])+" "+str(n[2]))

        file.write("\r\ns 1")    
        file.write("\r\ng ")
        file.write(str(p))
        file.write("\r\nusemtl test")
        for faceIndex, face in enumerate(p.faces):
            faceVertices = face.getVertices()
            faceUV0 = face.getUVIndex(0)+1
            faceUV1 = face.getUVIndex(1)+1
            faceUV2 = face.getUVIndex(2)+1

            faceNor0 = face.normalIndex(0)+1
            faceNor1 = face.normalIndex(1)+1
            faceNor2 = face.normalIndex(2)+1


            file.write("\r\nf ")
            faceVertices0 = int(faceVertices[0])+1
            faceVertices1 = int(faceVertices[1])+1
            faceVertices2 = int(faceVertices[2])+1
            temp3 = (str(faceVertices0)) + "/" + (str(faceUV0)) +"/" + (str(faceNor0)) + " " + (str(faceVertices1)) + "/" + (str(faceUV1)) +"/" + (str(faceNor1)) + " " + (str(faceVertices2)) + "/" + (str(faceUV2)) +"/" + (str(faceNor2))         
            file.write(str(temp3))
    file.close()

meshFile()   


def MTLFile():
    file2 = open("C:/Users/Blondiegirls/Desktop/test2.mtl", "wb")

    object = cmds.ls(sl=1)[0].split(':')[0]
    #print('object: '+object)

    shipTX = pm.PyNode(object)
    shadingGroups = shipTX.shadingGroups()
    sg1 = shadingGroups[0]

    material = sg1.listConnections(source=True, destination=False, type=nt.Lambert)[0]
    file = material.color.listConnections(type=nt.File)[0]
    filename = file.fileTextureName.get()

    materialColor = material.getColor() #for Kd
    materialAmbient = material.getAmbientColor() #for Ka
    materialSpecular = material.getSpecularColor() #for Ks
    refractiveIndex = material.getRefractiveIndex() #for Ni

    file2.write("newmtl "+"test"+"\r\n")
    file2.write("Ka "+str(materialAmbient[0])+" "+str(materialAmbient[1])+" "+str(materialAmbient[2])+"\r\n")
    file2.write("Kd "+str(materialColor[0])+" "+str(materialColor[1])+" "+str(materialColor[2])+"\r\n")
    file2.write("Ks "+str(materialSpecular[0])+" "+str(materialSpecular[1])+" "+str(materialSpecular[2])+"\r\n")

    file2.write("d 1.0\r\n")
    file2.write("Illum 2\r\n")
    file2.write("map_Kd "+filename+"\r\n") #for map_Kd



    file2.close()

MTLFile()

1 个答案:

答案 0 :(得分:0)

问题在于这一行:

mesh = pm.ls(g=True)[0]
U,V = mesh.getUVs()

这是查询场景中的所有几何体并返回第一个对象,但是您应该在迭代中对当前网格进行操作。我想你想要的是:

U,V = p.getUVs()

此外,您应该考虑向meshFile()添加一个参数,而不是依赖于全局范围内的变量。