Blender Python: Mesh Definition

<<< Return to the Blender Python Notebook

Summary

Understanding how meshes are defined and created is essential for scripting geometry within Blender. The process is fairly straightforward and requires the user to define the following mesh attributes:

  • Vertices (Points defined by X, Y, and Z)
  • Edges (Wireframe curves defined by vertex indices)
  • Faces (3D surfaces defined by vertex indices)

Simple Mesh Definition

4-Corner Plane

In this example, we will define a simple plane to demonstrate the relationship between vertex and face index. The plane will be composed of four points.

Vertices and Faces

First we define the vertex and face variables… These are defined as arrays. (As always, define your import to bpy!)

import bpy
 
#Define vertices, faces
#The vertex array contains 4 items with X, Y, and Z definitions
verts = [(0,0,0),(0,5,0),(5,5,0),(5,0,0)]
 
# the faces array contains 1 item.  
# The number sequence refers to the vertex array items.
# The order will determine how the face is constructed.
faces = [(0,1,2,3)]
Mesh and Object variables

We now need to define variables for mesh and scene object…

#Define mesh and object
mymesh = bpy.data.meshes.new("Plane")
 
#the mesh variable is then referenced by the object variable
myobject = bpy.data.objects.new("Plane", mymesh)

We now to to define where the mesh will be created. We can set the mesh to be created at our cursor location…

#Set location and scene of object
myobject.location = bpy.context.scene.cursor_location # the cursor location
bpy.context.scene.objects.link(myobject) # linking the object to the scene
Create the Mesh

Now we can create the mesh…

#Create mesh
# this method has an optional 'edge' array input. This is left as an empty array
mymesh.from_pydata(verts,[],faces) 
mymesh.update(calc_edges=True) #so the edges display properly...
Final Code

And all together….

import bpy
 
#Define vertices and faces
verts = [(0,0,0),(0,5,0),(5,5,0),(5,0,0)]
faces = [(0,1,2,3)]
 
# Define mesh and object variables
mymesh = bpy.data.meshes.new("Plane")
myobject = bpy.data.objects.new("Plane", mymesh)  
 
#Set location and scene of object
myobject.location = bpy.context.scene.cursor_location
bpy.context.scene.objects.link(myobject)
 
#Create mesh
mymesh.from_pydata(verts,[],faces)
mymesh.update(calc_edges=True)
Mesh_Plane.JPG

Cube

After a plane, a cube isn't that much of a leap forward. The key is keeping track of your vertex order and face indices…

import bpy
 
#Define vertices, faces, edges
verts = [(0,0,0),(0,5,0),(5,5,0),(5,0,0),(0,0,5),(0,5,5),(5,5,5),(5,0,5)]
faces = [(0,1,2,3), (4,5,6,7), (0,4,5,1), (1,5,6,2), (2,6,7,3), (3,7,4,0)]
 
#Define mesh and object
mesh = bpy.data.meshes.new("Cube")
object = bpy.data.objects.new("Cube", mesh)
 
#Set location and scene of object
object.location = bpy.context.scene.cursor_location
bpy.context.scene.objects.link(object)
 
#Create mesh
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)
Mesh_Cube.JPG

Pyramid

This pyramid demonstrates how to create triangle using 3 indices instead of 4…

import bpy
 
#Define vertices, faces, edges
verts = [(0,0,0),(0,5,0),(5,5,0),(5,0,0),(2.5,2.5,4.5)]
faces = [(0,1,2,3), (0,4,1), (1,4,2), (2,4,3), (3,4,0)]
 
#Define mesh and object
mesh = bpy.data.meshes.new("Cube")
object = bpy.data.objects.new("Cube", mesh)
 
#Set location and scene of object
object.location = bpy.context.scene.cursor_location
bpy.context.scene.objects.link(object)
 
#Create mesh
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)
Mesh_Pyramid.JPG
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License