1
$\begingroup$

good day. I have a group hierarchy which should be counted by scenes separately. for this, my code creates scenes, and then I manually turn off what I don’t need in a particular scene. I also have code that can exclude 1 group, but I could not adapt it to exclude everything that I do not need. Help completeenter image description here

        def include_only_one_collection(view_layer: bpy.types.ViewLayer, collection_include: bpy.types.Collection):
        for layer_collection in view_layer.layer_collection.children:
            if layer_collection.collection != collection_include:
                layer_collection.exclude = False
            else:
                layer_collection.exclude = True


    view_layer = bpy.data.scenes["1_env"].view_layers["View Layer"]
    collection_include = bpy.data.collections["space"]
    include_only_one_collection(view_layer, collection_include)

    def include_only_one_collection(view_layer: bpy.types.ViewLayer, collection_include: bpy.types.Collection):
        for layer_collection in view_layer.layer_collection.children:
            
            if layer_collection.collection != collection_include:
            
                layer_collection.exclude = False

            else:
                layer_collection.exclude = True
$\endgroup$

1 Answer 1

1
$\begingroup$

Assuming you have a list of collection names you do want to include in the view layer.

There are a few solutions that use recursion to traverse the view layer's layer collection tree but it has some shortcomings, like you can attain the python stack recursion limit if you have a big tree. This solution uses a while loop instead.

import bpy

included_collection_list_names = ["Collection 1", "Collection 2"]
layer_collections = list(bpy.context.view_layer.layer_collection.children)

while layer_collections:
    layer_collection = layer_collections.pop(0)
    layer_collections.extend(layer_collection.children)
    layer_collection.exclude = layer_collection.name not in included_collection_list_names

Here's a more in depth explanation :

# Replace your actual collection names here :
included_collection_list_names = ["Collection 1", "Collection 2", "Collection 3"]
for layer_collection in bpy.data.scenes["1_env"].view_layers["View Layer"].layer_collection.children:
    layer_collection.exclude = layer_collection.name not in included_collection_list_names 

Conversely if you have a list of collection names you want to exclude :

excluded_collection_list_names = ["Collection 1", "Collection 2", "Collection 3"]
for layer_collection in bpy.data.scenes["1_env"].view_layers["View Layer"].layer_collection.children:
    layer_collection.exclude = layer_collection.name in excluded_collection_list_names 

Also, instead of

for layer_collection in bpy.data.scenes["1_env"].view_layers["View Layer"].layer_collection.children:

You can use

for layer_collection in bpy.context.view_layer.layer_collection.children:

if the view layer you're trying to modify is the one currently open in the interface.

$\endgroup$
4
  • $\begingroup$ figured it out. What if I need to turn off a collection that is in a collection? This only works in main collections(( $\endgroup$
    – Welet
    May 25, 2022 at 12:22
  • $\begingroup$ @Welet you're right ! I added an extension for this case at the top of the answer. Cheers $\endgroup$
    – Gorgious
    May 25, 2022 at 14:10
  • $\begingroup$ It won't work if you want to exclude more deeply nested collections though, as "view_layer.layer_collection.children" doesn't return all children recursively. $\endgroup$ Aug 21, 2022 at 10:48
  • $\begingroup$ @JanKadeřábek Well no it doesn't, but the children are recursively added to the base list on each iteration of the while loop on line 8. Then their children are added to the list when their turn comes, and so on... Add print([l.name for l in layer_collections]) after the while line to see what the loop actually does. $\endgroup$
    – Gorgious
    Aug 21, 2022 at 20:36

You must log in to answer this question.

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