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 »

4 Responses 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. [...]

  2. Neil Says:

    This is a pretty cool script – thanks! The only issue I’m facing right now is that after the script is run, and I select the object to control the other object, I can’t undo the selection, which will cause major issues in the animation process. Do you know a fix around this? Thanks!

  3. meetar Says:

    Hi Neil – to be honest I’m not sure – I believe in some cases undo must be implemented explicitly by a script, and I don’t believe I did that, though it’s been a few years since I wrote it so my recall of the details is hazy. :) I don’t believe there’s any reason why it couldn’t be implemented, though – if I remember the process correctly, you’d just store the selection state before the first selection, and add that to the history queue. Good luck! – P

  4. Neil Says:

    Cool, I’ll look into it. If I find a solution I’ll share it. Haha I realize that you wrote this ages ago, so I’m surprised you replied at all. Thanks again!

Leave a Reply