2

I'm trying to get the filename and directory of the currently selected files in the file browser. When I hover over the directory with the cursor I see "FileSelectParams.directory", and this is what I find in the docs: https://docs.blender.org/api/current/bpy.types.FileSelectParams.html

Says bpy.types.FileSelectParams.filename is "the active file in the file browser"

So here's what I tried:

bl_info = {
    "name": "Image Putter Simple",
    "blender": (2, 90, 0),
    "category": "Object",
}

import bpy

def main(context): filename = bpy.types.FileSelectParams.filename directory = bpy.types.FileSelectParams.directory # Do things with the file

class ImagePutterSimple(bpy.types.Operator): """Image Putter Simple""" bl_idname = "object.browser_image_putter_simple" bl_label = "Image Putter Simple"

def execute(self, context):
    main(context)
    return {'FINISHED'}


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

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

if name == "main": register()

I get the error message:

Python: Traceback (most recent call last):
  File "/home/johne/.config/blender/2.93/scripts/addons/image_putter_simple.py", line 36, in execute
    main(context)
  File "/home/johne/.config/blender/2.93/scripts/addons/image_putter_simple.py", line 14, in main
    filename = bpy.types.FileSelectParams.filename
AttributeError: type object 'FileSelectParams' has no attribute 'filename'

location: <unknown location>:-1

What's going on? And if this doesn't work, is there another way to do what I'm trying to?

1 Answers1

4

Have a look into the Operator File Import template (Text Editor > Templates > Python).

The ImportHelper class comes with a filepath member which holds the actual filepath. You can get the folder using python's os or pathlib module:

import bpy

ImportHelper is a helper class, defines filename and

invoke() function which calls the file selector.

from bpy_extras.io_utils import ImportHelper from bpy.props import StringProperty from bpy.types import Operator import os

class ImportSomeData(Operator, ImportHelper): """This appears in the tooltip of the operator and in the generated docs""" bl_idname = "import_test.some_data" # important since its how bpy.ops.import_test.some_data is constructed bl_label = "Import Some Data"

# ImportHelper mixin class uses this
filename_ext = &quot;.txt&quot;

filter_glob: StringProperty(
    default=&quot;*.txt&quot;,
    options={'HIDDEN'},
    maxlen=255,  # Max internal buffer length, longer would be clamped.)

def execute(self, context):
    print (&quot;Filepath:&quot;, self.filepath)
    print (&quot;Folder name:&quot;, os.path.dirname(self.filepath))
    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')

Console Output:

Filepath: /Users/<user>/Desktop/test.txt
Folder name: /Users/<user>/Desktop

For multiple files, you'd have to declare a CollectionProperty to store the actual selection coming from the File Browser:

import bpy

ImportHelper is a helper class, defines filename and

invoke() function which calls the file selector.

from bpy_extras.io_utils import ImportHelper from bpy.props import StringProperty, BoolProperty, EnumProperty, CollectionProperty from bpy.types import Operator, PropertyGroup import os

class ImportSomeData(Operator, ImportHelper): """This appears in the tooltip of the operator and in the generated docs""" bl_idname = "import_test.some_data" # important since its how bpy.ops.import_test.some_data is constructed bl_label = "Import Some Data"

# ImportHelper mixin class uses this
filename_ext = &quot;.txt&quot;

filter_glob: StringProperty(
    default=&quot;*.txt&quot;,
    options={'HIDDEN'},
    maxlen=255,  # Max internal buffer length, longer would be clamped.
)

# Property to store all files in selection
files: CollectionProperty(type=PropertyGroup)

def execute(self, context):
    directory = os.path.dirname(self.filepath)
    print (&quot;Folder name:&quot;, os.path.dirname(self.filepath))
    for c, i in enumerate(self.files, start=1):
        print (&quot;File {}&quot;.format(c), os.path.join(directory, i.name))
    return {'FINISHED'}

Console Output:

Folder name: /Users/<user>/Desktop
File 1 /Users/<user>/Desktop/a.txt
File 2 /Users/<user>/Desktop/b.txt
File 3 /Users/<user>/Desktop/c.txt
...

Related:

brockmann
  • 12,613
  • 4
  • 50
  • 93
  • Related https://blender.stackexchange.com/questions/30678/bpy-file-browser-get-selected-file-names and https://blender.stackexchange.com/a/207665/15543 re file browser parameters – batFINGER Jun 30 '21 at 15:09
  • Voted to close as dupe @batFINGER Haven't found these... – brockmann Jun 30 '21 at 15:12