# [Bf-committers] Cursor to center of mass

CoDEmanX codemanx at gmx.de
Wed Nov 21 14:38:21 CET 2012

```It's pretty easy to handle:

http://www.pasteall.org/37405/python

Am 21.11.2012 04:55, schrieb Brecht Van Lommel:
> Tessfaces includes both triangles and quads, it doesn't triangulate
> entirely, so you'd need to handle the quads still.
>
> Brecht.
>
> On Wed, Nov 21, 2012 at 4:30 AM, Ummi Nom <umminom at gmail.com> wrote:
>> Hi!
>>
>> 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
>> http://www.pasteall.org/37396/python
>>
>> # 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:
>>      me.update(calc_tessface=True)
>>
>>      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
>>
>>      print(tess_count)
>>
>>      print(sumc/summ)
>> _______________________________________________
>> Bf-committers mailing list
>> Bf-committers at blender.org
>> http://lists.blender.org/mailman/listinfo/bf-committers
> _______________________________________________
> Bf-committers mailing list
> Bf-committers at blender.org
> http://lists.blender.org/mailman/listinfo/bf-committers
>
```