Monday 2 November 2009

namespace editor, stoleded from internetzz

/* This file downloaded from Highend3d.com
''
'' Highend3d.com File Information:
''
'' Script Name: ks_nameSpaceManager
'' Author:
'' Last Updated: May 30, 2008
'' Update/Change this file at:
'' http://Highend3d.com/maya/downloads/mel_scripts/utility_external/renaming/4923.html
''
'' Please do not alter any information above this line
'' it is generated dynamically by Highend3d.com and will
'' be changed automatically on any updates.
*/
//
// ks_NameSpaceManager by Katrin Schmid 2007,
//
//Features:
// View, rename or remove up to 7 levels of namespaces.
// Remove empty namespaces only and select objects in namespavce options.
// Rename and keep in hierarchy or move namespace to root namespace.
// Rename and remove is only possible with unreferenced objects.
// You can apply "Make unique names" after removing namespaces to ensure unique object names

// Install: Put ks_nameSpaceManager.mel in you script directory.
// Start the GUI by typing "ks_nameSpaceManager" in the script editor.
//
// Usage:
// List namespaces, then you can start renaming.
// Apply "Make unique names" after removing namespaces to ensure unique object names
//

string $allrGlobalsNodes[] = `ls -l -type renderGlobals`; //defaultRenderGlobals
catch(`setAttr -type "string" ($allrGlobalsNodes[0] + ".currentRenderer") "mentalRay"`);
global int $gl_debug;

////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////main
////////////////////////////////////////////////////////////////////
global proc ks_nameSpaceManager()
{
global string $gl_frNameSpc;

if (`window -query -exists ks__nSpaceManager`)
deleteUI ks__nSpaceManager;

// Strings declarations
string $title = " ks_NameSpaceManager";
string $sh = "showHelp -absolute \"http://www.lo-motion.de/scripts\"";

//UI-Window
window -wh 400 620 -menuBar true
-title $title
-s 1
ks__nSpaceManager;

// Help menue
menu -label "Help" -helpMenu true;
menuItem -label "About" -c $sh;

// Layout
string $form = `formLayout`;
string $child1 = `columnLayout -adjustableColumn true`;
frameLayout -label "" -li 7 -borderStyle "out" -marginHeight 0 -marginWidth 8 -labelAlign "top";
rowLayout -numberOfColumns 1 -columnAlign1 "left" ; // -columnWidth2 230 300
// column 1
columnLayout -rowSpacing 3 -columnWidth 230 col2; //-columnAttach "both" 5

text -align left -w 200 -fn boldLabelFont -label " Check namespaces:";
// text -align left -w 200 -fn boldLabelFont -label "Check name spaces:" ch_ge_namespaces;
radioButtonGrp -l "" -numberOfRadioButtons 3 -columnWidth2 10 230 -p "col2"
-ct2 "left" "left" -select 1 -vr
-labelArray3 "List name spaces" "Remove empty" "Remove all (unreferenced objects)"
rbg_collNmspces;
// text -label "Rename selected namespace to:";
button -bgc .5 .7 .7 -align "left" -w 155 -l " Check/remove" -c "pa_checkScene()";
// button -align "left" -enable 0 -w 150 -l "add namespace" -c "" bu_addNmspc;

text -align left -w 200 -fn boldLabelFont -label "All namespaces:";
$gl_frNameSpc = `optionMenuGrp -adj 0 -cw 1 4 -enable false -w 170 -cat 1 "left" 0 -cc "" me_nameSpc`;
menuItem -p ($gl_frNameSpc + "|OptionMenu") -l " ";
button -align "left" -enable 0 -w 155 -l " Select objects in namespace" -c "pa_selectObjInNamespaces()" bu_selNmspc;
text -label "";
text -align left -w 200 -fn boldLabelFont -l "Rename selected namespace to:" ;
radioButtonGrp -l "" -numberOfRadioButtons 2 -columnWidth2 1 180 //-p "col3"
-ct2 "left" "left" -select 1 -vr
-labelArray2 "keep namespace hierarchy" "move to root" rbg_hierNmspces;
textField -w 155 tx_renameNmspce;

button -align "left" -enable 0 -w 155 -l "Rename selected namespace" -c "pa_renameNamespace()" bu_renNmspc;
setParent ..;
setParent ..;
setParent ..;
////
frameLayout -label "" -li 7 -borderStyle "out" -marginHeight 7 -marginWidth 10 -labelAlign "bottom";
rowLayout -numberOfColumns 2 -columnWidth2 230 300 -columnAlign1 "left" ;
// column 1
columnLayout -rowSpacing 6 -columnWidth 230 col4; //-columnAttach "both" 5
button -bgc .5 .7 .7 -align "left" -enable 1 -w 155 -l " Rename to unique names" -c "makeUniqueName" bu_uniNm;
setParent ..;
columnLayout -rowSpacing 6 -columnWidth 230 col5; //-columnAttach "both" 5
setParent ..;
setParent ..;
setParent ..;
///
showWindow ks__nSpaceManager;
}

