When I’m creating meshes in python, there are often functions that are repetitive or annoying. I use a file called toolbox.py to house custom functions that I use regularly. For this tutorial, I want to add a function to my toolbox file that will let me add faces to a mesh I’m constructing more easily than having to manually add vertices to the list of vertices and faces to the list of faces.
To do this, I will make a function called
makeFace() that will take three or four vertex points, add them to the vertex list, and add a face to the face list. This is all seems reasonably simple, but we need to think about what happens at the corners of the mesh, we don’t want to add new vertices on top of old ones, so we also need to check the list of vertices for any identical vertices, and make sure not to create any duplicate vertices that look ugly and get annoying. Finally, we want to return the updated list of vertices and faces.
So, let’s get started.
We want to take two lists (of vertices and faces), lets call them
def makeFace(vList, fList):
Now we need to take at least 3 vertexes, and an optional fourth, so to do this we tell python that if no value for the fourth is supplied, just use none. We will call the vertices
def makeFace(vList, fList, v1, v2, v3, v4=None):
Excellent, now we’re ready to start defining the function!
Before we start adding faces, we need to know if we’re dealing with a tri or a quad, so let’s make a variable called tri, and set it to true if
v4 is set to None.
def makeFace(vList, fList, v1, v2, v3, v4=None): tri = True if v4 == None else False
I’m using an inline if/else statement here, but it would work just as well if you used:
tri = False if v4 == None: tri = True
Now comes the fun part. For each of our vertices, we need to go and check if they’re in the list. If they are in the list, we’ll store their index in an indexes variable. Otherwise, we will append the vertex onto the end of the list, and store that index.
Let’s start by defining a list that we’ll use to store the indexes.
v1 will be stored in
indexes, etc. Ultimately, this list will be added to the
fList, and it will define a new face.
Now, because I’m lazy, I want to use a list comprehension to go through my vertices, so let’s make a quick list called
newVertList = [v1,v2,v3]
And add in v4 if it’s needed
if not tri: newVertList.append(v4)
Alright, now we can iterate through this list, and see if it’s in the main list of vertices. If it is, let’s store it in indexes
for vert in newVertList: if vert in vList: indexes.append(vList.index(vert))
And if it’s not, we’ll add the new vertex to vList and store the index:
for vert in newVertList: if vert in vList: indexes.append(vList.index(vert)) else: vList.append(vert) indexes.append(vList.index(vert))
indexes variable now contains all of the information needed to make a face from the
vList, so let’s go ahead and add it to
Great, now the new face has been added, all that remains is to return the new
return vList, fList
The new function can be called now as follows:
from toolbox import makeFace (vList,fList) = makeFace(vList, fList, v1, v2, v3)
And your new
fList will be updated with a new face according to the values of
Congratulations! You’ve just written very handy script that, if you continue to script in blender, I’m sure you’ll use all the time!