To get the current active collection through the Blender Python API we need to access it through the “view_layer” layer.
The following command will return the active collection from the scene:
1 2 3 |
bpy.context.view_layer.active_layer_collection.collection # <bpy_struct, Collection("Collection 2") at 0x000001F872BE3608> |
The active collection can also be accessed directly through the current context:
1 2 3 |
bpy.context.collection # <bpy_struct, Collection("Collection 2") at 0x000001F872BE3608> |
Note that toggling the collection visibility in the outliner using the “eye” and “monitor” icons resets the currently active collection and makes the scene’s main collection active.
*.blend file with the example code for my Patreon subscribers