////////////////////////////////////////////////
global proc pa_handleRadio(string $btgrName, int $onOff)
{
//string $btgrName, int $onOff
radioButtonGrp -e -enable1 $onOff -enable2 $onOff $btgrName;
}



//////////////////////////////////////////////////////////////////////////////////////
//////////////////////break connection of given dest attribute
global proc pa_breakConnection(string $destName)
{
global int $gl_debug;
if ($gl_debug)
{
print ("pa_breakConnection(string $destName)");
}

if (`connectionInfo -isDestination $destName`)
{
string $destination = `connectionInfo -getExactDestination $destName`;
string $srcConn[] = `listConnections -s 1 -d 0 -type character $destination`;
if (size($srcConn))
{
string $warnMsg = ("Removed \'"+ $destination +"\' from character \'" + $srcConn[0]+"\'.");
warning($warnMsg);
character -e -rm $srcConn[0] $destination;
}
string $sArr[1] = `ls -l -ro $destination`;

if (size($sArr))
{
string $src = `connectionInfo -sourceFromDestination $destination`;
disconnectAttr $src $destination;
}
else
{
delete -icn $destination;
}
}
}



//////////////////////////////////////////////////////////////////
global proc pa_checkScene()
{
global int $gl_debug;
if ($gl_debug)
{
print ("pa_checkScene()"+"\n");
}

int $nVal=`radioButtonGrp -q -sl rbg_collNmspces`;
if($nVal==1)
{
pa_listNamespaces(1);
}
else if($nVal==2)
{
pa_listNamespaces(0);
}
else if($nVal==3)
{
pa_removeUnrefNamespaces();
}
}


//////////////////////////////////////////////////////////////////
global proc pa_selectObjInNamespaces()
{
global int $gl_debug;
if ($gl_debug)
{
print("pa_selectObjInNamespaces()" +"\n");
}


select -d;
string $nameSpc =`optionMenuGrp -q -v me_nameSpc`;
if (`size($nameSpc)`!=0 && `namespace -exists (":" + $nameSpc)`)
{
namespace -set (":" + $nameSpc);
string $objsNamesp[] =`namespaceInfo -dagPath -ls`;
for ($t = 0; $t < size($objsNamesp); $t++)
{
if (`objExists $objsNamesp[$t]`)
{
select -add $objsNamesp[$t];
}
}
namespace -set ":";
}
}

/*
for ($i = 0; $i < size($nameSpaces); $i++)
{
if ($nameSpaces[$i]!= "UI" && $nameSpaces[$i]!= "shared")
{
// delete empty
namespace -set (":" + $nameSpaces[$i]);
string $objs[]= `namespaceInfo -ls`;
if (`size($objs)`==0 && `namespace -exists (":" + $nameSpaces[$i])`)
{
print ("Removed empty namespace: " + $nameSpaces[$i] +"\n");
namespace -set ":";
catch(`namespace -removeNamespace (":" + $nameSpaces[$i])`);
}
else
{
//level 2
$allNameSpc[`size($allNameSpc)`]= $nameSpaces[$i];
string $level2NameSpaces[]= pa_getNamespaces($nameSpaces[$i]);
}
}
}
*/

