# How to set bpy.context.window.scene in the right context?

I would like to set the active scene with bpy.context.window.scene = my_scene

Now the problem is that normally this works if you are in the right context, the problem (in my specific case) is inside a function that is executed immediately after bpy.ops.render.render ('INVOKE_DEFAULT') immediately after the end of the render I get:

for s in bpy.data.scenes:
if s == my_scene:
context.window.scene = s


AttributeError: 'NoneType' object has no attribute 'scene'

so I guess currently the context has changed to that of rendering, am I wrong?

How can I solve this?

• The issue here is context.window is None Similar to issue the other day in my deleted answer re closing render preview window. It seems somewhere the context gets "lost". Try windows = set(context.window_manager.windows[:]) before and after opening the render window. The difference will be the new window if one was opened. – batFINGER Jan 17 '20 at 15:26
• @batFINGER I'd like to review your canceled answer, to better understand what you tried to do. I am doing an internet search and some tests with blender to see if I can find a solution. I no longer have that code because I haven't saved it – Noob Cat Jan 17 '20 at 18:11

I think the answer to my question is a simple copy of context. So:

ctx = bpy.context.copy()

bpy.ops.render.render('INVOKE_DEFAULT')

###This is handled with some handlers, but essentially seems to work with a simple context copy

for s in bpy.data.scenes:
if s == my_scene:
ctx['window'].scene = s


I think this is the answer to my question, but I won't vote for it right now as I could probably cheat in future problems. For now everything seems to work ok.