Addon select a file from current dir?

i look at the template ui_previews_dynamic_enum.py
in 2.79

i can see list of file in current dir but cannot select any file ?

is this possible with this addon ?

or is there another template to select a file in current dir
that can be added to a panel addon as a button ?

thanks
happy bl

found this old one in 2.71
but one of the module does not exit ?

might have been replace with something else but what ?

thanks
happy bl

found this one which is beginning to work

but how to get the file name and path >

and how to modify it for 2.9 ?

thanks
happy bl

Hey, I did not continue with that work so unfortunately I cannot help you with that. Good luck though.

was it for the first or the second addon ?

second one looks almost there
but need the file and path name

then i think i could use it in a panel set up addon

thanks
happy bl

got this

	print('  here name filepath  = ',self.properties.filepath)

using a property

is there a way to extract only the file name ?

thanks
happy bl

Operator(bpy_struct) — Blender Python API

also

Property Definitions (bpy.props) — Blender Python API

look at string subtypes especially. From what i gather so quickly, you’ll need to add another stringproperty with a subtype set to ‘FILE_NAME’. Assuming that this call:

context.window_manager.fileselect_add(self)

invokes the actual file browser and that it populates those string properties for you. I can’t find a way to open it while specifying a directory to look at, i think it defaults to the last known directory. You could try adding a ‘DIR_PATH’ stringproperty and hope for the best though. Note that this code example is from the ‘Operators’ page I also linked.

import bpy


class ExportSomeData(bpy.types.Operator):
    """Test exporter which just writes hello world"""
    bl_idname = "export.some_data"
    bl_label = "Export Some Data"

    filepath: bpy.props.StringProperty(subtype="FILE_PATH")

    @classmethod
    def poll(cls, context):
        return context.object is not None

    def execute(self, context):
        file = open(self.filepath, 'w')
        file.write("Hello World " + context.object.name)
        return {'FINISHED'}

    def invoke(self, context, event):
        context.window_manager.fileselect_add(self)
        return {'RUNNING_MODAL'}


# Only needed if you want to add into a dynamic menu
def menu_func(self, context):
    self.layout.operator_context = 'INVOKE_DEFAULT'
    self.layout.operator(ExportSomeData.bl_idname, text="Text Export Operator")


# Register and add to the file selector
bpy.utils.register_class(ExportSomeData)
bpy.types.TOPBAR_MT_file_export.append(menu_func)


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

An alternative if you can’t get it to work would be to just populate a pulldown menu/previewscollection with files from the selected directory. This is done with enumproperties, item callback and update functions.

I found this too:

bpy.ops.wm.path_open(filepath='')

if it does what i think it does, it’s actually just a matter of calling that when clicking a button.
Wm Operators — Blender Python API

edit: That opens your OS file browser at the specified location

new property with 

filename = bpy.props.StringProperty(subtype=“FILE_NAME”)

works fine

now will try to integrate into a real panel addon with some button operator

thanks
happy bl

1 Like