# Creating cycles background light (world lighting) from Python

I'm rendering a scene from the Python interface, and I can't get the python API commands to work from a script.

In the Blender UI I would go to world->Surface->Use Nodes and then adjust the surface color and strength to get the desired light.

From python I run the following commands

bpy.data.scenes['Scene'].render.engine = 'CYCLES'
bpy.data.worlds['World'].use_nodes = True
bpy.data.worlds['World'].node_tree.nodes['Background'].inputs[0].default_value[:3] = (1,1,1)
bpy.data.worlds['World'].node_tree.nodes['Background'].inputs[1].default_value = 2.0


If I run these from the Blender Python console they change the values as expected. But when running the commands from a script via

blender -b my_scene.blend -P my_script.py


These commands have no effect. Other commands (moving objects around etc) work from the same script.

• the basic scenario does appear to work ok, perhaps you need to expand a little and explain if there are more parts to what you're doing. – zeffii Jun 9 '15 at 12:02

This does work on my machine though, I've tested it. Note here i've used aliasing to make the lines less long.

my_script.py

import bpy
import time
import os

bpy.data.scenes['Scene'].render.engine = 'CYCLES'
world = bpy.data.worlds['World']
world.use_nodes = True

# changing these values does affect the render.
bg = world.node_tree.nodes['Background']
bg.inputs[0].default_value[:3] = (0.5, .1, 0.6)
bg.inputs[1].default_value = 1.0

bpy.ops.render.render()

faux_name = "tensor_" + str(time.time()) + ".png"
fp = os.path.join("/home/zeffii/Desktop", faux_name)
bpy.data.images['Render Result'].save_render(filepath=fp)