[EMS Discuss] Python scripts and Blender

John Burridge burridge.john at gmail.com
Fri Apr 20 21:45:38 PDT 2012


There was quite the discussion (and instruction) about Blender tonight
at the open house.  I mentioned a Python script that I'd written to
make a simple star mesh.  Here's the (spaghetti) code I wrote
(apparently a year and a half ago...)


=================

#
# Dec 22 2010
# Script to make a sculpted star mesh
#

import bpy   # You have to import bpy calls or you can't script primitives
from mathutils import Vector
import math # You have to import cmath calls to do sine and cosine functions
import types

#
# phi should be the XY plane, theta should be the YZ plane
#

verts = []  #no face
edges = []  #no face
faces = []  #no face
Point = []
theta = phi = a = b = c = i = x = y = z = 0   #  defining x y and z by
assigning a value

segments = 20  # number of segments per step 3 = triangle, etc
segsize = .05  # how big to make the segment "ring"
step = .5  # 1 = really smooth shapes DONT MAKE NEGATIVE
lobe = .8 # fiddle with lobes to get different patterns

#
#  Set the star mesh's origin point
#

BaseX = 0
BaseY = 0
BaseZ = 0


#
# construct a 3D star.
#

radius1 = 2.25
radius2 = radius1 * (1 - 0.618)
points = 5 # 5 for a five pointed star

Point.append([BaseX, BaseY, ((radius2/2) + BaseZ)])

while i < points:
    print (i)
    xr1 = BaseX + math.cos(i * 2 * 3.14159/points) * radius1
    yr1 = BaseY + math.sin(i * 2 * 3.14159/points) * radius1
    zr1 = BaseZ
    xr2 = BaseX + math.cos((i * 2 * 3.14159/points) + 3.14159/points) * radius2
    yr2 = BaseY + math.sin((i * 2 * 3.14159/points) + 3.14159/points) * radius2
    zr2 = BaseZ

    Point.append([xr1, yr1, zr1])
    Point.append([xr2, yr2, zr2])
    i = i + 1

Point.append([BaseX, BaseY, ((radius2/-2) + BaseZ)])

John_mesh = bpy.data.meshes.new("JohnSculpt")  # create the mesh




#
#  Use Points to define a list of verts
#


for q in range(len(Point)): # all list indexes start with 0
    verts.append(Point[q])
    print (verts[q])



#
# specify vert element number to build list faces
#

for q in range(len(verts) - 2):
    faces.append([0, q+1, q+2])     # three vert list elements define a face
    faces.append([(2*points+1), q+1, q+2])     # three vert list
elements define a face


faces.append([0, (2*points), 1])
faces.append([(2*points+1), (2*points), 1])






John_mesh.from_pydata(verts,edges,faces)
John_mesh.update()
sc = bpy.context.scene
obj_new = bpy.data.objects.new("John Sculpt",John_mesh)
sc.objects.link(obj_new)

bpy.ops.object.select_all(action='TOGGLE')
bpy.ops.object.select_name(name="John Sculpt", extend=False)
bpy.ops.transform.rotate(value=(3.1415/4,), axis=(1, 0, 0),
constraint_orientation='GLOBAL')


=============================
-- 
----

John Burridge
burridge.john at gmail.com
http://johnburridge.blogspot.com


More information about the Discuss mailing list