一半君的总结纸

听话只听一半君

#06 How do I get the global or world-space coordinates of an object or component?

Querying the global, or world-space, coordinates of an object and component can be achieved with the same MEL command: ‘xform

Say you have one object parented to another. The child object is offset from the parent, and the parent is moved and rotated in its own local space. How do you determine the global position of the child?

// Create two objects for the purpose of this example
string $sphereA[] = `polySphere -r 1 -sx 16 -sy 8 -ax 0 1 0 -tx 1`;
string $sphere = $sphereA[0];

string $cubeA[] = ` polyCube -w 1 -h 1 -d 1 -sx 1 -sy 1 -sz 1 -ax 0 1 0 -tx 1`;
string $cube = $cubeA[0];

// Move the cube to some arbitrary location
move 1.23 3.14 5.55 $cube;

// Parent the cube to the sphere;
parent $cube $sphere;

// Move and rotate the sphere in some arbitrary fashion
move 8.22 4.87 9.99 $sphere;
rotate 12 34 45 $sphere;

// Now, where's the cube??
// Use the 'xform' command with the -worldSpace flag to -query -translation:
float $position[3] = `xform -worldSpace -query -translation $cube`;

print ( "Cube's location in world-space: { " +
   $position[0] + ", " + $position[1] + ", " + $position[2] + " }\n" );

// And to prove it, place a locator at the cube's center:
spaceLocator -p $position[0] $position[1] $position[2] -name "centerOfCube";

If you look at the coordinates of a vertex or CV in the Attribute Editor from Maya’s UI, it will often report a position of {0, 0, 0} for all points. This is because you are getting the coordinate of the component based on the local coordinate space for the component. You may think this is truly less than useful — I know I did — but there is some method behind Maya’s madness.

Try this:

Make a nurbsSphere with construction history. Select a CV. Open the Attribute Editor and expand the “CVs (click to show)” area. Note that all CVs have a location of {0, 0, 0}. They have no offset from the input of the ‘makeNurbSphere’ node.

Tweak one of the CVs by moving it. Now its local coordinates will be slightly offset, but all the others will remain at their local origin.

Now delete history for the sphere and look again. Since the CVs’ local space is not being acquired from the ‘makeNurbSphere‘ node, but rather from the object itself, the coordinate of each CV is relative to the center of the sphere. But yes, they are “local space” coordinates and not “world space.”

To get the world-space coordinates for the CV, use the ‘xform‘ command using the ‘-worldSpace’ flag:

xform -ws -q -t nurbsSphere1.cv[4][3] ;
// Result: 0.4612167 0.9553883 -0.7423176 //

The ‘−ws‘ flag instructs the command to return “world space” coordinates.

This works for polygon vertices as well:

float $coords[] = `xform -ws -q -t pSphereShape1.vtx[14]`;
// Result: 0.3535534 -0.9238795 0.1464466 //

The ‘xform‘ command is great for all sorts of simple voodoo. Check it out in the docs.

05 August 2002

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: