1
$\begingroup$

How do you set ex. use_filter_movie to true before opening the file browser(using ExportHelper)?

enter image description here

This doesn't work:

import bpy
from bpy_extras.io_utils import ExportHelper

class MyOperator(bpy.types.Operator, ExportHelper):
    bl_idname = "my_operator.export"
    bl_label = "Export My Operator"

    filename_ext = ".txt"

    # Set use_filter_movie property to True
    use_filter = True
    use_filter_movie = True

    def execute(self, context):
        # Export code here
        return {'FINISHED'}

    def invoke(self, context, event):
        # Call the file browser with settings defined in operator class
        context.window_manager.fileselect_add(self)
        return {'RUNNING_MODAL'}

def register():
    bpy.utils.register_class(MyOperator)

def unregister():
    bpy.utils.unregister_class(MyOperator)

if __name__ == "__main__":
    register()

And this doesn't work:

import bpy
from bpy_extras.io_utils import ExportHelper

class MyOperator(bpy.types.Operator, ExportHelper):
    bl_idname = "my_operator.export"
    bl_label = "Export My Operator"

    filename_ext = ".txt"

    def execute(self, context):
        # Export code here
        return {'FINISHED'}

    def invoke(self, context, event):

        # Call the file browser with "Movies" filter enabled
        context.window_manager.fileselect_add(self)

        # Set use_filter_movie to True
        screen = context.screen
        for area in screen.areas:
            if area.type == 'FILE_BROWSER':
                space = area.spaces.active
                params = space.params
                params.use_filter = True
                params.use_filter_movie = True

        return {'RUNNING_MODAL'}

def register():
    bpy.utils.register_class(MyOperator)

def unregister():
    bpy.utils.unregister_class(MyOperator)

if __name__ == "__main__":
    register()
$\endgroup$
2
  • $\begingroup$ Could you please express your question? $\endgroup$
    – Muzammil
    Mar 27 at 14:18
  • $\begingroup$ So, when exporting a file via a custom Python addon, you want the file dialog to only show folders and video files? $\endgroup$
    – TheLabCat
    Mar 29 at 13:24

1 Answer 1

2
$\begingroup$

You can use the operators draw() method to set filter options. Just add this to your operator:

set_default_filter_settings: bool = True
def draw(self, context):
    if self.set_default_filter_settings:
        context.space_data.params.use_filter = True        # enable Filter
        context.space_data.params.use_filter_movie = True  # set movie filter as default
        self.set_default_filter_settings = False

Like @scurest mentioned, draw() is called every frame. So you have to make sure to set your default settings only once during the operators draw call, otherwise the user won't be able to change these settings. This can be done using a boolean flag like set_default_filter_settings in the example above.

$\endgroup$
2
  • 1
    $\begingroup$ Note that you probably want to do this only once. If you do it every frame the user won't be able to change them manually. $\endgroup$
    – scurest
    Apr 20 at 22:00
  • $\begingroup$ Good point! I have modified my answer. $\endgroup$
    – Robert L.
    Apr 21 at 15:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .