// ------------------------------------------------------------------------------ // -- // -- SAVE a file with skinclusters datas // -- // ------------------------------------------------------------------------------ // (c) Copyright by David Lanier 2003 // http://www.dl3d.com // contact@dl3d.com //---------------------------------- DEBUG STUFF ----------------------- proc DavMsgBox(string $s) { confirmDialog -title "Error" -message $s -b "OK"; } proc DebugPrintArray(string $msg[]) { global int $SkinDebug; if ($SkinDebug) { print($msg); print("\n"); } } proc DebugPrint(string $msg) { global int $SkinDebug; if ($SkinDebug) { print($msg + "\n"); } } //------------------------ FILE STUFF ---------------------------------------- proc WriteMyFile(string $StringToWrite) { global int $WritefileID; fprint $WritefileID $StringToWrite; } //--------------------- NODE PARSING ---------------------------------- proc string GetSelNodeFromSelection() { string $currentSelection[] = `ls -sl`; DebugPrint("Current Selection :"); DebugPrintArray($currentSelection); int $SizeOfCurSel = size($currentSelection); if ($SizeOfCurSel != 1) { DavMsgBox("Select 1 skinned object"); return ""; } return $currentSelection[0]; } proc WriteWeightsToFileFromSkinCluster(string $ClusterName, int $NumJoints) { int $i =0; string $ClusterwithAttr = $ClusterName + ".wl"; string $WeightListVtx[] = `listAttr -m -st "weightList" $ClusterwithAttr`; int $size = size($WeightListVtx); for ($i=0;$i<$size;$i++) { WriteMyFile("vtx"+$i); //Write vertex number WriteMyFile("\n"); //Get all weights for vertex number i int $j=0; for ($j=0;$j<$NumJoints;$j++) { string $Temp = $ClusterName + ".weightList[" + $i + "].weights[" + $j + "]"; float $WeightForThisVertexAndTheJointNum_J = `getAttr $Temp`; WriteMyFile($WeightForThisVertexAndTheJointNum_J); WriteMyFile(" "); } WriteMyFile("\n"); } WriteMyFile("\n"); } proc string[] GetJointsFromSkinCluster(string $ClusterName) { string $NULLStringArr[]; string $Joints[]; string $RealJoints[]; //We have twice the list of joints with the following command $Joints = `listConnections -type joint $ClusterName`; //So copy half the array int $size = size($Joints); if ($size <= 0) return $NULLStringArr; DebugPrint("Size of joints array is "+$size); int $halfSize = 0.5 * $size; DebugPrint("Half Size of joints array is "+$halfSize); int $Count = 0; string $Temp = ""; for ($Temp in $Joints) { $RealJoints[$Count++] = $Temp; //Copy the array if ($Count == $halfSize) break; //Finished } return $RealJoints; } //----------------------------------SkinClusters stuff-------------------------------- global proc WriteWeightsToFile( string $filename, string $fileType ) { global int $SkinDebug; //EXTERN DebugPrint ("FullPathName of file to write is : "+$filename); //DEBUG A ENLEVER //$filename = "c:\KSkin.skn"; //Open file for writing global int $WritefileID; $WritefileID = `fopen $filename "w"`; global string $KTag = "*************Skinning-Weights-File-DLanier*************"; WriteMyFile($KTag); WriteMyFile("\n"); //Get Selected node string $SelNode = GetSelNodeFromSelection(); if ($SelNode == "") return; DebugPrint("The selected node is : "+$SelNode); //Get the skin cluster linked to our selected node string $ClusterName = findRelatedSkinCluster($SelNode); if ($ClusterName == "") { DavMsgBox("The object "+$SelNode +" is not skinned ! Please choose a skinned object"); return; } DebugPrint("SkinCluster name = "+$ClusterName); string $Joints[] = GetJointsFromSkinCluster($ClusterName); string $CurJoint; global string $JointTag = "*************Joints*************"; WriteMyFile($JointTag); WriteMyFile("\n"); for ($CurJoint in $Joints) { WriteMyFile($CurJoint); WriteMyFile(" "); } WriteMyFile("\n"); global string $WeightTag = "*************Weights*************"; WriteMyFile($WeightTag); WriteMyFile("\n"); int $NumJoints = size($Joints); WriteWeightsToFileFromSkinCluster($ClusterName, $NumJoints); //Close file fclose $WritefileID; DavMsgBox("Ok, everything went right for me."); } global proc w(int $ParamDebug) { //Set the debug or not global int $SkinDebug; $SkinDebug = $ParamDebug; //Choose the file to write fileBrowserDialog -m 1 -fc "WriteWeightsToFile " -an "WriteWeights" -om "SaveAs"; } //-------------------------- MEL CODE EXECUTED ------------------------------ w(0); //Don't show debug info