filter files when a filemanager is called from a StringProperty FILE_PATH subtype ?

I understand I can define a file filter_glob property when writing my own operator.
can I do the same when using the FILE_PATH subtype of stringproperty ?

ok, please find below a snippet for review/improvements/better code.

it’s for loading a kind of file from a script panel.

it shows something that looks like a “stringproperty subtype=FILE_PATH” button to load a file or type a filepath, but that can filterfile extension and also report warning/error messages in the gui, with some common tests you have to do on such case.

I did this because :
. I don’t know how to filter files from a stringproperty subtype=FILE_PATH,
. I don’t know how to report ‘out of context’ in the blender ui report area.

import bpy

def loadText(self,context) :
    f=self.filepath.strip().replace('\\','/')
    
    if f != bpy.context.scene.myvars.pfile :
        # '' check
        if f == '' :
            bpy.ops.myvars.report(icon = 'ERROR', msg = 'Type something please' )
        # ext check
        elif f.split('.')[-1] != 'txt' :
            bpy.ops.myvars.report(icon = 'ERROR', msg = 'not a text file' )
        else :
            # file check
            try : 
                file = open(f, 'r')
                file.close
                try : bpy.data.texts.remove(bpy.data.texts['mytextfile'])
                except : pass
                pgnblock = bpy.data.texts.load(f)
                pgnblock.name = 'mytextfile'
                bpy.context.scene.myvars.pfile = f
                bpy.context.scene.myvars.filepath = f
                bpy.ops.myvars.report(icon = 'INFO', msg = '%s loaded'%f )
                return
            except :
                bpy.ops.myvars.report(icon = 'ERROR', msg = 'Wrong path or corrupted file' )
        bpy.context.scene.myvars.filepath = bpy.context.scene.myvars.pfile

class MyVars(bpy.types.PropertyGroup) :
    pfile = bpy.props.StringProperty()
    filepath = bpy.props.StringProperty(description="Select a text file", update=loadText)

class MyVarsPanel(bpy.types.Panel) :

    bl_label = 'my panel'
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
    bl_idname = 'MyPanel'

    def draw(self, context):
        myvars = bpy.context.scene.myvars
        layout  = self.layout
        row = layout.row(align=True)
        row.alignment = 'CENTER'
        row.prop(myvars,'filepath')
        row.operator( "myvars.load",text='',icon='FILESEL')
        row.operator( "myvars.unload",text='',icon='X')


class MyVarsLoad(bpy.types.Operator) :
    '''Load a TXT file.'''
    bl_idname = 'myvars.load'
    bl_label = 'load a txt file through blender file manager'

    filepath = bpy.props.StringProperty(subtype="FILE_PATH")
    filter_glob = bpy.props.StringProperty(default="*.txt", options={'HIDDEN'})

    def execute(self, context):
        loadText(self,context)
        return {'FINISHED'}

    def invoke(self, context, event):
        context.window_manager.fileselect_add(self)
        return {'RUNNING_MODAL'}

class MyVarsUnload(bpy.types.Operator) :
    '''remove the TXT file.'''
    bl_idname = 'myvars.unload'
    bl_label = 'remove the txt file'
    
    @classmethod
    def poll(context,self) :
        return 'mytextfile' in bpy.data.texts.keys()
    
    def execute(self, context):
        bpy.context.scene.myvars.pfile = ''
        bpy.context.scene.myvars.filepath = ''
        bpy.data.texts.remove(bpy.data.texts['mytextfile'])
        return {'FINISHED'}


class MyVarsReport(bpy.types.Operator) :
    '''popup a message'''
    bl_idname = 'myvars.report'
    bl_label = 'Report logs in the gui'
    
    icon = bpy.props.StringProperty()
    msg = bpy.props.StringProperty()

    def draw(self, context):
        layout = self.layout
        row = layout.split(0.20)
        try : row.label( text = '', icon = self.icon)
        except : pass
        row.label(self.msg)

    def execute(self, context):
        return context.window_manager.invoke_popup(self)

def register() :
    bpy.utils.register_class(MyVars)
    bpy.utils.register_class(MyVarsPanel)
    bpy.utils.register_class(MyVarsLoad)
    bpy.utils.register_class(MyVarsUnload)
    bpy.utils.register_class(MyVarsReport)
    bpy.types.Scene.myvars= bpy.props.PointerProperty(type=MyVars)

def unregister() :
    bpy.utils.unregister_class(MyVars)
    bpy.utils.unregister_class(MyVarsPanel)
    bpy.utils.unregister_class(MyVarsLoad)
    bpy.utils.unregister_class(MyVarsUnoad)
    bpy.utils.unregister_class(MyVarsReport)
    bpy.types.Scene.myvars= bpy.props.PointerProperty(type=MyVars)

if __name__ == '__main__' :
    register()

anyone has something simpler (should be) ?

your not using OS here ?
interesting i can read a file in local folder on vista WOW nice one man

note i tried with your global filter and seems to work fine?
tried for txt or py file and it filters out quit well on vista

so what else do you want to do here?

thanks for sharing and finding a way to read local files on vista !

wish this could be use with ordinary file read !

happy 2.5

yeah I know it work ricky but it’s so a waste of lines for two simple features :
filtering from subtype=FILE_PATH does not seem to exist,
gui report seems unavailable because of contexts mecanism (but I like it, I like this context thing, don’t ban me… it’s great but in some occasions, how to say that…)