Using Blender’s filebrowser with Python というのを見つけた。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
import bpy import os from bpy.props import StringProperty, BoolProperty from bpy_extras.io_utils import ImportHelper from bpy.types import Operator class OT_TestOpenFilebrowser(Operator, ImportHelper): bl_idname = "test.open_filebrowser" bl_label = "Open the file browser (yay)" filter_glob: StringProperty( default='*.jpg;*.jpeg;*.png;*.tif;*.tiff;*.bmp', options={'HIDDEN'} ) some_boolean: BoolProperty( name='Do a thing', description='Do a thing with the file you\'ve selected', default=True, ) def execute(self, context): """Do something with the selected file(s).""" filename, extension = os.path.splitext(self.filepath) print('Selected file:', self.filepath) print('File name:', filename) print('File extension:', extension) print('Some Boolean:', self.some_boolean) return {'FINISHED'} def register(): bpy.utils.register_class(OT_TestOpenFilebrowser) def unregister(): bpy.utils.unregister_class(OT_TestOpenFilebrowser) if __name__ == "__main__": register() # test call bpy.ops.test.open_filebrowser('INVOKE_DEFAULT') |
そのままBlenderから実行して、確かにファイル名やフルパスが得られることを確認した。 これを、自分のスクリプトに組み込めばファイルの指定が自在にできるようになって便利だ。 試しに、今やっている、位置データのCSVファイルを読み込んでObjectにアニメーションをつけるPythonスクリプト、に組み込んでみる。 下が組み込む前のスクリプトで、ファイル名を決め打ちしている。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
import bpy from mathutils import * from math import * from bpy.props import * import os import time # Init position_vector = [] # Open file file = open("D:\\part3-002_Camera_tr_Track_arr1.csv", "r") obj = bpy.context.object temporalScale=bpy.context.scene.render.fps for line in file: # Split line at ";" line = line.replace("\n","") locRotArray = line.split(",") #for lrt in locRotArray: obj.location = (float(locRotArray[1]), float(locRotArray[2]), float(locRotArray[3])) #time = float(locRotArray[0])*temporalScale #time = float(locRotArray[0])*0.0166666666666667 time = float(locRotArray[0]) obj.keyframe_insert(data_path="location", frame=time) |
これが、組み込んだ後のスクリプトだ。 [crayon-67ea7d3732d