How can I get bring up a popup file browser in Blender 2.80 invoked by a button click in my addon, to define the name and location of a file to be exported?
In Blender 2.7x, I did this via the context window manager in the invoke function, but it is very possible that this hasn't been the preferred method for years, as I'm not finding useful documentation when I search for how to update this.
class MYTOOL_OT_write_data(bpy.types.Operator):
bl_idname = "mytool.write_data"
bl_label = "Write data file"
directory: bpy.props.StringProperty(subtype="FILE_PATH")
filename: bpy.props.StringProperty(subtype="FILE_NAME")
def invoke(self, context, event): # 2.7x method
WindowManager = context.window_manager
context.window_manager.FILEBROWSERect_add(self) # <-- error here
return{"RUNNING_MODAL"}
def execute(self, context):
directory = self.directory
filename = self.filename
# Write csv data file using normal python functionality
return{'FINISHED'}
In Blender 2.80, I am getting the error "AttributeError: 'WindowManager' object has no attribute 'FILEBROWSERect_add'".
Thanks for your help.
UPDATED SOLUTION based on brockmann's solution below. The trick is to use the ExportHelper util (which existed long before Blender 2.80):
from bpy_extras.io_utils import ExportHelper
class MYTOOL_OT_write_data(bpy.types.Operator, ExportHelper):
bl_idname = "mytool.write_data"
bl_label = "Write data file"
filename_ext = ".csv" # ExportHelper mixin class uses this
def execute(self, context):
filepath = self.filepath
# f = open(filepath, 'w')
# f.write(stuff)
# f.close()
return{'FINISHED'}