Having trouble running code on Blender. What this code is supposed to do is make a sphere of cubes. It can make a circle just fine but is having troubles with the embedded while loop. It runs through the inner loop fine and makes a circle (of radius 0), but does not go through the "phi" while loop to make a sphere.circle of 8 cubes a distance RadDist above the cursor and RadDist*sin(phi) = 0

import bpy
from bpy import context 
from math import sin, cos, radians

cubeobject = bpy.ops.mesh.primitive_cube_add

#get the cursor location in the scene
cursor = context.scene.cursor_location

#increase the radial distance of cubes in loop
RadDist = 5.0

#initialize some variables
xsize = 1.0
ysize = 1.0
zsize = 1.0
phi = 0
theta = 0.0
RadStep = 6.28/8.0
while phi < 3.14:
    while theta < 6.28:
        x = cursor.x + RadDist*cos(theta)*sin(phi+3.14/2)
        y = cursor.y + RadDist*sin(theta)*sin(phi+(3.14/2))
        z = cursor.z + RadDist*cos(phi)
        cubeobject(location = (x,y,z))
        #add cubes along the radial
        theta += RadStep
    #add cubes along the azimuthal    
    phi += RadStep   

theta = 0.0
phi = 0.0

1 Answer 1


I figured out the solution. The issue was that once i got out of the embedded while loop the theta value remained at 6.28 instead of returning to 0.0 it was remedied by putting theta = 0.0 in the outer while loop.

lesson of the day When you leave a while loop the values you have made remain the same when you leave, remember to reinitialize them.

here is my sphere of cubes.Sphere of cubes


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .