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,
1 2 |
from bpy_extras.io_utils import ImportHelper from bpy.types import Operator |
and make it:
1 2 3 4 5 6 7 8 |
class TEST_OT_import_tst(Operator, ImportHelper): bl_idname = 'test.import_tst' bl_label = 'test import test' bl_options = {'PRESET', 'UNDO'} def execute(self, context): print('imported file: ', self.filepath) return {'FINISHED'} |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class TEST_OT_import_tst(Operator, ImportHelper): bl_idname = 'test.import_tst' bl_label = 'test import test' bl_options = {'PRESET', 'UNDO'} filename_ext = '.txt' filter_glob: StringProperty( default='*.txt', options={'HIDDEN'} ) def execute(self, context): print('imported file: ', self.filepath) return {'FINISHED'} |
Register it in the API and call as an operator for a demonstration.
1 2 3 4 5 6 |
import bpy from bpy.utils import register_class register_class(TEST_OT_import_tst) bpy.ops.test.import_tst('INVOKE_DEFAULT') |
The final code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import bpy from bpy_extras.io_utils import ImportHelper from bpy.types import Operator from bpy.props import StringProperty from bpy.utils import register_class class TEST_OT_import_tst(Operator, ImportHelper): bl_idname = 'test.import_tst' bl_label = 'test import test' bl_options = {'PRESET', 'UNDO'} filename_ext = '.txt' filter_glob: StringProperty( default='*.txt', options={'HIDDEN'} ) def execute(self, context): print('imported file: ', self.filepath) return {'FINISHED'} register_class(TEST_OT_import_tst) bpy.ops.test.import_tst('INVOKE_DEFAULT') |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import bpy from bpy_extras.io_utils import ExportHelper from bpy.types import Operator from bpy.props import StringProperty from bpy.utils import register_class class TEST_OT_export_tst(Operator, ExportHelper): bl_idname = 'test.export_tst' bl_label = 'test export test' bl_options = {'PRESET', 'UNDO'} filename_ext = '.txt' filter_glob: StringProperty( default='*.txt', options={'HIDDEN'} ) def execute(self, context): print('exported file: ', self.filepath) return {'FINISHED'} register_class(TEST_OT_export_tst) bpy.ops.test.export_tst('INVOKE_DEFAULT') |
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.
How do I get the imported file to show in the 3D space to work with? (a script to import stl file to then work on in the space, as part of a bigger add on)
As I know, you can set only to variants in the Preferences – Interface – Temporary Editors – File browser parameter – Maximized area and New window.