How to set active 'Scene Collection'? bpy.context.view_layer.active_layer_collection = bpy.data.collections['Master Collection'] not working.
Asked
Active
Viewed 3,412 times
4
Ray Mairlot
- 29,192
- 11
- 103
- 125
Alex
- 197
- 2
- 9
3 Answers
7
Set active 'Scene Collection'
scene_collection = bpy.context.view_layer.layer_collection
bpy.context.view_layer.active_layer_collection = scene_collection
Alex
- 197
- 2
- 9
3
You can loop trough all your [first level] collections and set the one you want with it's name
collections = bpy.context.view_layer.layer_collection.children
for collection in collections:
if collection.name == "name_of_the_collection_you_want_active":
bpy.context.view_layer.active_layer_collection = collection
If you want to loop in all collections looking for "My Collection" and set it active you can use this code Change active collection from johnzero7 :
#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
#Change the Active LayerCollection to 'My Collection'
layer_collection = bpy.context.view_layer.layer_collection
layerColl = recurLayerCollection(layer_collection, 'My Collection')
bpy.context.view_layer.active_layer_collection = layerColl
cscholl
- 153
- 7
-
I don't think @cscholl's solution works recursively - e.g. it will only work if the collection is not nested in another collection. – ddd May 25 '20 at 11:01
-
@ddd : you're right, I've edited the code to add a recursive exemple – cscholl May 26 '20 at 13:36
-1
#Change active collection
bpy.context.view_layer.active_layer_collection = bpy.context.view_layer.layer_collection.children['Master Collection']
-
Hi, thanks for the post. This site is not a regular forum, answers should be substantial, stand on their own, and thoroughly explain the solution and required steps. One liners and short tips rarely make for a good answer. If you can, [edit] your post and provide some more details about the code and how it works, perhaps add a few images illustrating some steps and final result. See How to write a good answer?, otherwise it may be converted to a comment. – Duarte Farrajota Ramos Jul 07 '23 at 14:09