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

Get texture filename from Maya python script?

我正在编写一个 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

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

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

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

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