3
$\begingroup$

I'd like to control HDRI with collection visibility. Created 3 different Worlds with 3 different HDRIs, and would like to change them with collection visibility.

Here's what I've been trying so far:

import bpy

    if bpy.data.collections["Dark HDRI"].hide_render == False :
        bpy.context.scene.world.name = "Dark"
    
    if bpy.data.collections["Yellow HDRI"].hide_render == False :
        bpy.context.scene.world.name = "Yellow"
    
    if bpy.data.collections["Green HDRI"].hide_render == False :
        bpy.context.scene.world.name = "Green"

Tried to find correct context for it but had no luck, and i don't know if this is the best way to solve it, i'm open to ideas. It doesn't have to be worlds, i just want the HDRIs.

Tried MixRGB node as well, it's just updates the lights, but not background:

Here's what i've tried for that method also:

import bpy
    
if bpy.data.collections["Dark HDRI"].hide_render == False :
    bpy.data.worlds["Dark"].node_tree.nodes["Dark"].inputs[0].default_value == 0
    
if bpy.data.collections["Yellow HDRI"].hide_render == False :
    bpy.data.worlds["Multiple"].node_tree.nodes["Yellow"].inputs[0].default_value == 1

if bpy.data.collections["Green HDRI"].hide_render == False :
    bpy.data.worlds["Multiple"].node_tree.nodes["Green"].inputs[0].default_value == 1
$\endgroup$
1
  • 1
    $\begingroup$ Hello, not exactly sure what your end goal is but it seems you're only changing the current world's name, when you seem to be wanting to swap the world objects, so you should try something like : if ...: bpy.context.scene.world = bpy.data.worlds["Dark"] etc. $\endgroup$
    – Gorgious
    Commented Apr 27, 2022 at 14:20

1 Answer 1

2
$\begingroup$

If you can settle for the first collection that is enabled for rendering, then you need code that looks something like this:

import bpy

def enable_world():
    for world in bpy.data.worlds:
        collection_name = world.name + " HDRI"
        try:
            collection = bpy.data.collections[collection_name]
            if not collection.hide_render:
                bpy.context.scene.world = world
                return
        except:
            pass


enable_world()

As Gorgious says in the comments, you need to set the Scene world to the world you are interested in, so this code iterates through all of your worlds, looking for the first one that has a collection associated with it where the collection is enabled for rendering. The try block is in case you have worlds that you forgot to set up a collection for.

But why do it this way? It's very error prone. It doesn't work well if you forget to add a collection, and it doesn't know which one to pick if you have many enabled for render.

$\endgroup$
1
  • $\begingroup$ Thanks mate, appreciate it! $\endgroup$ Commented May 6, 2022 at 8:46

You must log in to answer this question.

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