Search code examples
pythonmayamel

List all external assets in maya


Is it possible in maya to list all external assets being used in a file such as textures or other scenes being referenced?

Ideally this would be done using python. I'm more familiar with 3ds max, so Im not sure if maya has the equivalent of 3ds max where you can simply do this by the following...

Collect all assets of class type texturemap, which returns filepaths. Then collect all assets of class type xref, which returns referenced scenes and that's it!


Solution

  • You can list all referenced textures in the scene and then use : cmds.ls("REF_NAME:*", type="file")

    To query all references : cmds.file(q=1,r=1)

    ---EDIT--- I had time to write a full code example :

    import maya.cmds as cmds
    import glob
    
    files = cmds.ls(type=["file", "imagePlane"])
    result = []
    for i in files:
        if cmds.objectType(i) == "file":
            #animated ?
            testAnimated = cmds.getAttr("{0}.useFrameExtension".format(i))
            if testAnimated:
                # Find the path
                fullpath= cmds.getAttr("{0}.fileTextureName".format(i))
    
                # Replace /path/img.padding.ext by /path/img.*.ext
                image = fullpath.split("/")[-1]
                imagePattern = image.split(".")
                imagePattern[1] = "*"
                imagePattern = ".".join(imagePattern)
    
                # You could have done a REGEX with re module with a pattern name.padding.ext
                # We join the path with \\ in order to be Linux/Windows/Apple format
                folderPath = "\\".join(fullpath.split("/")[:-1] + [imagePattern])
    
                # Find all image on disk
                result+=(glob.glob(folderPath))
            else:
                result.append(cmds.getAttr("{0}.fileTextureName".format(i)))
        elif cmds.objectType(i) == "imagePlane":
            #animated ?
            testAnimated = cmds.getAttr("{0}.useFrameExtension".format(i))
            if testAnimated:
                # Find the path
                fullpath= cmds.getAttr("{0}.imageName".format(i))
                # Replace /path/img.padding.ext by /path/img.*.ext
                image = fullpath.split("/")[-1]
                imagePattern = image.split(".")
                imagePattern[1] = "*"
                imagePattern = ".".join(imagePattern)
    
                # You could have done a REGEX with re module with a pattern name.padding.ext
                # We join the path with \\ in order to be Linux/Windows/Apple format
                folderPath = "\\".join(fullpath.split("/")[:-1] + [imagePattern])
    
                # Find all image on disk
                result+=(glob.glob(folderPath))
            else:
                result.append(cmds.getAttr("{0}.imageName".format(i)))
    #clear multiple instance
    result = list(set(result))