1
$\begingroup$

I'm a bpy beginner and I'm facing a error with python scripting on the line bpy.ops.import_scene.fbx(filepath="humor.fbx") I tried several method but still not working. Anything would be appreciated, thanks.

Here're 3 lines to reproduce this problem.

import bpy
bpy.ops.wm.open_mainfile(filepath="bear.blend") 
bpy.ops.import_scene.fbx(filepath="humor.fbx")

And here're the error logs.

Error: Python: Traceback (most recent call last):
  File "/media/johnny/5CD86FA3D86F7A62/checkpoints/proHMR_vec/1122/bear.blend/Text", line 3, in <module>
  File "/home/johnny/Documents/blender-2.93.2-linux-x64/2.93/scripts/modules/bpy/ops.py", line 132, in __call__
    ret = _op_call(self.idname_py(), None, kw)
RuntimeError: Error: Python: Traceback (most recent call last):
  File "/home/johnny/Documents/blender-2.93.2-linux-x64/2.93/scripts/addons/io_scene_fbx/__init__.py", line 220, in execute
    return import_fbx.load(self, context, filepath=self.filepath, **keywords)
  File "/home/johnny/Documents/blender-2.93.2-linux-x64/2.93/scripts/addons/io_scene_fbx/import_fbx.py", line 2849, in load
    _(); del _
  File "/home/johnny/Documents/blender-2.93.2-linux-x64/2.93/scripts/addons/io_scene_fbx/import_fbx.py", line 2843, in _
    root_helper.build_hierarchy(fbx_tmpl, settings, scene, view_layer)
  File "/home/johnny/Documents/blender-2.93.2-linux-x64/2.93/scripts/addons/io_scene_fbx/import_fbx.py", line 2278, in build_hierarchy
    child.build_hierarchy(fbx_tmpl, settings, scene, view_layer)
  File "/home/johnny/Documents/blender-2.93.2-linux-x64/2.93/scripts/addons/io_scene_fbx/import_fbx.py", line 2238, in build_hierarchy
    bpy.ops.object.mode_set(mode='EDIT')
  File "/home/johnny/Documents/blender-2.93.2-linux-x64/2.93/scripts/modules/bpy/ops.py", line 132, in __call__
    ret = _op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.object.mode_set.poll() failed, context is incorrect

location: /home/johnny/Documents/blender-2.93.2-linux-x64/2.93/scripts/modules/bpy/ops.py:132
```
$\endgroup$

1 Answer 1

1
$\begingroup$

When you load a new file with open_mainfile the current file gets thrown out. That includes the context of the Python script you're running. So you usually shouldn't put code after an open_mainfile call.

You can run code after a file loads by using handlers instead. It would look like:

import bpy
from bpy.app.handlers import persistent

# @persistent tells Blender this function should remain
# across file loads
@persistent
def load_handler(dummy):
    bpy.ops.import_scene.fbx(filepath="humor.fbx")

    # Unregister ourselves
    bpy.app.handlers.load_post.remove(load_handler)

# Register load_handler to run after we load a file
bpy.app.handlers.load_post.append(load_handler)

bpy.ops.wm.open_mainfile(filepath="bear.blend")
$\endgroup$

You must log in to answer this question.

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