Import fbx file(s) and process result - not possible?

I’m looking for a way to import fbx files via the built-in fbx importer (including it’s file browser/properties) and then do further process after the import.

Maybe I’m missing something obvious, but so far (after several nights of testing code/searching the internet) I haven’t found a way.
Is this not possible in Blender? I understand that this is related to how operators work and the fact that code execution doesn’t wait for file-browsers etc.

I found a method that is quite close to working, but it’s missing a few key parts (how to stop if cancel is pressed or if the fbx is invalid).

Any other suggestions how to accomplish this?

import bpy
from bpy_extras.io_utils import ImportHelper

class ImporterFBX(bpy.types.Operator, ImportHelper):
    bl_idname = "importer.fbx"
    bl_label = "Import"
    bl_options = {'PRESET'}
    
    sceneobjects = 0
    currentobjects = 0

    def modal(self, context, event):

        # Objects imported, import done.
        if self.currentobjects > self.sceneobjects:
            return {'FINISHED'}

        self.currentobjects = len(bpy.data.objects)

        # Stop if esc is pressed.
        if event.type in {'ESC', 'RIGHTMOUSE'}:
            print("stopped")
            return{'CANCELLED'}       

        # How to stop if the cancel button was pressed?
        # How to stop if an error occured?

        # Print the result if still running.
        print("still running", event)
        return {'PASS_THROUGH'}

    def invoke(self, context, event):
        #save current number of objects on scene
        self.sceneobjects = len(bpy.data.objects)       

        # Deselect everything.
        bpy.ops.object.select_all(action='DESELECT')

        #start the import
        bpy.ops.import_scene.fbx("INVOKE_REGION_WIN")
        context.window_manager.modal_handler_add(self)

        return {'RUNNING_MODAL'}

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

if __name__ == "__main__":
    register()

    fbx_paths = ["path/to/file1.fbx", "path/to/file2.fbx"]

    # Loop over a list of paths to fbx files and open the dialog window and process the result for each one of them.
    # Every file might need to have different import properties. Below is just a quick example.
    for path in fbx_paths:
        bpy.ops.importer.fbx("INVOKE_DEFAULT", filepath=path)

        # Get the result of import via selected objects.
        print(bpy.context.selected_objects)

        # Do other stuff...
1 Like