Take the 2-minute tour ×
Blender Stack Exchange is a question and answer site for people who use Blender to create 3D graphics, animations, or games. It's 100% free, no registration required.

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

share|improve this question
    
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: .... –  CoDEmanX Feb 17 at 13:02
    
If it was only one object then the vertex indices would be unique. But it is several objects... –  P i Feb 17 at 15:29
    
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... –  pink vertex Feb 17 at 19:31

2 Answers 2

It is possible to tag vertices with the bmesh module:

bm = bmesh.from_edit_mesh(obj.data)
#or
bm = bmesh.new()
bm.from_mesh(obj.data)

#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']

#access
bm.verts[0][my_id] = 42

#apply the changes
bmesh.update_edit_mesh(obj.data)
#or
bm.to_mesh(obj.data)
share|improve this answer
    
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. –  CoDEmanX Feb 17 at 13:32
    
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. –  pink vertex Feb 17 at 19:51

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)
        print()

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':
                ob.update_from_editmode()
            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
    file.close()
# 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':
        ob.update_from_editmode()
    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)
            )
    file.close()

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

share|improve this answer
    
But how can I select a vertex in blender and match it to the appropriate entry in the output file? –  P i Feb 17 at 17:48
    
What do you mean by 'match'? I posted another script to enumerate all selected verts of all meshes, is that what you need? –  CoDEmanX Feb 18 at 1:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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