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.
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:
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:
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?