How to create a new scene and make it active one in 2.5x

hi,
working with python in 2.56,
i need to draw new objects into a new scene
i can create a new scene, but making it the active one effectively happens first after script closes. Seems I am not able to change the context from within the running script.

# how to switch to the new scene?? (migius)
cur_scene = bpy.context.scene
print("current Scene?: %s" % (cur_scene))
new_scene = bpy.data.scenes.new('newname')
bpy.data.screens.scene = new_scene
new_context = bpy.context.scene
print("newScene?: %s" % (new_context))

Is it a bug, or is there another method to do it?

bpy.ops.scene.new(type=‘EMPTY’) will create a new empty scene and bring it into context… I may be wrong but if you use ops to create something it gets context… if you don’t it doesn’t.

As for changing context to an existing scene in script… no idea. But you can access it through bpy.data.scenes[“newscene”]

That is odd, the first thing that came to my mind was…

s = bpy.context.scene["Scene"]

But this does not work, I get a key error even though the scene “Scene” does exist.

bpy.context.scene is the current Scene, the collection of Scenes in the document is in bpy.data.scenes. If you query the value of the former in the Python Console, it returns the exact reference you have to use to access it from the latter:

>>> bpy.context.scene
bpy.data.scenes["Scene"]

can someone make the first little script work ok
i cannot make it work !

also if you add an empty new scene then how do you give it a new name
or does it takes a default name like scene.001 but then how to change it!

Thanks happy 2.5

Change its name property to rename. If it’s the last scene you’ve added it can always be accessed by index… note you can name all your scenes the same, prob not recommended.

bpy.data.scenes[len(bpy.data.scenes)-1].name = "New Name"

or if added with ops.scene.new() use the bpy.context.scene.name

cur_scene = bpy.context.scene
print("current Scene?: %s" % (cur_scene))
bpy.ops.scene.new(type='EMPTY')     
bpy.context.scene.name = "newscene"
new_context = bpy.context.scene
print("newScene?: %s" % (new_context))

thanks for hints, but the problem is still not solved.
it seems, that as long as the script is running the context.scene can not be changed

  • bpy.ops.scene.new(‘new_name’) made from within the script will update the context first after terminating the script
    @batFINGER, your code doesn’t switch to new scene, only changes the name of current scene.

edit add: i need this functionality for my importer. It should import multiple DXF files into separate scenes.

edit2: a workaround is to use bpy.data… instead of bpy.ops…

Oops … serves me right for not testing first… using the indexing method is prob the go then., my apologies.

after testing second example

got this little problem

it will add a new scene with proper name
but the First scnene’s name has also been change for scene.001

so why is the first scene’s name changing too?

Thanks

@Ricky: Unless you manage the name of datablocks explicitly, Blender will auto create names for you.

@Migius:

as long as the script is running the context.scene can not be changed

You may not really need to change the context, just create the scenes you require and use those variables for linking objects.

At some level, your import routine is linking the object to the current scene. You may want to take the approach of “pre-creating” new objects in each scene. Pre-create the scenes, if they do not exist as well. Then after you run your multiple imports, run a routine to “map/re-assign” all the imported meshes to the pre-created objects


myScene1.objects.link(ob1)
myScene2.objects.link(ob2)
myScene3.objects.link(ob3)

yes but in little last example
it did add a new scene with right name but the first scene name was changed!

how come ?

cause here we added a new scene with name and did not touch the first scene as such!

happy 2.5

@Ricky: It looks like the 2.5 API scene creation needs to be extended.
http://www.blender.org/documentation/250PythonDoc/bpy.ops.scene.html

If you examine the link, you will see that the .new() function only accepts a type. It should accept a type and an optional name. Given the current state of the API I don’t think you can avoid the situation you experienced.

It is a bug in the API.

Blender created the new scene called “Scene”. Because there was already a scene called “Scene” it renamed that one to Scene.001 to make sure it could make a new scene called “Scene”.

We should be able to pass something like this…

bpy.ops.scene.new(type='EMPTY', "my scene name")

But the API is not done yet.

ok a bug is a bug !

hope it goes away soon !

got ot experoment later this weekend

but got some notes from last month

bpy.data.screens[‘Default’].scene = bpy.data.scenes[‘Scene’]
bpy.context.screen.scene=bpy.data.scenes[‘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()
but have to check theses out

happy 2.5

What gets me is the context thing… if you add a new object using bpy.ops it gets context in a script … just like if you did it in the UI… If you add a new scene in the UI it gets context… but not if you do it in a script.

Hello -

I have a related question in another thread - please see:
http://blenderartists.org/forum/showthread.php?t=201501

Any advice is appreciated! Thanks in advance…