Graphics Reference
In-Depth Information
1.
First, let's get the active object (which is necessarily going to be the cone that was just created) and
assign it to the local variable
obj
.
obj = bpy.context.object
2.
Next, we create a new material datablock object using the standard
bpy
syntax for creating new data
objects:
mat = bpy.data.materials.new(name="Cone Material")
3.
The red, green, and blue values for the material's diffuse color value can be set as follows:
mat.diffuse_color.r = self.r
mat.diffuse_color.g = self.g
mat.diffuse_color.b = self.b
4.
Finally, we set the material to be the object's active material like this:
obj.active_material = mat
Keyframing the Animation
AnimationkeyframesaresetanalogouslytothewaykeyframeswouldbesetinordinaryBlenderuse,bycalling
the appropriate operators in Python:
1.
First, place the object at the location determined by the script:
obj.location[2] = self.location[2]
2.
Then set the start frame to 1 like this:
startframe = 1
3.
Call the
keyframe_insert()
method to add a keyframe:
obj.keyframe_insert(data_path="location",
frame=startframe,
index=2)
4.
Change the location of the object by adding the bounce height value like this:
obj.location[2] = self.location[2] + self.bounce_height
5.
Now, set another keyframe, this time with an increased
frame
value. Note that
self.speed
is used
as a denominator to control the closeness of the keyframes.
obj.keyframe_insert(data_path="location",
frame=startframe+(10/self.speed),
index=2)
6.
Place the object back at the original position and add a third keyframe, advancing another few frames
(depending on the speed value).
obj.location[2] = self.location[2]
obj.keyframe_insert(data_path="location",
frame=startframe+(20/self.speed),
index=2)
7.
Finally, add a cyclic modifier to the animation F-curve to make it repeat, like this:
obj.animation_data.action.fcurves[0].modifiers.new('CYCLES')
8.
Younowshouldbeabletoinstallandactivatethisscriptasanordinaryadd-on.Itwillcreateabouncing
colored cone with values that can be interactively adjusted, as shown in
Figure 13-9
. In order to see the
bouncing movement, you will need to press Alt+A to play animation in the 3D viewport.