PYTHON: How to Code Circles with the Math Module

DUN-DUN-DUUUUUUUUUUN!

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 MathIsFun.com. 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):
i=0
while i < 6:
x=cx+math.cos(i)
y=cy+math.sin(i)
bpy.ops.mesh.primitive_uv_sphere_add(size=size,location=(x,y,z))
i+=.5

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?

 

11 Replies to “PYTHON: How to Code Circles with the Math Module”

  1. 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. 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)gmail.com and i’ll tell you!

    Liked by 2 people

Leave a comment