Is it possible to associate a custom tag with each vertex?

I would like to be able to number them...

I have an export script that dumps 6000 vertices to file, I then run a simulation that generates an array for each vertex.

But I would like to be able to give each vertex a unique ID as it is written to file, so that I can select a dozen vertices in blender, and export a list of IDs (and then process the array for each)

How can I do this?

LINK: http://blenderartists.org/forum/showthread.php?318031-Property-for-each-individual-face

  • $\begingroup$ Tell me if I'm wrong, but can't you use vertex groups or just the active selection? With vertex groups, you would assign vertices to be exported to groups, and export one or many groups by script. You could either enumerate the verts or use their indices, but can't see a reason to store an ID for every vert in Blender. Active selection: you would export verts if vert.select: .... $\endgroup$ – CodeManX Feb 17 '14 at 13:02
  • $\begingroup$ If it was only one object then the vertex indices would be unique. But it is several objects... $\endgroup$ – P i Feb 17 '14 at 15:29
  • $\begingroup$ Count the vertices for the objects. Create an index for the objects defining an order. For the first object the vertex indices begin with 0. For the second object you simply add the vertex count of the previous object to the indices. And so on... $\endgroup$ – pink vertex Feb 17 '14 at 19:31

It is possible to tag vertices with the bmesh module:

bm = bmesh.from_edit_mesh(obj.data)
bm = bmesh.new()

#create custom data layers
my_id = bm.verts.layers.int.new('id')

#get the custom data layer by its name
my_id = bm.verts.layers.int['id']

bm.verts[0][my_id] = 42

#apply the changes
  • $\begingroup$ IDs won't stay unique as soon as you extrude or do similar operations on the mesh. If IDs are needed for the export only, then they shouldn't be stored as custom data - it's unnecessary and quite slow. $\endgroup$ – CodeManX Feb 17 '14 at 13:32
  • $\begingroup$ It is possible to tag vertices. I just answered the first question present in the caption which may be searched by other people. He has been warned now that it may not be a good solution to his problem. $\endgroup$ – pink vertex Feb 17 '14 at 19:51
  • $\begingroup$ Thanks for this topic. And how to remove the custom property 'my_id' from all verts? $\endgroup$ – mifth Jun 2 '15 at 11:15
  • $\begingroup$ I asked new question here blender.stackexchange.com/questions/31849/… $\endgroup$ – mifth Jun 2 '15 at 14:59
  • $\begingroup$ How can I access these custom data layers again when I only have a mesh? $\endgroup$ – Ray Jun 20 '16 at 22:16

You can count vertices on export and use that instead of the vertex indices:

import bpy

c = 0

for ob in bpy.context.scene.objects:
    if ob.type == 'MESH':
        me = ob.data
        print("Mesh %s (%s):" % (me.name, ob.name))

        for i, v in enumerate(me.vertices, c):
            print("  Vert %i - %i" % (i, v.index))
        c += len(me.vertices)

And here all selected verts of all mesh objects in scene:

# Simple

def count_selected_verts(file):
    c = 0

    for ob in bpy.context.scene.objects:
        if ob.type == 'MESH':
            if ob.mode == 'EDIT':
            me = ob.data
            file.write("Mesh %s (%s):" % (me.name, ob.name))

            for v in me.vertices:
                if v.select:
                    file.write("%5i - Vert %i\n" % (c, v.index))
                    c += 1
# Pythonic - generator and list comprehension with a hacky filter

# This is infinite, don't cast to sequence!
def countup():
    i = 0
    while True:
        yield i
        i += 1

def ob_update(ob):
    # Prints all mesh objects at the top of the file :/
    #file.write("Mesh %s (%s):" % (ob.data.name, ob.name))
    if ob.mode == 'EDIT':
    return True

def count_selected_verts_gen(file):
    c = countup()
    for ob in bpy.context.scene.objects:
        if ob.type == 'MESH' and ob_update(ob):
            file.write("Mesh %s (%s):" % (me.name, ob.name))
            file.write("\n".join("%5i - Vert %i" % (c.__next__(), v.index)
                for v in ob.data.vertices if v.select)

I compared the performance: 22.0 vs. 9.4 secs (pythonic is win!).

  • $\begingroup$ But how can I select a vertex in blender and match it to the appropriate entry in the output file? $\endgroup$ – P i Feb 17 '14 at 17:48
  • $\begingroup$ What do you mean by 'match'? I posted another script to enumerate all selected verts of all meshes, is that what you need? $\endgroup$ – CodeManX Feb 18 '14 at 1:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.