【问题标题】:import and read all files from a folder Python从 Python 文件夹中导入和读取所有文件
【发布时间】:2016-10-14 15:17:19
【问题描述】:

大家好,我是 python 初学者,我的代码有问题,我想从特定文件夹导入和读取所有 .BVH 文件,但程序只从文件夹中获取第一个文件。这是我的代码.我使用搅拌机进行可视化。

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++

【问题讨论】:

  • 我不认为 files++ 是有效的 python 代码。
  • 你有什么问题?如果您遇到错误,请在错误中显示您的输出。
  • files++ 应该做什么?如果它计算导入的文件,它应该在 try 子句中缩进。无论如何python不允许++运营商所以使用files += 1
  • 正如所写,看起来您实际上并没有打开/使用for 循环中的任何文件。
  • 这不是错误...问题是代码只读取一个文件,我希望每个 bvh 文件都进行轮换等。关于命令文件 ++ 是的,我也不确定是否正确。

标签: python import blender


【解决方案1】:

您没有在 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")                

【讨论】:

  • FileNotFoundError 等异常应按此处理。避免尝试捕获,其中您不处理错误而只是捕获任何内容。
【解决方案2】:

您正在使用files 在每次迭代中计算和保存当前文件路径。并且在迭代中您没有将当前文件路径输入到import_anim,您只是使用了硬编码的文件路径。 此外,++ 不是有效语法。

files = 0
for file_path in dir:
    if file_path.lower().endswith('.bvh'):
        try:
            bpy.ops.object.delete() # Deletes the cube
            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') # 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=file_path, 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
            files += 1
        except:
            print ("Couldn't open file: {}".format(file_path))

【讨论】:

  • 您确定您使用的是 bvh 函数吗?调试你的代码,打印是你的朋友。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-25
  • 2018-06-06
  • 1970-01-01
  • 1970-01-01
  • 2017-10-29
  • 1970-01-01
相关资源
最近更新 更多