4
$\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$
4
$\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$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.