I'm using the fbx importer to import an fbx file and after the import is done I want to process the imported objects.
I need to open the importer window from bpy.ops.import_scene.fbx as I want to have access to all the fbx properties without having to hard-code my own duplicate of all the properties.
This seems to be quite complicated and I haven't found a good solution. Below is a method that seems to work but there is a few problems with it.
What if the user press the "cancel" button? The modal operator is still running then.
What if the fbx file can't be imported (corrupted file for example), the operator will still be running.
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() # 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: # The above operator is called from another operator via: bpy.ops.importer.fbx("INVOKE_DEFAULT", filepath=path) # Get the result of import via selected objects. print(bpy.context.selected_objects) # Do other stuff...```