I'm writing a python script that allow us to open a file in it like that:

enter image description here

It's a string properties wich is coded like that :

bpy.props.StringProperty(description="Path of .DAT File", default="C:", subtype='FILE_PATH') 

When the user click on the little folder on right it open a file browser.

In the file browser, in my case I want to filter only the .DAT files. I know that it's possible to filter the extension of file for exemple here it filter the .py or .zip file:

enter image description here

How can I do that with Python ?


Unfortunately this isnt documented, however it is used in the templates that come with blender, see Text -> Templates -> Python -> Operator File Export, there is a filter_glob string property.


filter_glob = StringProperty(
  • 1
    $\begingroup$ Mmmmh if I understand correctly, this cannot be implemented with only my bpy.props.StringProperty(description="Path of .DAT File", default="C:", subtype='FILE_PATH'), if I want to put a filter I've to do an importer class? $\endgroup$
    – lucblender
    Mar 20 '14 at 9:27
  • 1
    $\begingroup$ You would need to define an importer that will translate the data into blender objects. Your folder icon doesn't have to initiate the file import. You may be able to break it into parts, so that choosing the file only sets properties instead of actually reading the data and creating new objects, which could be done at a later stage. $\endgroup$
    – sambler
    Mar 20 '14 at 11:50
  • $\begingroup$ Is there a shorthand to filter types of files? E.G. Set FileSelectParams.use_filter_image? $\endgroup$
    – Will Chen
    Apr 3 at 22:08

I wrote class following above answer:

Instead of register:

bpy.types.Scene.my_unfiltered_filepath =
                bpy.props.StringProperty(name = 'Unfiltered',
                                         description="Path of .* File",

and in draw method:

col.prop(context.scene, 'my_unfiltered_filepath')

use in draw method:

FilteredFiledialog.add(col, context.scene, 'my_filtered_filepath', 'txt', 'py')

one or more extensions can be used, this filters txt and python files.

And register:

bpy.types.Scene.my_filtered_filepath =
                bpy.props.StringProperty(name = 'Filtered',
                                         description="Path of .txt and .py File",
                                         subtype='NONE') # important

from bpy_extras.io_utils import ImportHelper

class FilteredFiledialog(bpy.types.Operator, ImportHelper):
    bl_idname = "pathload.test"
    bl_label = ''
    filename_ext = ''
    filter_glob = bpy.props.StringProperty(
        maxlen=255  # Max internal buffer length, longer would be clamped.

    def execute(self, context):
        setattr(self.string_prop_namespace, self.string_prop_name, bpy.path.relpath(self.filepath))
        return {'FINISHED'}

    def invoke(self, context, event):
        self.filter_glob = "*" + ";*".join(self.ext)
        return super().invoke(context, event)

    def add(cls, layout, string_prop_namespace, string_prop_name, *ext):
        cls.ext = ext
        cls.string_prop_namespace = string_prop_namespace
        cls.string_prop_name = string_prop_name
        col = layout.split(.33)
        row = col.row(align=True)
        if string_prop_namespace.bl_rna.properties[string_prop_name].subtype != 'NONE':
            row.label("ERROR: Change subtype of {} property to 'NONE'".format(string_prop_name), icon='ERROR')
            row.prop(string_prop_namespace, string_prop_name, icon_only=True)
            row.operator(cls.bl_idname, icon='FILESEL')

File dialog looks identical.


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.