2

I need to list the extremities of an edge. I've got a selected edge in edit mode and I need to list the coordinates. This will be very useful!

This because I'm looking for the rotation of an edge on the XY plane. I've already figured out how to get the angle but I need the two vertices coordinates to start the math. Hope there is a built-in method!

Thank you for your attention.

p2or
  • 15,860
  • 10
  • 83
  • 143
isar
  • 427
  • 1
  • 3
  • 12

2 Answers2

2

This will display the coordinates of the selected vertices of the selected object ( you have the local and the world coordinates as @poor have noted )

import bpy

obj = bpy.context.object
bpy.ops.object.mode_set(mode = 'OBJECT')
#all selected vertices indecies
verts_ind = [i.index for i in obj.data.vertices if i.select]
bpy.ops.object.mode_set(mode = 'EDIT')

for i in verts_ind :
    local_co = obj.data.vertices[i].co
    world_co = obj.matrix_world * local_co
    print(world_co)
Chebhou
  • 19,533
  • 51
  • 98
0

Bmesh module variant, which does not require a mode change:

import bpy
import bmesh

ob = bpy.context.object
me = ob.data
bm = bmesh.from_edit_mesh(me)

mat = ob.matrix_world

for edge in bm.edges:
    if edge.select:
        print("Edge {}\n".format(edge.index))
        for vert in edge.verts:
            print("  Vertex {}:".format(vert.index))
            print("    ", vert.co, "local")
            print("    ", mat * vert.co, "global\n")
        break

Note that above code works in edit-mode only, but it's also possible to extend it with support for object mode.

CodeManX
  • 29,298
  • 3
  • 89
  • 128