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$
  • $\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 '19 at 12:13
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()
... 
| improve this answer | |
$\endgroup$
  • 1
    $\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 '19 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 '19 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 '19 at 14:09
1
$\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)
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you, Robert! $\endgroup$ – Axel Dec 12 '19 at 12:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.