0
$\begingroup$

I would like to replicate in a script what happens if i exclude a collection from a view layer via hotkey E (bpy.ops.outliner.collection_exclude_set()).

I cannot seem to find that exclude in the tree. Is the E hotkey doing a hide render and viewport, or is there more to that? How should i best approach this?

Thanks, Axel

$\endgroup$
1
  • $\begingroup$ Hi Robert, unfortunately not really - i can get it done with the active_layer_collection, but what i want to achive is the following: Lets say i got 3 view layers (A,B,C) and 3 collections (a,b,c). I need collection a to be the only active one in view layer A, and so on. So i have to do things outside the currently active layer. $\endgroup$
    – Axel
    Dec 12, 2019 at 12:13

2 Answers 2

0
$\begingroup$

IMO is a dupe easier to explain with answer... see RG has beaten me to it.. anyhoo

  • i can get it done with the active_layer_collection, but what i want to achive is the following: Lets say i got 3 view layers (A,B,C) and 3 collections (a,b,c). I need collection a to be the only active one in view layer A, and so on. So i have to do things outside the currently active layer
>>> for vl in C.scene.view_layers:
...     for l in vl.layer_collection.children:
...         l.exclude = l.name.lower() != vl.name.lower()
... 
$\endgroup$
3
  • 2
    $\begingroup$ I didn't get the path right. C.scene.view_layers[x].layer_collection.children[y].exclude was what i was looking for. Thanks! $\endgroup$
    – Axel
    Dec 12, 2019 at 12:58
  • $\begingroup$ This only works great, yet only for non-nested collections - which rough direction would i go if the layers are nested? Thanks! $\endgroup$
    – Axel
    Dec 17, 2019 at 14:04
  • $\begingroup$ Think the answers are basically the same. Would need to recurse the collections see blender.stackexchange.com/a/137866/15543 $\endgroup$
    – batFINGER
    Dec 17, 2019 at 14:09
3
$\begingroup$

First you need to retrieve the view layer you'd like to modify. The view layers in the scene can be accessed through bpy.context.scene.view_layers or if you want to access view layers of other scenes you can use bpy.data.scenes["Name of the scene"].view_layers.

In order to exclude a specific collection the LayerCollection has to be accessed, because that is where the exclude property is stored. While iterating over the children of the master layer collection from the view layer, we can check for each entry if it matches the collection that we want to include. If it doesn't, it can be excluded through its LayerCollection and otherwise be included.

import bpy

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 = True
        else:
            layer_collection.exclude = False

if __name__ == "__main__":
    view_layer = bpy.context.scene.view_layers["View Layer"]
    collection_include = bpy.data.collections["Collection"]
    include_only_one_collection(view_layer, collection_include)
$\endgroup$
3
  • $\begingroup$ Thank you, Robert! $\endgroup$
    – Axel
    Dec 12, 2019 at 12:59
  • $\begingroup$ And if you need to exclude a few more collections? I'm writing a script to have several enabled and disabled groups in each scene. but for some reason it does not turn off more than one $\endgroup$
    – Welet
    May 12, 2022 at 13:27
  • $\begingroup$ @Welet You would have to set the exclude property for each collection accordingly. Perhaps it would be a good idea to post a new question to explain the particular problem you're facing if this answer does not solve it. $\endgroup$
    – Robert Gützkow
    May 12, 2022 at 18:50

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