__author__ = 'thoth'
# http://blender.stackexchange.com/questions/71179/script-for-hexagonal-distribution/71193
# https://en.wikipedia.org/wiki/Close-packing_of_equal_spheres
# Fill a rectangular prism with cannonballs according to hexagonal close-packed (triangular orthobicupola variant).
# An alternate orientation not supported by this code is face-centered
# cubic which is geometrically similar, just aligned to a different axis.
import bpy
from math import *
def construct_cannonball(mesh, x,y,z):
scn = bpy.context.scene
obj = bpy.data.objects.new("ball", mesh)
scn.objects.link(obj)
obj.location = [x,y,z]
def mission1(mesh, r, xyz1, xyz2, construct_cannonball=construct_cannonball):
dx = 2*r
dy = sqrt(0.75)*dx
dz = sqrt(2/3)*dx # thank you, CRC Concise Encyclopedia of Mathematics
w = 0
z=xyz1[2]
while z<xyz2[2]:
v=0
y=xyz1[1]
if 1==w%2:
y += dx/2 * tan(pi/6)
while y < xyz2[1]:
x = xyz1[0]
if 1==w%2:
if 1==v%2:
x -= dx/2 / cos(pi/6)
else:
x += dx/2 / cos(pi/6)
if 1 == v%2:
x += dx/2
while x < xyz2[0]:
construct_cannonball(mesh, x,y,z)
x += dx
y += dy
v+=1
z += dz
w += 1
def purge(scn, prefix):
for obj in scn.objects:
if obj.name[0:len(prefix)] == prefix:
obj.name = "discard"
scn.objects.unlink(obj)
purge(bpy.context.scene, "ball")
mission1(bpy.data.objects['prototype'].data,
1, [-5,-5,-5], [5,5,5])
|
Blender python API quick-start
Syntax highlighting by Pygments.