Remove all collections and objects in outliner with phython

Hi all,

I would like to reset my outliner and clean it, no matter what I have in my General file. The goal is an empty canvas.

In a second step I would like to add collections for the ‘Studio’ (Camera, lights, background,…)
and another collection for the items to be rendered

When I run my script below, it does not delete the collections and I get Studio.001, Studio.002,… when I run it more often.

I would like to get python commands for the three manual actions in the outliner to select the “Scene Collection”, “select all” and “delete”

Here is my code:

import bpy

context = bpy.context
scene = context.scene

reset the complete scene

for c in scene.collection.children:
scene.collection.children.unlink(c)
scene.collection.children.remove(c)

#Recursivly transverse layer_collection for a particular name
def recurLayerCollection(layerColl, collName):
found = None
if (layerColl.name == collName):
return layerColl
for layer in layerColl.children:
found = recurLayerCollection(layer, collName)
if found:
return found

Create a known scene with Studio including a camera, light and a plane

and later a collection called items for new elements to be drawn

#Create first collection for Camera, lights and a plane
myColl = bpy.data.collections.new(‘Studio’)
bpy.context.scene.collection.children.link(myColl)

#Create another collection for items
myColl = bpy.data.collections.new(‘Items’)
bpy.context.scene.collection.children.link(myColl)

#Change the Active LayerCollection to ‘Studio’
layer_collection = bpy.context.view_layer.layer_collection
layerColl = recurLayerCollection(layer_collection, ‘Studio’)
bpy.context.view_layer.active_layer_collection = layerColl

add a light

light_data = bpy.data.lights.new(‘Light’, type=‘POINT’)
light = bpy.data.objects.new(‘Light’, light_data)
bpy.context.collection.objects.link(light)
light.location= (3, -4, 5)
light.data.energy=200.0

add a camera

bpy.ops.object.camera_add(enter_editmode=False, align=‘VIEW’, location=(0, -10, 5), rotation=(1.17461, 0, 0), scale=(1, 1, 1))

#Change the Active LayerCollection to ‘Items’
layer_collection = bpy.context.view_layer.layer_collection
layerColl = recurLayerCollection(layer_collection, ‘Items’)
bpy.context.view_layer.active_layer_collection = layerColl

#add items

pos = Vector((0, 0, 0.5))
init_x = pos.x
init_y = pos.y
init_z = pos.z

base =4
sc=0.5

adding two loops full of cubes

for ycubes in range(base):
for xcubes in range(base):
bpy.ops.mesh.primitive_cube_add(size=1, location = pos, scale=(sc, sc, sc))
pos.x+=1

pos.x = init_x
pos.y +=1

To remove all collections and objects:

import bpy
scene = bpy.context.scene
bpy.data.scenes.new("Scene")
bpy.data.scenes.remove(scene, do_unlink=True)

Hi JuhaW,

Thank you for your suggestion. Unfortunatelly it does not work.

I added you code and it cleans the list once. But when I run the script 2 or 3 times, including the rest of the code above, I get a “Studio.001” and with the next a “Studio.002”.

To me it looks, that some unlinked collections are still existing in the memory and allthough they are not visible after your code, they still increase the extentional count when running the code again.

How can I completely delete an unlinked collection would be my rephrased question be.

Thank you for assistence!!

Dirk

Since the new scene is created while the old scene still exists the new Scene can not be named “Scene” because that name is allready taken.

But you can just rename the Scene by adding the following line:

bpy.data.scenes[0].name = “Scene”

Thank you, I changed the order and now it works well!