simple question. Mesh vertex

Hi all:) I have simple/stupid question.I would like to animate location Z one of vertex in mesh.
I would say that i must use a bpy.ops.mesh.select_by_number_vertices() to choose that one i need
I was trying example from blender.org.

“bpy.ops.mesh.select_by_number_vertices(number=4, type=‘EQUAL’)”
All the time I have error.
TypeError: Converting py args to operator properties: : keyword “number” unrecognized
I thought that “number” isn’t defined.
when i tried write only digit i got another error but it tell me that i need a dict,excellent.
What i made later can be completely stupid but i tried
number={}
number[“1”]=1
and i wrote
“bpy.ops.mesh.select_by_number_vertices(number[“1”], type=‘EQUAL’)”
I thught that it can work but it didn’t.
In fact i perfectly know that it can’t be working properly.
That would’t have any sense because if object has a lot of vertex it would be necessary to make a loop to make a dictionary.
But it was the last idea i had.
Back to the topic.
My question is how the hell i should make it?
It would be great if anyone could help me:)

You probably want to go a totally different direction here. You cannot really animate vertices directly like this.

If you want the mesh structure of your object to change over time then you need to make “shape keys” for your mesh. You can key these objects so you can animate them.

Another option is to add a “hook” modifier to the vertex. This ties the vertex to an external object which can be animated. The usual approach is to tie these hooks all the way to a bone in an armature. Then the “hook” is called a “bone hook” and you can use all the pose tools on the armature to manipulate the underlying mesh vertices.

For the record, here is some Python code that will select a number of vertices by their index value.


import bpy

def select_by_index(vertIndices):
  """Selects all the vertices in the active Object that are in the supplied 'vertIndices' list."""
  
  # Access the active Object and the mesh for the object
  obj = bpy.context.active_object
  mesh = obj.data
  
  # Ensure that this really is a mesh
  if type(mesh) != bpy.types.Mesh:
    return
  
  # Mark all the supplied vertices as "selected"
  for index in vertIndices:
      
    if index > 0 and index < len(mesh.vertices):
        mesh.vertices[index].select = True
          
if __name__ == "__main__":
    # Select vert 0, 2, 4, 6 from the default cube
    # vert 8 is a test case b/c the default cube does not
    # have a vert #8
    select_by_index([0, 2, 4, 6, 8])    

I have also a new question.How can i get vertex global position? Simple “location.z” doesn’t work in doc i read that there aren’t that function. Man from http://oscurart.blogspot.com/2011/05/blender-addon-oscurart-io-txt-rawmeshes.html do something with “.co” but i don’'t know exactly what? When i scale and grab object it doesn’t work too.".co" is some kind of vector depending from origin?
I have one idea how to get global location co-ordinates.
1 select vertex
2 3d cursor position to selection
3 read to variable 3d cursor location value
4 deselect vertex
A litte bit confusing but i do not have any other idea.
There are any simpler way to do this?

Depends on if you want to see that number in the UI or not. In edit mode you can get the UI to show you the global coordinates just by clicking a toggle button.


If you want to get the location of a vertex in world coordinates than you need to multiple the vertex location by the Object.matrix_world matrix. The vertex coordinates are always stored in object space.


obj = bpy.context.active_object
mesh = obj.data

for v in mesh.vertices:
  print(obj.matrix_world * v.co)

ok:) it is really useful. Thanks a lot.
Could you tell me how it is work?
It return me a Vector.I would like to have float.
“obj.matrix_world” Gives me matrix 4x4.But why? Space has got 3 dimensions. What about the last one?

“bpy.data.meshes[‘Cube’].vertices[0].co
Vector((1.0, 0.9999999403953552, -1.0))”
This gives me one vector 1*3

But when I wrote
“dir(bpy.data.meshes[‘Cube’].vertices[0].co)”
i got lots of “‘wwx’, ‘wwxw’, ‘wwxx’, ‘wwxy’, ‘wwxz’, ‘wwy’, ‘wwyw’, ‘wwyx’, ‘wwyy’, ‘wwyz’, ‘wwz’, ‘wwzw’, ‘wwzx’, ‘wwzy’, ‘wwzz’, ‘wx’, ‘wxw’, ‘wxww’, ‘wxwx’, ‘wxwy’, ‘wxwz’, ‘wxx’, ‘wxxw’, ‘wxxx’, ‘wxxy’, ‘wxxz’, ‘wxy’, ‘wxyw’, ‘wxyx’, ‘wxyy’, ‘wxyz’, ‘wxz’,”
What is most confusing there are 3 options:
4 wwzx,wwxx etc.
3 wwx,wwy etc.
2 wx etc.
1 w etc.
There are lots of possibilities…4^4+4^3+4^2+4^1…

I think that i know how vector works…I know that if I know one point co-ordinates and vector values i can get second point position.But in fact it is a much more confusing…

If you have a Vector then the Z coordinate can be referenced a couple of ways.


v = Vector((1, 2, 3, 4))
print(v.z)
print(v[2])

“obj.matrix_world” Gives me matrix 4x4.But why? Space has got 3 dimensions. What about the last one?

The short answer is that the 4x4 matrix is a clever trick that combines a 3x3 rotation or “change of basis” matrix with a translation operation. For most 3D objects you want to rotate them and move them around in space. The 4x4 matrix lets you rotate and translate with a single matrix multiply.

The long answer is that this question goes back to the basic linear algebra that 3D graphics are based on. The full answer would take pages to explain fully and covers several different topics.

Ironically, my “long” answer appears to be shorter than my “short” answer.

“bpy.data.meshes[‘Cube’].vertices[0].co
Vector((1.0, 0.9999999403953552, -1.0))”
This gives me one vector 1*3

But when I wrote
“dir(bpy.data.meshes[‘Cube’].vertices[0].co)”
i got lots of “‘wwx’, ‘wwxw’, ‘wwxx’, ‘wwxy’, ‘wwxz’, ‘wwy’, ‘wwyw’, ‘wwyx’, ‘wwyy’, ‘wwyz’, ‘wwz’, ‘wwzw’, ‘wwzx’, ‘wwzy’, ‘wwzz’, ‘wx’, ‘wxw’, ‘wxww’, ‘wxwx’, ‘wxwy’, ‘wxwz’, ‘wxx’, ‘wxxw’, ‘wxxx’, ‘wxxy’, ‘wxxz’, ‘wxy’, ‘wxyw’, ‘wxyx’, ‘wxyy’, ‘wxyz’, ‘wxz’,”
What is most confusing there are 3 options:
4 wwzx,wwxx etc.
3 wwx,wwy etc.
2 wx etc.
1 w etc.
There are lots of possibilities…4^4+4^3+4^2+4^1…

You don’t have need for that. It is common when working with vectors in 3D math to need to convert them from 3 coordinates to 4 or 4 to 3 and to swap the order of the coordinates around. Just access “Vector.z” to get the value you want.

I think that i know how vector works…I know that if I know one point co-ordinates and vector values i can get second point position.But in fact it is a much more confusing…

Just be aware that a “Vector” object in Blender might encode an actual mathematical “vector” (which represents a direction in 3D space) or it might encode a coordinate point (which represents a location).

You can translate vertices/points around by adding vectors to them but if you want to rotate them then you need to do a matrix multiply.

Thanks a lot:)