24 Ⅰ 2011

I’ve recently been using some sine wave math to get nice and smooth results in python for Blender 3D. This page is merely here so I don’t forget what I did, and why.

The basics

The math itself is really simple… as you can see in the image above… the trick is to use this stuff in a “math loop”.

A basic loop moving through a sine wave

# Get the math module
import math

# Loop twenty one times (21 because then it starts at 0 and ends with i as 20)
for i in range(21):

	# Make a value between 0.0 and 2.0
	x = i * 0.1

	# Get the y value
	y = math.sin(math.pi * x)

	# Print it out

A nice increasing curve

# Get the math module
import math

# Make a factor so we know how much of an increase each step is
# in this case we want to increase the x value 0.5 in ten steps
factor = (0.5 / 10)

# Loop twenty times (range should be 11 so we end up with i as 10)
for i in range(11):

	# find out the x position for this value
	x = i * factor

	# Multiply by pi
	x = x * math.pi

	# but now we add 1.5 * pi because we want the last quarter of the curve
	x = x + (1.5 * math.pi)

	# Get the y value
	y = math.sin(x)

	# And because we don't want a curve from -1 to 0, but from 0 to 1 we add 1
	y = y + 1

	# Print it out