I want to find non excluded collections on a script, this are the collections who have this box checked (the one to the left of the eye)

enter image description here

So far I found that I can list all collections like this

for collection in bpy.data.collections:

But then I don't know how to tell if the collection is excluded or not, I checked the methods of the collection but I couldn't find this checkbox state there.

Is this the right way to read this data?

In the blender console when I toggle that checkbox I get this log bpy.data.scenes["Scene"].(null) = False but this (null) member is not present when I print the methods on the script


1 Answer 1


Similar to https://blender.stackexchange.com/a/267486/86891 LayerCollection is a construct that ties a Collection with a ViewLayer. The reason is because a single view layer can contain as many layer collections pointing to the same collection as you want.

import bpy

def all_layer_collections(view_layer):
    stack = [view_layer.layer_collection]
    while stack:
        lc = stack.pop()
        yield lc

def get_excluded_collections(view_layer):
    for lc in all_layer_collections(view_layer):
        if lc.exclude:
            yield lc.collection
def get_included_collections(view_layer):
    for lc in all_layer_collections(view_layer):
        if not lc.exclude:
            yield lc.collection

excluded_collections = get_excluded_collections(bpy.context.view_layer)
print([c.name for c in excluded_collections])
included_collections = get_included_collections(bpy.context.view_layer)
print([c.name for c in included_collections])
  • $\begingroup$ Thank you very much! It worked fine $\endgroup$ Oct 18, 2023 at 9:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .