/* 'OBJ Sequence Import/Export' MEL script Author: Carlos Rico Adega - carlos.rico.3d@gmail.com Date: May 3, 2013 Copyright (c) 2013 Carlos Rico Adega Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,k OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. SCRIPT NAME: craOBJSequences.mel Main procedure: craOBJSequences; DESCRIPTION: - This script helps to import and export OBJ sequences. - It uses choice nodes to handle the animation of imported sequences. - Allows to export seleted objects as one sequence (combining objects) or one sequence per object. LAUNCH SCRIPT : craOBJSequences; TESTED ON: Windows 7 x64 - Maya 2013 x64 Fedora 14 x64 ( Virtualbox Virtual Machine ) - Maya 2012 x64 CHANGE LOG: Version: 1.2 Date: November 22, 2013 - Added export options Version: 1.1.1 Date: October 10, 2013 - Added flushUndo command for clearing RAM (thanks to Lossless22 for tip) Version: 1.1.0 Date: May 8, 2013 - Import/Export are now interruptable processes. - Fixed a bug that caused the script to stop working when a file fails to import. Version: 1.0.0 Date: May 3, 2013 - First Release. */ global proc craOBJSequences() { if(!`pluginInfo -q -l "objExport"`) loadPlugin -quiet "objExport"; if (`window -exists craOBJSequencesWin`) deleteUI craOBJSequencesWin; if(!`optionVar -exists craOBJCollapseExportOptionsOV`) optionVar -iv craOBJCollapseExportOptionsOV 1; if(!`optionVar -exists craOBJGroupsOV`) optionVar -iv craOBJGroupsOV 1; if(!`optionVar -exists craOBJPointGroupsOV`) optionVar -iv craOBJPointGroupsOV 1; if(!`optionVar -exists craOBJMaterialsOV`) optionVar -iv craOBJMaterialsOV 1; if(!`optionVar -exists craOBJSmoothingOV`) optionVar -iv craOBJSmoothingOV 1; if(!`optionVar -exists craOBJNormalsOV`) optionVar -iv craOBJNormalsOV 1; window -t " OBJ Sequence Import/Export" -tlb 1 -s 0 craOBJSequencesWin; frameLayout -mw 4 -mh 4 -lv 0 -bs "etchedOut" craImportExportOBJsMainFL; columnLayout -adj 1 craImportExportOBJsMainCL; frameLayout -mw 4 -mh 4 -lv 0 -bs "etchedOut" craImportExportOBJsFL1; columnLayout -adj 1 ; rowLayout -nc 2 -adj 2; text -l " "; checkBox -v 1 -l " Import under one transform" craOBJSeqOutputCB; setParent ..; button -bgc .5 .7 .5 -l "Import OBJ Sequence" -c ("craImportOBJs") -ann "Select all sequence files"; setParent ..; setParent ..; separator -h 5 -st "none"; frameLayout -w 190 -mw 4 -mh 4 -lv 0 -bs "etchedOut" craImportExportOBJsFL2; columnLayout -adj 1; rowLayout -nc 4 -adj 1; text -w 40 -l "Frames: start " -ann "Start frame"; intField -w 35 -ann "Start frame" -v `playbackOptions -q -min` craStartFrameIF; text -w 30 -l " end" -ann "End frame"; intField -w 35 -ann "End frame" -v `playbackOptions -q -max` craEndFrameIF; setParent ..; separator -h 5 -st "none"; frameLayout -cll 1 -ec "optionVar -iv craOBJCollapseExportOptionsOV 0" -cc "optionVar -iv craOBJCollapseExportOptionsOV 1" -cl `optionVar -q craOBJCollapseExportOptionsOV` -l "Export Options" -mw 5 -mh 4; columnLayout -adj 1; rowLayout -nc 2; checkBox -w 75 -l "Groups" -v `optionVar -q craOBJGroupsOV` -cc "optionVar -iv craOBJGroupsOV #1"; checkBox -l "Point Groups" -v `optionVar -q craOBJPointGroupsOV` -cc "optionVar -iv craOBJPointGroupsOV #1"; setParent ..; rowLayout -nc 2; checkBox -w 75 -l "Materials" -v `optionVar -q craOBJMaterialsOV` -cc "optionVar -iv craOBJMaterialsOV #1"; checkBox -l "Smoothing" -v `optionVar -q craOBJSmoothingOV` -cc "optionVar -iv craOBJSmoothingOV #1"; setParent ..; rowLayout -nc 2; checkBox -w 75 -l "Normals" -v `optionVar -q craOBJNormalsOV` -cc "optionVar -iv craOBJNormalsOV #1"; nodeIconButton -bgc .4 .4 .4 -h 18 -w 18 -i "pickOtherObj.png" -ann "About" -c "craAboutOBJScript"; setParent ..; setParent ..; setParent ..; separator -h 5 -st "none"; button -bgc .7 .5 .2 -l "Export OBJ Sequence" -c ("craExportOBJs"); setParent ..; setParent ..; showWindow craOBJSequencesWin; } global proc craExportOBJs( ) { string $mySel[] = `ls -sl`; int $multiple = 0; int $craCancelled; if(!size($mySel)) error "Select one object at least.\n"; string $craName; string $craResult = `promptDialog -title "OBJ Sequence" -message "Enter sequence name:" -button "OK" -button "Cancel" -defaultButton "OK" -cancelButton "Cancel" -dismissString "Cancel"` ; if ($craResult == "OK") { string $craOBJExportOptions; $craOBJExportOptions += "groups=" + string(`optionVar -q craOBJGroupsOV`) + ";"; $craOBJExportOptions += "ptgroups=" + string(`optionVar -q craOBJPointGroupsOV`) + ";"; $craOBJExportOptions += "materials=" + string(`optionVar -q craOBJMaterialsOV`) + ";"; $craOBJExportOptions += "smoothing=" + string(`optionVar -q craOBJSmoothingOV`) + ";"; $craOBJExportOptions += "normals=" + string(`optionVar -q craOBJNormalsOV`); //"groups=1;ptgroups=1;materials=0;smoothing=1;normals=1"; if(size($mySel) > 1) { string $answer = `confirmDialog -title "Question" -message " Multiple objects selected. What do you want to do?" -button " One OBJ sequence for all objects " -button " One OBJ sequence per object " -button "Cancel" -defaultButton "One OBJ sequence for all objects" -cancelButton "Cancel" -dismissString "Cancel"`; if($answer == " One OBJ sequence per object ") $multiple = 1; else if($answer == "Cancel") return; } $craName = `promptDialog -query -text`; string $exportDir[] = `fileDialog2 -caption "Select Export Folder" -okCaption "Export" -fileMode 3 -dialogStyle 2`; $startTime = `timerX`; if(size($exportDir)) { int $start = `intField -q -v craStartFrameIF`; int $end = `intField -q -v craEndFrameIF`; int $maxValue = $end - $start; progressWindow -status "Exporting OBJ Sequence..." -maxValue $maxValue -title "Exporting" -isInterruptable true; currentTime -e $start; dgeval $mySel[0]; for( $i=$start; $i<=$end; $i++ ) { if ( `progressWindow -query -isCancelled` ) { $craCancelled = 1; break; } progressWindow -e -step 1 ; if($multiple) { for($j in $mySel) { select -r $j; dgeval $j; duplicate; craOBJsChannelControlProc {"tx", "ty","tz","rx","ry","rz","sx","sy","sz","v"} 0 1; catchQuiet(`parent -w`); sysFile -md ($exportDir[0] + "/" + $craName + "_OBJ_Seq/" + $j); file -force -options $craOBJExportOptions -typ "OBJexport" -pr -es ($exportDir[0] + "/" + $craName + "_OBJ_Seq/" + $j + "/" + $j + "-" + cra_numberPadding (string($i), 1, "0") + ".obj"); delete; } } else { select -r $mySel; duplicate; if(size($mySel) > 1) polyUnite -ch 0; else catchQuiet(`parent -w`); craOBJsChannelControlProc {"tx", "ty","tz","rx","ry","rz","sx","sy","sz","v"} 0 1; file -force -options $craOBJExportOptions -typ "OBJexport" -pr -es ($exportDir[0] + "/" + $craName + "-" + cra_numberPadding (string($i), 1, "0") + ".obj"); delete; } flushUndo; currentTime -e ($i+1); } } select -r $mySel; progressWindow -endProgress; $totalTime = `timerX -startTime $startTime`; if($craCancelled) warning "Exporting OBJ sequence cancelled.\n"; else print ("All done! " + $craName + " exported successfully in " + $totalTime + " seconds.\n"); } } global proc craImportOBJs() { int $mode = `checkBox -q -v craOBJSeqOutputCB`; string $OBJfiles[] = `fileDialog2 -caption "Select All Sequence files" -okCaption "Import" -fileMode 4 -fileFilter "Wavefront Object (*.obj) (*.obj)" -sff "Wavefront Object (*.obj) (*.obj)" -dialogStyle 2`; if(size($OBJfiles)) craImportOBJsCmd($OBJfiles, $mode); else print "No files selected.\n"; } global proc craImportOBJsCmd( string $OBJfiles[], int $mode ) { $startTime = `timerX`; string $craErrors[]; int $craCancelled; string $origMeshNodes[] = `ls -l -type "mesh"`; string $craOrigObjectSetNodes[] = `ls -type "objectSet"`; string $currentMeshNodes[] = `ls -l -type "mesh"`; if(!$mode) if(!`objExists "OBJ_Sequences_grp"`) group -em -n "OBJ_Sequences_grp"; string $filename = basenameEx("\"" + $OBJfiles[0] + "\""); string $groupName[]; tokenize $filename "." $groupName; tokenize $groupName[0] "_" $groupName; select -cl; int $status = `scriptEditorInfo -q -sr`; scriptEditorInfo -sr 1; int $count = 1; int $maxValue = size($OBJfiles); progressWindow -status "Importing OBJ Sequence..." -maxValue $maxValue -title "Importing" -isInterruptable true; for($file in $OBJfiles) { if ( `progressWindow -query -isCancelled` ) { $craCancelled = 1; break; } progressWindow -e -step 1 ; if(catchQuiet(`file -force -import -type "OBJ" -options "mo=0" $file`)) { $craErrors[size($craErrors)] = $file; continue; } select -r `ls -l -type "mesh"`; select -deselect $currentMeshNodes; if(!$mode) pickWalkUp; string $newMesh[] = `ls -sl`; if(size($newMesh)) rename $newMesh[0] ( $groupName[0] + "_" + cra_numberPadding(string($count), 4, 0) + "_mesh" ); else $craErrors[size($craErrors)] = $file; if(!$mode) { select -add "OBJ_Sequences_grp"; parent; } hide; $currentMeshNodes = `ls -l -type "mesh"`; $count++; flushUndo; } select -r `ls -l -type "mesh"`; select -deselect $origMeshNodes; if(!$mode) { pickWalkUp; $newGroup = `group -n ($groupName[0] + "_OBJ_seq")`; select -r `listRelatives -f -c $newGroup`; } craConnectToChoice $groupName[0] $mode; scriptEditorInfo -sr $status; // remove imported sets string $craCurrentSets[] = `ls -type "objectSet"`; string $craRemoveSets[] = `stringArrayRemove $craOrigObjectSetNodes $craCurrentSets`; delete $craRemoveSets; $totalTime = `timerX -startTime $startTime`; if(size($craErrors)) { for($i in $craErrors) print ($i + " failed to import.\n"); warning ("There were errors importing " + size($craErrors) + " files. See Script Editor for details.\n"); } else { if($craCancelled) warning "Importing OBJ sequence cancelled.\n"; else print ("All done! " + $groupName[0] + " imported successfully in " + $totalTime + " seconds.\n"); } progressWindow -endProgress; } //cra_numberPadding ("20", 4, "0") global proc string cra_numberPadding(string $str,int $pad,string $chr) { string $return; for ($i=0;$i<$pad;$i++) { if (size($str)<=$i) $return += $chr; } return ($return + $str); } // Usage: // Select your objects and run. It creates a new choice node, and a new target mesh. // Connects every shape node to the choice node. No error checking so select only meshes. global proc craConnectToChoice( string $craName , int $mode) { // get selection string $mySel[] = `ls -sl`; // create choice node with name string $choice = `createNode -n ($craName + "_OBJ_Seq_choice") choice`; // create new mesh to hold the animation string $newMesh[] = `polyCube -ch 0 -n ($craName + "_OBJ_Seq_Mesh")`; // connect choice output to new mesh inMesh attr string $meshShape[] = `listRelatives -s $newMesh[0]`; connectAttr -f ($choice + ".output") ($meshShape[0] + ".inMesh"); // connect time to choice selector attr connectAttr -f time1.outTime ($choice + ".selector"); // connect obj outMesh to choice input attr for( $i=0; $i