1
$\begingroup$

If I have selected a vertex in edit mode, how can I get its index in the on the list of vertices or its vertex location using python? For example, this is my selection.

enter image description here

WHat I am looking for is to print to console the index of that vertex (107) or its location ([0.19,1.23, -0.5]).

$\endgroup$
0
$\begingroup$

Using this question and its numerous answers as a base for an efficient code, you can use this script :

import bpy
import numpy as np

mode = bpy.context.active_object.mode
# Keep track of previous mode
bpy.ops.object.mode_set(mode='OBJECT')
# Go into object mode to update the selected vertices

obj = bpy.context.object
# Get the currently select object
sel = np.zeros(len(obj.data.vertices), dtype=np.bool)
# Create a numpy array with empty values for each vertex

obj.data.vertices.foreach_get('select', sel)
# Populate the array with True/False if the vertex is selected

for ind in np.where(sel==True)[0]:
    # Loop over each currently selected vertex
    v = obj.data.vertices[ind]
    print('Vertex {} at position {} is selected'.format(v.index, v.co))
    # If you just want the first one you can break directly here
    # break

bpy.ops.object.mode_set(mode=mode)
# Go back to the previous mode
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Thank you very much Gorgious. That is exactly what I was looking for. I managed to get to show the index with bm.verts.select but every time I run it, was showing the same index. Yours works perfectly. $\endgroup$ – Jorge Diaz Apr 8 at 10:58

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.