[Bf-committers] Cursor to center of mass

Ummi Nom umminom at gmail.com
Wed Nov 21 04:30:33 CET 2012


I've written some center of mass calculations in python for closed meshes,
but there is an issue with using tessfaces in my code; I run mesh.update
with tessfaces=True but when I loop over mesh.tessfaces there are only 6
faces in default scene cube so the center of mass is offset.

It seems to work when the mesh is triangulated

# Code
import mathutils
from mathutils import *
import bpy

tess_count = 0

# Handle tri as a tetrahedron with fourth point at origo
def handleTri(v1, v2, v3):
    global tess_count
    tess_count = tess_count + 1

    temp = (v3-v1).cross(v2-v1)
    nor = temp.normalized()
    area = 0.5*temp.dot(nor)
    vol = area*nor.dot(v1)/3.0
    centroid = (v1+v2+v3)/4.0

    return (centroid, vol)

for me in bpy.data.meshes:

    sumc= Vector()
    summ = 0

    for f in me.tessfaces:
        v1 = Vector(me.vertices[f.vertices[0]].co)
        v2 = Vector(me.vertices[f.vertices[1]].co)
        v3 = Vector(me.vertices[f.vertices[2]].co)

        centroid, mass = handleTri(v1, v2, v3)
        sumc += centroid * mass
        summ += mass



More information about the Bf-committers mailing list