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