Background color : Rasterizer.setBackgroundColor


how would you change from :
Rasterizer.setBackgroundColor(1.0,1.0,1.0,1.0) (ex)
using Python and a loop, and be able to control the change speed?

I could do it by using a while loop and different variables, but my loop is read to quickly and I can’t see the change from one color to another one. It’s too fast.

I would like to use it for a sunset scene, a change from day to night atmosphere, etc…

Any help would be appreciated.

A while loop will run the full loop during the same frame, so you won’t be able to see what is happening.

Maybe use a timer property?

Hi, Cray. Check the blend file.

changeBackground.blend (129 KB)

For speed change alter the property actuator.

Thank you all for the help.
I’m having difficulties to set different colors (let’s say blue, red, yellow) and change from one color to another when a key is pressed, using Python only and allowing speed control.
I will try to spend more time on it later.

Basically, I would use this :

Rasterizer.setBackgroundColor([col1,col2,col3, 1.0])

and find a way to let my script slow down the bg change.
Finally that shouldn’t be hard, I’m just too tired today because of other works.

Using a timer property this can be done:

timer = ob['timer_prop']
time = 5 # time taken for colour change to complete
col = [1,0,0,1]

# Assuming that we want the colour to fade from black:
col[0] *= (timer/time)
col[1] *= (timer/time)
col[2] *= (timer/time)

if timer < time: Rasterizer.setBackgroundColor(col)

But thats pretty simple and not ver flexible, a more complex method can be done by taking the difference of the current colour and the desired colour and ramping it for the time specified, EG:

#Current colour
cur = [0,0,1,1]

# Next colour
next = [1,0,0,1]

# Difference in colours
dif = next
dif[0] -= cur[0]
dif[1] -= cur[1]
dif[2] -= cur[2]

timer = ob['timer_prop']
time = 5

col = cur
col[0] += dif[0]*(timer/time)
col[1] += dif[1]*(timer/time)
col[2] += dif[2]*(timer/time)

if timer < time: Rasterizer.setBackgroundColor(col)

I don’t know of any methods to get the BG colour, so keep a tab on it.

Note that the timer_prop should start at 0 or you will get some weird results, so reset the timer when you start the colour change.

Thanks to your help I could modify your script and achieve the effects I was working on.
Thank you :slight_smile:

I don’t know of any methods to get the BG colour
Same for me, I have been searching for that information everywhere I could.

Check the blend file.


I just get a white screen here - does this only work with GLSL or something?

Never mind, I just discovered I had it in textured mode.

Just a question :

Type:  List [ r, g, b, a]

   r (red channel):
Type:  float
Range: 0.0 to 1.0

   g (green channel):
Type:  float
Range: 0.0 to 1.0

   b (blue channel):
Type:  float
Range: 0.0 to 1.0

   a (alpha channel):
Type:  float
Range: 0.0 to 1.0

What tool can I use to know wich values (float) for r, g, b I need to use to get a color or another one?
Is that possible with the Gimp?

There are many ways to do that. If you have the right color on an image and wants to put the exact same value on setBackgroundColor you could get the color with Gimp, using the color picker tool (“O” shortcut") and double click the little button with the color. You’ll see the RGB value in 0-255 there, so just divide it by 255 and write it on Blender.

But most of the time you can just get a good enough color off the top of your head if you know how RGB works:

You can use the Blender colour picker, its in RGBA and ranges from 0.0 - 1.0.

Thank you!

Hi Cray,

What tool can I use to know wich values (float) for r, g, b I need to use to get a color or another one?
Is that possible with the Gimp?
Software like gimp usualy deals with colors encoded from 0 to 255.
Let’s say you’ve picked up some pink color (with the color pick’up tool : la pipette, or simply by changing the color of your brush) :
Gimp color :
RGB = [ 255, 0, 128 ]
You just have to divide each component by 255 in order to map the value. You’ll get something like that :
Blender color :
RGB = [ 1.0, 0.0, 0.5 ]

Well, it seams you’re trying to animate the World background color. Of course you’ll be able to achieve it by code through python. But unfortunatly, a sunset is not a linear blending function from cyan to orange.
Python will interpolate some ugly (grays) colors between them.

Moreover it’s not the best way for an artist to keep control over his scene.

Here it is some clues for you :

  • Animate the background color blending with an IPO (could pass through a proxy mesh if not possible directly)
  • or animate the color of a skybox with IPO
  • or animate the color of a skybox with UV offset animation (this way, you can control your blend colors with a tiny 1*256 pixels texture)
  • or paint all your colors onto the VertexColor of a low poly skyball. Then turn your ball to get the right color up ahead…
  • GLSL script…

There’s many way to achieve quite the same thing…

Hi Zelouille,
I wanted to be able to change the background color/ambiant color/animate mist and now I know what’s good with it and what’s not.
Your post was also helpful.
I will search the forums for more informations on the techniques you told me about.

Thank you :slight_smile: