# Make a sphere of cubes in Blender using python script

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.

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

• – p2or
Jan 16, 2016 at 22:54

## 1 Answer

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.