Visibilities in Python

The different visibility settings in Blender
Blender
bpy
Author

ozm

Published

September 10, 2025

There are three ways in which an object can be hidden in Blender:

The outliner shows these three states in the following way:

On top of that, collections can be included/excluded per ViewLayer:

So, if you are using ViewLayers and want to control object visibilities in the render, you have to control it via collections (which, in my opinion, forces a clean way of working).

This function controls all of the above via python. In this example, handling collections is not recursive and only works on the first collections under the Scene Collection of a ViewLayer:

import bpy

def control_visiblity(eye, display, render, collection_exclude, scenename="Scene", viewlayername="ViewLayer"):
    
    # Controls objects
    for o in bpy.data.objects:
        o.hide_viewport = display
        o.hide_render = render
        
        # Handle object visibility in ViewLayer - does not affect rendering!
        vl_o = bpy.data.scenes[scenename].view_layers[viewlayername].objects.get(o.name)
        if hasattr(vl_o, "hide_set"):
            vl_o.hide_set(eye)

    # Controls Collections
    lc = bpy.data.scenes[scenename].view_layers[viewlayername].layer_collection
    for c in lc.children:
        c.exclude = collection_exclude

eye_icon = True # temporarly hide in viewport per ViewLayer
display_icon = True # hide in viewport across all ViewLayers
render_icon = True # hide in render
collection_exclude = True # include/exclude from ViewLayer

control_visiblity(eye_icon, display_icon, render_icon, collection_exclude)

To recursively walk the ViewLayer collection hierarchy, see batFINGER’s answer

Back to top