Python script changing color of object in animation

Hello,

I am starting with Blender after working in the past with PovRay. Normally I was using PovRay for some very simple animations for mathematical and physics illustrations. Nothing very ambitious and I prefer to use python for it.

In the last week I already learnt some of the basics of python scripting with Blender. I wrote a very simple script (copied from many code snippets from the web), which you can find below, to learn step by step what I need. Until now I found all what I need by searching the web or using the GUI and checking commands in the “info” console but now I have a problem to which I do not find the solution in the web.

Well, what I would like to do is to write a python script which creates an animation in which a sphere is changing its color. For changing its location it was no problem but changing the color does not work.

I guess that data_path=“color” in the cone3.keyframe_insert(data_path=“color”, frame=1.0, index=-1) is not the right data_path although I do not get any error message but the color of the sphere just does not change.

Anyone could tell me what is the right command to animate the color? Or even explain it even a bit more general? For example from where I get the data_path which are existing? This time I would like to animate the color but the next step will be to let the sphere disappear and I guess the principle for this will be the same as for the color animation.

Thanks

#----------------------------------------------------------

File test.py

#----------------------------------------------------------
import bpy
import mathutils
from mathutils import Vector

#######################################################

scene stype:

NEW New, Add new scene.

EMPTY Copy Settings, Make a copy without any objects.

LINK_OBJECTS Link Objects, Link to the objects from the current scene.

LINK_OBJECT_DATA Link Object Data, Copy objects linked to data from the current scene.

FULL_COPY Full Copy, Make a full copy of the current scene.

def addScene(name, stype):
bpy.ops.scene.new(type=stype)
ob = bpy.context.scene
ob.name = name
return ob

def makeMaterial(name, diffuse, specular, alpha):
mat = bpy.data.materials.new(name)
mat.diffuse_color = diffuse
mat.diffuse_intensity = 1.0
mat.specular_color = specular
mat.specular_intensity = 0.5
mat.alpha = alpha
mat.ambient = 1
return mat

def createMeshFromPrimitive(name, origin):
bpy.ops.mesh.primitive_uv_sphere_add(location=origin, segments=64, ring_count=64)

ob = bpy.context.object
ob.name = name
ob.show_name = True
bpy.ops.object.shade_smooth()
me = ob.data
me.name = name+'Mesh'
return ob

def setMaterial(ob, mat):
me = ob.data
me.materials.append(mat)
return

def addTrackToConstraint(ob, name, target):
cns = ob.constraints.new(‘TRACK_TO’)
print(dir(cns))
cns.name = name
cns.target = target
cns.track_axis = ‘TRACK_NEGATIVE_Z’
cns.up_axis = ‘UP_Y’
cns.owner_space = ‘LOCAL’
cns.target_space = ‘LOCAL’
return

def addCamera(name, place):
bpy.ops.object.camera_add(location=place)
ob = bpy.context.object
ob.name = name
return ob

#######################################################

Lamp ltype:

POINT Point, Omnidirectional point light source.

SUN Sun, Constant direction parallel ray light source.

SPOT Spot, Directional cone light source.

HEMI Hemi, 180 degree constant light source.

AREA Area, Directional area light source.

def addLamp(name, position, ltype):
bpy.ops.object.lamp_add(location=position, type = ltype)
ob = bpy.context.object
ob.name = name
return ob

def run(origo):
chCol = makeMaterial(‘chCol’,(1,1,1), (1,1,1), 1)
origin = Vector(origo)

scene1=addScene('scene1', 'NEW')
bpy.context.screen.scene=bpy.data.scenes[scene1.name]

lamp1 = addLamp('lamp1', (0,0,20), 'SPOT')
lamp1.data.energy = 5.

cone3 = createMeshFromPrimitive('PrimCone', origin+Vector((0,0,0)))
cone3.active_material = chCol
cam1 = addCamera('cam1', (0,0,10))

cam1.data.angle=3.14/2.1
        
cone3.keyframe_insert(data_path="color", frame=1.0, index=-1)    
cone3.keyframe_insert(data_path="location", frame=1.0, index=-1)    

addTrackToConstraint(cam1,"myTrackTo",cone3) 
addTrackToConstraint(lamp1,"myTrackTo1",cone3) 
cone3.location=(10,10,10)
cone3.active_material.diffuse_color = (1,0,0)
cone3.keyframe_insert(data_path="color", frame=20.0, index=-1)    
cone3.keyframe_insert(data_path="location", frame=30.0, index=-1)    
cone3.location=(0,0,0)
cone3.keyframe_insert(data_path="location", frame=60.0, index=-1)    
return

if name == “main”:
run((0,0,0))

Hello,

finally I found the solution how to achieve that an object changes its color during an animation. Since this might be useful for others, which for example want to animate a LED which is changing its color, and I did not find an example for it in the web, I leave you here my solution. Not sure if it is the best but at least it works.

Well, in my first post I tried to change the color of the object and afterwards I added a keyframe to animate the object. This was the wrong approach. The object does not have a color but a material is assigned to the object and this material has some properties, as for example the diffuse color, which can be animated.

