I'm working on a Blender (2.93) addon that outputs models to a few different formats. There is an EnumProperty in the filebrowser which lets the user choose which format to output to. The formats are different enough that I feel they should each have their own filename extension, but I cannot figure out how to do this properly.
This is the code I'm using for exports, with some of the irrelevant properties trimmed out:
class ExportData(Operator, ExportHelper):
bl_idname = "export.gtd"
bl_label = "Export"
filename_ext = ".buf"
def update_ext(self, context):
new_ext = ".buf"
if self.output_type == 'vertex_buffer':
new_ext = "buf"
else:
new_ext = "gml"
params = context.space_data.params
context.space_data.params.filename = f"{Path(self.filepath).stem}.{new_ext}"
filter_glob: StringProperty(
default="*.buf;*.gtd",
options={'HIDDEN'},
maxlen=255,
)
output_type: EnumProperty(
name = "Output Type",
description="The type of data to output",
items = (
('vertex_buffer', "Vertex Buffer", "A vertex buffer that can be loaded at runtime"),
('debug', "GTD Script (debug)", "A human-readable script that can be used to import and debug"),
),
default = 'vertex_buffer',
update = update_ext,
)
def execute(self, context):
return export_gtd(context, self.filepath, self.output_type)
When I run this code, I can use the console to verify that the filename_ext changes, and it will print the correct filename if I print context.space_data.params.filename. There are two problems, however:
- The
filename_extvariable always forces a single filename extension on the output, and I can't figure out how to change it. - The actual filename at the bottom of the filebrowser doesn't update when I change the choice in the EnumProperty.
I've been studying this answer: Addon: how to make Blender run check()/redraw when option changed?, but the suggestions/solutions there don't seem to work. Would appreciate any help pointing me in the right direction on this!
on_export_format_changedandensure_filepath_matches_export_format, and I can print out what's returned at the end to see that the correct filename is produced.The filename in the window does not update, though - and any attempt to change the extension by typing a new one in is just reset every time the filename box loses focus.
Is there some aspect of the filebrowser window that needs to be overridden?
– Marty Jun 25 '21 at 22:41check()to your export class, and now that seems to be working correctly for me.I think that might be the solution I was missing. Thank you both for your help!
– Marty Jun 25 '21 at 22:46