/* NAME: dbPoseWin.mel VERSION: 1.1 CATEGORY: Animation FUNCTION: Allows saving a library of poses, with the ability to create thumbnails for each pose. Allows for multiple pose files and multiple poses within those files. USAGE: 1. Copy the dbPoseWin.mel into your Maya Scripts directory (Usually found in "My Documents\maya\\scripts\"). 2. In the Maya Script Editor, type in "source dbPoseWin.mel; dbPoseWin;" AUTHOR: David Bokser E-MAIL: me@davidbokser.com WEBSITE: www.davidbokser.com TODO: - Add the ability to save only selected channels as well as the ability to append objects to a saved pose. - Also add the option of viewing what objects are modified in the pose file. VERSION HISTORY: 1.1 - added thumbnail feature with ability to save thumbnails within the viewport via automatic playblast - added the ability to ignore namespaces when loading a pose 1.0 - initial release */ global proc dbPoseWin() { if(`window -exists poseListWin`) deleteUI -window poseListWin; string $filePath = ""; if(`optionVar -exists "dbPoseDirectoryVar"`) $filePath = `optionVar -q "dbPoseDirectoryVar"`; else { string $fileLoc = `file -q -loc`; $filePath = `match "^.*[/\]" $fileLoc`; } string $fileName = ""; string $files[] = dbGetPoseFiles(); $fileName = $filePath + "/" + $files[0]; string $poses[] = dbGetPosesFromFile($fileName); string $window = `window -title "Pose List v1.1" -iconName "Short Name" -widthHeight 200 55 -menuBar 1 poseListWin`; menu -label "File" -tearOff false; menuItem -label "Load Thumbnail" -command("dbLoadThumbnail"); menuItem -label "Make Thumbnail" -command("dbMakeThumbnail"); menuItem -divider 1; menuItem -label "Load Directory" -command ("dbLoadPoseDir") dbPoseLoadMI; menuItem -divider 1; menuItem -label "New Pose File" -command ("dbNewPoseFile"); menuItem -label "Delete Pose File" -command ("dbDeletePoseFile"); menuItem -divider 1; menuItem -label $filePath -en 0 dbPoseFilenameMI; menu -label "Options" -tearOff false; menuItem -label "Remove Namespaces" -cb 1 remNS; columnLayout -adjustableColumn true; rowColumnLayout -numberOfColumns 3 -cw 1 150 -cw 2 170 -cw 3 180; textScrollList -w 200 -ams 0 -h 125 -sc ("dbLoadPoseList") poseFileListTSL; for($file in $files) { string $tempName = `substitute ".dbf" $file ""`; textScrollList -e -append $tempName poseFileListTSL; } string $tempName = `substitute ".dbf" $files[0] ""`; textScrollList -e -si $tempName poseFileListTSL; textScrollList -w 300 -ams 1 -h 125 -dcc ("dbLoadPoseCall") -sc ("image -e -image `dbGetThumbnail` thumbNailImage;") poseListTSL; for($pose in $poses) { textScrollList -e -append $pose poseListTSL; } paneLayout -w 180 -h 125; image -image `dbGetThumbnail` thumbNailImage; setParent ..; setParent ..; text -label "Search for:"; textField -cc ("optionVar -sv \"poseWinSearch\" `textField -q -tx searchField`") searchField; text -label "Replace with:"; textField -cc ("optionVar -sv \"poseWinReplace\" `textField -q -tx replaceField`") replaceField; text -label "Prefix:"; textField -cc ("optionVar -sv \"poseWinPrefix\" `textField -q -tx prefixField`") prefixField; button -label "Save Pose" -command ("\ string $filePath = `menuItem -q -label dbPoseFilenameMI`;\ string $poseFileName[] = `textScrollList -q -si poseFileListTSL`;\ string $fileName = $filePath + \"/\" + $poseFileName[0] + \".dbf\"; \ dbSavePose($fileName);\ dbLoadPoseList;"); button -label "Load Pose" -command ("dbLoadPoseCall"); button -label "Delete Pose" -command ("dbDeletePoseCall"); separator; button -label "Reload" -command ("source dbPoseWin.mel; dbPoseWin"); setParent ..; showWindow $window; if(`optionVar -exists "poseWinSearch"`) textField -e -tx `optionVar -q "poseWinSearch"` searchField; if(`optionVar -exists "poseWinReplace"`) textField -e -tx `optionVar -q "poseWinReplace"` replaceField; if(`optionVar -exists "poseWinPrefix"`) textField -e -tx `optionVar -q "poseWinPrefix"` prefixField; window -e -h 380 -w 510 $window; } global proc dbLoadPoseList() { string $filePath = `menuItem -q -label dbPoseFilenameMI`; string $poseFileName[] = `textScrollList -q -si poseFileListTSL`; string $fileName = $filePath + "/" + $poseFileName[0] + ".dbf"; string $poses[] = dbGetPosesFromFile($fileName); textScrollList -e -ra poseListTSL; for($pose in $poses) { textScrollList -e -append $pose poseListTSL; } } global proc dbSavePoseOptionWin() { string $window = `window -title "Save Pose" -iconName "Save Pose" -widthHeight 200 55`; columnLayout -adjustableColumn true; text "Pose Name:"; textField; button -label "OK"; button -label "Cancel" -command ("deleteUI -window " + $window); setParent ..; showWindow $window; window -edit -widthHeight 300 300 $window; } global proc dbSavePose(string $fileName) { string $text; string $result = `promptDialog -title "Save Pose" -message "Pose Name:" -button "OK" -button "Cancel" -defaultButton "OK" -cancelButton "Cancel" -dismissString "Cancel"`; if ($result == "OK") { $text = `promptDialog -query -text`; $fileId = fopen($fileName,"a"); fprint($fileId,("START POSE: " + $text + "\n")); string $selected[] = `ls -sl`; for($obj in $selected) { fprint($fileId,("Obj:" + $obj + "\n")); string $attrs[] = `listAttr -keyable -u $obj`; for($attr in $attrs) { string $val = `getAttr ($obj + "." + $attr)`; fprint($fileId,($attr + " " + $val + "\n")); } } fprint($fileId,("END POSE\n")); fclose($fileId); } } global proc dbAppendToPose(string $fileName, string $pose) { } global proc dbDeletePoseCall() { string $dirName = `menuItem -q -label dbPoseFilenameMI`; //$dirName = `substitute "/$" $dirName ""`; string $poseFileName[] = `textScrollList -q -si poseFileListTSL`; $poseFileName[0] = `substitute "/" $poseFileName[0] ""`; $poseFileName[0] = $poseFileName[0] + ".dbf"; string $selectedPoses[] = `textScrollList -q -si poseListTSL`; for($pose in $selectedPoses) { dbDeletePose($poseFileName[0], $dirName, $pose); } } global proc dbDeletePose(string $file, string $dir, string $poseName) { string $fileName = $dir + "/" + $file; string $tempFilename = `substitute "...$" $fileName "tmp"`; $fileId=fopen($fileName,"r"); $fileIdTmp=fopen($tempFilename,"w"); string $nextLine = fgetline($fileId); while ( size( $nextLine ) > 0 ) { if(`gmatch $nextLine ("START POSE: " + $poseName + "\n")`) { while(!`gmatch $nextLine "END POSE*"` && size( $nextLine ) > 0) { $nextLine = fgetline($fileId); } $nextLine = fgetline($fileId); } else { fprint($fileIdTmp,$nextLine); $nextLine = fgetline($fileId); } } fclose($fileId); fclose($fileIdTmp); sysFile -copy $fileName $tempFilename; sysFile -delete $tempFilename; dbLoadPoseList; } global proc dbLoadPoseCall() { string $dirName = `menuItem -q -label dbPoseFilenameMI`; string $poseFileName[] = `textScrollList -q -si poseFileListTSL`; string $fileName = $dirName + "/" + $poseFileName[0] + ".dbf"; string $selectedPoses[] = `textScrollList -q -si poseListTSL`; string $search = `textField -q -tx searchField`; string $replace = `textField -q -tx replaceField`; string $prefix = `textField -q -tx prefixField`; int $remNS = `menuItem -q -cb remNS`; for($pose in $selectedPoses) { dbLoadPose($fileName, $pose, $search, $replace, $prefix, $remNS); } } global proc dbLoadPose(string $fileName, string $poseName, string $search, string $replace, string $prefix, int $remNS) { $fileId=fopen($fileName,"r"); string $nextLine = fgetline($fileId); while ( size( $nextLine ) > 0 ) { if(`gmatch $nextLine ("START POSE: " + $poseName + "\n")`) { $nextLine = fgetline($fileId); while(!`gmatch $nextLine "END POSE*"` && size( $nextLine ) > 0) { if(`gmatch $nextLine "Obj:*"`) { string $object = `substitute "Obj:" $nextLine ""`; $object = `substitute "[\n]$" $object ""`; if($remNS) { $object = `substitute ".*:" $object ""`; } if( size( $search ) > 0) { $object = `substitute $search $object $replace`; } if( size( $prefix ) > 0) { $object = $prefix + $object; } //warning ("Working on " + $object); $nextLine = fgetline($fileId); while(!`gmatch $nextLine "Obj:*"` && size( $nextLine ) > 0 && !`gmatch $nextLine "END POSE*"`) { string $broken[] = {}; tokenize($nextLine, $broken); if($remNS) { string $sceneObjects[] = `ls -r 1 $object`; if (`size($sceneObjects)` == 1) $object = $sceneObjects[0]; else if (`size($sceneObjects)` > 2) { warning ("multiple objects found named " + $object + ". Using this one: " + $sceneObjects[0]); $object = $sceneObjects[0]; } } if(`objExists $object`) { string $multiObjs[] = `ls $object`; if(`size($multiObjs)` > 1) { warning ("Multiple objects match name " + $object + ". Using the first one: " + $multiObjs[0]); $object = $multiObjs[0]; } if(`attributeExists $broken[0] $object`) { float $val = $broken[1]; if(!`getAttr -l ($object + "." + $broken[0])`) { string $connections[] = `listConnections -s 1 -d 0 ($object + "." + $broken[0])`; //print $connections; if(`size($connections)` <= 1) { string $type = ""; if($connections[0] != "") { string $type = `nodeType ($connections[0])`; } if(`size($connections)` < 1 || $type == "") catchQuiet(`setAttr ($object + "." + $broken[0]) $val`); else if(`gmatch $type "animCurve*"`) catchQuiet(`setAttr ($object + "." + $broken[0]) $val`); else{ warning ($object + " has incoming connections. Can't set the attribute"); } } } } else { warning ("Attribute " + $broken[0] + " doesn't exist"); } } else { warning ("Object " + $object + " doesn't exist."); } $nextLine = fgetline($fileId); } } else { $nextLine = fgetline($fileId); } } } else $nextLine = fgetline($fileId); } fclose($fileId); } // Gets the list of pose files available global proc string[] dbGetPoseFiles() { string $directory = ""; if(`optionVar -exists "dbPoseDirectoryVar"`) { $directory = `optionVar -q "dbPoseDirectoryVar"`; $directory = $directory + "/"; } else { string $fileLoc = `file -q -loc`; $directory = `match "^.*[/\]" $fileLoc`; } string $files[] = `getFileList -folder $directory`; string $poseFiles[] = {}; for($file in $files) { if(`gmatch $file "*.dbf"`) $poseFiles[`size($poseFiles)`] = $file; } $poseFiles = sort($poseFiles); return $poseFiles; } // Creates a new pose file global proc dbNewPoseFile() { string $fileName; string $result = `promptDialog -title "New Pose File" -message "Enter Name:" -button "OK" -button "Cancel" -defaultButton "OK" -cancelButton "Cancel" -dismissString "Cancel"`; if ($result == "OK") { $fileName = `promptDialog -query -text`; if($fileName == "") { error "You must put something for the filename."; } else { string $filePath = ""; if(`optionVar -exists "dbPoseDirectoryVar"`) { $filePath = `optionVar -q "dbPoseDirectoryVar"`; $filePath = $filePath + "/"; } else { $filePath = `file -q -loc`; $filePath = `match "^.*[/\]" $filePath`; } $fileId=`fopen ($filePath + $fileName + ".dbf")`; fclose $fileId; dbLoadPoseDirDo($filePath, ""); textScrollList -e -si ($fileName) poseFileListTSL; dbLoadPoseList; } } } // deletes a pose file global proc dbDeletePoseFile() { string $filePath = `menuItem -q -label dbPoseFilenameMI`; string $poseFileName[] = `textScrollList -q -si poseFileListTSL`; string $fileName = $filePath + "/" + $poseFileName[0] + ".dbf"; sysFile -delete $fileName; dbPoseWin; } global proc string[] dbGetPosesFromFile(string $fileName) { string $poses[] = {}; if(`filetest -f $fileName`) { $fileId=fopen($fileName,"r"); string $nextLine = `fgetline $fileId`; while ( !feof($fileId) ) { if(`gmatch $nextLine "START POSE:*"`) { string $tempName = `substitute "START POSE: " $nextLine ""`; $tempName = `substitute "[\n]$" $tempName ""`; $poses[`size($poses)`] = $tempName; } $nextLine = `fgetline $fileId`; } fclose($fileId); } $poses = sort($poses); return $poses; } global proc dbLoadPoseDir() { string $fileName = `fileBrowserDialog -m 4 -fc "dbLoadPoseDirDo" -ft "directory" -an "Load_Directory" -om "Import"`; } global proc string dbLoadPoseDirDo(string $dirName, string $fileType) { if(`filetest -d $dirName`) { textScrollList -e -ra poseFileListTSL; menuItem -e -label $dirName dbPoseFilenameMI; optionVar -sv "dbPoseDirectoryVar" $dirName; string $files[] = dbGetPoseFiles(); for($file in $files) { string $name = `substitute ".dbf" $file ""`; textScrollList -e -a $name poseFileListTSL; } dbLoadPoseList; } else warning "No directory found."; return ""; } global proc string dbGetThumbnail() { string $tempLocation = `whatIs dbPoseWin.mel`; $tempLocation = `substitute "Script found in: " $tempLocation ""`; $tempLocation = `substitute "dbPoseWin.mel" $tempLocation ""`; string $blank = $tempLocation + "blankThumbnail.jpg"; string $filePath = `menuItem -q -label dbPoseFilenameMI`; string $poseFileName[] = `textScrollList -q -si poseFileListTSL`; string $thumbnailPath = $filePath + "/" + $poseFileName[0] + "/"; string $selectedPoses[] = `textScrollList -q -si poseListTSL`; string $thumbnail = ""; if($selectedPoses[0] != "") { $thumbnail = $thumbnailPath + $selectedPoses[0] + ".jpg"; } else return $blank; if(`file -q -exists $thumbnail`) return $thumbnail; else return $blank; } global proc dbLoadThumbnail() { string $filePath = `menuItem -q -label dbPoseFilenameMI`; string $poseFileName[] = `textScrollList -q -si poseFileListTSL`; string $thumbnailPath = $filePath + "/" + $poseFileName[0] + "/"; string $selectedPoses[] = `textScrollList -q -si poseListTSL`; string $thumbnail = ""; if($selectedPoses[0] != "") { $thumbnail = $thumbnailPath + $selectedPoses[0] + ".jpg"; } else error "No pose selected"; string $thumb = `fileDialog -dm "*.jpg"`; sysFile -makeDir $thumbnailPath; sysFile -copy $thumbnail $thumb; image -e -image $thumbnail thumbNailImage; } global proc dbMakeThumbnail() { string $filePath = `menuItem -q -label dbPoseFilenameMI`; string $poseFileName[] = `textScrollList -q -si poseFileListTSL`; string $thumbnailPath = $filePath + "/" + $poseFileName[0] + "/"; string $selectedPoses[] = `textScrollList -q -si poseListTSL`; string $thumbnail = ""; if($selectedPoses[0] != "") { $thumbnail = $thumbnailPath + $selectedPoses[0] + ".jpg"; } else error "No pose selected"; sysFile -makeDir $thumbnailPath; int $now = `currentTime -q`; setAttr "defaultRenderGlobals.imageFormat" 8; // turn off all HUDS source xltHUDdisplays.mel; int $showSettings[] = getHUDAttributes(1); int $hudSettings[] = getHUDAttributes(2); hideHUDAttributes; playblast -format image -cf $thumbnail -st $now -et $now -forceOverwrite -fp 0 -clearCache 1 -viewer 1 -showOrnaments 1 -percent 100 -widthHeight 180 125; // turn HUDS back on restoreHUDAttributes($showSettings, $hudSettings); image -e -image $thumbnail thumbNailImage; }