Thus, the right way for this problem is:

  1. Create a material:
    chCol = makeMaterial(‘chCol’,(1,1,1), (1,1,1), 1)
  2. Assign material to object e.g. with:
    cone3.active_material = chCol
  3. Add a keyframe for the diffuse color of the material for frame 1:
    chCol.keyframe_insert(data_path=“diffuse_color”, frame=1.0, index=-1)
  4. Change the diffuse color of the material to something else:
    chCol.diffuse_color = (1,1,0)
  5. Add a second keyframe for the difffuse color:
    chCol.keyframe_insert(data_path=“diffuse_color”, frame=20.0, index=-1)

Below there is the complete script which tests this.

I animated the diffuse color but of course you can do it in this way also with other properties of the material. The keypoint is that one has to know what one needs to animate, an object, a material, etc which sometimes for n00bs like me is not so obvious.

Well, since I did not find this informatin nowhere else, I hope this post might help others for their projects.

Ciao


#----------------------------------------------------------
# File test.py
#----------------------------------------------------------
import bpy
import mathutils
from mathutils import Vector

################################################## #####
# scene stype:
# NEW New, Add new scene.
# EMPTY Copy Settings, Make a copy without any objects.
# LINK_OBJECTS Link Objects, Link to the objects from the current scene.
# LINK_OBJECT_DATA Link Object Data, Copy objects linked to data from the current scene.
# FULL_COPY Full Copy, Make a full copy of the current scene.

def addScene(name, stype):
    bpy.ops.scene.new(type=stype)
    ob = bpy.context.scene
    ob.name = name
    return ob


def makeMaterial(name, diffuse, specular, alpha):
    mat = bpy.data.materials.new(name)
    mat.diffuse_color = diffuse
    mat.diffuse_intensity = 1.0
    mat.specular_color = specular
    mat.specular_intensity = 0.5
    mat.alpha = alpha
    mat.ambient = 1
    return mat

def createMeshFromPrimitive(name, origin):
    bpy.ops.mesh.primitive_uv_sphere_add(location=origin, segments=64, ring_count=64)

    ob = bpy.context.object
    ob.name = name
    ob.show_name = True
    bpy.ops.object.shade_smooth()
    me = ob.data
    me.name = name+'Mesh'
    return ob

def setMaterial(ob, mat):
    me = ob.data
    me.materials.append(mat)
    return


def addTrackToConstraint(ob, name, target):
    cns = ob.constraints.new('TRACK_TO')
    print(dir(cns))
    cns.name = name
    cns.target = target
    cns.track_axis = 'TRACK_NEGATIVE_Z'
    cns.up_axis = 'UP_Y'
    cns.owner_space = 'LOCAL'
    cns.target_space = 'LOCAL'
    return


def addCamera(name, place):
    bpy.ops.object.camera_add(location=place)
    ob = bpy.context.object
    ob.name = name
    return ob

################################################## #####
# Lamp ltype:
# POINT Point, Omnidirectional point light source.
# SUN Sun, Constant direction parallel ray light source.
# SPOT Spot, Directional cone light source.
# HEMI Hemi, 180 degree constant light source.
# AREA Area, Directional area light source.

def addLamp(name, position, ltype):
    bpy.ops.object.lamp_add(location=position, type = ltype)
    ob = bpy.context.object
    ob.name = name
    return ob


def run(origo):
    chCol = makeMaterial('chCol',(1,1,1), (1,1,1), 1)
    origin = Vector(origo)


    scene1=addScene('scene1', 'NEW')

    bpy.context.screen.scene=bpy.data.scenes[scene1.name]

    lamp1 = addLamp('lamp1', (0,0,20), 'SPOT')
    lamp1.data.energy = 5.

    cone3 = createMeshFromPrimitive('PrimCone', origin+Vector((0,0,0)))
    cone3.active_material = chCol

    cam1 = addCamera('cam1', (0,0,10))

    cam1.data.angle=3.14/2.1

    chCol.keyframe_insert(data_path="diffuse_color", frame=1.0, index=-1)
    cone3.keyframe_insert(data_path="location", frame=1.0, index=-1)

    addTrackToConstraint(cam1,"myTrackTo",cone3)
    addTrackToConstraint(lamp1,"myTrackTo1",cone3)

    cone3.location=(10,10,10)
    chCol.diffuse_color = (1,1,0)
    chCol.keyframe_insert(data_path="diffuse_color", frame=20.0, index=-1)
    cone3.keyframe_insert(data_path="location", frame=30.0, index=-1)

    cone3.location=(0,0,0)
    cone3.keyframe_insert(data_path="location", frame=60.0, index=-1) 
    return


if __name__ == "__main__":
    run((0,0,0))

thanks so much for your post I was running into the same problem. I used the following (functions omitted for brevity)
def animateColor(obj,material,idx,scale=10):
index = idx
# key as visible on the current frame
material.diffuse_color = (0.8, 0.00778373, 0.0094196)
material.keyframe_insert(‘diffuse_color’,frame=index)
material.diffuse_color = (0.8, 0.8, 0.8)
material.keyframe_insert(‘diffuse_color’,frame=index-1)
obj.active_material = material