Skip to content
Home » Sine Wave Animation in Blender with Python Scripting

Sine Wave Animation in Blender with Python Scripting

Spread the love

Today we’ll see how to make a simple sine wave animation using Python. I’ll be using the 2.81 version of Blender.

In our animation we’re going to create a couple of objects that will move along sine waves. We will cover the following topics related to Python programming in Blender and we will see how to do the following in code:

  • Text Editor
  • bpy module
  • math module (sin, pi)
  • access objects
  • set frames
  • for loops
  • set location
  • insert keyframes

If you want to watch the video first, here it is:

And here are the steps:

Step 1 – Edit the Material

Keep the default cube. Yes, you heard me right ! Don’t delete it. Instead change the color of its material to red and go to Material preview to see what it looks like. Rename the cube ‘red’ in the Outliner.

edit the material

Step 2 – Duplicate the Cube

Go to top view. Duplicate the cube twice, each time moving it 10 units up :Shift + D to duplicate, then Y 10 to move. In the materials tab you can see the number 3 to the right of the name of the material. This means that three objects share this material. If you now changed the color of the material for any of the cubes, all three will change because they share the same material. This is not what we want, because we want three different colors.

Duplicate the Cube

Step 3 – Copy the Material

Select the upper cube and click on the button with the number 3 mentioned above. This will make a copy of the material for private use of the first cube. The number 3 button will disappear (A). Now change the color of the cube to blue. Repeat the same step for the middle cube, but this time change its color to yellow. Rename the two cubes blue and yellow respectively in the Outliner.

Copy the Material

Step 4 – The Scripting Workspace

Now we’re going to script three animations, for each cube separately. First of all let’s go to the scripting workspace (A). In this workspace you can see the 3D View editor in the upper left corner (B). Go to top view, Material preview in this editor.

We will also need a Timeline for our animations, so hover your mouse cursor near the upper right corner of the Python Console and when a + sign appears, drag it down to open a new window. Change the editor type to Timeline (C).

The Scripting Workspace

Step 5 – Text Editor for Scripting the Sine Wave Animation

The big editor on the right is the Text Editor. It’s the one where we’re going to enter our Python code. First click on New and then open the sidebar by checking the Sidebar box in the View menu. For better readability you can change the font size to 16.

Text Editor

Step 6 – Import the Python Modules Necessary To Create the Sine Wave

When done with the font size settings, you can close the sidebar in the View menu. Now let’s import the Python modules and functions that we’re going to use. First, the Blender module, bpy. Second, the sin function and the pi constant from the math module:

import bpy
from math import sin, pi

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Step 7 – Make References to the Objects

Now we’re going to continue with our code. We need references to our three cubes. All our objects are in the bpy.data.objects dictionary and we can access them by key. The keys match the names that were given to the objects in the Outliner. So, let’s save the three objects in three variables : red, blue and yellow:

red = bpy.data.objects['red']
blue = bpy.data.objects['blue']
yellow = bpy.data.objects['yellow']

Step 8 – Set Current Frame

This is going to be an animation, so we’ll be working with frames and keyframes in the Timeline. The animation should start at frame 0, so add the following line to the script:

frame_number = 0

Step 9 – Insert Keyframes in a Loop

And now we’re going to work in a loop. The loop will iterate over the frames in the Timeline and will insert keyframes at some of the frames. In each iteration of the loop the location of the appropriate cube will be calculated (X, Y and Z – position). This is best visible in the code itself, so let’s first animate the red cube in the loop. This cube will move along the X axis in a sine wave. Here’s the full code so far with explanations:

import bpy
from math import sin, pi

red = bpy.data.objects['red']
blue = bpy.data.objects['blue']
yellow = bpy.data.objects['yellow']

frame_number = 0

