//////////////////////////////////////////////////////////////////////////////// // Name: findAndReplace.mel // // Description: Finds objects with one name root and replaces them with // another object maintaining translation, rotation and scale. // Updates to this on the way... // // Created on: 12/17/2001 // Modified on: 04/22/2002 // // Created by: Rob O'Neill (oneill@parsons.edu) // // 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. // The command is undoable. // // Use and modify at your own risk. // Comments and suggestions always wanted. //////////////////////////////////////////////////////////////////////////////// //////////////////////////DEFINES/////////////////////////////////////////////// // Application Information global string $farVersion = "Find and Replace: Version 1.0"; global int $farReplaceMeth; global int $farGroupVal; global int $farObjDisp; ///////////////////////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(){ string $oldObj = `textField -q -text WrigleyField`; string $replacerObj = `textField -q -text DoubleDayField`; global int $farReplaceMeth; global int $farGroupVal; global int $farObjDisp; // Grab the old objects and throw them into an array. select $oldObj; string $findSpace[] = `ls -sl -tr`; string $waldoObj; // Work Loop: Get Busy for ($waldoObj in $findSpace) { // Get the attributes of the original object float $gotx = `getAttr ($waldoObj+".translateX")`; float $goty = `getAttr ($waldoObj+".translateY")`; float $gotz = `getAttr ($waldoObj+".translateZ")`; float $temprotx = `getAttr ($waldoObj+" .rotateX")`; float $temproty = `getAttr ($waldoObj+" .rotateY")`; float $temprotz = `getAttr ($waldoObj+" .rotateZ")`; float $tempscalex = `getAttr ($waldoObj+" .scaleX")`; float $tempscaley = `getAttr ($waldoObj+" .scaleY")`; float $tempscalez = `getAttr ($waldoObj+" .scaleZ")`; select $waldoObj; // Switch to determine the original objects disposition switch ($farObjDisp) { case 1: print "objects kept"; break; case 2: delete; break; case 3: hide; break; } // Select the Replacer Object select $replacerObj; // Pick a replacement method and create the new objects using switch. switch ($farReplaceMeth) { case 1: duplicate; break; case 2: instance; break; } // Take the old objects attributes and apply them to the new created object setAttr ".translate" -type "double3" $gotx $goty $gotz; setAttr ".rotate" -type "double3" $temprotx $temproty $temprotz; setAttr ".scale" -type "double3" $tempscalex $tempscaley $tempscalez; }; // G r o u p ? // If the group option has been selected, grab all the new objects and group them. switch ($farGroupVal) { case 0: print "\n Find and Replace Done \n"; break; case 1: group -n ($replacerObj+"grp") ($replacerObj+"*"); print "\n Find and Replace Done and Objects Grouped \n"; break; } }; /////////////////////////Let's get GUI!!!////////////////////////////////////// global proc findAndReplace(){ global string $farVersion; global int $farReplaceMeth; global int $farGroupVal; global int $farObjDisp; string $oldObj; string $replacerObj; string $replacerObjField; string $oldObjField; int $width = 100; int $height = 200; if (`window -exists findAndReplace_ui` == true) deleteUI findAndReplace_ui; window -maximizeButton false -resizeToFitChildren true -width $width -height $height -sizeable true -title $farVersion -iconName "Find and Replace" findAndReplace_ui; columnLayout -adjustableColumn 1 -columnAttach "both" 0 -rowSpacing 0 -columnWidth $width -columnAlign "left" mainColumnLayout; setParent findAndReplace_ui|mainColumnLayout; radioButtonGrp -numberOfRadioButtons 2 -label "Creation Type:" -labelArray2 "Duplicate" "Instance" -on1 "$farReplaceMeth = 1" -on2 "$farReplaceMeth = 2" -en1 1 replaceMethGroup; separator; text -label "All of these:"; string $oldObjField = `textField -text "Enter objects to be replaced. Use wildcards (*) ie: myObject*" WrigleyField`; string $oldObj = `textField -q -text WrigleyField`; text -label "With this:"; string $replacerObjField = `textField -text "Enter the replacer object" DoubleDayField`; string $replacerObj = `textField -q -text DoubleDayField`; separator; checkBox -label "Group Results" -onCommand "$farGroupVal = 1" -offCommand "$farGroupVal = 0" -align "center"; separator; columnLayout -adjustableColumn 1 -columnAttach "both" 0 -rowSpacing 0 -columnWidth 150 -columnAlign "center" variableColumnLayout; radioButtonGrp -numberOfRadioButtons 3 -label "Originals:" -labelArray3 "Keep" "Delete" "Hide" -on1 "$farObjDisp = 1" -on2 "$farObjDisp = 2" -on3 "$farObjDisp = 3" -en 1 -columnWidth3 50 50 50 -columnAlign3 "left" "left" "left" originalDisposition; separator; button -label "Find and Replace" -align "center" -command "doFindReplace"; button -label "Undo" -align "center" -command "undo"; button -label "Close" -align "center" closeButton; separator; text -label "findAndReplace.mel: Find and Replace Script"; text -label "Rob O'Neill (oneill@parsons.edu)"; button -edit -command "deleteUI findAndReplace_ui" closeButton; showWindow findAndReplace_ui; }; { deleteUI findAndReplace_ui; };