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

July 11, 2011

getting index of currently selected vertex ( or vertices)

this tells you the index of the currently selected vertex (or if more than one, then it returns the list of selected vertices)
# you must be in 'OBJECT' mode to run these.
>>> [i.index for i in bpy.context.active_object.data.vertices if i.select == True]
[5]
# or slightly shorter, when i.select returns False the element is discarded,
# but when i.select returns True, the if statement compares True with True. (overkill)
>>> [i.index for i in bpy.context.active_object.data.vertices if i.select]
[5]
# or use shorthand to access active_object (more pythonic ):
>>> current_object = bpy.context.active_object
>>> [i.index for i in current_object.data.vertices if i.select]
[5]

# to get a coordinate: (will return all selected vertices if more than one selected)
>>> [i.co for i in bpy.context.active_object.data.vertices if i.select]
[Vector((2.4389050006866455, -3.9095726013183594, 0.9682117700576782))]
Here is a similar snippet that works with bmesh and updates without requiring to enter/exit edit mode.

What about Faces?

Please see this post, it shows two ways.

6 comments:

  1. Thanks to you again. This blog helped me a lot so many times with simple scripts. Thanks again...

    ReplyDelete
  2. Hi there, is their a way to select meshes in a object with the same amount of vertices? So i have 5 meshes which are scattered in the object these 5 meshes all have different number of vertices. When i select one mesh with ie 54 verts, i would like to be able to select simluar meshes by vertices number

    ReplyDelete
    Replies
    1. This is possible. In Graph Theory this is called finding disjoint subgraphs in a supergraph. Meshes are just 3d Graphs, mostly. It's not a problem i've had to write a script for so cannot offer an instant solution to you. Ask on BlenderStackExchange.com (free, community driven Blender help site) -- You'll probably get several anwsers.

      The algorithm will involve something like:
      - find all subgraphs of this graph, and keep a list of their vertex counts
      - count how many verts are in the currently selected subgraph
      - if there are other subgraphs with this vertex count, select them.

      Delete
  3. Thank you! I had to find the reference numbers of twelve points out of over a thousand, and this script was perfect.

    ReplyDelete
  4. This is nice, but how to find all polygons which are selected? I did not find any functions for that so far...

    ReplyDelete
    Replies
    1. please see this post: http://blenderscripting.blogspot.nl/2016/04/getting-index-indices-of-selected-faces.html

      Delete

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