# Using python to manipulate vertices

I would like to perform the following things:

• for the selected object get all the vertices, treat the vertices as a vector, perform a Matrix x Vector multiplication
• do the same Matrix x Vector multiplication for all the objects on a scene

The purpose of this is to visualize the result of a given operation using concepts from linear algebra, but also have a quick way to manipulate the source (the 3D mesh).

So far I don't understand the reasoning behind the naming and the general design of the python API under Blender. Multiple things access the same information which is not always related to the name of the access methods used.

Sometimes the autocomplete feature CTRLSPACE gets in the way, for example

bpy.context.selected_objects[0]


with CTRLSPACE becomes

bpy.context.selected_objects[0]['cycles'].name


which doesn't seem to be a useful piece of information when there is no object named cycles. Instead what I want is something like

bpy.context.selected_objects[0].name


Can show me how to perform these tasks simply?

## 1 Answer

You can do this by iterating through all of the objects in the scene which are found at bpy.data.objects. This is all the objects in all the all the scenes. You could also use bpy.contex.scene.objects which would be the objects in the current scene. Then you would want to make sure it was a mesh object by checking its type. Then get all the vertex positions, convert them to world space, and make them tuples, if you want. In the end you get this:

import bpy
for object in bpy.data.objects:
if object.type == "MESH": #check if mesh object
verts = [object.matrix_world * vert.co for vert in object.data.vertices] #get vertex data
tup_verts = [vert.to_tuple() for vert in verts] #convert to tuples
print(tup_verts) #display list


You could add tup_verts to a larger list if you wanted, and you can leave the positions as Vectors by accessing verts instead of tup_verts

In more recent versions of blender (>=2.80) it is necessary to use the "@" operator for the element-wise matrix multiplication instead of "*". I.e. "object.matrix_world @ vert.co". See this answer: https://blender.stackexchange.com/a/129474/22220

• interesting, I need to experiment with this, but if just want the local object, without translations, scalings and rotations I'll just skip the vert.co * object.matrix_world part ? Oct 27, 2014 at 14:27
• yep @user2485710, just do verts = [vert.co for vert in object.data.vertices]. Oct 27, 2014 at 14:35