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

MEL How-To #98

Back · Previous · Next Maya

How do I determine if an object is visible?

A "cheap" test for visibility would be to simply test a node's '.visibility' attribute; however, this would not be entirely accurate. There are other factors to consider:

  • An object's visibility is determined not only by its own '.visibility' attribute, but also by all of its parents' '.visibility' attributes. If a parent node is hidden, the child node is hidden as well.

  • If the node is a mesh node, and if its '.intermediateObject' attribute is TRUE, then it is not drawn nor rendered.

  • If the object is a member of a displayLayer, and if that displayLayer is hidden, then the object is not visible.

The following MEL procedure shows a method for querying a node's visibility:

proc int nodeIsVisible( string $node )
{
  // If user is asking about a bogus node, return FALSE.
  if ( !`objExists $node` ) return false;

  // Object must be a DAG node, or it's not visible.
  // There's no MEL query to identify a DAG node, but the kDagNode class adds
  // the '.visibility' attribute, so we'll use its existence as a cue.
  if ( !`attributeQuery -node $node -exists "visibility"` ) return false;

  // The obvious: Start with the '.visibility' attribute on the node.
  int $visible = `getAttr ( $node + ".visibility" )`;

  // If this is an intermediate mesh, it's not visible.
  if ( `attributeQuery -node $node -exists "intermediateObject"` )
  {
    $visible = $visible && !`getAttr ( $node + ".intermediateObject" )`;
  }

  // If the object is in a displayLayer, and the displayLayer is hidden,
  // then the object is hidden.
  if ( `attributeQuery -node $node -exists "overrideEnabled"` &&
       `getAttr ( $node + ".overrideEnabled" )` )
  {
    $visible = $visible && `getAttr ( $node + ".overrideVisibility" )`;
  }

  // Ascend the hierarchy and check all of the parent nodes.
  if ( $visible )
  {
    string $parents[] = `listRelatives -parent $node`;
    if ( `size $parents` > 0 )
    {
      $visible = $visible && nodeIsVisible( $parents[0] );
    }
  }

  return $visible;
}

11 Apr 2004