2016-06-14 48 views
1

こんにちは皆私はPythonで初心者です。私のコードに問題があります。特定のフォルダからすべての.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

私はファイル++が有効なpythonコードではないと思います。 – mattsap

+0

あなたの質問は何ですか?エラーが発生している場合は、エラーを出力してください。 – mattsap

+0

'ファイル++ 'は何をすると思いますか?インポートされたファイルを数えている場合は、try節で字下げする必要があります。とにかくpythonは '++ '演算子が' files + = 1'を使うことを許可していません –

答えて

2

forループで実際のファイルを使用していません。毎回同じハードコーディングされたパスを使用しています。

多分あなたは以下のようなものが欲しいですか?

その変数の内容をよりよく表現するために、filesfile_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関数を正しく使用していますか?あなたのコードをデバッグして、あなたは友人ですか? –

関連する問題