2
$\begingroup$

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?

$\endgroup$
0

1 Answer 1

4
$\begingroup$

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 = ".txt"

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

    def execute(self, context):
        print ("Filepath:", self.filepath)
        print ("Folder name:", 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 = ".txt"

    filter_glob: StringProperty(
        default="*.txt",
        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 ("Folder name:", os.path.dirname(self.filepath))
        for c, i in enumerate(self.files, start=1):
            print ("File {}".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:

$\endgroup$
2

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