March 2019: ewertb.soundlinker.com is getting relocated to ewertb.mayasound.com, please update your bookmarks.

MEL How-To #52

Back · Previous · Next Maya

How do I get the names of the locators that animate a two- or three-node Camera's "View" and "Up" control if all I know is the name of the Camera?

A two-node camera creates a locator which acts as the "view." The "view" locator feeds its translation values into a "camera group" node which is actually a node of type "lookAt". This "lookAt" node provides the constrain values to control the camera's rotation attributes (‘.rotateX’, ‘.rotateY’ and ‘.rotateZ’ ).

A three-node camera also creates the "view" node and connections as above, with the addition of an "up" node. This is a simple locator node which feeds its ‘.worldMatrix’ attribute into the ‘.worldUpMatrix’ attribute of the lookAt node (which, again, serves as the "group" node for the camera).

To determine the locators that serve as the "view" and "up" controls, use the connectionInfo MEL command on the Camera to determine what is driving the rotation attributes of the camera. If the camera's rotation is driven by a "lookAt" node, trace that upstream to the locator the feeds the ‘.target’ attribute of this group node (that would be the "view") and the locator that feeds the ‘.worldUpMatrix’ (that would be the "up").

The two MEL procedures below -- ‘getCameraView()’ and ‘getCameraUp()’ -- demonstrate how this can be done:

proc string getTransform( string $shape )
{
  string $transform = $shape;

  if ( "transform" != `nodeType $shape` )
  // If given node is already a transform, just pass on through
  {
    string $parents[] = `listRelatives -fullPath -parent $shape`;
    $transform = $parents[0];
  }

  return $transform;
}

proc string rootNode( string $object )
// Description: Strips the dot-suffix of the specified string.
{
  string $buffer[];
  tokenize $object "." $buffer;
  return $buffer[0];
}

proc string getCameraView( string $cam )
{
  string $cameraView = "";

  // assert that we are querying the Camera's transform
  $cam = getTransform( $cam );

  // Is Camera's rotation constrained via a "lookAt" constraint?
  if ( `connectionInfo -id ( $cam + ".rotateX" )` &&
       nodeType( `connectionInfo -sfd ( $cam + ".rotateX" )` ) == "lookAt"
     )
  {
    string $lookAt = rootNode( `connectionInfo -sfd
      ( $cam + ".rotateX" )` );

    // What's the transform feeding the .target attribute?
    if ( `connectionInfo -id ( $lookAt +
          ".target[0].targetTranslateX" )` )
    {
      $cameraView = rootNode( `connectionInfo -sfd ( $lookAt +
        ".target[0].targetTranslateX" )` );
    }
  }

  return $cameraView;
}

proc string getCameraUp( string $cam )
{
  string $cameraUp = "";

  // assert that we are querying the Camera's transform
  $cam = getTransform( $cam );

  // Is Camera's rotation constrained via a "lookAt" constraint?
  if ( `connectionInfo -id ( $cam + ".rotateX" )` &&
       nodeType( `connectionInfo -sfd ( $cam + ".rotateX" )` ) == "lookAt"
     )
  {
    string $lookAt =
      rootNode( `connectionInfo -sfd ( $cam + ".rotateX" )` );

    // What's the transform feeding the .worldUpMatrix attribute?
    if ( `connectionInfo -id ( $lookAt + ".worldUpMatrix" )` )
    {
      $cameraUp = rootNode( `connectionInfo -sfd ( $lookAt +
        ".worldUpMatrix" )` );
    }
  }

  return $cameraUp;
}

Wednesday, December 13, 2000