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))
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”]
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:
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
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…
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
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”.
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
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.