There are three ways in which an object can be hidden in Blender:
- An object can be hidden in the viewport
- an object can be disabled in the viewport
- an object can be hidden in the render
The outliner shows these three states in the following way:
- Display icon: global disable in the viewport (is set across all ViewLayers)
- Eye icon: temporarly hidden in the viewport (is set per ViewLayer)
- Render icon: hidden in render (is set across all ViewLayers)
On top of that, collections can be included/excluded per ViewLayer:
- Checkmark on collections
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