Blender Python: Mathematical Mesh

### Summary

After grasping the basics of creating a mesh using Vertices and Faces, it is important to understand some techniques for generating them using more efficient processes. This page walks through some code for plotting a mesh from mathematical equations. Along the way, you can see how Vertices and Faces are organized using variables and loops.

### Wave Surface

#### Variables

These variables are both essential to creating the wave surface as well as giving us control over creating variations.

```# mesh arrays
verts = []  # the vertex array
faces = []  # the face array

# mesh variables
numX = 10  # number of quadrants in the x direction
numY = 10  # number of quadrants in the y direction

# wave variables
freq = 1  # the wave frequency
amp = 1  # the wave amplitude
scale = 1  #the scale of the mesh
```

#### Vertex Array

With the variables defined, we can use them to control the parametric equation for a Wave surface. The wave surface is defined with the parametric form:

• x = i
• y = j
• z = Cos(i) + Sin(j)

To control the surface, we can use our defined variables for scale, frequency and amplitude:

• x = Scale * i
• y = Scale * j
• z = Scale * (Amplitude*Cos(i * Frequency) + Amplitude*Sin(j * Frequency))

By placing x, y, and z variables in to a nested For loop with variables i and j, we can plot a grid of vertices which map the parametric surface.

At this stage, we can verify the how the points are plotted by creating our mesh with the vertex array and an empty face array.

```#fill verts array
for i in range (0, numX):
for j in range(0,numY):

x = scale * i
y = scale * j
z = scale*((amp*math.cos(i*freq))+(amp*math.sin(j*freq)))

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

#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)
```

#### Face Array

After filling out the vertex array, we need to fill the Face array. Each item in the face array should contain 4 indices that refers to an item in the vertices array.

```#fill faces array
count = 0
for i in range (0, numY *(numX-1)):
if count < numY-1:
A = i  # the first vertex
B = i+1  # the second vertex
C = (i+numY)+1 # the third vertex
D = (i+numY) # the fourth vertex

face = (A,B,C,D)
faces.append(face)
count = count + 1
else:
count = 0
```

#### Final Code

```import bpy
import math

# mesh arrays
verts = []
faces = []

# mesh variables
numX = 10
numY = 10

# wave variables
freq = 1
amp = 1
scale = 1

#fill verts array
for i in range (0, numX):
for j in range(0,numY):

x = scale * i
y = scale * j
z = scale*((amp*math.cos(i*freq))+(amp*math.sin(j*freq)))

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