6
$\begingroup$

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

$\endgroup$
3
  • $\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, 2014 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, 2014 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$ Feb 17, 2014 at 19:31

2 Answers 2

6
$\begingroup$

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)
$\endgroup$
5
  • $\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, 2014 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$ Feb 17, 2014 at 19:51
  • $\begingroup$ Thanks for this topic. And how to remove the custom property 'my_id' from all verts? $\endgroup$
    – mifth
    Jun 2, 2015 at 11:15
  • $\begingroup$ I asked new question here blender.stackexchange.com/questions/31849/… $\endgroup$
    – mifth
    Jun 2, 2015 at 14:59
  • $\begingroup$ How can I access these custom data layers again when I only have a mesh? $\endgroup$
    – Ray
    Jun 20, 2016 at 22:16
2
$\begingroup$

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!).

$\endgroup$
2
  • $\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, 2014 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, 2014 at 1:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .