selectTrigger v01

December 23rd, 2010

Here’s a Maya script I wrote to trigger the selection of one object with the selection of another, inspired by Hamish McKenzie‘s Trigger UI, as seen in Andrew Silke‘s venerable Generi rig.

The script makes a scriptNode, which creates and tracks a scriptJob. The scriptJob checks selected objects each time something is selected, and if the control object is first in the selection list, the target objects will be selected as well. Since scriptJobs only last until the scene is closed, the scriptNode also re-creates the scriptJob when the scene is reopened.

I’d like to draw your particular attention to line 34, wherein I escape a backslash six times.

Download script here: selectTrigger_v01.mel

Code follows:

Maya MEL code

/* selectTrigger v01

http://zoomy.net/2010/12/23/selecttrigger-v01/

Select a control object followed by one or more target objects,
then run this script.

A scriptNode will be created which creates and tracks a scriptJob.
The scriptJob checks selected objects, and if the control object
is first in the selection list, the target objects will be selected
as well.

Since scriptJobs only last until the scene is closed, the scriptNode
also re-creates the scriptJob when the scene is reopened.

*/

string $sel[] = `ls -sl`;

if (size($sel) < 2) {
  print "selectTrigger: Select two or more objects.";
} else {
  int $size = `size($sel)`;

  string $node = `scriptNode -n "scriptNode"`;
  addAttr -longName selection -dt "stringArray" $node;

  string $items = stringArrayToString($sel, "\" \"");
  $items = " \""+$items+"\" ";

  string $cmd = "setAttr "+$node+".selection -type stringArray "+$size+" " + $items ;
  eval $cmd;

  $items = stringArrayToString($sel, "\\\\\\\", \\\\\\\"");
  $items = "\\\\\\\""+$items+"\\\\\\\"";

  string $procInit = "global proc reactToSelection(string $args[]) { string $a = $args[0]; string $b[]; for ($i = 1; $i < size($args); $i++) { $b[size($b)] = $args[$i]; } string $why = `undoInfo -q -undoName`; if ( `gmatch $why \"select*\"` ) { string $sel[] = `ls -sl`; int $hasA = stringArrayCount( $a, $sel ); if ( $hasA ) { $sel = stringArrayCatenate( $b, $sel ); select -r $sel; } } }";

  string $attrInit = "$cmd = \"scriptJob -kws -event \\\"SelectionChanged\\\" \\\"reactToSelection({"+ $items + "})\\\"\";int $jobNum = `eval $cmd`;setAttr(\"" + $node + ".jobID\", $jobNum);";

  string $before = $procInit + "select " + $node + ".selection;" + $attrInit + "print(\"Created node: "+$node+"\");";

  string $after = "print(\"deleted job # \" + `getAttr(\"" + $node + ".jobID\")`);scriptJob -kill `getAttr(\"" + $node + ".jobID\")`;";

  addAttr -longName "jobID" -at long $node;
  scriptNode -e -st 2 -bs $before $node;
  scriptNode -e -st 2 -as $after -ea $node;

  scriptNode -executeBefore $node;
}
« previously: Ramp rig | Home | next: Voxatron »

One Response to “selectTrigger v01”

  1. scriptTrigger, trigger selections in maya Says:

    [...] scriptTrigger is a Maya script from Peter Richardson that will trigger the selection of one object with the selection of another. It is very similar to what is seen in Andrew Silke’s long-time Generi-Rig. [...]

Leave a Reply