2016-06-14 4 views
1

안녕하세요 모두 저는 파이썬 초보자입니다. 내 코드에 문제가 있습니다. 특정 폴더에서 모든 .BVH 파일을 가져오고 읽으려고하지만 프로그램은 첫 번째 파일 만 가져옵니다. 하나는 폴더에서. 내 코드는. 시각화를 위해 블렌더를 사용한다.폴더에서 모든 파일 가져 오기 및 읽기 Python

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

필자는 파일 + +이 유효한 파이썬 코드라고 생각하지 않습니다. – mattsap

+0

무엇이 당신의 질문입니까? 오류가 발생하면 출력에 오류를 표시하십시오. – mattsap

+0

''파일 ++ ''은 무엇을할까요? 가져온 파일을 계산하는 경우 try 절에서 들여 쓰기되어야합니다. 어쨌든 파이썬은'++'연산자가'files + = 1'을 사용하도록 허용하지 않습니다. –

답변

2

for 루프에서 실제 파일을 사용하고 있지 않습니다. 매번 동일한 하드 코딩 된 경로를 사용하고 있습니다.

어쩌면 아래와 같은 것을 원할 것입니까?

해당 변수의 내용을 더 잘 나타내려면 files에서 file_path으로 이름이 바뀌 었습니다. 그런 다음 그 값을 import_anim.bvh에 사용한 다음 export_anim.bvh에 다시 사용했습니다. (이 나는 파일 이름의 끝에 "_exported.bvh"에 압정으로 고정. 난 당신이 뭘하려고했는지 정말 확신하지 못했습니다.) 당신은 모두 계산에 대한 files를 사용하여 각각의 현재 파일 경로를 들고

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

대단히 감사합니다 .. 나는 그것을 얻을 !!! –

+2

FileNotFoundError와 같은 예외는 이와 같이 취급되어야합니다. 당신이 오류를 다루지 않는 캐치를 피하십시오 하지만 그냥 잡기. –

1

되풀이. 그리고 반복에서 현재 파일 경로를 으로 입력하지 않으면 방금 하드 코딩 된 파일 경로를 사용했습니다. 또한 ++은 유효한 구문이 아닙니다.

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

bvh 함수를 올바르게 사용하고 있습니까? 코드를 디버그하십시오. 인쇄 하시겠습니까? –

관련 문제