[Solved]Switching scenes in 2.8 possible?

OK, I was trying to see if I could get one of my addons to work in 2.8. I need to create a new scene, new collection, and then add some objects to the new collection. I need to switch my scenes before I add my collection and my objects. Right now, my objects are added to the current scene I am on, unless I switch it. Is it possible to switch scenes anymore in 2.8 with python? In 2.79 you could do something like this ,but it no longer works.

    #Get current scene
    my_current_scene = bpy.context.screen.scene


    #Switch to my new scene		
    bpy.context.screen.scene = bpy.data.scenes['switch to different scene']
    #.......add my stuff to my new scene here......


    #Now switch back to the previous scene
    bpy.context.screen.scene = my_current_scene
1 Like

In 2.8 you can set bpy.context.window.scene to change the active scene.

2 Likes

@brecht Yeah, that did the trick. I ran into a issue I never had before and not sure if you would know what’s going on. If I run this code from the text editor it works like I want it to ,but if I try it as a operator it doesn’t work the same. As a operator it adds the new scene, switches to the scene, but it adds the sphere to the previous scene and errors out at renaming the sphere. Any ideas why that’s happening?

import bpy

#Add new scene
new_scene = bpy.data.scenes.new(name='My New Scene')
#Make "My New Scene" the active one
bpy.context.window.scene = new_scene
#Add sphere
bpy.ops.mesh.primitive_uv_sphere_add(segments=64, ring_count=32, location=(0.0, 0.0, 0.0))
#Add smooth shading
bpy.ops.object.shade_smooth()
#Rename sphere
act_obj = bpy.context.active_object
act_obj.name = 'My Sphere' 

It depends on the editor you are running the operator in, hard to say without knowing the specifics of how and where you use this in an operator.

Right now, I just stuck it in properties under scene. Here’s a example that does different than the code above that works when running in the text editor.

import bpy

#####################################################################
#Panel
#####################################################################
class NewScenePanel(bpy.types.Panel):
	"""Creates a Panel in the Tool properties window"""
	bl_label = "Create New Scene"
	bl_idname = "scene.create_new_scene"
	bl_space_type = 'PROPERTIES'
	bl_region_type = 'WINDOW'
	bl_context = "scene"

	def draw(self, context):
		layout = self.layout

		row = layout.row()
		row.operator("scene.new_scene_operator")

#####################################################################
#New scene operator
#####################################################################
def main(context):
	#Add new scene
	new_scene = bpy.data.scenes.new(name='My New Scene')
	#Make "My New Scene" the active one
	bpy.context.window.scene = new_scene
	#Add sphere
	bpy.ops.mesh.primitive_uv_sphere_add(segments=64, ring_count=32, location=(0.0, 0.0, 0.0))
	#Add smooth shading
	bpy.ops.object.shade_smooth()
	#Rename sphere
	act_obj = bpy.context.active_object
	act_obj.name = 'My Sphere'


class NewSceneOperator(bpy.types.Operator):
	"""Tooltip"""
	bl_idname = "scene.new_scene_operator"
	bl_label = "New Scene Operator"


	def execute(self, context):
		main(context)
		return {'FINISHED'}


#####################################################################
def register():
	bpy.utils.register_class(NewScenePanel)
	bpy.utils.register_class(NewSceneOperator)

def unregister():
	bpy.utils.unregister_class(NewScenePanel)
	bpy.utils.unregister_class(NewSceneOperator)

if __name__ == "__main__":
	register()

I think I got it to work doing this.

new_scene = bpy.data.scenes.new(name='My New Scene').
override = {'scene': new_scene}
#Add sphere
bpy.ops.mesh.primitive_uv_sphere_add(override, segments=64, ring_count=32, location=(0.0, 0.0, 0.0))

The active scene in the properties editor doesn’t change until after it redraws, I believe this is the same in 2.7. So you can indeed work around this problem by setting it in the context explicitly.