Hamaro
Hamaro

Reputation: 49

How to create .obj and .mtl exporter in maya

I have a task in school to create an custom .obj and .mtl exporter in maya but the documentation is really hard to understand and use.

Found a blogpost that looked about like this, the code works but i would like to know more about what it actually does and tips for handling problems like this in the future.

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()

Upvotes: 0

Views: 1006

Answers (1)

Achayan
Achayan

Reputation: 5885

Since its a homework i would better give some documentation rather than code right ? ;) .. Here is a documentation for custom file translator from maya. And example translator can find here, some more deep example .

Upvotes: 1

Related Questions