Visualizing indices in the 3D view

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.

http://sites.google.com/site/bartiuscrouch/images/show_indices.png

# 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.

nice script, useful tool for developing, instant visual feedback from time to time missing in blender
thanks for sharing.

What is edge indices? I tried the script, but can’t see anything.

Blender gives each vertex a different number (an index), which can be used to refer to that specific vertex. An edge connects two vertices, so you can simply define it by referring to the indices of two different vertices. Each edge also gets a number itself (an index again). The same goes for faces. They are made of 3 or 4 vertices and also get an index.
All indices range from zero till whatever number is needed. (so there will be both a vertex number zero and an edge number zero, but the vertex doesn’t have to be part of the edge)
The indices information isn’t very interesting for artists who use Blender, but for scriptwriters it is. For example:
Suppose you wish to move some vertices using a script. In that script you will have to tell Blender to move vertex number ## and to what location. When you’re debugging that script you can print what numbers you moved, but when you look at the 3D view you have no idea what vertices that exactly are. That’s where the above code snippet comes in handy.

Now on why nothing showed up: did you run it as a spacehandler? The script won’t do anything when you run it with Alt+P. You’ll have to load it into Blender’s text editor and then go to the 3D view. Go to the ‘View’ menu --> Space Handler Scripts --> activate the script. Then whenever you go into editmode you’ll see the indices.

Yeah, now I see it. That is so cool, that you can simply make a script and use it (visible) in Blender.

I think I’m gonna stick here a little longer and perhaps try to create my own script.

Thanks for the nice script.

Good luck with your own experiments.

Quick note: I’ve just updated the script, because it was using local coordinates instead of world coordinates. Local coordinates are fine as long as an object is located at the origin and isn’t scaled, but of course won’t work in any other cases.

Awesome script… Thanks for sharing…

Another useful script from Crouch! :RocknRoll:

Awesome script.
May I include it in one of my projects?

These scripts are very useful. I am interested in Python language and I added this script to my script collection. Thank you.

vincentweb: go ahead. You might want to add a quick line of credit, but that’s up to you.

I have pasted the code into a text window and named it crouch.
I press ALT-P and nothing happens.

I have never used spacehandlers. How do I install it?

i was looking for something like that to see indice in 3D view

and you got one

very nice and easy to use

the only thing is the size of the indices in view

anyway to make bigger be more readable on screen

also is this going to be available in 2.5

Happy blendering

To use this script:

  • copy/paste it into blender’s text editor
  • go to a window with a 3d-view
  • menu bar of the window --> View --> Space Handler Scripts --> name of the text
    (the ‘Space Handler Scripts’ option is right at the top of the menu, and isn’t always visible. Use ctrl+UP and ctrl+Down to see it)
  • the indices should now show up in editmode

The size of the index numbers can be increased by replacing line 34 with:
Draw.Text(text, ‘large’)

I have plans to port this script to blender 2.5, but the last time I looked it wasn’t possible yet (no gl methods, and no way to draw in the 3d-view).

Ok, I activated the script, but nothing happens.

I have the default cube scene.
There is a check mark by the space handler which indicates it is active.
I put the cube in edit mode and it looks the same.

My bad, I forgot to mention that scriptlinks need to be enabled. You can find this option in the script panel (buttons window, to the left of the shading button).

where is this script panle in 2.49 ?

are you talking about the opening a new panel instaed of buttons window?

can you show pic please

Thanks

Ah, got it!

It does not do much on the default cube, but I do see a single number 10 in my display now.

Alright I was thinking to add a line of credits. Is it OK if I only mention your nickname and the address of your website?

i tried with cube and it works fine

but i had some problems at the beginning

i had to start the script 2 times and then select object in 3d window

and then click on the view last selection at top
but now when i open the file it works automatically i can see all indices of selected object anytimes

i don’t even have to start it - it goes by itself when i open the file

but there seems to be another way to start this thing

hope Crouch can explain it another way

happy blendering