使用Unreal Python API处理字符串列表:移除路径重复后缀
Unreal Python API 资源路径去重后缀解决方案
你用unreal.EditorAssetLibrary.list_assets()获取的资源路径会带有重复的对象名后缀(比如/Game/ProjectName/Textures/Utility/Texture1_01a.Texture1_01a),可以用以下两种方法清理路径:
方法一:字符串分割(rsplit)
使用rsplit('.', 1)从末尾开始仅分割一次,取前半部分即可去掉后缀:
import unreal def listAssetPaths(): EAL = unreal.EditorAssetLibrary assetPaths = EAL.list_assets('/Game') # 清理路径后缀 cleaned_paths = [path.rsplit('.', 1)[0] for path in assetPaths] return cleaned_paths # 输出测试 for path in listAssetPaths(): print(path)
注:如果直接用
split('.')会分割路径中所有的.,容易出错,rsplit('.',1)只处理最后一个分隔符,更安全。
方法二:用Unreal内置路径工具(推荐)
Unreal提供了专门的路径处理APIunreal.Paths.get_path(),可以直接返回不带对象名后缀的资源路径,更贴合引擎逻辑:
import unreal def listAssetPaths(): EAL = unreal.EditorAssetLibrary assetPaths = EAL.list_assets('/Game') cleaned_paths = [unreal.Paths.get_path(path) for path in assetPaths] return cleaned_paths
内容的提问来源于stack exchange,提问作者Bachalo
相关产品推荐
相关产品推荐

