2.5 - how can i select a new mesh on a new scene

If I create a new scene, and a new mesh-object on this new scene,
then i can’t select the new-object from python script.
I tried with this script:

import bpy

# crate new scene and activate it
scene = bpy.data.scenes.new( "NewScene" )
bpy.context.screen.scene = scene

# create new object
mesh = bpy.data.meshes.new( "NewMesh" )
mesh.from_pydata( [ [0,0,0], [0,1,0], [1,1,0], [1,0,0] ], [], [[0,1,2,3]] ) # simple plane
obj = bpy.data.objects.new( "NewObject", "MESH" )
obj.data = mesh
scene.objects.link( obj )

# deselect all objects on scene
for ob in scene.objects: ob.selected = False

# try to select the new object -- DOESN'T WORK!
obj.selected = True

From interactive python shell it is working, but as a script it doesn’t.
How can i do it?

(Exactly i want to duplicate the new object with bpy.ops.object.duplicate(),
but it should be selected.)

Thanks for any answers,

Try bpy.ops.object_select_name instead of those last two lines in your current script. Don’t deselect or select objects yourself, but use the available operators to do the job.

Stukfruit: that’s not the real problem though (besides, objects.select_name() doesn’t even seem to work in revision 27070). The real problem is in creating the new scene. For instance, this works correctly in 27070 (same code but without creating a new scene):

mesh = bpy.data.meshes.new( "NewMesh" )
mesh.from_pydata( [ [0,0,0], [0,1,0], [1,1,0], [1,0,0] ], [], [[0,1,2,3]] ) # simple plane
obj = bpy.data.objects.new( "NewObject", mesh )
scene = bpy.context.scene
scene.objects.link( obj )

for i in scene.objects: i.selected = False
obj.selected = True
scene.objects.active = obj

The problem is that even though context.screen.scene correctly sets the scene to the new scene, the context is not updated. This can be easily checked by inserting the following after the first paragraph of prem’s script:


It will show that the scene in the context is still the old scene. Selecting your object in this wrong context will fail, since the object isn’t even present in that scene. If you’d add the duplicate() function and removed the deselect part, it would even duplicate the previous selected object.

So the solution has to be found in a way to update the context with the new scene. Unfortunately I can’t find a way to do that.

i got the version 27115

i tried the first script and get an error on this line

obj = bpy.data.objects.new( “NewObject”, “MESH” )
error with

Traceback (most recent call last):
File “obadd1.py”, line 12, in <module>
TypeError: MainObjects.new(): error with argument 2, “object_data” - expected a
ID type

is it the same error you get ?
and doe sit ahve anything to do with the scene thing ?

anway to correct this ?
cause i need that on to work too and cannot

anyway to correct this ?

It does not have anything to do with the scene problem.
It’s just a change in the new object function (changed in revision 27070). Instead of passing the string “MESH”, you now need to pass the actual mesh data. Compare the two scripts above (prem’s and mine) and you’ll notice the difference.
Or as ideasman42 wrote in the commit:

ok sorry i did not see that one coming - i just change to the latest version
and i just reloaded in this last version the pipe script and it’s not working anymore

with same error for this MESH word !

i change it for the word mesh and it is working fine now !


ok see the mesh thing instead of “MEHS” word

good to know this

but wait a second here
i got theses other scripts using class
and they are working ok with the big word MESH

so why is it working ok when used in a class
and not working ok outside of theses class?


Yes, as Crouch wrote the problem is that the “bpy.context.scene” is not updated correctly.
Strange thing, if I type the script line by line in interactive python console then it is working.
May be some refresh or redraw function can help, like “Window.Redraw()” in Blender 2.49.
Is it exists in 2.5?

Related to mesh/“MESH” thing:

Under linux(64bit) system Blender2.5-r27115 accepts the “MESH” string as second argument
of bpy.data.objects.new() function. r27135 reported problem.

so it’s not use all over the versions ?

but is it going to stay with the mesh data or will it be back with the Word “MESH”


STILL a problem! Bugged it.

Here’s the response:

At the moment setting the current scene (or screen) is delayed until the next event loop, after everything has been handled. It would be useful if changing scenes was possible, but not sure if this should be considered a bug or current design limitation.