//////////////////////////////////////////////////////////////////
global proc string[] pa_getNamespaces(string $nameSpace)
{
global int $gl_debug;
if ($gl_debug)
{
print ("$nameSpace: "+ $nameSpace +"\n");
}


namespace -set (":" + $nameSpace);
string $nSpaces[]=`namespaceInfo -lon`;

for ($t = 0; $t < size($nSpaces); $t++)
{
string $objs[]= `namespaceInfo -dagPath -ls`;
if (`size($objs)`== 0 && `namespace -exists $nameSpace`)
{
print ("Removed empty namespace: " + $nameSpace +"\n");
namespace -removeNamespace (":" + $nameSpace);
string $remove[]={$nameSpace};
stringArrayRemove($remove, $nSpaces);
}
}

return $nSpaces;
}



//namespaceInfo -currentNamespace;
//////////////////////////////////////////////////////////////////
global proc string [] pa_listNamespaces(int $showInMenu)
{
global int $gl_debug;
if ($gl_debug)
{
print("pa_listNamespaces()" +"\n");
}

global string $gl_frNameSpc;
string $allNameSpc[];
string $level2NameSpaces[];
string $level3NameSpaces[];
string $level4NameSpaces[];
string $level5NameSpaces[];
string $level6NameSpaces[];
string $level7NameSpaces[];
int $count=0;

if ($showInMenu == 1)
{
button -e -enable true bu_selNmspc;
button -e -enable true bu_renNmspc;
}
else
{
button -e -enable 0 bu_selNmspc;
button -e -enable 0 bu_renNmspc;
}

//deleteMenueItems
string $menItems[] = `optionMenuGrp -q -itemListLong me_nameSpc`;
for ($t = 0; $t < size($menItems); $t++)
{
deleteUI $menItems[$t];
}

namespace -set ":";
string $nameSpaces[] = `namespaceInfo -lon`;
optionMenuGrp -e -enable true me_nameSpc;

for ($i = 0; $i < size($nameSpaces); $i++)
{
if ($nameSpaces[$i]!= "UI" && $nameSpaces[$i]!= "shared")
{
// delete empty
namespace -set (":" + $nameSpaces[$i]);
string $objs[]= `namespaceInfo -dagPath -ls`;
if (`size($objs)`==0 && $showInMenu == 0 )
{
$count++;
print ("Removed empty namespace: " + $nameSpaces[$i] +"\n");
namespace -set ":";
catch(`namespace -removeNamespace (":" + $nameSpaces[$i])`);
}
else
{
//level 2
$allNameSpc[`size($allNameSpc)`]= $nameSpaces[$i];
$level2NameSpaces= pa_getNamespaces($nameSpaces[$i]);
if ($showInMenu==1)
{
menuItem -p ($gl_frNameSpc + "|OptionMenu") -l $nameSpaces[$i];
}

for ($p = 0; $p < size($level2NameSpaces); $p++)
{
$allNameSpc[`size($allNameSpc)`]= $level2NameSpaces[$p];
if ($showInMenu==1)
{
menuItem -p ($gl_frNameSpc + "|OptionMenu") -l $level2NameSpaces[$p];
}
}
}
}
}
// level 3
for ($f = 0; $f < size($level2NameSpaces); $f++)
{
// print ("$level2NameSpaces[$f]: " +$level2NameSpaces[$f]+"\n");
$level3NameSpaces= pa_getNamespaces(":" + $level2NameSpaces[$f]);
//print ("$level3NameSpaces:+++++" +"\n");
// print ($level3NameSpaces);

for ($g = 0; $g < size($level3NameSpaces); $g++)
{
$allNameSpc[`size($allNameSpc)`]= $level3NameSpaces[$g];
if ($showInMenu==1)
{
menuItem -p ($gl_frNameSpc + "|OptionMenu") -l $level3NameSpaces[$g];
}
}
}

// level 4
for ($h = 0; $h < size($level3NameSpaces); $h++)
{
// print ("$level3NameSpaces[$f]:+++++ " +$level3NameSpaces[$h]+"\n");
$level4NameSpaces= pa_getNamespaces(":"+$level3NameSpaces[$h]);

// print ("$level4NameSpaces" +"\n");
print ($level4NameSpaces);

for ($u = 0; $u < size($level4NameSpaces); $u++)
{
$allNameSpc[`size($allNameSpc)`]= $level4NameSpaces[$u];
if ($showInMenu==1)
{
menuItem -p ($gl_frNameSpc + "|OptionMenu") -l $level4NameSpaces[$u];
}
}
}

// level 5
for ($q = 0; $q < size($level4NameSpaces); $q++)
{
$level5NameSpaces= pa_getNamespaces(":" +$level4NameSpaces[$q]);
for ($v = 0; $v < size($level5NameSpaces); $v++)
{
$allNameSpc[`size($allNameSpc)`]= $level5NameSpaces[$v];
if ($showInMenu==1)
{
menuItem -p ($gl_frNameSpc + "|OptionMenu") -l $level5NameSpaces[$v];
}
}
}

// level 6
for ($b = 0; $b < size($level5NameSpaces); $b++)
{
$level6NameSpaces= pa_getNamespaces(":" +$level5NameSpaces[$b]);
for ($w = 0; $w < size($level6NameSpaces); $w++)
{
$allNameSpc[`size($allNameSpc)`]= $level6NameSpaces[$w];
if ($showInMenu==1)
{
menuItem -p ($gl_frNameSpc + "|OptionMenu") -l $level6NameSpaces[$w];
}
}
}

// level 7
for ($a = 0; $a < size($level6NameSpaces); $a++)
{
$level7NameSpaces= pa_getNamespaces(":" +$level6NameSpaces[$a]);
for ($x = 0; $x < size($level7NameSpaces); $x++)
{
$allNameSpc[`size($allNameSpc)`]= $level7NameSpaces[$x];
if ($showInMenu==1)
{
menuItem -p ($gl_frNameSpc + "|OptionMenu") -l $level7NameSpaces[$x];
}
}
}
radioButtonGrp -e -label2 ("Remove empty: " + $count +" removed.") rbg_collNmspces;
return $allNameSpc;
}


