Every DAG object in Maya knows about its own position in world space. This information is not immediately visible through either the Attribute Editor or the Channel Box, but this quick trick will show how to access the world space transformations of a DAG object, and connect the data to keyable atributes on your object.
For the sake of this example, put a polyCube in a Group then another, and another, and another....Modify the values of the transformations of every node in the hierarchy - just so the translation attribute of pCube doesn't reflect its position in world space.
-
On the pCube1 node (transform), add an attribute of type Vector, called WS
We'll use this attribute to 'store' the position of pCube1 in world space.
-
Now, open the Plugin Manager window and make sure you have the Decompose Matrix plugin loaded.
-
In the MEL command line type
createNode decomposeMatrix;
It is this node that will parse the information in pCube1.worldMatrix and give us the values of the object's translation from the Origin.
-
Open the Connection Editor and in the left-hand side, load the pCube1 object. In the right-hand side, load the decomposeMatrix node.
-
Connect pCube1.worldMatrix > decomposeMatrix1.inputMatrix
-
Now reverse the Connection Editor so you can connect
decomposeMatrix.outputTranslate > pCube1.WS
It's as simple as that.
Of course, you can use an expression instead of creating the decomposeMatrix node. Once you have added your extra attribute WS to pCube1, you write an expression similar to this:
float $WSpos[] = `xform -q -ws -t pCube1`;
pCube1.WSX = $WSpos[0];
pCube1.WSY = $WSpos[1];
pCube1.WSZ = $WSpos[2];
..and this works equally well.
However there is a very important point to consider: what happens if you want to duplicate pCube1?
Use the first method - connect a decomposeMatrix node to your object - and you can simply Duplicate with the option Duplicate input graph, and each duplicate object has its own decomposeMatrix node and all the connections to return its world space position.
If, however, you choose to use the expression, you will get unexpected results: the attribute WS on each duplicate object refers back to the original pCube !
This is because the first line of your Expression is somewhat ambiguous. Open the Expression connected to your duplicate object and it will read (note the first line):
float $WSpos[] = `xform -q -ws -t pCube1`;
pCube2.WSX = $WSpos[0];
pCube2.WSY = $WSpos[1];
pCube2.WSZ = $WSpos[2];
Yes, you could modify each Expression so that it identifies the correct DAG node, but I would opt for the simple solution and use a decomposeMatrix node instead.
Owen
Thanks for explaining this; it will be very useful.
-- David
Posted by: djx | 06/11/2009 at 03:00 AM