5
$\begingroup$

I am trying to disable all collections in the render with python in Blender 2.8

It works on top-level collections with the code below, but ignores nested collections.

import bpy
coll = bpy.context.view_layer.layer_collection
for x in bpy.context.view_layer.layer_collection.collection.children:
        x.hide_render = True

Is there a way to get all collections, regardless of hierarchy?

$\endgroup$
1
  • $\begingroup$ Welcome to Blender.se. Thank you for your question, be aware, that the 2.8 api is still under development, although I'd think, that collection access won't change anytime soon. $\endgroup$
    – Leander
    Apr 23, 2019 at 14:28

2 Answers 2

3
$\begingroup$

Access to Blender's internal data is achieved through bpy.data. This is usually also easier to maintain, since context access (bpy.context) is context-dependent.

Access all collections through:

bpy.data.collections

Render-restricting all collections:

import bpy

coll = bpy.data.collections

for c in coll:
    c.hide_render=True
$\endgroup$
2
  • 1
    $\begingroup$ Ah! That was easy. Thanks Leander. This works: import bpy coll = bpy.data.collections for c in coll: c.hide_render=True $\endgroup$ Apr 23, 2019 at 14:39
  • 2
    $\begingroup$ Forgot to include the final code, added your example, glad it worked. For future reference, if any answer is missing a piece of information (like your code example) feel free to suggest an edit yourself. Comments are only temporary and may get removed. $\endgroup$
    – Leander
    Apr 23, 2019 at 14:43
5
$\begingroup$

With recursion

As well as setting attributes on all collections in bpy.data.collections can recursively walk the children.

This will only set the property on descendants of the collection. In this case bpy.context.view_layer.layer_collection.collection

import bpy

def traverse_tree(t):
    yield t
    for child in t.children:
        yield from traverse_tree(child)

coll = bpy.context.view_layer.layer_collection.collection

for c in traverse_tree(coll):
    c.hide_render = True 
$\endgroup$
5
  • $\begingroup$ This doesn't seem to work for me. I am trying actually to activate an excluded collection so I've tried to set c.hide_render=False but it reports back an error that AttributeError: 'LayerCollection' object has no attribute 'hide_render'. Then I've tried also bpy.context.layer_collection.children[c.name].exclude = False layer_collection = bpy.context.view_layer.layer_collection.children[c.name] bpy.context.view_layer.active_layer_collection = layer_collection but then I've got KeyError: 'bpy_prop_collection[key]: key "<collection_name>" not found' any idea what is wrong? $\endgroup$
    – ttsesm
    Sep 25, 2020 at 11:27
  • $\begingroup$ Please note difference between context.view_layer.layer_collection and context.view_layer.layer_collection.collection $\endgroup$
    – batFINGER
    Sep 25, 2020 at 11:32
  • $\begingroup$ Yup, you are right I overlooked the last .collection but still c.hide_render = False doesn't seem to activate my collection. $\endgroup$
    – ttsesm
    Sep 25, 2020 at 11:47
  • 1
    $\begingroup$ Look for set active collection. $\endgroup$
    – batFINGER
    Sep 25, 2020 at 11:55
  • 1
    $\begingroup$ Got it to work ;-) $\endgroup$
    – ttsesm
    Sep 25, 2020 at 12:39

You must log in to answer this question.

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