9

Can anybody help me write a script that excludes certain collections (for rendering)?

In the outliner the tooltip says: Layer.Collection.exclude and bpy.data.scenes["Scene"].exclude, so I've tried things like:

bpy.ops.outliner.LayerCollection('Structure').exclude
bpy.data.scenes["Scene"].LayerCollection('Structure').exclude

The online Python link is here: https://docs.blender.org/api/master/bpy.types.LayerCollection.html#bpy.types.LayerCollection.exclude but I don't know what to do with that information.

enter image description here

Ray Mairlot
  • 29,192
  • 11
  • 103
  • 125
edna
  • 816
  • 6
  • 13

3 Answers3

11

Use the View Layer

enter image description here

From the context view layer

>>> C.view_layer.active_layer_collection
bpy.data.scenes['Scene']...LayerCollection

>>> C.view_layer.active_layer_collection.name 'My Leaf 0.0'

>>> C.view_layer.active_layer_collection.exclude False

Alternatively from scene

>>> C.scene.view_layers['View Layer'].active_layer_collection
bpy.data.scenes['Scene']...LayerCollection

>>> C.scene.view_layers['View Layer'].active_layer_collection.name 'My Leaf 0.0'

>>> C.scene.view_layers['View Layer'].active_layer_collection.exclude = True

Related: Change active collection

brockmann
  • 12,613
  • 4
  • 50
  • 93
batFINGER
  • 84,216
  • 10
  • 108
  • 233
5

Today I struggled with the same question. I found out I could just use this in 2.83:

bpy.context.layer_collection.children['My Collection'].exclude = True bpy.context.layer_collection.children['My Collection'].exclude = False

Edit: tested in 2.90.1

bpy.data.collections['My Collection'].hide_render = True;
bpy.data.collections['My Collection'].hide_render = False;

Thomas Huijzer
  • 211
  • 2
  • 3
  • Thanks, the python tool tips seem to be incredibly hard to infer what the Python API is. I'm sure it was a lot easier to infer in <=2.79 – alex.p Oct 22 '20 at 21:46
  • This doesn't seem to work in 2.9. It's incredibly annoying having an API that constantly changes – alex.p Oct 22 '20 at 21:50
1

Great, thanks. My script now looks like this and works well:

def CollControlAll (context, CollList, CollLen):
    layer_collection = bpy.context.view_layer.layer_collection

    for lay in range(0,CollLen):
        layerColl = recurLayerCollection(layer_collection, CollList[lay])
        bpy.context.view_layer.active_layer_collection = layerColl
        layerColl.exclude = False
        layerColl.hide_viewport = False
edna
  • 816
  • 6
  • 13