Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

May 14, 2011

Blender Python Printing Vertex Coordinates of selected Mesh

Below you will find the necessary code to get vertex information for yourself in a few easy steps. Running Blender in debug mode has index visualizer built in to allow you to see index information in realtime.
import bpy
for item in bpy.data.objects:
    
    print(item.name)
    if item.type == 'MESH':
        for vertex in item.data.vertices:
            print(vertex.co)
or
import bpy
for item in bpy.data.objects:
    
    print(item.name)
    if item.type == 'MESH':
        vert_list = [vertex.co for vertex in item.data.vertices]
        for vert in vert_list:
            print(vert)
we can go even further.. print faces(polygons), vertices, indices, face normals
# blender 2.6x
import bpy

current_obj = bpy.context.active_object

print("="*40) # printing marker
for face in current_obj.data.polygons:
    verts_in_face = face.vertices[:]
    print("face index", face.index)
    print("normal", face.normal)
    for vert in verts_in_face:
        print("vert", vert, " vert co", current_obj.data.vertices[vert].co)
if you need world coordinates, blenderscripting -- matrixworld will be a good place to find information. Compare the following snippet with the previous one.

17 comments:

  1. thankyou for posting this, its very helpfull

    ReplyDelete
  2. Thanks mate! This was just what I have looking for.

    ReplyDelete
  3. Given a vertex, do you know how to find out its parent/owner?

    In other words, I have a list of vertices and want to multiply their location by the world matrix of the object to which each vertex belongs in order to get the absolute world coordinates. But, I don't know how to figure out to which object the vertex belongs.

    ReplyDelete
    Replies
    1. how are you getting this list of vertices for which you don't know which object they belong to -- in the first place?

      Delete
    2. sounds to me like you have a list of world coordinates and what to find out for each coordinate does it correspond with a local coordinate of a vertex in any of your scene objects.

      I won't elaborate unless you can respond to the first question.

      Delete
    3. Your first reply made me immediately realize that I will already have the object in question based on the user selection. I suppose I was thinking I needed a general method for determining the object in question, but I don't think there will be a case in which I don't already know the object ahead of time.

      Thanks for the thought provoking question. :-)

      Delete
  4. Is it possible to easily get World Position of each vertex? I look around the web, but most answers are involving Multiplication of Vector with World Matrix and using mathutils. Very confusing.

    In Maya, there is xform() command to get position of any object and vertex in 3D scene. Kind of like if we select vertex of mesh and snap the 3D Cursor and get the location of 3D cursor.

    ReplyDelete
    Replies
    1. I updated the tail of this post to include how to get world coordinates.

      Delete
  5. xform() is probably a routine that abstracts away the matrix multiplication.

    http://blenderscripting.blogspot.com/search/label/matrix_world is essentially how to do it, it might seem a little cumbersome at first but perhaps it's the idea that there is math involved is putting you off.

    In the math of the [ world_coord = ob.matrix_world * point ] the order of the multiplication is important, matrix first .. point second. matrix_world contains the row vectors for all of the transforms (scale, rotation, translate..). Multiplying the matrix by the (local) point vector performs all the transform changes on it and results in a global coordinate.

    ReplyDelete
    Replies
    1. Thanks for complete explanation Dealga. Strange for me at first that the order of operation is apparently important in this case. Another Blender Python artist told me that the order has changed from previous.


      Yes, the xform() in Maya is just another function. Super handy that is.

      Your blog really contains a lot of super useful Blender Python scripting information. This I will study for the next 1 year :)

      Delete
    2. Jimmy, yeah the blog is about 3 years of weekly and daily explorations. Plenty of material to poke through. The "search this blog" helps me find stuff when I haven't used it for a while.

      Yep, the order was changed because initially it was implemented in reverse.
      Khanacademy has excellent videos about matrix multiplication, watch and follow along with those and it starts to make sense.

      Delete
  6. Ah, but theres a big problem. What if you had a cube made up of 16 vertices? 4 for one face, and 12 on the opposite face? With these methods, you could only get either 8 vertices, or 24 vertices. How do you get the EXACT number of vertices?

    ReplyDelete
    Replies
    1. I think this code enumerates the number of vertices on a per polygon bases, notice nowhere is 3 or 4 vertices a hard-coded limit -- the script doesn't care.

      Delete
  7. i posted a link for more info on vertex data . i think blender uses its verts in counter clockwise for faces and verts are local space relative to object origon, anyway i hope this helps

    http://en.wikibooks.org/wiki/Blender_3D:_Noob_to_Pro/Advanced_Tutorials/Python_Scripting/Export_scripts

    ReplyDelete
    Replies
    1. Subtle update mistakes in that page, the accessors for faces is now data.polygons and not data.faces. So the reference on that page to "cubedata.faces" is invalid in current Blender 2.6+, it should be "cubedata.polygons"

      Delete
  8. I ran the script in the Blender text editor and nothing happened. I assume I'm doing something wrong, but it didn't even give me an error message to use to try to fix it. I'm very new to python (I mostly do everything in R), so I have no clue what I'm doing. Any help would be appreciated. I know I'm not giving you much to go on tho...

    ReplyDelete
    Replies
    1. start blender from a console, the print statements go straight to stdout. If you are on windows you can even go to Window -> Toggle system console.

      Delete

Please use Blender.StackExchange.com for python scripting questions unrelated to this post.