python - 导入并读取Python文件夹中的所有文件

标签 python import blender

大家好,我是 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/

相关文章:

python - 将python代码内列表中的doc计数添加到elasticsearch中的字段

android - 无法解析符号 'support'(使用 Android Studio,遵循入门指南)

python - 如何在不会​​对我的计算机造成危害的环境中导入模块? (Python)

3d - 推荐的游戏内角色模型 3D 模型类型,手动加载?

unity-game-engine - 在 Unity 中的模型上创建动画

python - Blender 可以根据文本文件生成场景吗?

python - Wtforms:如何使用具有动态选择值的选择字段生成空白值

由于缺少 ._ufuncs dll,Python scipy 模块导入错误

python - 一种仅针对频繁值的热编码

r - 为 R 中的多个表指定导入顺序