PYTHON: How to Code Circles with the Math Module


Today is less of a how-to day. It’s more of a blind hate rant day because everyone needs those on occasion.

Math is a haaaaaaaaaaaateful bastard. But unfortunately, when you want interesting effects, I’ve been told it can come in handy. So to get some practice, I started off by trying to code some objects into circle. Even I, with my rudimentary understanding of Python, should be able to do that. Right?

3 Hours Later: Scripting a Circle in Blender

So much worse than I expected, but I realize now that it wasn’t really a coding issue. It was me not being able to comprehend trigonometry. Which seems more forgivable.

For anyone else out there genuinely confused by the words sine and cosine, there exists a website called I assume it’s run by a wildly ironic genius because it makes geometry somewhat understandable. The page on the Unit Circle was particularly helpful in getting me to comprehend that the radius of a circle is the hypotenuse (the longest side) of a right triangle. And the other two sides of that right triangle conveniently can be used for the X and Y coordinates of a circle.Yes, it makes my brain hurt too.

Remembering that a circle is 360 degrees, I wanted to make a loop that would find X and Y for every 10 or so degrees on the circle. MathIsFun tells me you can plug those degrees into that weird cosine button on your calculator to get the X value and the sine button to get the Y value.

The Python documentation page told me that the Math module has a sine and cosine function, but here’s the problem:

math.cos(x) #Returns the cosine of x radians
math.sin(x) #Returns the sine of x radians

If you’re anything like me, you’re probably wondering, “What the hell is a radian?”

It’s equal to about 57.2958 degrees. There are about 6.283 of them in a circle. So if you want to code a circle into blender, you can plug that into a while loop (i<6.283) and use i as your radian variable to get sine and cosine. Incrementing i by a larger value will run the loop fewer times, resulting in fewer circles. Like so:

Oh my. What pretty circles.

If you turn that into a function, you can add a parameter for the x and y coordinates of the circle center, so you can reuse all over a scene. Here’s the copy/paste version:

import bpy
import math

def circloop(cx,cy,z,size):
while i < 6:

What’s that good for?

Well, I plugged the circle function into a loop, and it gave me a tunnel. Or corn on the cob. It’s all in how you texture things.

Here’s the script dressing up some columns:

What lovely columns

Success. But if you’re anything like me, now you’re wondering how to get the objects to MOVE in circles. That should be easier than getting the circles in the first place. Right?


Tagged , , , , , , , ,

11 thoughts on “PYTHON: How to Code Circles with the Math Module

  1. benjmorgan says:

    Animating something moving in a circle is pretty straightforward. One way to do it is to have x = cos(t) and z = sin(t) …. Do this on the desired time range, move the object to that position, set a keyframe, etc… offsetting the object would just include adding its position to that point array…

    Liked by 3 people

  2. benjmorgan says:

    For instance:
    while t<=24:
    #move object to [math.cos(t),0,math.sin(t)]
    # set a keyframe at current time
    t += 1

    Liked by 3 people

  3. Hey gifGuide2Code! I will try this Python code out as soon as I can. I love your blog. Cheers! Epic Chas Gamer 😎

    Liked by 1 person

  4. ARJ says:

    try processing

    Liked by 2 people

  5. ARJ says:

    try the coding train on you tube, channel by daniel shiefman. best tutorials.

    else i’m a huge processing fan, just send me a mail on timeofsands(at) and i’ll tell you!

    Liked by 2 people

  6. ARJ says:

    ah now really appreciating this blender post. what a nice idea !

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: