# How to change world background based on active camera?

I'm trying to create a scene consisting of multiple cameras which all should have it's own unique environment. But I'm having trouble setting up an automated node setup.

How can I setup the world node editor so when a certain camera is set as the active camera, the correspondent background is activated for rendering?

EDIT:

Blendfile for testing:

• one (different) way could probably be animate the world settings in sync with camera switching... or, link objects (not cameras) to more than one scene, and set each camera in different scene, changing scene world settings... – m.ardito Jun 19 '17 at 15:35
• @m.ardito This would be a nice enough workaround for a simple 'still' project. I already looked at using the timeline to change cameras and nodes. But this will cause conflict when rendering animations. – Delagone Jun 19 '17 at 15:38
• maybe you can use a "script" node to switch world output setting different "background" nodes strenght based on active camera... ? that would require creating a python script, though, I can't yet help with that... – m.ardito Jun 19 '17 at 16:14
• I don't think you can access active camera from inside a shader, so it is fundamentally impossible to do what you want directly without workarounds. You should probably setup several different scenes with linked objects so they are kept in sync, and add different worlds to each, and different cameras – Duarte Farrajota Ramos Jun 19 '17 at 18:20
• Actually, I believe it is possible with drivers - to capture the location of the active camera - and you can then set up your environment to react to that stored location. I have successfully tested this and it's working (so animating the active camera is successfully updating the scene). I'll write up and submit a proper answer later. – Rich Sedman Jun 20 '17 at 14:01

This can be achieved using Drivers to feed an attribute of the Active Camera into the Environment material.

The current active camera can be determined using Python (as defined here) using :

obj_camera = bpy.context.scene.camera


We can distinguish between the different cameras by setting the Pass Index of each camera object to specific values - eg, 1, 2, 3.

In the Environment material, add a Value node, right-click the value field and select 'Add Driver'.

Ensure the Value node is selected and swap to a Graph Editor window and select Drivers. Highlight the newly added driver and open the properties panel with N. In the Drivers panel, set the Type to Scripted Expression and set Expr to 'bpy.context.scene.camera.pass_index'.

Note : You'll need to enable the File/Auto Execution/Auto Run Python Scripts option in the File/User Preferences settings if it isn't set already (this allows Blender to run Python code).

You should now find that as the Active Camera changes the value of the Value node in the Environment material changes to reflect that camera's Pass Index and this can be used to generate values (using Greater Than/Less Than maths nodes) to drive the Mix Shaders to select the desired material.

Note : The displayed Value value for driven values doesn't always update immediately in the node tree - but the actual value used by the Environment material is correct (it has simply not been refreshed on-screen). Moving the mouse over the Value node should trigger the display to refresh.