Today we’re going to do some very basic color animation in Python. We’ll add a material to an object and insert keyframes at which it will change its color.
Here’s the video version of the article:
Table of Contents
Step 1 – Create an ico sphere
Delete the default cube and save the file as colors.blend. Create an ico sphere and scale it: S 5. In the Materials tab you can see there isn’t any material, so we’ll create one in code.
Step 2 – Create a New Script
Go to the Scripting workspace (A), create a new script by pressing the New button and check Sidebar in the View menu (B). In the Sidebar set Font Size to 16 so that the code is more readable (C). We’re also going to need the Timeline editor, so open one at the bottom of the Text editor (D). To do that just hover your mouse cursor over the lower right corner of the Text editor (it’s the big one in the center) until a plus sign appears. Then with the left mouse button down drag your mouse up and in the upper left corner of the newly opened editor set its type to Timeline.
Step 3 – Go to Rendered Shading
Uncheck Sidebar in the View menu do hide it (A). As we are going to create a material, in order to view it, go to Rendered shading in the 3D View editor (B).
Step 4 – Type In the Python Code
Type in the following Python code in the Text editor. It’s going to create a material and add it to the ico sphere. Then it’s going to add keyframes at which the sphere will change its color. You will find all the explanations in the comments. Here’s the code:
import bpy
# Create a new material and name it 'colored'.
mat = bpy.data.materials.new(name= 'colored')
# Save the ico sphere you just created in a variable.
ico = bpy.data.objects['Icosphere']
# Set the new material you just created to the ico sphere's active material.
ico.active_material = mat
# Let's define the colors that we're going to use. Each color is defines as a 4-tuple
# where each of the four values is a float number between 0 and 1. The numbers are for
# red, green, blue and alpha respectively. Our object is going to be fully opaque, so
# alpha is always set to 1. The colors are stored in a list.
colors = [(1, .7, .2, 1), # golden
(.1, 1, .1, 1), # green
(.1, .7, 1, 1), # blue
(.7, 0, 1, 1), # purple
(1, 0, 0, 1), # red
(0, 0, 0, 1) # black
]
# Let's create a list of all the frames along the timeline where you want to insert keyframes.
# There will be as many keyframes as colors in the colors list because we want to change colors
# at these locations on the Timeline.
frames = [1, 30, 60, 90, 120, 250]
# Now let's insert the keyframes. We can do it in a for loop. The loop will iterate over both
# frames and colors, so we need two loop variables (f for frames and c for colors).
# Besides we have to zip the two lists using the zip function.
for f, c in zip(frames, colors):
# Set the material's diffuse color to the current color from the colors list.
mat.diffuse_color = c
# Insert a diffuse color keyframe at the current frame.
mat.keyframe_insert(data_path='diffuse_color', frame=f, index=-1)
If you want to learn more about the zip function that I used in the code above, here’s a link.
Step 5 – Check Materials and Timeline
Make sure the name of the object in the Outliner and in the code match (A). Go to the Materials tab (B). As you can see, there isn’t any material yet. Also, in the timeline, there are no keyframes yet (C).
Step 6 – Run the Script
Run the script by pressing the button at the top of the Text editor (A). As you can see the script created a material and named it ‘colored’ (B). Now you can also see all the keyframes the script inserted in the timeline at the frames specified in it (C). In the 3D View editor you can see the sphere as it looks at the end of the animation (D).
Step 7 – Go to Frame 1
If you now click the Go to Frame 1 button in the timeline editor (A), you will see the object as it looks at frame 1 (B). You will also see its initial color in the Materials tab (C).
Step 8 – Play the Animation
Play the animation (A) and watch the sphere change its color (B).
That’s all. This is a very basic animation but I hope it’ll encourage you to experiment with colors or maybe other animated properties in Python.