# Order of vertices added using Python script and bmesh

I am using Blender 2.71 and Python 3.4.0 and I would like to add a row of vertices in such a way that I can call them using bmesh.verts[] in the same order that I placed them (or in reverse).

I have placed them using the for loop:

for x in range (0,5):
bmesh.verts.new((x,0,0))


This results in five vertices in a row starting from the origin. However, the call order for bm.verts[] is totally muddled. For example, if I then run the following:

bmesh.verts.remove(bm.verts[0])


the one that disappears is the second from the right. I would expect it to be either the left or right-most vertex. Can anyone explain to me which method Blender uses to store these vertices?

The object contains no other vertices.

I have noticed that when adding four or less the order is sequential from right to left, but something changes at five. Is this something to do with polygons?

Thanks

BMesh only ensure vertices are added in-order when they are added to a newly created (or converted) mesh (which is OK for importers and geometry generators). Once vertices are removed you cant rely on this.

Best use a list of your own to keep a reference to vertices added if you need this.

## Sorting

if your really need to ensure some order, you can sort verts using the same method used with Python lists eg:

bm.verts.sort(key=lambda v: v.co.x)


However it would be inefficient to sort vertices every time you add once.

• Thanks. What would you suggest to be the best way to reference the vertices? – William Aug 2 '14 at 9:12
• Ah, a "list" I suppose! – William Aug 2 '14 at 9:21
• Is there a way to create the vertex without using bmesh.verts.new(position)? I first tried to set the index immediately after creating a new vertex, and also tried implementing a list, however in both cases the new vertex is lost in amongst the muddled ordering before the reference can be applied, and I have to iterate through all the created vertices to find the new one (index=-1). There is a lot of excess computation as I am creating a terrain with many, many vertices. The only other thing I can think of is to use some function of the initial position as the index. Thanks for your help! – William Aug 2 '14 at 11:02
• Added note that you can sort bmesh vertices. but really you should add new vertices to a list if you care about order. – ideasman42 Aug 2 '14 at 11:37
• I guess I will set the index/list reference by (co.y*rowsize + co.x) since it will be a rectangular grid. The reason I need to do this is to be able to use it like an topview image or heightmap for my terrain. I have some image processing experience and would like to apply the same morphology techniques to this project. – William Aug 2 '14 at 12:06