0
$\begingroup$

I want to "isolate" collection for rendering. The code is pretty massive IMHO. Is there an elegant way to code this task? I need code exactly. This task is just a part of project.

# Remember state of "hide_render" parameter of other collections
is_hide_render = []
for collection in bpy.data.collections:
    is_hide_render.append(collection.hide_render == True)

# Turn my collection on
bpy.data.collections["Collection for isolate rendering"].hide_render = False

# Return "hide_render" parameter of other collections
count = 0  # what a shame
for collection in bpy.data.collections:
    collection.hide_render = is_hide_render[count]
    count = count + 1

# Turn my collection off
bpy.data.collections["Collection for isolate rendering"].hide_render = True

The code actually not accurate, because you need to activate "parents" (up hierarchy) of a collection, to really activate rendering on it.

Thу question can also be put as follows: how to emulate Ctrl+Click on Collection to isolate rendering

enter image description here

$\endgroup$

1 Answer 1

1
$\begingroup$

Usually, view layers are used for this type of tasks. You can create new render layer and use exclude to hide layer_collection. It is not more "elegant" but looks like a proper way to do this task

import bpy

scene = bpy.context.scene

if "Render" in scene.view_layers.keys():
    renderlayer = scene.view_layers["render"]
else:
    renderlayer = scene.view_layers.new("Render")
    
for col in renderlayer.layer_collection.children:
    col.exclude = col.name != "Collection for render"

currentlayer = bpy.context.view_layer
    
bpy.context.window.view_layer = renderlayer

get back:

bpy.context.window.view_layer = currentlayer
$\endgroup$
1
  • $\begingroup$ Agreed this is the better way. Also gets rid of the problem of parents needing to be allowed too. And excluded collections won't be taken into account in the viewport for modifiers calculations etc. whereas if you only use the render setting they will. $\endgroup$
    – Gorgious
    Oct 30, 2023 at 12:42

You must log in to answer this question.

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