Question

Please forgive me, but I only really know how to somewhat code in VB, and python is not what I'm used to. I did try to see if other people have made and or shown an algorithm that I'm trying to accomplish.

I have a visualizer design in my head and What I have been trying to do is get a number of cubes (variable input for now) to be placed a certain distance (maybe 5-10 blender units) from the center of the scene and angle the faces so that there will be one face pointing to the center and one face pointing the opposite direction. I'm trying to just start with 10 cubes because I feel like it will be a fair number to hopefully show a circle shape.

I made an image to help describe what I am trying to do:

enter image description here

All I have been able to figure out so far is that I need to add a cube with a certain rotation, and that rotation needs to be stepped for each cube. so a small equation is needed, something like this.

    (10)           (36)
360 / numberOfCubes = steppedAngle

That's all I have been able to figure out because I don't know how to program python to do such.

Any help is greatly appreciated, and will be credited in the final render.

Update: 1

Thanks to the help from the answer below, I finally got it to work how i wanted. img http://vvcap.net/db/bKKUz3Uw4WUqL_WVDU0j.png

and here is the code written in help from the answer below. ' import bpy import math

##num of cubes
n = 10

##distange from center
radius = 7

for i in range(1, n + 1):
    angle = (i - 1) * math.pi * 2 / n
    xcoord=(radius * math.cos(angle))
    ycoord=(radius * math.sin(angle))
    bpy.ops.mesh.primitive_cube_add(location=(xcoord,ycoord,0),rotation=(0,0,angle))

'

Was it helpful?

Solution

Let's start with cubes in a circle, and we will work our way from there.

You have N cubes, and you want to place them in a circle of radius R around the center of the universe (0,0,0).

Applying basic trigonometry: Each cube is on one of the radius of the circle, when you divide the circle by N. Therefore, your first cube is at 0 rad, your second cube is at 2*pi/N rad, your third cube is at 2 * 360/N rad, ... your N cube is at (N-1) * 2*pi/N rad. Now we have a formula: Location of the cube in the circle = (i - 1) * 2*pi/N in radians, for each i from 1 to N. Now, the location in space coordinates is (r*cos(angle), r*sin(angle), 0) for a circle that is placed on the XY plane and it's center is on (0,0,0).

My Blender Python is very rusty, so I won't provide you a complete solution, but it should be this way:

import math

for i in range(1, N + 1):
    angle = (i - 1) * math.pi * 2 / N
    x_coord = radius * math.cos(angle)
    y_coord = radius * math.sin(angle)
    z_coord = 0
    cube = place_cube(x_coord, y_coord, z_coord)

This will place the cubes on the right coordinates, but it won't turn them the right way. Fortunately, you can rotate each cube by angle, and get it in the right orientation. So you can do:

import math

for i in range(1, N + 1):
    angle = (i - 1) * math.pi * 2 / N
    x_coord = radius * math.cos(angle)
    y_coord = radius * math.sin(angle)
    z_coord = 0
    cube = place_cube(x_coord, y_coord, z_coord)
    cube.rotate_around_z(angle)

I have not provided the place_cube and rotate_around_z functions because I hardly remember the Blender Python api, but it shouldn't be too hard.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top