Face Center world_matrix not working?

Hi All,

I am trying to get coordinates of the center of a face in world space. I thought all you had to do was multiply by the object world_matrix, but I get the same return value no matter where my plane is located in the scene.


import bpy, random, bmesh
from mathutils import Vector

def returnPolygonCenter(ob,polygon_index):
    mx = ob.matrix_world
    tmp = [ob.data.vertices[el].co*mx for el in ob.data.polygons[polygon_index].vertices]
    v0 = Vector()
    for el in tmp:
        v0 += el
    nr_verts = len(tmp)
    return v0/nr_verts

ob_source = bpy.data.objects.get("Plane")

for i,p in enumerate(ob_source.data.polygons):
    face_center = returnPolygonCenter(ob_source,i)
    print(face_center)

Whats the trick to getting the real world coordinates?

Hi Atom


tmp = [mx * ob.data.vertices[el].co for el in ob.data.polygons[polygon_index].vertices]

Thanks batFinger, that works, but doesn’t that go against all we have been taught in math class?

5 * x is the same as x * 5 right?

So why is matrix * coordinate not the same as coordinate * matrix?

I’m not matrix math expert, but I think matrix * vector in NOT the same as vector * matrix.

One hopes not.