Search code examples
pythonmaya

extracting only texture filenames being used in maya file from the list of filename paths


I have got the following code for listing the texture files being used in my Maya file. When I run the code I get a list of texture files with their paths. But I just need a list of filenames and I'm trying to extract them but not getting the list as I expect.

import maya.cmds as cmds

# Gets all 'file' nodes in maya
fileList = cmds.ls(type='file')

texture_filename_list = []

# For each file node..
for f in fileList:
    # Get the name of the image attached to it
    texture_filename = cmds.getAttr(f + '.fileTextureName')
    texture_filename_list.append(texture_filename)

print texture_filename_list

The output is  [u'D:/IRASProject/RVK/SAFAA/Shared/sourceimages/chars/amir/4k/safaa_amir_clean_body_dif.jpg', u'D:/IRASProject/RVK/SAFAA/Shared/sourceimages/chars/amir/4k/safaa_amir_body_nrlMap.tif', etc]

Now from this list I need to extract the filenames only so I add the code as,

for path in texture_filename_list :
    path.split('/')
    path_list.append(path)    
print path_list 

When I execute then I get the same list for path_list. What could be the problem?


Solution

  • You will want to use os.path.basename(), something like this:

    import os
    import os.path
    
    new_list = []
    
    for each in texture_filename_list:
        file_name = os.path.basename(each)
        new_list.append(file_name)
    

    This will give you something like:

    ['safaa_amir_clean_body_dif.jpg', 'safaa_amir_body_nrlMap.tif']
    

    If you want it without the extension you can change it like this:

    import os
    import os.path
    
    new_list = []
    
    for each in texture_filename_list:
        file_name = os.path.basename(each)
        stripped_file_name = os.path.splitext(file_name)[0]
        new_list.append(stripped_file_name)