从Maya python脚本获取纹理文件名?

时间:2017-03-07 21:31:34

标签: python maya

我正在编写一个python脚本,将一个场景输出到一个简单易读的文件中。

我已成功输出位置,旋转,比例和网格名称,但如何获取应用于网格的纹理的文件名?

import maya.cmds as cmds
meshesWithoutShape = []
meshes = cmds.ls("mesh_*")
for mesh in meshes:
    if("Shape" not in mesh):
        meshesWithoutShape.append(mesh)

shapesInSel = cmds.ls(dag=1,o=1,s=1)
shadingGrps = cmds.listConnections(shapesInSel,type='shadingEngine')
shaders = cmds.ls(cmds.listConnections(shadingGrps),materials=1)
fileNode = cmds.listConnections('%s.color' % (shaders[0]), type='file')
currentFile = cmds.getAttr("%s.fileTextureName" % fileNode[0])

for mesh in meshesWithoutShape:
    print("\n" + mesh.rstrip('1234567890'))

    print round(cmds.getAttr("%s.translateX" % mesh), 2), round(cmds.getAttr("%s.translateY" % mesh), 2), round(cmds.getAttr("%s.translateZ" % mesh), 2)

    print cmds.getAttr("%s.rotateX" % mesh), cmds.getAttr("%s.rotateY" % mesh), cmds.getAttr("%s.rotateZ" % mesh)

    print cmds.getAttr("%s.scaleX" % mesh), cmds.getAttr("%s.scaleY" % mesh), cmds.getAttr("%s.scaleZ" % mesh)

    fileNode = cmds.listConnections('%s.color' % (shaders[0]), type='file')
    currentFile = cmds.getAttr("%s.fileTextureName" % fileNode[0])
    print currentFile

1 个答案:

答案 0 :(得分:5)

您可以使用ls列出所有文件节点并获取纹理路径吗?

allFileNodes = cmds.ls(et="file")
for eachFile in allFileNodes:
    currentFile = cmds.getAttr("%s.fileTextureName" % eachFile)

如果您想从选择或特定网格中获取

shapesInSel = cmds.ls(dag=1,o=1,s=1,sl=1)
shadingGrps = cmds.listConnections(shapesInSel,type='shadingEngine')
shaders = cmds.ls(cmds.listConnections(shadingGrps),materials=1)
fileNode = cmds.listConnections('%s.color' % (shaders[0]), type='file')
currentFile = cmds.getAttr("%s.fileTextureName" % fileNode[0])

<强>更新

以下是您希望满足需求的代码

import maya.cmds as cmds
meshesWithoutShape = []
meshes = cmds.ls("mesh_*", tr = True)

for mesh in meshes:
    print("\n" + mesh.rstrip('1234567890'))

    print round(cmds.getAttr("%s.translateX" % mesh), 2), round(cmds.getAttr("%s.translateY" % mesh), 2), round(cmds.getAttr("%s.translateZ" % mesh), 2)

    print cmds.getAttr("%s.rotateX" % mesh), cmds.getAttr("%s.rotateY" % mesh), cmds.getAttr("%s.rotateZ" % mesh)

    print cmds.getAttr("%s.scaleX" % mesh), cmds.getAttr("%s.scaleY" % mesh), cmds.getAttr("%s.scaleZ" % mesh)
    shadingGrps = cmds.listConnections(mesh,type='shadingEngine')
    shaders = cmds.ls(cmds.listConnections(shadingGrps),materials=1)
    fileNode = cmds.listConnections('%s.color' % (shaders[0]), type='file')
    currentFile = cmds.getAttr("%s.fileTextureName" % fileNode[0])
    print currentFile