# Add a filter for the extension of a file in the file browser

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

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:

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.

eg:

filter_glob = StringProperty(
default="*.txt;*;*.py",
options={'HIDDEN'},
)

• 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? Mar 20 '14 at 9:27
• 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. Mar 20 '14 at 11:50
• Is there a shorthand to filter types of files? E.G. Set FileSelectParams.use_filter_image? Apr 3 at 22:08

I wrote class following above answer:

bpy.types.Scene.my_unfiltered_filepath =
bpy.props.StringProperty(name = 'Unfiltered',
description="Path of .* File",
default="//",
subtype='FILE_PATH')


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",
default="//",
subtype='NONE') # important

from bpy_extras.io_utils import ImportHelper

class FilteredFiledialog(bpy.types.Operator, ImportHelper):
bl_label = ''
filename_ext = ''
filter_glob = bpy.props.StringProperty(
default="",
options={'HIDDEN'},
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)

@classmethod
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)
col.label(string_prop_namespace.bl_rna.properties[string_prop_name].name)
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')
else:
row.prop(string_prop_namespace, string_prop_name, icon_only=True)
row.operator(cls.bl_idname, icon='FILESEL')


File dialog looks identical.