0
$\begingroup$

In my workflow I use two environments (hdris) plugged into a mix shader. I use one HDRI to light up the scene and the other HDRI to create materials. I switch between them using the factor parameter in the Wrorld Properties.

I would like to know if it is possible to somehow map a key that would switch between these two hdris? For example pressing A would active the first HDRI and pressing A again would activate the second HDRI and so forth.

enter image description here

enter image description here

$\endgroup$
2
  • $\begingroup$ It is possible using scripting. $\endgroup$
    – Crantisz
    Commented Oct 25 at 11:27
  • $\begingroup$ Can you show me how? $\endgroup$
    – mts10
    Commented Oct 25 at 12:48

1 Answer 1

1
$\begingroup$

Tricky way

Well, actually you can do it without scripting:

enter image description here

Add a new item in key map in window section, operator is wm.context_toggle, attribute is scene.world.node_tree.nodes["Mix Shader"].inputs[0].default_value, where "Mix Shader" is the node name. Your node might have different name, check that do you have in here:

enter image description here

Operator way

The more proper way is to do the operator. This is the script that I made from simple operator preset:

import bpy


def main(context):
    node_tree = context.scene.world.node_tree
    input = node_tree.nodes["Mix Shader"].inputs[0]
    input.default_value = 0 if input.default_value == 1 else 1

class ToggleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "wm.toggle_update"
    bl_label = "Toggle Operator"
    
    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        main(context)
        return {'FINISHED'}

def register():
    bpy.utils.register_class(ToggleOperator)


def unregister():
    bpy.utils.unregister_class(ToggleOperator)


if __name__ == "__main__":
    register()

    # test call
    bpy.ops.wm.toggle_update()

Your node might have different name (not "Mix Shader"), check that do you have in here:

This script creates operator. You can set it like this in keymaps:

enter image description here

To make this script works across files, you can save it as .py file and install as an addon. In this case, you should add addon info block.

$\endgroup$
4
  • $\begingroup$ Thank you so much, you are very kind. I will try it when I get home. I hope it works. $\endgroup$
    – mts10
    Commented Oct 25 at 19:00
  • $\begingroup$ Oh man, this works just the way I wanted it. Thank you so much. $\endgroup$
    – mts10
    Commented Oct 25 at 20:38
  • $\begingroup$ Hey I have a small request. So I would like to switch the environement when pressing the A key which I also use for Local view (isolating the object). So that everytime I isolate the object using the A key the environments also get switched. But since the key is mapped to two actions it doesn't work. How can I make it work? $\endgroup$
    – mts10
    Commented Oct 25 at 20:43
  • $\begingroup$ I got it to work by setting the value from press to any. Thank you! $\endgroup$
    – mts10
    Commented Oct 25 at 20:47

You must log in to answer this question.

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