///////////////////////////////////////////////////////////////
// move namespace back to root ":"
global proc pa_removeUnrefNamespaces()
{
global int $gl_debug;
if ($gl_debug)
{
print ("pa_removeUnrefNamespaces()" +"\n");
}

int $remCount, $notRem=0;
namespace -set ":";
string $allNmspces[] = pa_listNamespaces(0);
string $refNmspces[];
string $refNames[]= `ls -l -type reference`;

for ($i = 0; $i < size($refNames); $i++)
{
if (`gmatch $refNames[$i] "*sharedReferenceNode*"` == 0 && `gmatch $refNames[$i] "*UNKNOWN_REF_NODE*"`==0)
{
string $selRefFileUnR = `getAttr ($refNames[$i] + ".fileNames[0]")`;
// string $selRefFileUnR = `referenceQuery -unresolvedName -filename $refNames[$i]`;
//print ("$refNmspces[size($refNmspces)]: " + $refNmspces[`size($refNmspces)`] +"\n");
if (`size($selRefFileUnR)` > 0)
{
$refNmspces[`size($refNmspces)`]= `file -q -ns $selRefFileUnR`;
}
}
}

string $unrefNamespaces[] = stringArrayRemove($refNmspces, $allNmspces);

for ($p = `size($unrefNamespaces)`;$p >= 0; $p--)
{
namespace -set ":";
if (`size($unrefNamespaces[$p])`> 0 && `namespace -exists (":"+ $unrefNamespaces[$p])` && $unrefNamespaces[$p]!= ":")
{
catch(`namespace -force -mv (":"+ $unrefNamespaces[$p]) ":"`);
namespace -set (":"+ $unrefNamespaces[$p]);
string $objsNamesp[] =`namespaceInfo -dagPath -ls`;
namespace -set ":";

if (`size($objsNamesp)`==0)
{
catch(`namespace -rm $unrefNamespaces[$p]`);
print ("REMOVED:$unrefNamespaces[$p]: "+ $p+ (" :"+ $unrefNamespaces[$p]) +"\n");
$remCount++;
}
}
else
{
$notRem++;
print ("NOTFOUND: "+ $p+ (" :"+ $unrefNamespaces[$p]) +"\n");
}
}
radioButtonGrp -e -label3 ("Remove all: " + $remCount +" removed, "+ ($notRem-1)+ " not removed.") rbg_collNmspces;
}




