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

Giving this a last bump in case someone has a suggestion.

Usually there is no code after the registration because… well it’s only to registration of the addon… the action takes place… well… in the addon… ( for Operators for example in the execute(self, context) method… ),

I myself had ( and have) my problems with with MODAL because it doesn’t seems to work like in usual GUIs that the dialog itself is modal but the workplace is still usable ( so the work) and the additional modal_handler_add(operator)going :crazy_face: myself… but i could (be / get it) totally wrong :sweat_smile:

The bpy.ops.import_scene.. is an operator so it returns FINISHED if it had worked or CHANCELED if it was aborted… (or a runtime error…)… If in doubt see Docs Blender Api bpy.ops… So you should use this info.

You may also look into the code of another (working) addon for batch import OBJ-file (maybe better than the found code snippet ??) which is according to the author

The add-on itself is basically just a wrapper of wm.obj_import and…

see:

there is essentially a:

    def execute(self, context):
...
        for selection in self.files:
...
                bpy.ops.wm.obj_import( ... )
...
        return {'FINISHED'}

So you might include any operation before the return

Of course any options have to be made in an additional panel or something… I hope this helps to understand it a little more ( and maybe i rethink the MODAL thing :wink: ).

Thanks for the reply.

The problem in my case is basically that I have a list of filepaths (that might require different import settings). So for each file I want to bring up the built-in fbx importer dialog. And after each import I want to do other stuff with the meshes (parenting, renaming etc) before proceeding to the next file

So the big problem is how to “pause” the operator while import properties are open.