Bad context after open new file with python

I make an addon that open a new clean file, but after i open the new file, i get an error after the code try to run this line:
bpy.ops.import_scene.fbx(filepath = path)

here is the error that i get:
RuntimeError: Operator bpy.ops.object.mode_set.poll() Context missing active object

bpy.ops commands are context-dependent, and should be avoided. If you absolutely must, you can do a context override:

The example provided is this:

Python
import bpy
 
override_context = bpy.context.copy()
area = [area for area in bpy.context.screen.areas if area.type == "VIEW_3D"][0]
override_context['window'] = bpy.context.window
override_context['screen'] = bpy.context.screen
override_context['area'] = area
override_context['region'] = area.regions[-1]
override_context['scene'] = bpy.context.scene
override_context['space_data'] = area.spaces.active
 
bpy.ops.wm.toolbar(override_context)

Please note that this code will not work for you as written, it’s just an example of the structure of what you need

Thx, i know that i should avoid ops, but i dont think i can in this case…
I will try to use the context override, but i dont understand why it should help in this case?
if i try to run this code without open a new file, its work fine, but after i open the new file its not work anymore, so i try to understand what are the different in the context before and after the new file was open

Your error makes it sound like you have to have an active object for this script to work, and I’m guessing your new scene doesn’t have an active object

i also try to set an active object before call this ops but it didnt work…

bpy.context.view_layer.objects.active = bpy.data.objects[‘Cube’]

What error are you getting with that command?

If you’re using Blender 3.2+, try using temp_override() instead.

import bpy
        
for w in bpy.context.window_manager.windows:
    s = w.screen
    for a in s.areas:
        if a.type == "VIEW_3D":
            with bpy.context.temp_override(window=w, area=a):
                bpy.ops.import_scene.fbx(filepath="D:\\Assets\\untitled.fbx")
            break
1 Like

This is the error that i get:
RuntimeError: Operator bpy.ops.object.mode_set.poll() Context missing active object

If your add-on isn’t commercial, could you share the code you have so far? Thanks.

thx, this work for me!

1 Like

It’s odd. As I discovered in this thread, the older context override API don’t seem to not work anymore. I wonder if they get officially deprecated, starting Blender 3.2 and after.

1 Like
bpy.ops.import_scene.fbx()

You want use a ops from an addon “io_scene_fbx”
you must contact Campbell Barton who is the coder of this addon.
https://developer.blender.org/p/campbellbarton/

or read the addon’s code before using it.

Hi… contacting @ ideasman42 has nothing to do with this thread’s topic…

The problem is not created by his addon but your use of his ops function…

Off course you must learn his addon’s code to use it easily…All is in his code. See about context and B3.2+

If not… you can contact this “Better FBX” addon’s author too:
https://www.mesh-online.net/fbx.html
@ Art Golf

You’re right that if I read his code and understand it, it could help me. but I also want to have the ability to use his code only by using the ops command. Most of the time the ops command for this command is working, but my problem was that after I open a new file it stop working anymore (and not only this ops, all the ops don’t have context so they don’t work after that…)

1first: you must verify if this io_scene_fbx addon is present and if is activated before to use this ops.

2nd:

so It’s that you don’t answer his last question: Blender answer something!

No, you must respect the context to use this ops. If you open a new file, you must return on a clean context.
Example: when you import a fbx file, blender 3.2 return automatically this:
bpy.ops.object.select_all(action=‘DESELECT’)
I tryed to import 3x FBX files and I had no problem.

1 Like