Here’s a simple code for getting the center of gravity or average center of several vectors.   This comes in handy when you are trying to find a centroid of a cluster of vertexes of polygons, control vertices of nurbs geometry, and/or map points of subdivision surfaces.

This acts as a fast, cheap way of getting the actual center of a worldspace collection of point vectors, instead of relying on the “.center” or “.translate” attribute.  Often, due to vertex manipulation or pivot point placement, “.center” and “.translate” may not accurately store the centroid of the object.  Also, those attributes only deal with a singular object, whereas a collection of vectors could return a centroid based on several objects, or more interestingly, several objects over time.

.

Code:
global proc vector ReturnNVectorCOG(vector $AllVecs[]) {
//Author Nick Pisca 0001d 2010
//$AllVecs[0]=<<0,0,0>>;$AllVecs[1]=<<1,-1,0>>;$AllVecs[2]=<<1,0,0>>;$AllVecs[3]=<<0,0,2>>;
vector $TempVec = <<0,0,0>>;
if (size($AllVecs) != 0 ) {
for ($i=0;$i<size($AllVecs);$i++) {
$TempVec = $TempVec + $AllVecs[$i];
}
$TempVec = $TempVec / size($AllVecs);
}
return $TempVec;
}

.

.

Nick Pisca MEL Multiple Topology Tests 2010 Copyright 0001d Pisca

Nick Pisca MEL Multiple Topology Tests 2010 Copyright 0001d Pisca

This procedure is designed to handle a n-length array of vectors.  One place to make some enhancements to this code would be to handle a zero or one element array better.  I know my current use of this function is pretty limited and will always have more than three elements, but in the future, it should be expanded.  If your codes require this type of possible functionality, consider revising it to accommodate for such small arrays.   🙂

Here is the link to the function on my 0001d BLAST wiki:  http://nickpisca.com/BLAST/index.php?title=ReturnNVectorCOG

See also:  http://nickpisca.com/BLAST/index.php?title=ReturnCOG

Code:
global proc vector ReturnCOG(vector $V1, vector $V2, vector $V3) {
//Author Nick Pisca 0001d 2010
//$V1 = <<0,0,0>>; $V2 = <<1,-1,0>>; $V3 = <<1,0,0>>
vector $TempVec = ($V1 + $V2 + $V3)/3;
return $TempVec;
}