How can I check which of my collections are hidden in viewport?

Using this part of code…

for collection in bpy.data.collections:
    print(collection.name, ' - ', collection.hide_viewport)

…I am trying to check which of my collections are hidden in viewport. But the result I get in console, has to do with the collection where are disabled in viewport!!! Any idea what is the correct way?

The show and hide state you are looking for is not part of a Collection, it is part of a LayerCollection. LayerCollections are separate types that point to a Collection in bpy.data. The viewport visibility is stored on the LayerCollection Python object, while the viewport and render restrict state is stored on the Collection Python object. This allows sharing a Collection with multiple LayerCollections and temporarily hide them, while keeping the restrict flags in sync across them.

Check this example (modified from yours) below to get the idea:

for collection in bpy.context.view_layer.layer_collection.children:
    print(collection.name, ' - ', collection.hide_viewport)

Unfortunately so far there is no API for retrieving all Layer Collections from the outliner at once afaik (LayerCollections can be nested). You will need to recurse through all children yourself with a helper method, and then query the visibility state.

1 Like