一半君的总结纸

听话只听一半君

#89 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

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: