# Creating dynamic, shaded backgrounds for animations

I am still new to blender and am working on an animation of a light-responsive protein network in blender-python. I have been asked to create a shaded background whose color changes in accordance with the frequency of light shining on it. My question: how do I code a shaded background in python whose color I can change over the course of the animation:

Implementing the basic background using Blender's UI was straight forward: add blend-sky to the world and adjust its color's accordingly (Picture 1). Using the info tab, I attempted to recreate these commands in python:

 bpy.context.space_data.context = 'WORLD'
bpy.context.scene.world.use_sky_blend = True
self.world.horizon_color = (0, 0, 0)
self.world.zenith_color = (1.0, 0.0, 1.0)


This however only results in "AttributeError: 'SpaceTextEditor' object as no attribute 'context'".

Next, I used a snippet of code from a previous answer I found on this very site (source: Creating cycles background light (world lighting) from Python), which produced a colored background but lacked the shading of the desired result (Picture 1).

 enter codbpy.data.scenes['Scene'].render.engine = 'CYCLES'
world = bpy.data.worlds['World']
world.use_nodes = True
bg = world.node_tree.nodes['Background']
bg.inputs[0].default_value[:3] = (0.5, .1, 0.6)
bg.inputs[1].default_value = 1.0e here


Even if one of these two methods had worked, I would still be unsure how to program a change in background color. Can I create background animations using keyframes similar to object animation? How do I even create the desired type of background in the first place?

Your help is much appreciated.

Pictures: http://imgur.com/a/IAA6G

• Which render engine do you want to use (BI or Cycles)? The solution will be slightly different depending on that. – Todd McIntosh Jul 22 '15 at 17:42
• See, this is where I out myself as a blender noob: considering all my animations are done with keyframes, is one preferable to the other? – Henry Ehlers Jul 22 '15 at 17:43
• Cycles is preferrable if you need photorealistic lighting and shading. Your sample scripts seemed to be targeting the older BI render engine. The world shader is different in Cycles as well. – Todd McIntosh Jul 22 '15 at 17:45
• Since I do not need photo realistic lighting or shading (indeed the animation is going to be very stylized) would you recommend going with BI? If so, can you recommend a way to achieve my goals? – Henry Ehlers Jul 22 '15 at 17:47
• Do you need to keyframe the background colour change in python, or would it be enough to keyframe it through the UI? – Todd McIntosh Jul 22 '15 at 17:47

## 1 Answer

Here's my solution. It provides for altering the Horizon Color and the Zenith Color, and assumes you are using the Blender Internal render engine.

It also allows you to provide a list of frame numbers and colour values for setting up multiple keyframes.

Also, the default interpolation type for animation curves is Bezier which would give you smooth transitions between colors. Because you want abrupt color changes I added code to alter the interpolation mode to Constant for the new animation curves.

import bpy

scn = bpy.context.scene
world = bpy.context.scene.world
world.use_sky_blend = True

#bgColorList[frameNumber, horizonColor, zenithColor)
bgColorList = [
[1,     (1.0, 1.0, 1.0),    (0.8, 0.8, 0.8)   ],
[15,    (0.8, 0.8, 0.8),    (0.8, 0.8, 0.8)   ],
[30,    (0.5, 0.5, 0.5),    (0.2, 0.2, 0.2)   ],
[45,    (0.8, 0.8, 0.8),    (0.8, 0.8, 0.8)   ],
[60,    (1.0, 1.0, 1.0),    (0.8, 0.8, 0.8)   ],
[75,    (0.5, 0.5, 0.5),    (0.2, 0.2, 0.2)   ]
]

#set up all keyframes for background colors
for bg in bgColorList:
frame = bg[0]
hColor = bg[1]
zColor = bg[2]

scn.frame_current = frame
world.horizon_color = hColor
world.zenith_color = zColor
world.keyframe_insert("zenith_color")
world.keyframe_insert("horizon_color")

#modify the interploation mode of the animationt curve to be consant for immediate changes on frame entry
for f in world.animation_data.action.fcurves:
if f.data_path == "zenith_color" or f.data_path == "horizon_color":
for kf in f.keyframe_points:
kf.interpolation = 'CONSTANT'

• Hey, no problem! – Todd McIntosh Jul 22 '15 at 20:49