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

半世苍凉 提交于 2019-12-11 07:32:18

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/20089647/extracting-only-texture-filenames-being-used-in-maya-file-from-the-list-of-filen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!