Hi Blender coders and artists.

I am updating one of my add-ons for Blender 2.8 and have run into some issues finding the appropriate way to hide a collection that is added to my scene.

I want to deselect the eyeball on the toolbar with a python command in a script but I don't want to disable the collection in the view-port entirely as it is used as a collection in a particle system.

I just want to deselect the eyeball circled in green below.

Anyone know the python API for this?

enter image description here


1 Answer 1


You can hide the active collection using the following code:

bpy.context.view_layer.active_layer_collection.hide_viewport = True

If you want to hide more than the active collection you will have to iterate through the children of layer_collection

collections = bpy.context.view_layer.layer_collection.children

for collection in collections:
    collection.hide_viewport = True

Be aware that bpy.data.collections and bpy.context.scene.collection also allow to access the collections and set hide_viewport. However, since these do not depend on the active view layer, they apply globally and therefore activate Disable in Viewports.

  • $\begingroup$ Seems that bpy.context.collection is a reference to the active collection as well...? $\endgroup$
    – brockmann
    Commented Oct 16, 2019 at 11:00
  • $\begingroup$ Yes, but hide_viewport will act like Disable in Viewports for that one as well. $\endgroup$ Commented Oct 16, 2019 at 12:03
  • $\begingroup$ Thank you so much! This works to hide the active collection. Unfortunately Now it turns out the active collection is not the one I am trying to hide lol. Any chance you know the code to make a collection in the scene active via scripting? Can't seem to find that anywhere. $\endgroup$ Commented Oct 16, 2019 at 18:06
  • $\begingroup$ @RobertGützkow if for example I click the Eye icon (eye closes) on the active collection and then try to set it via python back on using hide_viewport=False it does not open the eye. The eye is still closed. The collection is still disabled. How do you enable it again which opens the eye again? It looks like Blender is setting something else not just hide_viewport $\endgroup$
    – Harry McKenzie
    Commented May 10 at 7:04

You must log in to answer this question.

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