Is there a way in bpy to get a list of all selected files from the file browser?

I have this code, but it is only for the active file selection:

selectedFile = ''
for screenArea in bpy.context.window.screen.areas:
   if screenArea.type == 'FILE_BROWSER':
        params = screenArea.spaces[0].params
        selectedFile = os.path.join(params.directory, params.filename)

If you take the TextEditor Templates -> Python -> Operator File Import

  • add from bpy.props import CollectionProperty
  • and add this special property in the ImportSomeData operator

inside the importer class

class ImportSomeData(Operator, ImportHelper):

    # edit for brevity

    filename_ext = ".txt"
    files = CollectionProperty(type=bpy.types.PropertyGroup)

Then self.files, with 3 files selected, gives you

... <bpy_collection[3], IMPORT_TEST_OT_some_data.files>
>>> self.files[0]
... <bpy_struct, PropertyGroup("filename_mine2.txt")>

Then to get the fullpath of those files you use the self.path dirname

import os
dirname = os.path.dirname(self.filepath)
for f in self.files:
    print(os.path.join(dirname, f.name))

| improve this answer | |
  • $\begingroup$ I see, it's the ImportHelper that gives the functionality that I need. I'm not around my computer for now, I will try it when I get a chance. Thank you, that looks promising. $\endgroup$ – MakerDrone May 11 '15 at 20:05

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.