# Select points down a loop with a script

I am trying to write a script that will take a selected edge loop with a point at one end set as the active vertex, and make a separate vertex group for each vertex on the loop, numbered in order. From what I can tell, there is no tool in the API to work on edge loops in this way. I could load each vertex into an array and make a vertex group from them, but they would not necessarily be in the right order.

I can do it based on location in relation to something, but this may not work on loops that are not straight. I could also use vertex index, but this won't work if the indices are not in order. Before I go ahead with using one of these flawed methods, I want to be sure that there's no way to get the order of points down a loop. Can anyone confirm, or suggest another method of handling this problem?

• normal mesh doesn't have that option ,so you should start looking at Bmesh it may have something – Chebhou Mar 25 '15 at 1:50
• I've looked over Bmesh. I don't THINK it does, but I'm not sure. I am very much a novice scripter, so I could easily be missing or misunderstanding something. – Ascalon Mar 25 '15 at 1:55

You will have to use bmesh.

A bmesh object provides a much more advanced way of interrogating a mesh than as merely a set of vert/edge/face sequences. Given only raw vert/edge/face data, consisting of indices and lists of indices, you could of course build your own model of the inter-relations between elements of the mesh, making several passes over the mesh, noting various states such as selection, deducing which vertex is 'neighbor' to which others, and so forth. Thank Joe, you don't have to do all that.

All you have to do is bm = bmesh.from_edit_mesh(mesh), and that bm will have topological-relationship information already figured out. For examples of what I mean by topological-relationship information: You are able to ask a vertex for it's link_edges, and subsequently ask one of those edges for it's other_vert(vert) given the one you already have, to find the other, and then if it's selected, that is your next group.

There is even a selection history for you: BMesh.select_history, such that bm.select_history.active will be your group zero vertex. (If you are in vertex mode.)

While I can't write your script for you – only you know the particulars – I'd hate to leave you hanging without some code example, so here's a template I had from a while ago, from when I was testing some things in bmesh. It's just a bunch of boilerplate, but it puts you at a convenient place to test bmesh things:

import bpy
import bmesh

class BMOPEX_OT_bmop(bpy.types.Operator):

bl_idname = "bmopex.bmop"
bl_label = "Bmopex:Bmop"

def execute(self,context):
ob = context.active_object
mesh = ob.data
bm = bmesh.from_edit_mesh(mesh)
print("self,context,ob,mesh,bm:",self,context,ob,mesh,bm)
return {"FINISHED"}

@classmethod
def poll(self,context):
return context.active_object and \
context.active_object.type == 'MESH' and \
context.active_object.mode == 'EDIT' and \
context.scene.tool_settings.mesh_select_mode[0]
#                                vertex mode ^