I am puzzled by a weird behaviour of the Blender File Browser (v2.81) that I use to select the path to a folder containing the data my script processes:
when it is invoked from a Blender project that was never saved,
self.filepathreturns the path selected e.g.my/selected/path/. This is the behaviour I am expecting.when it is invoked once the Blender project file as been saved (
foo.blend), thenself.filepathreturns the path selected and appends the name of the project to itmy/selected/path/foo.blend
I am getting around this by stripping foo.blend but I am puzzled by this.
Test code (courtesy of this page):
import bpy
import os
from bpy_extras.io_utils import ImportHelper
from bpy.types import Operator
class OT_TestOpenFilebrowser(Operator, ImportHelper):
bl_idname = "test.open_filebrowser"
bl_label = "Select"
def execute(self, context):
filename, extension = os.path.splitext(self.filepath)
print('Selected:', self.filepath)
print('File name:', filename)
print('File extension:', extension)
return {'FINISHED'}
def register():
bpy.utils.register_class(OT_TestOpenFilebrowser)
def unregister():
bpy.utils.unregister_class(OT_TestOpenFilebrowser)
if name == "main":
register()
bpy.ops.test.open_filebrowser('INVOKE_DEFAULT')
Run from a never saved Blender project:

Run again after saving Blender project:

Why is that? Am I missing a setting or parameter?
self.filepathcontains the concatenation of the current path and the filename. There is no check whether or not that file actually exist, how your script is currently implemented. You can enter any string you want in the filename field or set it to an empty string. Perhaps it's easier for us to help if we know what you're trying to do. – Robert Gützkow Dec 01 '19 at 17:38context.space_data.params.filenamewhich you typed in when you saved the file. Other undocoed stuff like multi select files ie can define afilescollection property ofOperatorFileListElementsee https://blender.stackexchange.com/a/39855/15543 (may be somewhat outdated) Suggest too checking out the code ofbpy_extras.io_utils.py– batFINGER Dec 01 '19 at 18:39