billan maratos billan maratos - 5 months ago 18
Python Question

import and read all files from a folder Python

Hi everyone i am beginner with python and i have a problem with my code i would like to import and read all the .BVH file from a specific folder but the program takes only the first one from the folder.Here is my code.I use blender for visualization.

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

Answer

You're not using the actual file in the for loop. You're just using the same hardcoded path each time.

Maybe you want something like the below?

I renamed files to file_path to better represent what's in that variable. Then I used that value in the call to import_anim.bvh, and then I used it again in the call to export_anim.bvh. (There I tacked on "_exported.bvh" to the end of the file name. I wasn't really sure what you were trying to do.)

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")