2
$\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 Answer 1

7
$\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$
5
  • $\begingroup$ Seems that bpy.context.collection is a reference to the active collection as well...? $\endgroup$
    – brockmann
    Oct 16, 2019 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, 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$ Oct 16, 2019 at 18:06
  • $\begingroup$ @BradHamilton If an answer has solved your question please consider accepting it by clicking the green check mark above left. (and or upvoting too) (break your duck on both) Questions without accepted answers are constantly bumped by the community bot. $\endgroup$
    – batFINGER
    Jan 8, 2020 at 2:19

You must log in to answer this question.

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