Get World Positions Using Scripts or Nodes
In this article, ‘object’ will be shorthand for a Maya transform. This is any node with translate/rotate/scale attributes, and includes most things you can select and manipulate in the Maya viewports while in Object Mode. I won’t be discussing components, for example mesh vertices.
Script Method
Use the standard xform() command, which returns a list of three float values:
cubePosition = p.xform("pCube1",q=True,t=True,ws=True)
Converting this into a PyMEL Point instance yields useful object-oriented functionality. In the following example I’m using the distanceTo() method to get the cube’s distance from the origin:
distanceFromOrigin = cubePosition.distanceTo([0,0,0])
Use help() to discover more methods on Point instances.
Node Method #1
Maya locators track their position via a .worldPosition attribute. This sits on the locator shape node, but in most cases Maya dot syntax will allow you to access it via the transform too.
Therefore you can track any object by parenting a locator under it and reading, or connecting, this attribute. An advantage of this method is that you can introduce an offset by moving the locator within its parent space.
Node Method #2
If you don’t want to use locators, you can get a world position output using Maya utility nodes.
Here’s how to get the world position of an object called pCube1:
- Bring up the Maya Node Editor and load-up
pCube1. - Tap
Tabon your keyboard, start typingpointMatrixMultand select it from the drop-down list that appears. - Shift-select
pCube1first and the generatedpointMatrix1second and bring up the Connection Editor. - Connect
parentMatrixon the left intoinMatrixon the right. - Connect
translateon the leftintoinPointon the right.
Figure 1.
You should now be looking at something like Figure 1. From here you can read or connect the world position from the pointMatrix node’s output attribute.
vectorProduct node instead of a pointMatrixMult. Set its operation to Point Matrix Product and connect the cube’s translate and parentMatrix attributes into input1 and matrix on the node, respectively.