///////////////////////////////////////////////////////////////
global proc pa_checkReferences()
{
global int $gl_debug;
if ($gl_debug)
{
print("pa_checkReferences()" +"\n");
}

// getAttr TestspRN.fileNames[0]

string $selRefFileUnR;
string $selRefFile;

//textScrollList -e -enable true -ra ts_refFound;
//textScrollList -e -enable true -ra ts_refNotFound;

RNdeleteUnused;
string $refNames[]= `ls -l -type reference`;

for ($i = 0; $i < size($refNames); $i++)
{

if (`gmatch $refNames[$i] "*sharedReferenceNode*"` == 0 && `gmatch $refNames[$i] "*UNKNOWN_REF_NODE*"`==0)
{
//$selRefFileUnR = `referenceQuery -unresolvedName -filename $refNames[$i]`;
$selRefFileUnR = `getAttr ($refNames[$i] + ".fileNames[0]")`;
if (`size($selRefFileUnR)` > 0)
{
$selRefFile = `referenceQuery -filename $refNames[$i]`;
}
}
else if (`gmatch $refNames[$i] "*UNKNOWN_REF_NODE*"`)
{
$selRefFile ="";
$selRefFileUnR =$refNames[$i];
}
string $bufferPP[];

if (`size($selRefFileUnR)` > 0 && $selRefFile != $selRefFileUnR && `gmatch $refNames[$i] "*sharedReferenceNode*"` == 0)
{
string $dir= `workspace -q -rd`;
// Result: D:/maya/workflow/
int $numTokensPP = `tokenize $selRefFileUnR "//" $bufferPP`;
print ("Found ref: " +($dir + $bufferPP[$numTokensPP-1])+"\n");
//Found ref: D:/maya/workflow/B_data_3D/Testsp.mb

if ((`filetest -f ($dir + $bufferPP[$numTokensPP-1])`) == 1)
{
print ("Found ref: " +($refNames[$i]+":" + $selRefFile)+"\n");
// textScrollList -e -append ($bufferPP[$numTokensPP-1]) ts_refFound;
}
else
{
print ("Missing/ empty ref: " +($selRefFileUnR)+"\n");
}

}
else if (`gmatch $refNames[$i] "*sharedReferenceNode*"` == 0 )
{
if ((`filetest -f ($selRefFile)`) == 1)
{
// textScrollList -e -append ($refNames[$i]+":" + $selRefFile) ts_refFound;
print ("Found ref: " +($refNames[$i]+":" + $selRefFile)+"\n");
}
else
{
// textScrollList -e -append ($refNames[$i] + "c") ts_refNotFound;
print ("Missing ref: " +($selRefFile)+"\n");
}
}
}
}


/////////////////////////////////
global proc pa_renameNamespace()
{
global int $gl_debug;
if ($gl_debug)
{
print(" pa_renameNamespace()" +"\n");
}

int $nVal=`radioButtonGrp -q -sl rbg_hierNmspces`;
string $oldNameSpc =`optionMenuGrp -q -v me_nameSpc`;
string $newName =`textField -q -tx tx_renameNmspce`;
string $clName;
if (isValidString($newName, "([a-zA-Z]+)([a-zA-Z0-9_])*")==0)
{
$clName = substituteAllString($newName, " ", "_");
}
else
{
$clName = $newName;
}

// print ("$oldNameSpc: "+$oldNameSpc+"\n");
// print ("$clName: "+ $clName+"\n");
string $bufferPP[];
int $numTokensPP = `tokenize $oldNameSpc ":" $bufferPP`;
int $nSize= `size($bufferPP[$numTokensPP-1])`;
int $oSize= `size($oldNameSpc)`;
string $oldNamePath = `substring $oldNameSpc 1 ($oSize-$nSize)`;

namespace -set ":";
if (`namespace -exists (":" + $clName)`)
{
$clName= $clName +"1";
}

/////////
if(`size($bufferPP)`> 1 && $nVal==1 && `namespace -exists (":"+ $oldNamePath)`)
{
namespace -add $clName -p (":"+ $oldNamePath);
// print ("t:" +(":"+ $oldNamePath + $clName)+"\n");
if (`namespace -exists (":"+ $oldNamePath + $clName)`)
{
namespace -f -mv $oldNameSpc (":"+ $oldNamePath + $clName);
namespace -rm $oldNameSpc;
}
}
else //if ($nVal==2)
{
namespace -add ($clName) -p ":" ;
if (`namespace -exists (":"+ $oldNameSpc)` && `namespace -exists (":"+ $clName)`)
{
print ("hier: " +(":"+$clName)+"\n");
namespace -f -mv (":"+ $oldNameSpc) (":"+ $clName);
namespace -rm $oldNameSpc;
}
}

pa_listNamespaces(1);
}


