Wait until ImportHelper is finished, then process the imported Objects - EDITED TITLE

Hi!
I’m having problems with my Add-On.
I’m creating a new Collection, making it active, opening up an FBX-Import Window and if selected, loading the FBX File.
After that I would like to join and clean up the data, add materials, etc.
Unfortunately all of the commands after the FBX import are run before the import actually finished.

Here is part of the script:

bpy.ops.import_fbx.read_data('INVOKE_DEFAULT')
print("Import FBX is ready")

The message gets printed before the import process finishes.
If I import an FBX via the UI, Blender hangs as long as it imports.
I though this will be the same with my Add-on but it seems that my import routine is called parallel to the execution of the rest of my script.
(I don’t mind if my Add-On hangs as long as the import is running).
Is there a way to wait until it is finished?
I read some examples about modal operators but I don’t really understand them. In those examples it seems to be important that the UI does not lock up. In the current state of my python experience level, I don’t mind if its locked up as long as it runs in sequence.

I found this code sample:

sceneobjects = 0
currentobjects = 0

def modal(self, context, event):
    if self.currentobjects > self.sceneobjects:
        bpy.ops.object.select_all(action='DESELECT')
        print("finished")
        return {'FINISHED'}
    else:
        self.currentobjects = len(bpy.data.objects)
        print(context.window_manager)
        return {'PASS_THROUGH'}

def invoke(self, context, event):
    #save current number of objects on scene
    self.sceneobjects = len(bpy.data.objects)
    #start the import
    bpy.ops.import_scene.fbx("INVOKE_REGION_WIN")
    context.window_manager.modal_handler_add(self)
    return {'RUNNING_MODAL'}

but I don’t know how to get it in my code.

I’m using the following two functions to display a file browser (Import_Helper) and load the FBX file:

### FBX Import
def read_fbx_data(context, fbx_path, use_some_setting):
    bpy.ops.import_scene.fbx(filepath = fbx_path)

class ImportFBXData(bpy.types.Operator, ImportHelper):
    bl_idname = "import_fbx.read_data"  
    bl_label = "Import FBX"
    filename_ext = ".fbx"

    filter_glob: StringProperty(
        default="*.fbx",
        options={'HIDDEN'},
        maxlen=255,  
    )
    def execute(self, context):
        return read_fbx_data(context, self.filepath, self.use_setting)

I call this as stated above from an interface button:

... other functions run ahead ...
bpy.ops.import_fbx.read_data('INVOKE_DEFAULT')
... other functions should run after but these need to wait until the FBX import is finished...

Can someone help me understand how to modify my functions with the above code sample? Which function should run modal? The read_fbx_data or the Import_Helper?

Thanks for any help.

EDIT:
I was completely wrong. It’s not the FBX converter which runs parallel. It’s the Import_Helper which is {‘RUNNING_MODAL’}. I guess here lies the Problem.
Hard coding the FBX File name and path and only invoking bpy.ops.import_scene.fbx works as it should and executes everything sequentially.
So the question is now, how do I get my script to wait until the ImportHelper is finished?

Take a look at this. This gives answer for importing stl files then once finished, join them.

1 Like

Thanks for the link! :+1: Yesterday evening I was able to solve my problem the same way.
Everything what needs to be done after invoking the ImportHelper must be in the same Class. It’s actually very obvious but as a Blender Python novice I didn’t know that.

1 Like