# Iterate between 0-100, 101 is not included. This is how the
# range function works.
for i in range(0, 101):
    
    # Set the current frame
    bpy.context.scene.frame_set(frame_number)  
    
    # Calculate the X, Y and Z location of the cube.
    # When graphing the sine wave, by default we use radians instead of degrees.
    # The points on the X axis where the sine wave changes direction are
    # multiples of pi/2, so 0, pi/2, pi, 3/2 pi, 2 pi, 5/2 pi, etc.
    # This is exactly what our X locations are going to be. The loop variable
    # i changes between 0 and 100 in each iteration.
    x = i/2 * pi
    
    # The Y coordinate is calculated as the function of X. To make the
    # movement more visible, we'll multiply the result by 10, because 
    # the values range from -1 to 1, which is not much.
    y = sin(x) * 10
    
    # We want the movement only on the XY plane, so we can set z to 0.
    z = 0
    
    # Now we set the location of the cube using the coordinates just
    # calculated.
    red.location = (x, y, z)
    
    # And we insert a keyframe. We use the data_path keyword argument
    # to set the type of the keyframe. In our case it's a location keyframe.
    # We also set the index to -1, which means we're going to use X, Y and
    # Z location, so all three dimensions.
    red.keyframe_insert(data_path='location', index=-1)
    
    # Finally we increase the current frame number by 5 frames.
    frame_number += 5

Step 10 – Play the Sine Wave Animation

Now you’re ready to run the script. To this end press the Run Script button in the Text Editor (A). Then select the red cube in the 3D View and go to frame 0 (B). Set the end frame to 500, because this is how many frames we’re going to need. You can see all the keyframes in the Timeline (X Location, Y Location and Z Location). If you now play the animation (C), you will see the red cube moving along a sine wave.

Play the Animation

Step 11 – Animate the Blue Cube

Next, let’s animate the blue cube.

Here’s the full code with all the necessary changes:

import bpy
from math import sin, pi

red = bpy.data.objects['red']
blue = bpy.data.objects['blue']
yellow = bpy.data.objects['yellow']

frame_number = 0

for i in range(0, 101):
    
    bpy.context.scene.frame_set(frame_number)  
    
    # red cube
    # Let's rename the variables to xr, yr and zr.
    xr = i/2 * pi
    yr = sin(xr) * 10
    zr = 0    
    
    red.location = (xr, yr, zr)
    red.keyframe_insert(data_path='location', index=-1)
    
    # blue cube
    # The movement along the X axis should be slower than the red cube's.
    # To do that you can divide i by a number larger than 2.
    xb = i/3 * pi
    
    # The blue cube should move in the XY plane, but also in the XZ plane.
    # The base level of the blue cube should be 10 units above the X-axis,
    # so we must add 10. Also, this time let's multiply the value of the
    # sine function by 5 to flatten the movement.
    yb = 10 + sin(xb) * 5
    
    # We want the blue cube to also move along a sine wave in the XZ plane.
    zb = sin(xb) * 10  
    
    blue.location = (xb, yb, zb)
    blue.keyframe_insert(data_path='location', index=-1)
        
    frame_number += 5

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Step 12 – Watch the Blue Cube Moving Along a Sine Wave

Run the script. Then change the view to an angle from which it’ll be easier to see how the two cubes move. Play the animation. The blue cube is now moving in two planes.

Watch the Blue Cube Moving

Step 13 – Animate the Yellow Cube

Finally, let’s animate the yellow cube. Here’s the full code with all the necessary changes:

import bpy
from math import sin, pi

red = bpy.data.objects['red']
blue = bpy.data.objects['blue']
yellow = bpy.data.objects['yellow']

frame_number = 0

for i in range(0, 101):
    
    bpy.context.scene.frame_set(frame_number)  
    
          # red cube
    xr = i/2 * pi
    yr = sin(xr) * 10
    zr = 0    
    
    red.location = (xr, yr, zr)
    red.keyframe_insert(data_path='location', index=-1)
    
    # blue cube
    xb = i/3 * pi
    yb = 10 + sin(xb) * 5
    zb = sin(xb) * 10  
    
    blue.location = (xb, yb, zb)
    blue.keyframe_insert(data_path='location', index=-1)
    
    # yellow cube
    # This cube should follow a sine wave up the Y-axis. So, we have to 
    # swap the coordinates and make some tiny changes:
    # First we define the yy coordinate. The xy coordinate will be
    # dependent on yy. This time we're going to keep to the XY plane
    # just like with the red cube.    
    yy =  i/4 * pi + 20   
    xy = sin(yy) * 5 
    zy = 0 
    
    yellow.location = (xy, yy, zy)
    yellow.keyframe_insert(data_path='location', index=-1)    
    
    frame_number += 5

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

Step 14 – Play the Complete Sine Wave Animation

Run the script. Go back to the Layout workspace. There the animation will be more visible. Play the animation and watch it from different angles.


Spread the love

2 thoughts on “Sine Wave Animation in Blender with Python Scripting”

Leave a Reply