////////////////////////////
global proc makeUniqueName()
{
// longNmaeof
global int $gl_debug;
if ($gl_debug)
{
print ("makeUniqueNames: " +"\n");
}

namespace -set ":";

string $allobjs[] = `ls -l -tr -mat -tex`;//-tr
for ($i= 0; $i < size($allobjs); $i++)
{
// print ($i + "$allobjs[$i]: " + $allobjs[$i] +"\n");
int $found=0;
if(`objExists $allobjs[$i]` && $allobjs[$i]!="|front" && $allobjs[$i]!="|persp" && $allobjs[$i]!="|top" && $allobjs[$i]!="|side")
{
string $buffer[];
tokenize $allobjs[$i] "|" $buffer;
string $shortNameI = $buffer[`size($buffer)`-1];
for ($t= 0; $t < size($allobjs); $t++)
{
string $buffer1[];
tokenize $allobjs[$t] "|" $buffer1;
string $shortNameT = $buffer1[`size($buffer1)`-1];

if (`objExists $allobjs[$t]` && $i!=$t && $shortNameI == $shortNameT && `referenceQuery -isNodeReferenced $allobjs[$t]`== 0)
{
int $check[]=`lockNode -q -lock $allobjs[$t]`;
if ($check[0] ==1)
{
lockNode -lock 0 $allobjs[$t];
}
string $newName1 = "";
int $n = 0;
select -r $allobjs[$t];
if ($n = catchQuiet($newName1 = `rename($shortNameT + "#")`))
{
print ("Cannot rename.T +"+$shortNameT+"\n");
}
else
{
print ("Renamed.T "+$shortNameT+"\n");
}
if ($n==0)
{
string $longNewNameI[] =`ls -l -sl`;
$allobjs[$t]= $longNewNameI[0];
}
$found=1;
}
}
}
}
}

reinitialise setup machine menu script

if (`objExists TSM2FKIKSwitch`)
delete TSM2FKIKSwitch;
if (`menu -exists TSM2ControlsMenu`) deleteUI -menu TSM2ControlsMenu;

string $path = `pluginInfo -q -path "TSM2"`;
$path = (dirname($path) + "/TSM2/FKIK.mel");
int $file = `fopen $path "r"`;
string $fkikScript = "";
string $nextLine = `fgetline $file`;
while(size($nextLine) > 0) {
$fkikScript += $nextLine;
$nextLine = `fgetline $file`;
}
fclose $file;
$path = `pluginInfo -q -path "TSM2"`;
$path = (dirname($path) + "/TSM2/TSM2_mirrorPose.mel");
int $file = `fopen $path "r"`;
string $mirrorScript = "";
string $nextLine = `fgetline $file`;
while(size($nextLine) > 0) {
$mirrorScript += $nextLine;
$nextLine = `fgetline $file`;
}
fclose $file;
$mirrorScript += "\n\nTSM2_mirrorPose;";
string $afterScript = "if (`menu -exists TSM2ControlsMenu`) deleteUI -menu TSM2ControlsMenu";
string $beforeScript;
$beforeScript = "doEnableNodeItems false all; doEnableNodeItems true all;\n";
$beforeScript += "menu -to true -label \"TSM Controls\" -parent $gMainWindow TSM2ControlsMenu;";
$beforeScript += ("menuItem -label \"FKIK Switch\" -command \"" + (encodeString($fkikScript)) + "\";");
$beforeScript += ("menuItem -label \"Refresh Rig\" -command \"doEnableNodeItems false all; doEnableNodeItems true all;\";");
$beforeScript += ("menuItem -label \"Mirror Pose\" -command \"" + (encodeString($mirrorScript)) + "\";");
scriptNode -st 2 -bs $beforeScript -as $afterScript -n TSM2FKIKSwitch;
scriptNode -executeBefore TSM2FKIKSwitch;