1

I am trying to select a collection in blender by name. I was successful to find the collection in the outliner and make it an active collection using the following code

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
    
    layer_collection = bpy.context.view_layer.layer_collection
    layerColl = recurLayerCollection(layer_collection, 'Set')
    bpy.context.view_layer.active_layer_collection = layerColl

This highlights the collection I am looking for but does not actually select it, as shown in the picture.

Outliner Screenshot

As you can see, it highlighted the collection "Set", but it does not select it. How can I select it? I could not find anything in the API that can help. I will be grateful for any help or suggestion.

Bhavin
  • 95
  • 1
  • 8
  • Any luck? I am running into the exact same issue and haven't found a solution yet. And this makes duplicating a collection fail if the active and selected collections are not the same. – Glenn J. Schworak Jan 04 '23 at 14:31

2 Answers2

0
collection = bpy.data.collections["Collection"]
bpy.context.view_layer.active_layer_collection = bpy.data.scenes['Scene'].view_layers['ViewLayer'].layer_collection.children[collection.name]

don't know if you have to adjust the above for more deeply nested collections, but this seemed to work.

-1

select a collection in blender by 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
  • You simply repeated the code that doesn't work from the original question. bpy.context.view_layer.active_layer_collection activates but doesn't select the collection. – Glenn J. Schworak Jan 04 '23 at 14:29