I want to make a mesh object that looks like the outside surface of cubes stacked in the shape of a sphere. I thought I saw this once, but I can't find it.
The final size will be big (up to 100x100x100 cubes) so I think making a million cubes and merging them (modifier Boolean Union) is going to take quite a while, especially if I have to do it differently for every frame.
As a first step, I've stacked cubes in a hollow spherical shell.
What I really want is just a single mesh object of the outside surface. But for 100x100x100 that's a million cubes to Union. There must be an easier way? 2) if not, how can I select say 20,000 cubes in a "shell" and Union them at once?
Through brute-force application of logic & math & coffee, I guess one could define an algorithm to generate the mesh directly, but I somehow think that I saw this done before in an elegant way. "cubification"? "cubation"?
Here's an example of a small shell that takes about ten seconds to generate. In the picture I've removed the top.
import bpy
import numpy as np
nx, ny, nz = 21, 21, 21
x0, y0, z0 = 10, 10, 10
r1, r2 = 6.0, 7.4
scale = 0.5
x, y, z = np.arange(nx), np.arange(ny), np.arange(nz)
rsq = (x[None, None, :]-x0)**2 + (y[None, :, None]-y0)**2 + (z[:, None, None]-z0)**2
sphere_1 = rsq < r1**2
sphere_2 = rsq < r2**2
shell = sphere_2 * (-sphere_1) # I love python!
X, Y, Z = np.meshgrid(x, y, z)
ipoints = zip( X.flatten(),
Y.flatten(),
Z.flatten() )
points = zip(scale*(X.flatten()-x0),
scale*(Y.flatten()-y0),
scale*(Z.flatten()-z0) )
points = [thing for thing in points] # convert zip object to list (python 3!)
ipoints = [thing for thing in ipoints]
points_shell = [thing for i,thing in enumerate(points)
if shell[ipoints[i]]]
for point in points_shell:
bpy.ops.mesh.primitive_cube_add(radius=0.5*scale, location=point)