How to do this?

I've tried

bpy.context.view_layer.layer_collection["my_collection"].exclude = False


bpy.context.layer_collection["my_collection"].exclude = False

and tried looping through collections:

def execute(self, context):
    collections = get_viewport_ordered_collections(bpy.context)
    for c in collections:
          if c.name == "my_collection":

but the error says get_viewport_ordered_collections is not defined


1 Answer 1


So, it appears that you cannot explicitly tell a collection to turn off (exclude or turn off the check box) via Blender's python api. Evidently .exclude is not available to be set.

The way I finally managed to do this is by turning off the viewport display of the collection, which in turn turns off the viewport display and render flags of all the objects in the same named collection:

bpy.data.collections['My Collection'].hide_viewport = False
bpy.data.collections['Line Art'].hide_render = False
  • $\begingroup$ Suggested an edit to: How can I exclude a collection from rendering with Python? (the proper way to do it). I'd recommend use get('your_collection_name') instead to avoid our famous index error. Note that using the data block reference (bpy.data.collections...) might cause issues if the collection is not part of the current scene. Also bpy.context in your q example should be replaced by context which is already passed to the method. $\endgroup$
    – brockmann
    Aug 4, 2021 at 10:26

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