//////////////////////////////////////////////////////////////////////////////////////// // // Name: findAndReplace.mel // // Description: Finds objects with one name root and replaces them with // another object maintaining translation, rotation and scale. // // Created on: 12/17/2001 // Modified on: 02/26/2006 // // Created by: Rob O'Neill (rob@morphometric.com) // // Usage: Place findAndReplace.mel in the scripts directory. // Type findAndReplace at the command line or script editor in Maya. // Make a shelf button for easy access. // // As expected, this command works best on objects with transforms *not* frozen // // Use and modify at your own risk. // Comments and suggestions always welcome. // //////////////////////////////////////////////////////////////////////////////////////// global proc findAndReplace() { string $findAndReplaceVersion = "Find and Replace: Version 2.0"; int $width = 500; int $height = 200; if (`window -exists findAndReplace_ui` == true) deleteUI findAndReplace_ui; window -maximizeButton false -resizeToFitChildren true -width $width -height $height -sizeable true -title $findAndReplaceVersion -iconName "Find and Replace" findAndReplace_ui; columnLayout -adjustableColumn 1 -columnAttach "both" 0 -rowSpacing 0 -columnWidth $width -columnAlign "left" mainColumnLayout; setParent findAndReplace_ui|mainColumnLayout; string $oldObjField = `textFieldButtonGrp -label "Replace these objects:" -text "Use wildcards (myObject*)" -buttonLabel "Select" -buttonCommand farGrabSelectedReplace objectsToBeReplacedField`; string $replacerObjField = `textFieldButtonGrp -label "With this:" -text "Enter the replacer object" -buttonLabel "Select" -buttonCommand farGrabSelected objectToReplaceField`; separator; rowLayout -numberOfColumns 5; text "Attributes to Copy:"; checkBox -label "Translation" -value 1 far_translationCheckBox; checkBox -label "Rotation" -value 1 far_rotationCheckBox; checkBox -label "Scale" -value 1 far_scaleCheckBox; checkBox -label "Group Results" -value 0 far_groupCheckBox; setParent findAndReplace_ui|mainColumnLayout; columnLayout -adjustableColumn 1 -columnAttach "both" 0 -rowSpacing 0 -columnWidth 150 -columnAlign "center" variableColumnLayout; separator; radioButtonGrp -numberOfRadioButtons 2 -label "Creation Type:" -labelArray2 "Duplicate" "Instance" -select 1 replaceMethodGroup; separator; radioButtonGrp -numberOfRadioButtons 3 -label "Originals:" -labelArray3 "Keep" "Delete" "Hide" -columnWidth3 50 50 50 -columnAlign3 "left" "left" "left" -select 1 originalDisposition; separator; button -label "Find and Replace" -align "center" -command "doFindReplace"; button -label "Undo" -align "center" -command "undo"; separator; text -label ($findAndReplaceVersion + " - Rob O'Neill (rob@morphometric.com)"); setParent findAndReplace_ui|mainColumnLayout; window -e -width $width -height $height findAndReplace_ui; showWindow findAndReplace_ui; } global proc farGrabSelected() { string $selected[] = `ls -sl`; textFieldButtonGrp -e -text $selected[0] objectToReplaceField; } global proc farGrabSelectedReplace() { string $selected[] = `ls -sl`; // place holder to do something smart with the selected items textFieldButtonGrp -e -text $selected[0] objectsToBeReplacedField; } ///////////////////////Main Work Loop/////////////////////////////////////////// // Takes the string from the GUI, throws those objects into an array gets each // objects attributes, copies the new object (in some manner) and gives that // the translation/rotation/scale attributes of the original object. // global proc doFindReplace() { // query the UI string $oldObj = `textFieldGrp -q -text objectsToBeReplacedField`; string $replacerObj = `textFieldButtonGrp -q -text objectToReplaceField`; int $transSelection = `checkBox -q -value far_translationCheckBox`; int $rotSelection = `checkBox -q -value far_rotationCheckBox`; int $scaleSelection = `checkBox -q -value far_scaleCheckBox`; int $farGroupVal = `checkBox -q -value far_groupCheckBox`; int $farReplaceMeth = `radioButtonGrp -q -select replaceMethodGroup`; int $farObjDisp = `radioButtonGrp -q -select originalDisposition`; // the temp positions are held in these float $transX, $transY, $transZ, $rotX, $rotY, $rotZ, $scaleX, $scaleY, $scaleZ; vector $tempVector; // error checking if(($transSelection == 0)&&($rotSelection == 0)&&($scaleSelection == 0)) { warning "findAndReplace.mel: Please select Translation, Rotation, and/or Scale"; return; } if(($oldObj == "")||($replacerObj == "")) { warning "findAndReplace.mel: Please enter an object to replace and a replacer"; return; } if(!(`objExists $replacerObj`)) { warning "findAndReplace.mel: Please enter a valid replacer"; return; } if(size(`ls -tr $oldObj`) == 0) { warning "findAndReplace.mel: Objects to be replaced list is invalid"; return; } // Grab the old objects and throw them into an array. string $findSpace[] = `ls -tr $oldObj`; string $currentObj; // Work Loop for ($currentObj in $findSpace) { // Get the attributes of the original object using xform and getAttr $tempVector = `xform -q -ws -rp $currentObj`; $transX = $tempVector.x; $transY = $tempVector.y; $transZ = $tempVector.z; $rotX = `getAttr ($currentObj + ".rotateX")`; $rotY = `getAttr ($currentObj + ".rotateY")`; $rotZ = `getAttr ($currentObj + ".rotateZ")`; $scaleX = `getAttr ($currentObj + ".scaleX")`; $scaleY = `getAttr ($currentObj + ".scaleY")`; $scaleZ = `getAttr ($currentObj + ".scaleZ")`; // Switch to determine the original objects disposition switch($farObjDisp) { case 1: // do nothing, original object kept break; case 2: delete $currentObj; break; case 3: hide $currentObj; break; } // Pick a replacement method and create the new objects using switch. switch ($farReplaceMeth) { case 1: duplicate -ic $replacerObj; break; case 2: instance $replacerObj; break; } // Take the old objects attributes and apply them to the new created object if($transSelection == 1) { setAttr ($replacerObj + ".translate") -type "double3" $transX $transY $transZ; } if($rotSelection == 1) { setAttr ($replacerObj + ".rotate") -type "double3" $rotX $rotY $rotZ; } if($scaleSelection == 1) { setAttr ($replacerObj + ".scale") -type "double3" $scaleX $scaleY $scaleZ; } } // Group // If the group option has been selected, grab all the new objects and group them. switch ($farGroupVal) { case 0: print "\nfindAndReplace.mel: Find and Replace Done. \n"; break; case 1: group -n ($replacerObj+"Grp") ($replacerObj+"*"); print "\nfindAndReplace.mel: Find and Replace Done and Objects Grouped \n"; break; } }