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

MEL How-To #89

Back · Previous · Next Maya

How can I use a single hotkey to toggle a ‘Show’ Option (or other mode)?

The typical way is to do an XOR of the current state, e.g.:

$state = ( $state || 1 ) && !( $state && 1 );

(longhand used because Maya doesn't have an XOR function)

In the simple case of an on|off state, this is done by prepending a NOT operator ('!' in MEL):

$newstate = !$oldstate;

So, to toggle the visibility of Joints in the current modelPanel, the MEL is as follows:

// Find panel with focus
$currentPanel = `getPanel -withFocus`;

// Assert that the current panel is a modelPanel
string $panelType = `getPanel -to $currentPanel`;
if ($panelType == "modelPanel")
{
  // Toggle the state of the Joint visibility
  modelEditor -e -joints ( !`modelEditor -q -joints $currentPanel` ) $currentPanel;

  // or...

  // Toggle the state of the X-Ray mode
  modelEditor -e -xray ( !`modelEditor -q -xray $currentPanel` ) $currentPanel;
}

If your state needs to toggle between 0 (zero) and a value other than one:

$newstate = !$oldstate * $setValue;
So, to toggle between 0 and 2:
int $state = 0;
int $setValue = 2;

$state = !$state * $setValue;
// Result: 2 //
$state = !$state * $setValue;
// Result: 0 //
If your state needs to toggle between two non-zero values, 3 and 5 for example:
int $state;
int $stateLoValue = 3;
int $stateOffset = 2;  // difference between lo and hi values

$state = ( !( $state - $stateLoValue ) * $stateOffset ) + $stateLoValue;
// Result: 3 //
$state = ( !( $state - $stateLoValue ) * $stateOffset ) + $stateLoValue;
// Result: 5 //

Related How-To's

Monday, May 21, 2001