2016年11月14日月曜日

blender スクリプトメモ:fileDialogでファイルパスを取得する

Mayaのmelやpythonだと、fileDialogを出すのには全く苦労しませんが、
Blenderはかなり面倒くさかったです。
あちこち調べて、ようやくソースを見つけたので、こちらにメモします。

以下ソース
import bpy
import os
#if chanfe the referense filename, you should change below path.
armatPath='D:/Technical/blender/noBall/blender_RD/scene/bone_set_0928.blend'
#---------------------------------------------------------------
#file Dialog & do SetUp Class
#---------------------------------------------------------------
# ImportHelper is a helper class, defines filename and
# invoke() function which calls the file selector.
from bpy_extras.io_utils import ImportHelper
from bpy.props import StringProperty, BoolProperty, EnumProperty
from bpy.types import Operator
class ImportSomeData(Operator, ImportHelper):
"""This appears in the tooltip of the operator and in the generated docs"""
bl_idname = "import_test.some_data" # important since its how bpy.ops.import_test.some_data is constructed
bl_label = "Import Some Data"
# ImportHelper mixin class uses this
filename_ext = ".fbx"
filter_glob = StringProperty(
default="*.fbx",
options={'HIDDEN'},
)
#files = bpy.props.CollectionProperty(type=ImportFilesCollection)
files = bpy.props.CollectionProperty(type=bpy.types.PropertyGroup)
bpy.types.PropertyGroup
def execute(self, context):
print(len(self.files))
dirname = os.path.dirname(self.filepath)
#fileList=[]
for i, f in enumerate(self.files, 1):
#print("File %i: %s" % (i, f.name))
fbxPath = (os.path.join(dirname, f.name))
fbxPath.replace('\\', '/')
#-------------------------------------------------------------
#Do Proc
print(fbxPath)
#-------------------------------------------------------------
return {'FINISHED'}
# Only needed if you want to add into a dynamic menu
def menu_func_import(self, context):
self.layout.operator(ImportSomeData.bl_idname, text="Text Import Operator")
def register():
bpy.utils.register_class(ImportSomeData)
bpy.types.INFO_MT_file_import.append(menu_func_import)
def unregister():
bpy.utils.unregister_class(ImportSomeData)
bpy.types.INFO_MT_file_import.remove(menu_func_import)
if __name__ == "__main__":
register()
# test call
fileDialog = bpy.ops.import_test.some_data('INVOKE_DEFAULT')

参考:



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

本当は、fileDialogから、単にパスを返すだけのソースにしたかったのですが、解決方法がわからず、fileDialogを呼び出した処理の中に、本処理を書き込む流れになっています。

うーむ。。気持ち悪い。。

というか、たかだかfileDialogだすだけで相当時間を食ってしまった…南無三。


0 件のコメント:

コメントを投稿