Articles

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:

import pymel.core as p
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:

cubePosition = p.datatypes.Point(cubePosition)
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:

  1. Bring up the Maya Node Editor and load-up pCube1.
  2. Tap Tab on your keyboard, start typing pointMatrixMult and select it from the drop-down list that appears.
  3. Shift-select pCube1 first and the generated pointMatrix1 second and bring up the Connection Editor.
  4. Connect parentMatrix on the left into inMatrix on the right.
  5. Connect translate on the leftinto inPoint on 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.

You can also use a 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.
TutorialsKimon Matara