0
$\begingroup$

I’m creating an export/import add-on (similar to STL import/export add-on) and I would like the suggested filename in the file browser to take the name of the currently selected object in the scene or a generic filename if multiple objects are selected. My add-on uses the bpy_extras.io_utils’s ExportHelper.

In the example below, for a new, unsaved blend file, the default filename is untitled.sur (sur is the extension the file exported by my addon). I would like this name to be “Icosphere.sur” since the object “Icosphere” is selected in the scene.

enter image description here

$\endgroup$

1 Answer 1

1
$\begingroup$

Set in the invoke method.

As noted in these answers the IO helpers have invoke and check methods that are doing some things behind the scenes.

https://blender.stackexchange.com/a/198924/15543

https://blender.stackexchange.com/a/207665/15543

The export helper invoke method, that if not set sticks in the name Untitled, simply change this to that of the context object.

def invoke(self, context, _event):
    import os
    if not self.filepath:
        blend_filepath = context.blend_data.filepath
        if not blend_filepath:
            blend_filepath = context.object.name # <=== UPDATE
        else:
            blend_filepath = os.path.splitext(blend_filepath)[0]

        self.filepath = blend_filepath + self.filename_ext

    context.window_manager.fileselect_add(self)
    return {'RUNNING_MODAL'}
$\endgroup$
2
  • $\begingroup$ This method is defined in the internal blender scripts..so they can’t be changed (or rather should not). do you mean I can overload this method in my addon class inheriting from this helper class and do whatever I want to do in it to set the file path ? Btw, I ended up ditching the helper class and just defining the invoke method in my class directly. $\endgroup$ Mar 9, 2021 at 1:54
  • $\begingroup$ Congratulations that was the idea. Thanks heaps for the feed-back. $\endgroup$
    – batFINGER
    Mar 9, 2021 at 4:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.