1

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_ext variable 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!

batFINGER
  • 84,216
  • 10
  • 108
  • 233
Marty
  • 143
  • 1
  • 7
  • 3
    https://blender.stackexchange.com/questions/203747/addon-how-to-make-blender-run-check-redraw-when-option-changed https://blender.stackexchange.com/questions/198831/right-way-to-stop-export-operator-from-saving-with-empty-filename/198924#198924 – batFINGER Jun 25 '21 at 20:04
  • 2
    The code I ended up with is this. Most obvious difference is the update function is a free function, not a member function like you have. – scurest Jun 25 '21 at 20:13
  • Thanks for the replies - still a little unclear on what makes the filename update, however. @scurest - Just to test, I can drop in the two functions you've written, on_export_format_changed and ensure_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:41
  • Oops - I just noticed in the link that @batFINGER shared that you added the check() 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

0 Answers0