6
$\begingroup$

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'}
$\endgroup$

2 Answers 2

6
$\begingroup$

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')
$\endgroup$
3
$\begingroup$

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',
        )
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .