Get Center of Polygon?

I have a poly returned by a castRay,

how would I get the average of it’s Vertices, in world coordinates?

this will be used to align objects for compound assemblies

(I am so close!)

Thank you sincerely,

bpr

looks like
so far


c=0
local=mathutils.Vector((0,0,0))
while c < 4:
    if c==0:
        index=polyProxy.v1
    if c==1:
        index=polyProxy.v2
    if c==2:
        index=polyProxy.v3
    if c==3:
        index=polyProxy.v4
    mesh=polyProxy.getMesh()
    vert=mesh.getVertex(0,index)
    cords=vert.xyz
    local+=mathutils.Vector((cords[0]/4,cords[1]/4,cords[2]0)/4)
    c+=1
global=object.location+local


def getOrigin(mesh):
    poslistx = []
    poslisty = []
    poslistz = []
    count = mesh.getVertexArrayLength()
    for i in range(len):
        vert = mesh.getVertex(i)                
        poslistx.append(vert.XYZ[0])
        poslisty.append(vert.XYZ[1])
        poslistz.append(vert.XYZ[2])

    sum = mathutils.Vector([sum(poslistx),sum(poslisty),sum(poslistz)])
    return sum/count

Untested. Returns local origin.

Sent from my Nexus 5 using Tapatalk.

I need the center of 4 vertices of a poly proxy,

(polyProxy.v1.worldPosition+polyproxy.v2.worldPosition+polyProxy.v3.worldPosition+polyProxy.v4.worldPosition)

like


def Vglobal(vertex):
????


Jackii’s code is incorrect for your need, but the concept is sound and the same. Just add the position of each vertex together, and divide it by the number of vertices in the face.


x=0
y=0
z=0


x+=mesh[polyProxy.v1].XYZ[0]
y+=mesh[polyProxy.v1].XYZ[1]
z+=mesh[polyProxy.v1].XYZ[2]

x+=mesh[polyProxy.v2].XYZ[0]
y+=mesh[polyProxy.v2].XYZ[1]
z+=mesh[polyProxy.v2].XYZ[2]

x+=mesh[polyProxy.v3].XYZ[0]
y+=mesh[polyProxy.v3].XYZ[1]
z+=mesh[polyProxy.v3].XYZ[2]

x+=mesh[polyProxy.v4].XYZ[0]
y+=mesh[polyProxy.v4].XYZ[1]
z+=mesh[polyProxy.v4].XYZ[2]
local=Vector([x*.25,y*.25,z*.25])

global =???

am I close?

If you have a local position, you can get the world coordinate by:


# Assumes 'own' is the kx_gameobject that the target face belongs to
worldPos = own.worldMatrix * localPos

Perhaps you don’t mean ‘worldMatrix’ but ‘worldTransform’.


from mathutils import Vector


def get_local_center(polygon):
    sum_position = Vector()
    
    mesh = polygon.getMesh()
    material_id = polygon.material_id
    vertex_count = polygon.getNumVertex()
    
    for i in range(vertex_count):
        vertex = mesh.getVertex(material_id, i)
        sum_position += vertex.XYZ
     
    return sum_position / vertex_count


def polygon_to_world(position, obj):
    return obj.worldTransform * position


def main(cont):
    own = cont.owner
    
    mesh = own.meshes[0]
    poly = mesh.getPolygon(0)
    
    print(polygon_to_world(get_local_center(poly), own))

Thanks Agoose, this will really help with attaching compound objects in non-cube objects,(center of a 2x2x2 cube is always the same)

so now objects can have a sphere bounds, and the “jack map” tmesh can be moved along the surface :smiley:

(manipulatable joint object)