1

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?

Simos Sigma
  • 413
  • 3
  • 11

1 Answers1

2

Looping collections through bpy.data.collections gives you classes of type Collection: https://docs.blender.org/api/current/bpy.types.Collection.html#bpy.types.Collection

Looping collections through bpy.context.view_layer.layer_collection.children gives you classes of type LayerCollection: https://docs.blender.org/api/current/bpy.types.LayerCollection.html#bpy.types.LayerCollection

The meaning of the property hide_viewport seems to be slightly different in these two class types.

  • In type Collection it means: "Globally disable in viewports"
  • In type LayerCollection it means: "Temporarily hide in viewport"
  • Type LayerCollection additionally exposes the property is_visible, which is True, when the collection is both enabled and visible

I guess LayerCollection.hide_viewport is what you were looking for.

Code example to see the differences in action:

print('Looping type "Collection", property "hide_viewport":')
for collection in bpy.data.collections:
    print(collection.name, ' - Disabled in Viewport:', collection.hide_viewport)

print('Looping type "LayerCollection", property "hide_viewport":') for layer_collection in bpy.context.view_layer.layer_collection.children: print(layer_collection.name, ' - Hidden in Viewport: ', layer_collection.hide_viewport)

print('Looping type "LayerCollection", property "is_visible":') for layer_collection in bpy.context.view_layer.layer_collection.children: print(layer_collection.name, ' - Enabled and visible: ', layer_collection.is_visible)

Sighthound
  • 173
  • 7