Using the FileBrowser interface window to opening and saving files

In scripts and add-ons for importing-exporting files with formats that are not supported in Blender by default, we must give the user an ability to select files using the “FileBrowser” interface.

To open the file browser window, and after the user selects the necessary file, to return the path to it, we need to use the “ImportHelper” and the “ExportHelper” classes.

The “ImportHelper” class is used for importing files, the “ExportHelper” – for exporting.

Let’s make an operator that opens a file browser window, and after the user selects a file, returns the full path to it.

Import the necessary modules to create a custom operator class,

and make it:

To run our class as an operator, we inherit it from the “Operator” class. Inheriting from the “ImportHelper” class provides the “FileBrowser” window opening to select a file before executing the “execute” method.

After file selection and closing the “FileBrowser” window, the path to it will be stored to the class property “filepath”.

To proper work of our class, it is necessary to override the “filter_glob” property – a hidden property of the “StringProperty” type to specify an extensions filter to display files in the file browser window.

The whole class code:

Register it in the API and call as an operator for a demonstration.

The final code:

Open the “Text Editor” window, create a new script, and paste this code. When the script is executed (by clicking the “Run Script” button), the “FileBrowser” window will be opened in which only files with the *.txt extension will be displayed. After selecting any of the text files, the “FileBrowser” window will close and the full path to the selected file will be printed to the system console.

Similarly, for file exporting.

To export, we must override additional property “filename_ext” – the default extension for the saved file.

Full code for exporting:

Here, after the “FIleBrowser” window closing, the path to the file with the name specified by the user will be printed to the system console.


Author: Nikita

Blender add-ons developer and articles writer.