从文件名路径列表中仅提取maya文件中使用的纹理文件名 [英] extracting only texture filenames being used in maya file from the list of filename paths

查看:94
本文介绍了从文件名路径列表中仅提取maya文件中使用的纹理文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来列出Maya文件中使用的纹理文件.当我运行代码时,我会得到带有其路径的纹理文件列表.但是我只需要一个文件名列表,而我正在尝试提取它们,但没有按我的预期获得该列表.

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 

执行时,我会得到相同的path_list列表.可能是什么问题呢?

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

推荐答案

您将要使用os.path.basename(),如下所示:

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)

这篇关于从文件名路径列表中仅提取maya文件中使用的纹理文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