how to erase objects from a scene?

Hello,

my Python script generates some new objects in an empty scene.

To make sure, the script works properly, I would like to erase all (possible, unnecessary) objects (like cube.001,…) at the beginning of the script. How can I do this in Python?

Even select all “A” and erase “X” in 3D-view would not clear Python objects but only Blender objects

Thanks for any help

Stranger

because you didn’t erased it. you just unlinked the objects from the current scene.

To make sure, the script works properly, I would like to erase all (possible, unnecessary) objects (like cube.001,…) at the beginning of the script. How can I do this in Python?

Then if you think backward. Create an empty scene and link your object to it? :wink:
This gonna work for sure.

Create an empty scene and link your object to it

indeed, good idea.
It partly works as all objects from Blender are deleted.
But there are still all previous objects in the (Python) memory as I can find out with


camdata = Camera.New('ortho')           # create new camera data
camdata.setName('newCam')
camdata.setLens(16.0)
scene = Scene.New('NewScene')           # create a new scene
camobj = Object.New('Camera')           # create a new camera object
camobj.link(camdata)                    # (*) link data to object first
scene.link(camobj)                      # and then link object to scene

#################
#create one object (Cube)
#################
(...)
Library.Open(file)
Library.Load("Cube.001", "Object", 0)
fr=Blender.Object.Get("Cube.001")
Library.Update()
scene.link(fr)

Library.Close()
scene.makeCurrent()                     # make this the current sce
Blender.Redraw()

obj=Blender.Object.Get()
print "all objects: ", obj

output in console:
all objects: [Object “Cube”], [Object “Cube.001”], [Object “Cube.002”], [Object “Cube.003”],…, [Object “Camera”], [Object “Camera.001”], …

The list above shows all objects I have created since start of Blender, while my script only creates one Cube and one Camera.

The 3D-view in Blender shows correctly one Cube and one Camera.

Right at testing runs, this is confusing, as I do not want to restart Blender each time…

At the moment this causes big headache as I do not kow how to handle this further on in my script…

What do I need, so the “old” objects are not linked to the “new scene” anymore, or how do I get rid of the old objects without restart??

Look . The Object.Get return all object. Linked or not.
If you want to know what is in one scene, ask the scene instead. So the list will be more suitable for you.
scene.getChildren() return a list of objects who is only in the current scene.

This will do the job.

Thanks so far, Gabio

scene.getChildren()
returns a more reasonable result, eg. only the object linked to the scene (not really surpring)

At the moment, I still have to handle the change of name of my object after each execution of the script:
1.run: [Object “Cube”]
2.run: [Object “Cube.001”]
3.run: [Object “Cube.002”]

Obviously, this happens due to a re-use of the same name (“Cube”). Is there some sort of smart solution?

[edit]
in the 3D-View I can change the name of an object (rightMB - N) by “overwrting” a name which has been used before but is not in the scene anymore. This is what I am thinking about… [/edit]

Obviously, this happens due to a re-use of the same name (“Cube”). Is there some sort of smart solution?
This is a normal behavior, you can’t have 2 time the same name, so blender add a number to it.
The smart solution is to not name it the same name… Or, just before naming your new object, check if this object already exist, then rename it to an other name. Like “garbage”. It doesn’t matter if you got more than one with ame name, it will only do garbage.001 garbage.002… when you use something like “Cube”.

Thanks for pointing me the right direction.

I put this routine at the beginning of my script


#generate new, EMPTY scene

alte_szenen=Scene.get()
neue_szene=Scene.New('NewScene')
neue_szene.makeCurrent()
for sc in alte_szenen:
	Scene.Unlink(sc)

#rename old objects
alte_objekte=Blender.Object.Get()
for ob_name in alte_objekte:
		ob_name.setName("Garbage")
# continue here

This cured all isues I had so far :slight_smile: (it only generates a growing number of Garbage objects, which should not bother me at the moment …)