What You'll Do
In the tutorial, you'll make a 3D equilateral triangle from scratch.
An equilateral triangle is a triangle with three sides of equal length.
To make it interesting, we're also going to add a little depth to it.
While doing this, you'll learn how to create your own mesh from the ground up.
Clearing the Deck
Start a new Blender project and fire up the text editor.
Let's start off by getting some code in there.
import bpy
def removeObjects( scn ):
for ob in scn.objects:
if ob.type == 'MESH':
scn.objects.unlink( ob )
scn = bpy.context.scene
removeObjects( scn )
This code removes all meshes from the scene.
Go ahead and run it.
You won't be needing that default cube anyways.
Small Side Note:
There is one small distinction to make here though.
Unlinking the cube from the scene kills the association between the cube and the scene
It does NOT delete it.
The distinction doesn't matter for the purpose of this tutorial but it's something worth keeping in mind anyways.
If curious, try the following before moving on to the next section...
Switch the outliner to Datablock view
Scroll down to where it says Meshes
Click the (+) sign to expand.
Working with Objects
What you will need is a Mesh object and a Mesh datablock.
Add this code between the triforce creation start and end comments.
# initial setup of the triangle mesh and object.
triMesh = bpy.data.meshes.new( 'triMesh' )
triOb = bpy.data.objects.new( 'triOb', triMesh )
scn.objects.link( triOb )
Give it a run.
You won't see much except maybe a little dot at the (0,0,0) dead center point in the scene.
Take a look at the outliner view.
Note that you should have an object in your scene called triOb or perhaps triOb.xxx.
Click the little plus sign.
Now, you should see the mesh associated with the object.
The outliner provides an excellent visual representation of the hierarchy in place here.
The triMesh is just data that belongs to the triOb object.
The triOb is an object that belongs to the scene.
The relationship between objects, meshes, and scenes is important.
Scenes have objects and objects have data.
The kind of data depends on the kind of object.
Since, we are dealing with mesh objects, the data involved is vertices, edges, and faces.
Adding Vertices
Okay, now that we've discussed objects, let's deal with the specifics of meshes.
Specifically, we need to start off with vertices.
Let's take it a step further to make it runnable.
Add the code below directly below where you created the triOb object.
Run this and you have the beginnings of a triangle.
You may have to select this object in the outliner to make those vertices visible.
You should have something similiar to the image below.
Making Faces
The triangle is just dots in the air right now.
It needs faces to complete it.
Add this code in right after the code where you added in the vertices.
Be mindful of the fact that you will also be changing the allFaces line.
The numbers here refer to the indices for the list of vertices.
Since the bottom face requires the 3 vertices at the "bottom" vertices, we refer to the vertices that have the z level at 0.
Likewise, the "top" face requires the three vertices that are above the other 3 so we refer to the vertices with z level at .2.
So far so good.
Now that the triangles are taken care of, let's add some quads to the sides to complete this.
The main difference here is that you're dealing with quads which means groups of 4 coordinates instead of 3.
This also has to be planned carefully since the faces aren't dealing with consecutive indices in the vert list.
If it's hard to visualize, use scratch paper and number the vertices.
Done right, this will give you nice clean faces.
Run it one more time and your triangle should look something like this...
Here is the final source listing for this tutorial.
# triangle25_complete.py
import bpy
def removeObjects( scn ):
for ob in scn.objects:
if ob.type == 'MESH':
scn.objects.unlink( ob )
scn = bpy.context.scene
removeObjects( scn )
triMesh = bpy.data.meshes.new( 'triMesh' )
triOb = bpy.data.objects.new( 'triOb', triMesh )
# vertex data.
bottomVerts = ( (0,0,0), (1,0,0), (.5,1,0) )
topVerts = ( (0,0,.2), (1,0,.2), (.5,1,.2 ) )
allVerts = bottomVerts + topVerts
# face data
topAndBottomFaces = ( (0,1,2), (3,4,5) )
sideFaces = ( (0,2,5,3), (1,2,5,4) )
baseFace = ( (0,3,4,1), )
allFaces = topAndBottomFaces + sideFaces + baseFace
# load up the mesh data
triMesh.from_pydata( allVerts, (), allFaces )
scn.objects.link( triOb )
More Things To Try
This tutorial only focuses on the creation of the mesh itself.
To be truly useful for a user, it helps to have it be executed as an operator.
Also, suppose you wanted to let the user adjust the thickness of our triangle or some other property?
In such case, it learning how Blender properties work would be a good place to start.
Or, you could check out my step pyramid script to see an example of how all these ideas tie together.
In any case, best of luck in your projects and happy blendering.