1
$\begingroup$

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

$\endgroup$
1
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Seems that bpy.context.collection is a reference to the active collection as well...? $\endgroup$ – brockmann Oct 16 at 11:00
  • $\begingroup$ Yes, but hide_viewport will act like Disable in Viewports for that one as well. $\endgroup$ – Robert Gützkow Oct 16 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$ – Brad Hamilton Oct 16 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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