Skip to content

Instantly share code, notes, and snippets.

@batFINGER
Created November 24, 2016 13:05
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save batFINGER/2c0604be3620def01c4eeaff6ceb22f4 to your computer and use it in GitHub Desktop.
Save batFINGER/2c0604be3620def01c4eeaff6ceb22f4 to your computer and use it in GitHub Desktop.
Process multi files (ImportHelper) in blender.
import bpy
def process_files(context, directory, files):
import os
for file in files:
path = os.path.join(directory, file.name)
print("process %s" % path)
return {'FINISHED'}
# 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, CollectionProperty
from bpy.types import Operator, OperatorFileListElement
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 = ".txt"
filter_glob = StringProperty(
default="*.txt",
options={'HIDDEN'},
maxlen=255, # Max internal buffer length, longer would be clamped.
)
files = CollectionProperty(
name="BVH files",
type=OperatorFileListElement,
)
directory = StringProperty(subtype='DIR_PATH')
def execute(self, context):
return process_files(context, self.directory, self.files)
# Only needed if you want to add into a dynamic menu
def menu_func_import(self, context):
self.layout.operator(ImportSomeData.bl_idname, text="Process Multi files")
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
bpy.ops.import_test.some_data('INVOKE_DEFAULT')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment