Driving/Programming a Color Ramp

I’m working on a visualization that involves the Doppler shift, so I want the color of an object to subtly shift depending on the motion of the object: toward = bluer, away = redder. I tried using drivers to adjust the Diffuse RGB directly, but it got very messy and it was hard to fine-tune the range of colors. Instead, I would like to select RGB values from a Color Ramp. A couple of questions come to mind:

  1. I know Color Ramps are usually used to control shading over a range of illumination angles or energies. Can a single RGB value from a Color Ramp be assigned to the entire Diffuse color of an object?

  2. Is there a way to assign the Position on the Color Ramp using a Driver? I tried adding a Driver to the Color Ramp Input and found that the Driver only controls the Input (Result, Normal, Energy, Shader) not the Position itself. D’oh!

  3. Is there a way to assign the Position on the Color Ramp using Python? I can program in Python, but this would be my first attempt to use it within Blender.

  4. Is there a simpler way (perhaps Material Nodes?) to achieve this effect? I’d be happy if I could just map the object’s Diffuse from Red through White to Blue based on an Empty’s Z Rotation. I can tune the rotation to get the desired effect.

Thanks in advance!

Which colour ramp ? Which renderer ?

The colour ramp in the cycles renderer you can control the output of by driving a value node as the ramp input

For BI you could set the diffuse colour to red, the ramp to blue and then drive the ramp factor value

Hi Richard,

Sorry. I left important information out. I was thinking of a red-white-blue color ramp: (1,0,0) at position 0, (1,1,1) at position 0.5, (0,0,1) at position 1. And I’m using BI. I haven’t tried to learn the Cycles Materials yet. Fortunately, I’m usually rendering scenes that are harshly lit by one star (Point lamp) or from one direction (Sun lamp) so Cycles is less useful for me.

Thank you for the recommendation on the ramp factor idea.

After a bit more research, I’m finding that the Python API cannot yet manipulate or read from Color Ramps. But I’m realizing a Red-White-Blue color change is not too difficult to code in Python. I guess I’ll try my hand at it.

You can get a single color from a color ramp



and factor conveniently points the position on that. 0 = full red, 0.2 = pink, 1.0 = blue
You could right click on the factor field and add a driver, which you can change with a python expression or make a separate python script. How to get the object speed and direction and map it to a value between 0-1 in that driver, no idea. That would most likely require to calculate deltas and I haven’t done that in a driver script.

i did a script to make color ramp in cycles nodes
so it is possible to work with color ramp
in bl should be the same idea!

but not certain if it is possible yet to add a driver for one of the slot in a color ramp!

i can add a driver to roughness input in node editor
but not to a ramp slot color!

for now i guess it cannot be done!

happy bl

Here is a small snippet of code that shows how to evaluate the color ramp of a Blender Internal material.


    # Fetch the material with the color over time ramp.
    mat_color = bpy.data.materials.get(my_material_name)
    if mat_color != None:
        if mat_color.use_diffuse_ramp == True:
            ramp_color = mat_color.diffuse_ramp
            col_temp = ramp_color.evaluate(age) #age is in the range of 0.0-1.0.

You use the evaluate() method on a valid color ramp object to read the value. I’m not sure how to construct a color ramp, but it is probably achievable as well.

The elements of the ramp are a collection so you can add and remove them.
http://www.blender.org/documentation/blender_python_api_2_69_release/bpy.types.ColorRamp.html?highlight=evaluate