set active scene with python

hi everyone

probably i have a very simple problem, but didn’t find a solution so far. So I hope you can help me.

How can i set the active scene with python?

In my python script i link a new scene into my blender file. This works so far, but it does not set it as active scene.

Thanks for help
Thomas

not certain cause did not have time to test this

but try this

bpy.data.screens[‘Default’].scene = bpy.data.scenes[‘Scene’]
bpy.context.screen.scene=bpy.data.scenes[‘Scene’]
let me know if it works ok

both of your ways work perfectly for me.

Thank you so much!

Hi Everybody,

sorry for rising this question again. The solution above works fine to change the scen.

BUT i have not found any way to make the python script actually work on the scene were it changed to.
This scrip part should:
1 Step) change from any current scene (for exampe Scene.001) to the scene named “Scene”.
2.Step) render the scene were it change to. In this case “Scene”

#*************
import bpy

def scene_change():
bpy.data.screens[‘Default’].scene = bpy.data.scenes[‘Scene’]
bpy.context.screen.scene=bpy.data.scenes[‘Scene’]
print(bpy.context.scene)

def render_scene():
bpy.ops.render.render()

scene_change()
render_scene()
#********************

for some reason this example first render the wrong scene (scene.001) and then changes to the scene i wanted to go.
is there a way to render a scene without making it current before?

Thanks for help!

I’d like to second this request - I’m having the same problem. brino, did you ever figure out a solution?

Thanks!

To clarify: I’m looking to do the exact same thing as brino, to render multiple scenes in a .blend file via python. So I need to change the scene, and then call bpy.ops.render.render(), but the render call doesn’t work on the new (second) scene - it just renders the first scene again and again.

I found a strange behavior that may be related to this problem: When I change scenes from “Scene1” to “Scene2” by calling

bpy.context.screen.scene = bpy.data.scenes[‘Scene2’]

… and then check on the other screens’ scenes via

for scr in bpy.data.screens:
print(scr.scene.name)

… the other screens (for example, the modeling screen, or the compositing screen) have not changed over to the new scene. The same thing happens if I switch screens via the GUI - for example, if I change from “Scene1” to “Scene2” while in the modeling screen, and then I switch to the compositing screen, Blender will switch back to “Scene1” on me.

I tried to set all the screens to the same scene using a for loop:

for scr in bpy.data.screens:
scr.scene = bpy.data.scenes[‘Scene2’]

… but it did not work. When I checked the screens later with:

for scr in bpy.data.screens:
print(scr.scene.name)

… only the active screen had changed to the new scene. It seems to me that ALL the screens should always be associated with the same (active) scene - is this a bug in the API? Any chance of a fix? Or is there anything else I can do?

THANKS for your help…

1 Like

I came across the same issue here, my solution was to call the scene from the render operator as below

bpy.ops.render.render(scene=‘SCENE NAME’)

worked like a charm, for my purposes at least