how to selecting a collection via python??

import bpy    
Cam= bpy.context.scene.camera      
collections = bpy.context.view_layer.layer_collection.children
for collection in collections:
    if collection.name == Cam.name:
        bpy.context.view_layer.active_layer_collection = collection

This is the original code that can set collection active.now if collection have childen collection(as shown in pic). how to set collection chilren(“collction 10” or “collction 11”) as avtive collection via python?

enter image description here

Simple addon that make active collection by active object selected
SAC_0.1.0
mapped to “Y” key.

Switch_Active_Collection_0_1_0.py
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

import bpy
bl_info = {
    "name": "Switch Active Collection",
    "location": "3D View / Outliner, (Hotkey Y)",
    "version": (0, 1, 0),
    "blender": (2, 90, 0),
    "description": "Switching active Collection to the active Object selected",
    "author": "APEC",
    "category": "Object",
}

#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

class OUTLINER_OT_switch_collection(bpy.types.Operator):
    """Makes an active collection where the active object is located"""
    bl_idname = "outliner.switch_collection"
    bl_label = "Switch Active Collection"
    #bl_options = {'REGISTER', 'UNDO'}

    @classmethod
    def poll(self, context):
        ar = context.screen.areas
        __class__.area = next(
            (a for a in ar if a.type == 'OUTLINER'), None)
        return __class__.area

    def execute(self, context):
        obj = bpy.context.object
        ucol = obj.users_collection

        #Switching active Collection to active Object selected
        for i in ucol:
            layer_collection = bpy.context.view_layer.layer_collection
            layerColl = recurLayerCollection(layer_collection, i.name)
            bpy.context.view_layer.active_layer_collection = layerColl
        return {'FINISHED'}

def draw_sync_collection(self, context):
    self.layout.operator("outliner.switch_collection", text="", icon="FILE_TICK")

addon_keymaps = []


def register():
    bpy.types.OUTLINER_HT_header.append(draw_sync_collection)
    
    bpy.utils.register_class(OUTLINER_OT_switch_collection)
    wm = bpy.context.window_manager
    kc = wm.keyconfigs.addon.keymaps
    km = kc.get("Object Mode")
    if not km:
        km = kc.new("Object Mode")
    kmi = km.keymap_items.new("outliner.switch_collection", "Y", "PRESS")
    addon_keymaps.append((km, kmi))

    km = kc.get("Outliner")
    if not km:
        km = kc.new("Outliner", space_type="OUTLINER")
    kmi = km.keymap_items.new("outliner.switch_collection", "Y", "PRESS")
    addon_keymaps.append((km, kmi))


def unregister():
    bpy.types.OUTLINER_HT_header.remove(draw_sync_collection)
    
    bpy.utils.unregister_class(OUTLINER_OT_switch_collection)

    for km, kmi in addon_keymaps:
        km.keymap_items.remove(kmi)
    addon_keymaps.clear()
2 Likes

thank you i will test it.

I made a different version.
If it’s active, it will switch collection by clicking on object.
switch_collection_by_active_object



!updated

4 Likes

Nice ! Although it won’t work if your object is linked in several collections.

Animation3

BTW your script always selects the last collection an object is linked in, but it loops over all of them in the process.

I’m interested if you find a solution :slight_smile:

re Unique identifier for layer collections?

1 Like

This “linked” thing so weird.
It need somehow made exception for this type of things.
My script uses active object to set collection active, BUT
on you gif linked objects all active (all three of them) :crazy_face:
So it need somehow make active collection by click-select in Outliner…
(maybe with bpy.context.selected_ids) no.
But2 if click on linked object in 3DView it selects all in Outliner…

I don’t know if it even possible.

1 Like