View Full Version : WorldSpace coord of a poly face?

05 May 2006, 03:50 PM
How do I get the world space coordinate of a selected polygon face?

I've tried polyInfo and polyEvaluate, both of which seem to return sometimes working results? Its very odd.. If I create a default polySphere, don't touch it/move it/scale it .. then query a face, and place a locator at the return value coords, its fine..

If I move/scale or anything the sphere, all bets are off.

If I create anything besides a sphere (cube/cylinder) .. etc .. Does not work .. I'm using the 'faceNormal' flag for polyInfo .. but I now realize its not exactly returning the worldspace position of the face ...



05 May 2006, 04:24 PM
The face normal is returned in object space co-ords, hence when you first create the object the returned result is fine as the object space and the world space are co-incident. How ever when you move your object it will become apparent as you have found that the wrong co-ords are returned as the two `spaces` are not in the same place anymore.

You can either do a matrix multiplication of the face normal with the inverse matrix of the object I believe, but if you want the position of the face you could always get it`s verts, query the world space positions of those with xform or pointPosition and average the results to get your poly location.

Hope thats useful.

05 May 2006, 04:58 PM
Ahh.. Nevermind .. I figured it out .. Thank you for the quick reply though ..

I had to get the worldSpace transform of the face.. Which returns the actual XYZ coords of each vertex making up that face ..

parsed those coords and returned the average of the X Y Z coords of each vertex ... and gave me the center of the face .. works like a charm :)


05 May 2006, 07:13 PM
parsed those coords and returned the average of the X Y Z coords of each vertex ... and gave me the center of the face

I would like know how you parsed these information? is a command in mel can actually do that or you have to do a some sort of cross product to get what you want?

05 May 2006, 08:15 PM
Well ..

I know that no matter what, I'm going to get at least 3 sets of XYZ coordinates .. because to make up a polyFace .. you have to have at least 3 verts to build a triangle.

Knowing that I'll always have a minimum of XYZ XYZ XYZ .. I used the Modulo operator to pick out every 3rd X coordinate and put them in a new array ... While putting the leftovers (the Y and Z) into a junk array ..

I then looped through my new Junk Array that holds all of my (YZ) .. doing another modulo operator on EVERY OTHER item in the array .. if the Modulo = 0 i knew it was my Y coordinates .. and if it gave me 1 I knew that would be my Z coords .. so I thin threw each new coord into THEIR own array ..

After I now had an X array, Y array and Z array .. I averaged all of those out .. and returned those coordinates ..

Here is the script I wrote, mind you .. This is probably not the most efficient and quickest way, but .. it works.

Usage would be .... centerOfFace pSphere1 10

pSphere1 is my mesh object and 10 would be the face number .. ie pSphere1.f[10]

// Written by Seth Hall

global proc float[] centerOfFace(string $polyObject, int $faceNumber) {
float $pos[] = `xform -q -ws -t ($polyObject + ".f[" + $faceNumber + "]")`;
int $posSize = size($pos);
int $tempSize = 0;
int $amount = $posSize / 3;
float $temporary[];

float $X[];
float $Y[];
float $Z[];

float $avgX;
float $avgY;
float $avgZ;

int $tempCount = 0;
int $xCount = 0;
int $yCount = 0;
int $zCount = 0;

// get X
int $i = 0;
for ($i = 0; $i < $posSize; $i++) {

if ($i % 3 == $i / $posSize) {
$X[$xCount] = $pos[$i];
} else {
$temporary[$tempCount] = $pos[$i];

$tempSize = size($temporary);

// get Y & Z
int $j = 0;
for ($j = 0; $j < size($temporary); $j++) {
if ($j % 2 == 0) {
$Y[$yCount] = $temporary[$j];
} else {
$Z[$zCount] = $temporary[$j];

clear $temporary;
clear $pos;

// do our averaging
float $averageX = 0;
float $averageY = 0;
float $averageZ = 0;

float $finalAverage[];

int $x = 0;
for ($x = 0; $x < size($X); $x++) {
$averageX += $X[$x];
$averageY += $Y[$x];
$averageZ += $Z[$x];

$finalAverage[0] = $averageX / size($X);
$finalAverage[1] = $averageY / size($Y);
$finalAverage[2] = $averageZ / size($Z);

clear $X; clear $Y; clear $Z;

return $finalAverage;

Now, run this, create a locator .. and plugin the return coordinates to your translate X Y Z .. your locator should snap to the center of that face :)


CGTalk Moderation
05 May 2006, 08:15 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.