# How to make a mesh by using python script on Blender

I want to know how to make a mesh having Vertices and Faces by using python script from mathematical equations, for example, spherical harmonics, and so on.

If the sample code of an easy function is shown, the rest will be made by myself.

Ubuntu version: 12.04

Blender version: 2.62

I found very useful site,

Blender Python: Mathematical Mesh

Finally I got an example, this is the case of Normal Distribution.

``````import bpy
import math

# clear mesh and object
for item in bpy.context.scene.objects:
if item.type == 'MESH':
for item in bpy.data.objects:
if item.type == 'MESH':
bpy.data.objects.remove(item)
for item in bpy.data.meshes:
bpy.data.meshes.remove(item)
for item in bpy.data.materials:
bpy.data.materials.remove(item)

# mesh arrays
verts = []
faces = []

# mesh variables
numX = 100
numY = 100

# variance and scale variables
variance = .35
scale = 4

# fill verts array
for i in range (0, numX):
for j in range(0,numY):
# nomalize range
u = 2*(i/numX-1/2)
v = 2*(j/numY-1/2)

s = variance
x = scale*u
y = scale*v
z = scale*1/math.sqrt(2*math.pi*s*s)*math.exp(-(u*u+v*v)/(2*s*s))

vert = (x,y,z)
verts.append(vert)

# fill faces array
count = 0
for i in range (0, numY *(numX-1)):
if count < numY-1:
A = i
B = i+1
C = (i+numY)+1
D = (i+numY)
face = (A,B,C,D)
faces.append(face)
count = count + 1
else:
count = 0

# create mesh and object
mesh = bpy.data.meshes.new("wave")
object = bpy.data.objects.new("wave",mesh)

# set mesh location
object.location = bpy.context.scene.cursor_location

# create mesh from python data
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)
``````

Screenshot:

-