大家好,我是 python 初学者,我的代码有问题,我想导入并读取特定文件夹中的所有 .BVH 文件,但程序只获取该文件夹中的第一个文件。这是我的代码.我使用 blender 进行可视化。
import bpy # This module gives access to blender data, classes, and functions
import os # This module provides a unified interface to a number of operating system functions.
import sys # This module provides a number of functions and variables that can be used to manipulate different parts of the Python runtime environment.
path = "C:\\Users\\PC\\Desktop\\Rotate Prototype\\filtered"
dir = os.listdir("C:\\Users\\PC\\Desktop\\Rotate Prototype\\filtered")
files = 0
for files in dir:
if files.lower().endswith('.bvh'):
try:
bpy.ops.object.delete() # Deletes the cube
bpy.ops.import_anim.bvh(filepath="C:\\Users\\PC\\Desktop\\Rotate Prototype\\filtered\\pick_001_3_fil_Take_001.bvh", axis_forward='-Z', axis_up='Y', filter_glob="*.bvh", target='ARMATURE', global_scale=1.0, frame_start=1, use_fps_scale=False, update_scene_fps=False, update_scene_duration=False, use_cyclic=False, rotate_mode='NATIVE') # We import a bvh file with the appropriate settings
bpy.context.scene.render.fps = 72 # We configure the frame rate
bpy.ops.export_anim.bvh(filepath="C:\\Users\\PC\\Desktop\\Rotate Prototype\\trolled\\haha.bvh", check_existing=True, filter_glob="*.bvh", global_scale=1.0, frame_start=1, frame_end=1515, rotate_mode='XYZ', root_transform_only=True) # We export the file with the appropriate settings
except:
print ("Couldn't open file")
files++
最佳答案
您没有在 for 循环中使用实际文件。您每次只是使用相同的硬编码路径。
也许你想要像下面这样的东西?
我将 files
重命名为 file_path
以更好地表示该变量中的内容。然后我在对 import_anim.bvh
的调用中使用该值,然后在对 export_anim.bvh
的调用中再次使用它。 (我在文件名末尾添加了 "_exported.bvh"
。我不太确定您要做什么。)
for file_path in dir:
if file_path.lower().endswith('.bvh'):
try:
bpy.ops.object.delete() # Deletes the cube
# We import a bvh file with the appropriate settings
bpy.ops.import_anim.bvh(filepath=file_path,
axis_forward='-Z', axis_up='Y', filter_glob="*.bvh",
target='ARMATURE', global_scale=1.0, frame_start=1,
use_fps_scale=False, update_scene_fps=False,
update_scene_duration=False, use_cyclic=False,
rotate_mode='NATIVE')
bpy.context.scene.render.fps = 72 # We configure the frame rate
# We export the file with the appropriate settings
bpy.ops.export_anim.bvh(
filepath=file_path + '_exported.bvh',
check_existing=True, filter_glob="*.bvh",
global_scale=1.0, frame_start=1, frame_end=1515,
rotate_mode='XYZ', root_transform_only=True)
except:
print ("Couldn't open file")
关于python - 导入并读取Python文件夹中的所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37802712/