Copy file texture from a selected object


#1

Hi everyone !

I need to copy all my png texture from a shader into another folder. For example, I have a ‘Scratched_Metal’ shader created in substance painter with many maps (Base Color, Metalness, Roughness, etc…) and I want to create a script who copy all my textures file into another folder. So I created this script :

[i]import maya.cmds as mc
import shutil
dst = “C:\Users\bussi\Documents\maya\2017\prefs\scripts\HShaders_script\Icons\Shaders\METAL”
sel = cmds.ls(sl=1, dag=True, type=‘mesh’)[0]
fileList = mc.ls(type=‘file’)
for i in cmds.listConnections(sel, c=1, type=‘shadingEngine’):
for j in cmds.listConnections(i, d=1):
for f in fileList:

        fileName = mc.getAttr(f + '.fileTextureName')
        shutil.copy(fileName, dst)[/i]

But the problem in this script is that I copy every file image in my scene, and I want only the file texture on a selected object. I mean that I want to copy all the texture image of the shader of a selected mesh. And I don’t really know how to do that… Can you help me please ? Thank you !


#2

You would have to get all the connections of the mesh that are shadingEngine (SG), then get all the connections of that SG and filter the materials, then get all the file type nodes from that material

from maya import cmds
objs = cmds.ls(sl = True, mesh=1)
textures = []
for obj in objs:
    mesh = cmds.listRelatives(obj, type='shape', fullPath=1)[0]
    sgs = cmds.listConnections(mesh, type='shadingEngine')
    sgs = list(set(sgs))
    for sg in sgs:
        mats = cmds.ls(cmds.listConnections(sg), mat=True)
        for mat in mats:
            for t in cmds.ls(cmds.listConnections(mat), type='file'):
                textures.append( t )
for f in textures:
    print cmds.getAttr(f +'.fileTextureName')