// =========================================================================== // Copyright 2018 Autodesk, Inc. All rights reserved. // // Use of this software is subject to the terms of the Autodesk license // agreement provided at the time of installation or download, or which // otherwise accompanies this software in either electronic or hard copy form. // =========================================================================== // // // Creation Date: Nov, 2003 // // // // // // searchReplaceNames(string $searchString, string $replaceString, string $onString) // // // This script can be used to search for names that contain the specified // search string, and replacing that string with the specified replace string. // Note that referenced nodes cannot be renamed, so referenced nodes are // skipped by this method. // // // string $searchString The string to search for // string $replaceString The string to replace it // string $onString: What to rename: "all", "selected", "hierarchy" // // // An array of the objects names which were modified. // // // // // In the hierarchy below the selection, teplace all objects containing the // // string "george" with the string "jim". // // // searchReplaceNames("george","jim","hierarchy"); // // global proc searchReplaceNames( string $searchString, string $replaceString, string $onString) { if ($searchString == $replaceString) { return; } string $getObjCmd = "ls"; if ($onString == "all") { // nothing } else if ($onString == "selected") { $getObjCmd += " -sl"; } else if ($onString == "hierarchy") { $getObjCmd += " -sl -dag"; } else { string $fmtString = (uiRes("m_searchReplaceNames.kInvalidArgument")); string $errMsg = `format -s $onString $fmtString`; error($errMsg); } string $nameList[]; string $allNames[] = `eval $getObjCmd`; int $count = 0; int $renamedCount = 0; for ($name in $allNames) { // strip off any path and namespace info // string $pathless = match("[a-zA-Z0-9_]*$",$name); if (! `objExists $name`) { // renaming must have modified the parent names // $nameList = `eval $getObjCmd`; $name = $nameList[$count]; } if (`reference -q -isNodeReferenced $name`) { // referenced nodes cannot be renamed // $count++; continue; } string $newName = `substitute $searchString $pathless $replaceString`; if ($newName != $pathless) { rename $name $newName; $renamedCount++; } $count++; } string $formatString = (uiRes("m_searchReplaceNames.kReplacedNumberNames")); print `format -s $renamedCount $formatString`; }