为5月创建自己的.obj导出程序

2024-10-01 19:26:27 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

下面是一个多对象网格如何结束的示例。 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()

Tags: in网格mapforobjectfile2filewrite
1条回答
网友
1楼 · 发布于 2024-10-01 19:26:27

问题是这条线:

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

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

^{pr2}$

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

相关问题 更多 >

    热门问题