5
$\begingroup$

I am pretty new to Blender. I need to get coordinates of each selected vertex somehow. I don't really want to manually copy coordinates of each and single vertex that would just take a lot of time. I need them either printed somewhere or save them into some kind of text file. Again I need only those vertexes I have selected, not the entire model.

$\endgroup$

3 Answers 3

9
$\begingroup$

Open up a new Text window and copy and past the following.

import bpy
import bmesh

# Get the active mesh
obj = bpy.context.edit_object
me = obj.data


# Get a BMesh representation
bm = bmesh.from_edit_mesh(me)

bm.faces.active = None

# Modify the BMesh, can do anything here...
for v in bm.verts:
    if v.select:
        print(tuple(v.co) )

Make sure you are in Edit mode with the required vertices selected and click "Run Script"

The coordinates of each selected vertices is now printed in the System Console.

If you can not see the System Console select Toggle System Console in the Window menu.

To get this code I used the Bmesh Simple Editmode template from the Templates/Python

$\endgroup$
1
  • 1
    $\begingroup$ Would consider using instead an object MODE bmesh bm = bmesh.new() ... load with mesh bm.from_mesh(mesh) and for @JMRMEDEV bm.transform(obj.matrix_world) will convert to global coords. $\endgroup$
    – batFINGER
    Jul 11, 2021 at 15:11
3
$\begingroup$

Another possible way is to do it with vertex groups (as I do often in order to keep things in tact)

So you select the vertices you want to keep in track (in edit mode) and assign them into the group of your choice, then you change the groupName and filename (optional) variables accordingly if you want to keep track on different groups.

import bpy

groupName = "Group"

filename = "myVerts"


if (filename not in bpy.data.texts):
    myVerts = bpy.data.texts.new(filename)
else :
    myVerts = bpy.data.texts[filename]
    myVerts.clear()

myGroupsArr = [];

obj = bpy.context.selected_objects[0]

group = obj.vertex_groups[groupName]

for v in obj.data.vertices:
    for g in v.groups:
        print(g.group, group.index)
        if g.group == group.index:
            myGroupsArr.append()


for grp in myGroupsArr:
    myVerts.write( str(grp[0])+str(grp[1])+str(grp[2])+ "\n")
$\endgroup$
0
$\begingroup$

If you want the world coordinates of selected vertices multiply it with world matrix. Consider you have a Cube.... (Edited the original answer)

import bpy
import bmesh
# Get the active mesh
ob = bpy.data.objects['Cube']
obj = bpy.context.edit_object
me = obj.data

# Get a BMesh representation
bm = bmesh.from_edit_mesh(me)

bm.faces.active = None

for v in bm.verts:
if v.select:
    v =v.co
    mat = ob.matrix_world
    loc = mat @ v
    print(loc)
print('-------------')
$\endgroup$

You must log in to answer this question.

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