__author__ = 'thoth'
# mission by username12565 on IRC
import bpy
from math import *
def load_numbers(fname):
with open(fname) as file:
return [ float(line) for line in file if line.strip() !=""]
def mesh_for_cylinder(n, dz):
name = "cylinder %d"%n
mesh = bpy.data.meshes.get(name)
if mesh is None:
mesh = bpy.data.meshes.new(name)
verts = []
faces = []
for i in range(n):
theta = 2*pi*i/n
v0 = [ cos(theta), sin(theta), dz]
v1 = [ cos(theta), sin(theta), 0]
verts.append(v0)
verts.append(v1)
i0 = i*2
if i+1>=n:
i2 = 0
else:
i2 = i0+2
faces.append([i0, i0+1, i2+1, i2])
# caps
faces.append( [ i*2 for i in range(n)])
faces.append( [ (n-i)*2-1 for i in range(n)])
mesh.from_pydata(verts, [], faces)
return mesh
def make_cylinders(scn, radii, n, dz):
z=0
mesh = mesh_for_cylinder(n, dz)
for r in radii:
name = "cyl @%f"%z
obj = bpy.data.objects.new(name, mesh)
scn.objects.link(obj)
obj.location=(0,0,z)
obj.scale = (r,r,1)
z = z+dz
#
scn = bpy.context.scene
radii = load_numbers("/tmp/radii.txt")
make_cylinders(scn, radii, 24, 2)
|
Blender python API quick-start
Syntax highlighting by Pygments.