[英]import and read all files from a folder Python
嗨大家我是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++
您沒有在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")
您正在使用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))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.