How to get selected files in File Browser (editor window)

I’m trying to get a list of the files selected within a given File Browser (that is in an Editor Type file browser, one occupying a portion of the workspace layout and not a File Browser called from an operator or string/file gathering function).

A single file can be retrieved from a File Browser editor like this:


import bpy

screen = bpy.context.window.screen        
for area in screen.areas:
    if area.type == 'FILE_BROWSER':
        params = area.spaces[0].params
        singleFileName = params.filename


And a collection of files (if calling a file browser not retrieving the data from a File Browser Editor Type already open) can be gathered like this:


import bpy, os
from bpy.props import CollectionProperty
from bpy_extras.io_utils import ImportHelper
from bpy.types import Operator


class ImportSomeData(Operator, ImportHelper):
    bl_idname = "import_test.some_data"
    bl_label = "Import Some Data"
    
    files = CollectionProperty(type=bpy.types.PropertyGroup)


    def execute(self, context):
        for f in self.files:
            print(f.name)
    
        return{'FINISHED'}


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

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

if __name__ == "__main__":
    register()

    # test call
    bpy.ops.import_test.some_data('INVOKE_DEFAULT')

Where I’m falling flat is trying to combine the two. I see only a way to gather a single selected filename from a File Browser Editor Type window. Could someone help make this clear on how to do this, or another more simple way perhaps I’m missing? Would really like to be able to gather a list of the files selected in a File Browser editor type. Thanks.

Hi. Thanks batFINGER. Sorry for taking so long to respond. When I insert this code into the operator class which I want to be able to access the selected files of an already open File Browser editor type, I am able to print(self.files) and see that the collection property is registering when I check the console after the operator has run, however it always returns [0]. Do you know how I might fill that collection property up with whatever files happen to be selected in an open File Browser editor type as depicted in my first code example where I’m able to get just the actively selected file?

When I run the code you pointed to under “As shown here”, I noticed that it collected the files selected of the file browser it launched, not of an open editor type File Browser. I’m afraid this may be a bit more difficult task. I use a layout which gives the user immediate access to a file browser so there aren’t so many steps involved with the using the operator, is why I’m trying to do it this way. Thanks for your help.

I’m not sure if I understand you right… Do you mean you can retrieve all file paths via self.files in execute(), but not during the modal operation (i.e. file browser open)?

Maybe this helps:

@CoDEmanX in my last comment I was trying to convey to batFINGER that I was able to see that the collection property was registering within my operator but that I was unable to fill this property with the names of the selected files. I’m not interested in opening the file browser. I want to retrieve (or create) a list of all selected files in an (already open in the Blender workspace) File Browser editor type. One that stays open in the user interface all the time.

My first code example in the first comment expresses how to retrieve one of these selected files from an open File Browser left open in a given Blender workspace, but I don’t know how to get all selected files from a particular File Browser editor type (I keep mentioning “File Browser editor type” because I’m trying to get across the idea I need to retrieve multiple selected files from a File Browser already open within the Blender workspace, not one that is called). I haven’t used collection properties much so I’m pretty ignorant about them. Maybe this is my problem. Thanks.

Ah, got it! Well, it’s not possible, because “files” is not exposed for your use case:
https://developer.blender.org/diffusion/B/browse/master/source/blender/makesrna/intern/rna_space.c;0952a8e44ae2eb9617426c5878acf0224a0831b2$3807
filename and directory are there, similar to the operator ones. But no files, unlike in the modal file selector.

It’s apparently not handled here either:
https://developer.blender.org/diffusion/B/browse/master/source/blender/editors/space_file/file_ops.c;0952a8e44ae2eb9617426c5878acf0224a0831b2$1128
and here:
https://developer.blender.org/diffusion/B/browse/master/source/blender/editors/space_file/file_ops.c;0952a8e44ae2eb9617426c5878acf0224a0831b2$1199

Not sure if “files” should be handled differently here:
https://developer.blender.org/diffusion/B/browse/master/source/blender/editors/space_file/filesel.c;0952a8e44ae2eb9617426c5878acf0224a0831b2$83

You should ask a dev if it could be exposed for an open file browser too, not just the modal operator file dialog.

That’s cool. Thanks for the info.