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 matdef 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))