3
$\begingroup$

How to set active 'Scene Collection'? bpy.context.view_layer.active_layer_collection = bpy.data.collections['Master Collection'] not working.

$\endgroup$

2 Answers 2

5
$\begingroup$

Set active 'Scene Collection'

scene_collection = bpy.context.view_layer.layer_collection
bpy.context.view_layer.active_layer_collection = scene_collection
$\endgroup$
3
$\begingroup$

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


$\endgroup$
2
  • $\begingroup$ I don't think @cscholl's solution works recursively - e.g. it will only work if the collection is not nested in another collection. $\endgroup$
    – ddd
    May 25, 2020 at 11:01
  • $\begingroup$ @ddd : you're right, I've edited the code to add a recursive exemple $\endgroup$
    – cscholl
    May 26, 2020 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.