While working on another script today, I had the need to display the edge indices in the 3D view (for debugging reasons). As far as I know there is no button in Blender to display them, so I wrote a scripthandler to do this. It also has the possibility to display vertex and face indices.
# SPACEHANDLER.VIEW3D.DRAW
import bpy
import Blender
from Blender import *
vertexIndices = False
edgeIndices = True
faceIndices = False
onlySelection = True
def initialise():
if not Window.EditMode():
return False, False
scn = bpy.data.scenes.active
ob = scn.objects.active
if ob.type != 'Mesh':
return False, False
me = ob.getData(mesh = True)
return ob.matrix, me
def drawText(drawlist, colour):
viewMatrix = Window.GetPerspMatrix()
viewBuff = [viewMatrix[i][j] for i in xrange(4) for j in xrange(4)]
viewBuff = BGL.Buffer(BGL.GL_FLOAT, 16, viewBuff)
BGL.glLoadIdentity()
BGL.glMatrixMode(BGL.GL_PROJECTION)
BGL.glLoadMatrixf(viewBuff)
BGL.glColor3f(colour[0], colour[1], colour[2])
for info in drawlist:
loc, text = info
BGL.glRasterPos3f(loc[0], loc[1], loc[2])
Draw.Text(text)
def drawVerts(verts, matrix):
drawlist = []
colour = [1.0, 1.0, 1.0]
for v in verts:
drawlist.append([v.co*matrix, str(v.index)])
drawText(drawlist, colour)
def drawEdges(edges, matrix):
drawlist = []
colour = [1.0, 1.0, 0.0]
for ed in edges:
loc = ed.v1.co+((ed.v2.co-ed.v1.co)/2.0)
drawlist.append([loc*matrix, str(ed.index)])
drawText(drawlist, colour)
def drawFaces(faces, matrix):
drawlist = []
colour = [1.0, 0.0, 1.0]
for f in faces:
drawlist.append([f.cent*matrix, str(f.index)])
drawText(drawlist, colour)
def main():
matrix, me = initialise()
if me and vertexIndices:
if onlySelection:
verts = [v for v in me.verts if v.sel==1]
else:
verts = me.verts
drawVerts(verts, matrix)
if me and edgeIndices:
if onlySelection:
edges = [ed for ed in me.edges if ed.sel==1]
else:
edges = me.edges
drawEdges(edges, matrix)
if me and faceIndices:
if onlySelection:
faces = [f for f in me.faces if f.sel==1]
else:
faces = me.faces
drawFaces(faces, matrix)
main()
Since this is only of use to script writers I thought a GUI wasn’t needed, just change the options in line 7-10.
When you change your selection, the spacehandler doesn’t update what indices are shwon. Simply exit and re-enter editmode to update this. (this is mainly done to keep the spacehandler working at a good speed)
Hopefully this can save other scriptwriters some time while debugging their own scripts.