6

I'm working on an addon that uses an ExportHelper class to implement a file import dialog. I want the user to be able to select multiple files, but the filepath variable that is populated is just a string with one filename in it.

How can I modify my class to be able to import multiple files at once?

My current operator looks like this:

class SlideshowAddSlide(bpy.types.Operator, ExportHelper):
    bl_idname = 'slideshow.add_slide'
    filename_ext = ''
    filter_glob = bpy.props.StringProperty(
        default=types,
        options={'HIDDEN'},
    )
    def execute(self, context):
        print(self.filepath)
        return{'FINISHED'}
ideasman42
  • 47,387
  • 10
  • 141
  • 223
Snu
  • 143
  • 6

2 Answers2

6

You can access files for any operator using the file-selector, by defining a files collection of OperatorFileListElement.

Here is a complete example of an operator that opens a file selector and prints the paths you select.

Note: you may want to test os.path.isfile(filepath) since its possible to select directories too.

import bpy

from bpy_extras.io_utils import ExportHelper
from bpy.props import (
        StringProperty,
        CollectionProperty,
        )
from bpy.types import (
        Operator,
        OperatorFileListElement,
        )


class SlideshowAddSlide(bpy.types.Operator, ExportHelper):
    bl_idname = "slideshow.add_slide"
    bl_label = "Add Slide Show"
    files = CollectionProperty(
            name="File Path",
            type=OperatorFileListElement,
            )
    directory = StringProperty(
            subtype='DIR_PATH',
            )

    filename_ext = ""

    def execute(self, context):
        import os
        directory = self.directory
        for file_elem in self.files:
            filepath = os.path.join(directory, file_elem.name)
            print(filepath)
        return {'FINISHED'}

def register():
    bpy.utils.register_class(SlideshowAddSlide)


def unregister():
    bpy.utils.unregister_class(SlideshowAddSlide)


if __name__ == "__main__":
    register()

    # test call
    bpy.ops.slideshow.add_slide('INVOKE_DEFAULT')
ideasman42
  • 47,387
  • 10
  • 141
  • 223
3

If anyone has a problem using ideasman42's Code example in blender 3.0: The Properties have to be declared with a : instead of a = in order to work now. I have used quite some time to find that out, maybe it helps someone else.

So this would look like this:

files: CollectionProperty(
        name="File Path",
        type=OperatorFileListElement,
        )
directory: StringProperty(
        subtype='DIR_PATH',
        )
Hetarek
  • 31
  • 1