So you want to know the center point of a polygon, but you can't find an attribute that stores it...Nor can you find a MEL command that will give you this information.
Which all means we're going to have to write our own script to calculate the center point of a polygon.
First up, let's decide what we mean when we say the 'center' of a polygon. You (might) remember from school plenty of equations for regular polygons, and there's loads to be said about triangles...
But I could recall nothing about finding the center of an irregular polygon, so I was somewhat reassured when I read this:
"Irregular polygons are not considered as having a center, since there is unlikely to be any one point equally distant form each vertex. However, an irregular polygon can have a centroid, or center of gravity."
www.mathopenref.com/polygoncenter.html
I checked the equation of a polygon centroid, and then decided it was overkill. What's more, the centroid is not what determines the position of that little blue dot....
1) Find the average position of the vertices:
Here's the simplest and crudest way to find the 'center' of a polygon: add the vertex positions together and then divide by the number of points. For many polygons, this will give you a point somewhere near the center.....
// this script assumes that the first item
// on the selection list is a polyface.
// you need a locator called locator1
// for the last line to work
string $poly[] = `ls -sl`;
if($poly[0] != "")
{
string $verts[] = `polyInfo -fv $poly[0]`;
string $vert[] = stringToStringArray($verts[0],
"FACE :");
string $obj[] = `ls -sl -objectsOnly`;
int $numVerts = (`size ($vert)`-2);
float $px=0;
float $py=0;
float $pz=0;
for ($i=1; $i< ($numVerts+1); $i++)
{
string $vertID = ($obj[0]+".vtx["+$vert[$i]+"]");
float $pos[] = `xform -q -ws -t $vertID`;
$px += $pos[0];
$py += $pos[1];
$pz += $pos[2];
}
vector $center = <<($px/$numVerts), ($py/$numVerts),
($pz/$numVerts)>>;
print ("center point of "+$poly[0]+ " is: "+ $center);
xform -os -t ($center.x) ($center.y) ($center.z)
locator1;
}
But the problem with this method becomes apparent when you create a face with vertices clustered at one end: the locator is clearly off-center.
2) Weighted average:
This method uses a weighted average, based on edge length. The return value of this script will give you the location of Maya's little blue polyface selection handle.
This Python script returns an array, which contains the face ID followed by the XYZ positions of the point.
- download the file: getFaceCenters.py
- place it in your user scripts directory
- edit userSetup.mel to include the following:
- python "import getFaceCenters";
This script will iterate through a selection of faces:
In the MEL command line, type:
string $fc[] = `python "getFaceCenters.faceCenter()"`;
Number of objects in selection: 1
// Result: The center point of face 0 is:
1.06829440594 0.0 1.21612894535 //
float $x = $fc[1];
setAttr locator1.tx $x;
float $y = $fc[2];
setAttr locator1.ty $y;
float $z = $fc[3];
setAttr locator1.tz $z;
Of course, you could always change the data type of the return value to floats; that would make things even easier...
Enjoy,
Owen



Subscribe
I got a problem when i used it in a loop Function with mel..
my case is simple:
string $faceSl[] = `ls -sl -fl`;
for($i=0; $i<`size $$faceSl`; $i++)
{
string $fc[] = `python "getFaceCenters.faceCenter()"`;
//print $fc;
spaceLocator -p $fc[1] $fc[2] $fc[3];
}
then i've selected 2 faces but it didn't work for multiply faces.. it's okay for one face, but it's not for more than that..
what should i do?
note: i had to rename the downloaded file "getfacefenters-1.py" to "getFaceCenters"
to make it work.
thnx,..
bests,
Posted by: chavey naiara | 08/02/2012 at 10:32 PM
I tried to use it and it didn't work very well for me,I ran a few checks to see if I wrote something wrong and then realized your script is perfect, The mistake was mine! :)
Posted by: software test consultant | 29/10/2010 at 06:45 PM
Thank you for sharing this, it's pretty interesting to know about it. I was looking for it a few days back and fortunately I found it.
Posted by: Generic Viagra | 01/10/2010 at 11:04 PM
Hmm... the manipMoveContext query seems to return the center of the face's bounding box, so that only works for square polygons.
Posted by: Peter Richardson | 30/08/2010 at 02:51 AM
a quick and dirty alternative to this that is much faster and shorter it to simply query the position of the move Manipulator:
setToolTo moveSuperContext;
select object.face[0];
vector $pos = `manipMoveContext -q -p Move`;
Posted by: Naughty Nathan | 04/05/2010 at 10:21 PM
cool, so how does this differ from display>polygon>face center? or center pivot?
Posted by: confused | 17/11/2009 at 09:41 PM