如何在maya中创建.obj和.mtl导出器

时间:2017-05-24 23:37:30

标签: scripting maya .obj wavefront pymel

我在学校有一个任务是在maya中创建一个自定义的.obj和.mtl导出器,但文档很难理解和使用。

找到一个看起来像这样的博客帖子,代码可以工作,但我想知道更多关于它实际做了什么以及将来处理这类问题的提示。

import pymel.core as pm

def material():
    file2 = open("C:/Users/MyName/Desktop/test.mtl", "wb")
    textureName=""
    object = pm.ls(sl=1)[0].split(':')[0]

    selection = pm.ls(sl=1)
    for each in selection:
        object=pm.PyNode(each)
        shadingGroups = object.shadingGroups()
        print("SG "+str(shadingGroups))

        for shadingGroup in shadingGroups:
            material=shadingGroup.listConnections(source=True, destination=False, type=nt.Lambert)[0]
            print("Mat "+str(material))
            texture = material.color.listConnections(type=nt.File)[0]
            textureName=texture.fileTextureName.get()
            print("Texture "+str(textureName))

            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 " + textureName + "\r\n")  # for map_Kd

    file2.close()

1 个答案:

答案 0 :(得分:1)

既然是作业,我最好提供一些文档而不是代码吗? ;)..这是来自maya的自定义文件转换器的documentation。示例翻译可以找到here,更深一些example