 # how to add faces to mesh??

After managing adding vertices to a Mesh, I have problems adding new faces of these newly added vertices.
First of all if I print a vertice like:

``````print LList
[MVert (0.366026 0.000000 -1.366025) (0.258522 0.000000 -0.966002) 3]
``````

Why do I have 2 sets of coordinates? And what does the very last number (3) mean?
Now I try to make a face of these 3 vertices:

``````print LList
[MVert (0.366026 0.000000 -1.366025) (0.258522 0.000000 -0.966002) 3]

print LList
[MVert (0.316988 0.183013 -1.366025) (0.223884 0.129246 -0.966002) 4]

print LList
[MVert (0.183013 0.316988 -1.366025) (0.129246 0.223884 -0.966002) 26]
meP.faces.extend(LList,LList,LList)
``````

And it really creates a new face but the vertices of the new face can by no means be the 3 vertices above! What am I missing?
And what is the impact whether I am in edit mode or not (in the 3D window) while executing this script?

(0.366026 0.000000 -1.366025) = The vertex’ coordinates in local space.
(0.258522 0.000000 -0.966002) = The vertex’ normal. Read here on how a vertex can possibly have a normal: https://blenderartists.org/forum/viewtopic.php?t=40840#391347
3 = The vertex index. Each vertex has a unique index within a mesh.

And it really creates a new face but the vertices of the new face can by no means be the 3 vertices above! What am I missing?

No idea… Are you sure the indices of those vertices in LList don’t already exist?

And what is the impact whether I am in edit mode or not (in the 3D window) while executing this script?

I think scripts can’t change mesh data in Edit mode even when you’re using the Mesh module. You have to exit Edit mode at beginning of the script, then enter it again at the end. Look how cambo does it here:
http://en.wikibooks.org/wiki/Blender_3D:_Blending_Into_Python/Cookbook#Mesh_tool_template (if is_editmode: …)

Thanks! You guys are so helpful! If every vertex in a mesh has a unique index (like 3 in my example) why do I have to pass a complete vertex object to meP.faces.extend()?

No idea… Are you sure the indices of those vertices in LList don’t already exist?

What exactly do you mean? The index in LList[index] or the unique index every vertex in a mesh gets automatically?
The vertices that I pass in meP.faces.extend(v1,v2,v3) of course already exist in the Mesh.

I think I’m missing something too To the Python experts: How do you make a new face with new vertices using the Mesh module?

faces.extend only accepts MVert objects, that is I believe: vertices already in the mesh. So you would first have to add vertices to the mesh, then retrieve them and create a face. But vertices are added to the mesh by specifying their coordinates or their vector. Since vertices in a mesh can have the same coordinates, there’s no way of retrieving them to make a new face.

Call me stupid… edit:/ Is this the preferred way? It looks very clumsy.

``````me = ob.getData(False, True)

i_len = len(me.verts)

pv_list = [(1, -1, 0), (1, 1, 0), (-1, 1, 0), (-1, -1, 0)]
me.verts.extend(pv_list)

mv_list = [me.verts[i_len], me.verts[i_len+1], me.verts[i_len+2], me.verts[i_len+3]]
me.faces.extend(mv_list)
``````

And it really creates a new face but the vertices of the new face can by no means be the 3 vertices above!

Could you please it explain this a bit more? What should the face look like and what do you get?

Okay, what I try to do is this: I have a sphere and a plane. Now I want the sphere to “dive” into the plane and “bend it out” with the profile of the sphere.
The script works like this: first it creates a dictionary with an entry for every loop with a different z-value. Then it decides how many and which of these loops are “inside” (actually under) the plane. Then it copies the vertices from the sphere to the plane.
Finally I have to make faces between the vertices and that is where I got stuck. The project is here: As you see, at the end of the script I take the second loop (which should be the second loop of 12 verts from below; the first loop woud be the tip with only 1 vert) and make a face between the first 3 verts. But what it actually does is taking the tip vert + 1 vert from the 3rd loop and a vert from the square (which isn´t in any of my loop-lists) and makes a face from them.
Can I select a vert in 3D window and tell which index it has?

Well, yes…:

``````print me.verts.selected()
``````

Thanks for the explanation. Please, next time, post the code in text form too. That way people can just copy & paste I think this is the problem:
You’re copying vertex coordinates from the sphere to the plane. You also store MVert objects from the sphere. Then you try to use these MVert objects to create faces between the already created vertices, assuming that the already created vertices will have the same MVert object as they had in the sphere. But they don’t! Execute this on both the tip of the original sphere and the tip of the “cup” you made:

``````print me.verts[me.verts.selected()]
``````

As you can see they are different MVert objects. Newly added vertices get new indices. The only thing they have in common are their coordinates, because you told them so. This is why you can’t directly access them using the MVert objects from the sphere.

As I told from my last post, I can’t find a clean way to make faces with new vertices using the Mesh module myself. Here is a dumb version that only fills up some of faces at the bottom of the “cup”:

``````import Blender

is_editmode = Blender.Window.EditMode()
if is_editmode: Blender.Window.EditMode(0)

obS = Blender.Object.Get("Sphere")
meS = obS.getData(False, True)

dicLoops = {}
for v in meS.verts:
z_loc = round(v.co.z, 5)
# "try/except" often is faster than "is in keys()" for building dicts in BPy
try:
dicLoops[z_loc].append(v)
except KeyError:
dicLoops[z_loc] = [v]

objP = Blender.Object.Get("Plane.001")
meP = objP.getData(False, True)

init_len = len(meP.verts) # the inital number of plane vertices, in this case 4

VBelowPlane = []
for z_loc in dicLoops.keys():
if z_loc &lt; (meP.verts.co.z + objP.LocZ):
for mvert in dicLoops[z_loc]:
VBelowPlane.append(mvert.co)
meP.verts.extend(VBelowPlane)

mv_list = []
vn = 0 # vertice counter
# I simply did "range(11)", it doesn't work for other spheres
for n in range(11):
new_face = [meP.verts[init_len], meP.verts[init_len + vn +1], meP.verts[init_len + vn +2]]
mv_list.append(new_face)
vn += 1
meP.faces.extend(mv_list)

if is_editmode: Blender.Window.EditMode(1)
Blender.Redraw()
``````

It keeps track of the number of vertices it made and the initial number of vertices, then uses those numbers to create new faces.
I hope you don’t mind I changed a lot of the code. I’ll see if I can come up with something that actually works.

Then again, everything I told above might be poo because I’m new to Python myself %|