0

I am looking to retrieve al the starting and ending vertices per edge of an object (standard box in this case). For this I have created the following code:

import bpy
import bmesh
import numpy as np

get a reference to the active object

mesh_obj = bpy.context.active_object

create a new bmesh object

bm = bmesh.new()

initialize the bmesh object using the mesh data

bm.from_mesh(mesh_obj.data)

#Extract all vertex coords and package for export vertices = np.empty((0,3),int) for v in bm.verts: vertex = np.array([v.co.x, v.co.y, v.co.z]) vertices = np.append(vertices,[vertex], axis=0)

edges = np.empty((0,2),int) for e in bm.edges: edge = np.array(e.verts) edges = np.append(edges,[edge], axis=0)

print(vertices) print(edges)

bm.free

The result however is:

[[ 1.  1.  1.]
 [ 1.  1. -1.]
 [ 1. -1.  1.]
 [ 1. -1. -1.]
 [-1.  1.  1.]
 [-1.  1. -1.]
 [-1. -1.  1.]
 [-1. -1. -1.]]
[[<BMVert(0x0000020D001D3D68), index=5>
  <BMVert(0x0000020D001D3DD8), index=7>]
 [<BMVert(0x0000020D001D3C88), index=1>
  <BMVert(0x0000020D001D3D68), index=5>]
 [<BMVert(0x0000020D001D3C50), index=0>
  <BMVert(0x0000020D001D3C88), index=1>]
 [<BMVert(0x0000020D001D3DD8), index=7>
  <BMVert(0x0000020D001D3DA0), index=6>]
 [<BMVert(0x0000020D001D3CC0), index=2>
  <BMVert(0x0000020D001D3CF8), index=3>]
 [<BMVert(0x0000020D001D3D30), index=4>
  <BMVert(0x0000020D001D3D68), index=5>]
 [<BMVert(0x0000020D001D3CC0), index=2>
  <BMVert(0x0000020D001D3DA0), index=6>]
 [<BMVert(0x0000020D001D3C50), index=0>
  <BMVert(0x0000020D001D3CC0), index=2>]
 [<BMVert(0x0000020D001D3DD8), index=7>
  <BMVert(0x0000020D001D3CF8), index=3>]
 [<BMVert(0x0000020D001D3DA0), index=6>
  <BMVert(0x0000020D001D3D30), index=4>]
 [<BMVert(0x0000020D001D3D30), index=4>
  <BMVert(0x0000020D001D3C50), index=0>]
 [<BMVert(0x0000020D001D3CF8), index=3>
  <BMVert(0x0000020D001D3C88), index=1>]]

The results of the coordinates of the vertices are fine but I just don't see how te get the indices of the vertices belonging to an edge. Why are the results of the indices not just the index number but {BMVert(@@@@) ? How can I obtain just the index numbers?

Duarte Farrajota Ramos
  • 59,425
  • 39
  • 130
  • 187
Peter_Ko
  • 13
  • 1

1 Answers1

1

For edges try this instead :


import bpy

edges = bpy.context.selected_objects[0].data.edges

verts_pairs = [list(e.vertices) for e in edges]

for pair in verts_pairs:

print(pair)


For a box in this case output looks like :

[2, 0]
[0, 1]
[1, 3]
[3, 2]
[6, 2]
[3, 7]
[7, 6]
[4, 6]
[7, 5]
[5, 4]
[0, 4]
[5, 1]
Yaroslav
  • 421
  • 2
  • 10