0
$\begingroup$

I am new to blender, I have a bunch of collections say parent collections ( each collection has one or more sub collection say child collection)

What I want is , I want to select only one of the child collection for all parent collection and render it and save. Ofcourse I want to unselect those collections for next render

I am unable to write the code to select/unselect child collection, tried linking bpy.context.scene.collection.children.link(collection)/ bpy.context.scene.collection.children.unlink(collection)

working fine for the first render but not working for many renders

wondering if there is a simple way to achive this

attached the image where parent and sub collectionsenter image description here

$\endgroup$

1 Answer 1

1
$\begingroup$

I think it's easier to disable and enable collections on render.

For example:

import bpy

for parent in bpy.context.scene.collection.children:
    for child in parent.children:

        # Lets enable collection with "1" in the name
        # You can define any other logic if you want
        if child.name.find('1')!=-1:
            child.hide_render = False
        else:
            child.hide_render = True

Or example with random:

import bpy
import random

for parent in bpy.context.scene.collection.children:

    # Disable all collections:
    for child in parent.children:
        child.hide_render = True

    # and enable back one random collection
    index = random.randint(0, len(parent.children)-1)
    parent.children[index].hide_render = False

enter image description here

You can also hide in viewport by using hide_viewport

If you want to use Exclude option to hide objects you should look into view_layer instead of scene:

import bpy
import random

for parent in bpy.context.view_layer.layer_collection.children:

    # Disable all collections:
    for child in parent.children:
        child.exclude = True

    # and enable back one random collection
    index = random.randint(0, len(parent.children)-1)
    parent.children[index].exclude = False

enter image description here

$\endgroup$
11
  • $\begingroup$ Yes hide render , viewport fine, but actually the checkbox is what I want, say in a child collection there are 2 objects, those wont be enabled unless the checkbox of that child collection is ticked or selected $\endgroup$
    – Mady
    Commented May 12, 2022 at 14:04
  • $\begingroup$ can you add some objects in child collection and make it selectable the whole child collection? $\endgroup$
    – Mady
    Commented May 12, 2022 at 14:06
  • $\begingroup$ Agree, checkbox should be active. It controls trough View layer, so it is easier to use hide render in Python $\endgroup$
    – Crantisz
    Commented May 12, 2022 at 14:07
  • $\begingroup$ I didn't catch that. Do you want to set "hide_select"? $\endgroup$
    – Crantisz
    Commented May 12, 2022 at 14:08
  • $\begingroup$ Yeah I tried the hide_select, not working here is how I tried I have a coll name = "sample" col = bpy.data.collection['sample'] col.hide_select = False $\endgroup$
    – Mady
    Commented May 12, 2022 at 14:19

You must log in to answer this question.

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