///////////////////////////////////////////////////////////////////////////// // uvhero.mel by Andrew Osiow Copyright 2008 // Version 1.5 // // Gui interface to select polymesh faces based on the angle between the face normal // and the cameraview normal. It might be a little unpredictable under certain // circumstances with mulitple objects but can used repeatedly on the same object. // // With thanks to Mike Bailey (a former student) for the idea and an early gui layout // // v1.0 could choose either front or back faces with angle of tolerance // v1.1 adds the ability to choose both or neither (not) // v1.2 adds UV Mapping function the user optional try. // Options are None, Planar, Cylinder and Spherical // The projects are all based on the current camera // v1.3 adds a button to automatically select the camera from the current active viewport // adds a option to remap without reselecting // v1,4 corrects for different shape space - ie shapes that don't have zeroed transforms // v1.5 correctly orientate Spherical and Cylindrical projections based on the camera's view vector // Tested with either Y or Z up orientation // now displays which Axis the current Camera is looking down // renamed to UV Hero // // ToDo : Cylinder and Spherical Projection Maps don't automatically align properly to a Persp camera, // but can still be set manually ///////////////////////////////////////////////////////////////////////////// global proc uvhero() { // delete saved Window Pref if (`windowPref -ex fnsWindow`) windowPref -remove fnsWindow; // create a window structure if (`window -ex fnsWindow`) deleteUI fnsWindow; string $fnsWin = `window -w 20 -h 10 // a small window will stretch to fit gadgets in layout mode -title "UV Hero 1.5" -iconName "UVH" -rtf 1 -s 0 // resize to fit - no sizing gadget fnsWindow`; string $fnsLayout = `formLayout -numberOfDivisions 100`; // create a Form Layout with a 100 grid point // Controls // adding option menu for cameras in the scene string $allCameras[] = `listCameras`; string $cameraOption = `optionMenu -label "Camera" -cc "updateCameraDirection" camOpField`; for ($currentCamera in $allCameras) menuItem -label $currentCamera; setParent ..; // add text top annotate view axis text -width 20 -height 16 -label ("["+`grabAxis` +"]") viewAxisText; // add button to set camera fom current view button -width 90 -height 26 -label "Active View" -align "center" -command "grabActiveCamera" activeViewButton; intSliderGrp -label "Angle" -field true -minValue -0 -maxValue 90 -fieldMinValue -0 -fieldMaxValue 90 -value 0 -ct3 "left" "left" "left" -cw3 40 50 30 faceAngleSlider; radioButtonGrp -numberOfRadioButtons 4 -label "Facing" -labelArray4 "Front" "Back" "Both" "Not" -sl 1 -ct5 "left" "left" "left" "left" "left" -cw5 40 50 50 50 50 faceAngleRadio; string $projectOption = `optionMenu -label "UV Map" camProjField`; menuItem -label "None"; menuItem -label "Planar"; menuItem -label "Cylindrical"; menuItem -label "Spherical"; setParent ..; // add remap button that reapplys uv mapping without updating selection button -width 90 -height 26 -label "Remap" -align "center" -command "fnsUVremap" uvRemapButton; // Gui Buttons button -width 90 -height 26 -label "Select" -align "center" -command "selectFacebyCameraViewAngle(`optionMenu -q -v camOpField`,`intSliderGrp -q -v faceAngleSlider`,`radioButtonGrp -q -sl faceAngleRadio`); deleteUI fnsWindow " selectButton; // select and closes button -width 90 -height 26 -label "Apply" -command "selectFacebyCameraViewAngle(`optionMenu -q -v camOpField`,`intSliderGrp -q -v faceAngleSlider`,`radioButtonGrp -q -sl faceAngleRadio`)" applyButton; // select button -width 90 -height 26 -label "Close" -command "deleteUI fnsWindow" closeButton; // closes interface // arrange Gui formLayout -e // -af positions a gadget relative to the edge of the form // -ac positions a gadget relative to another gadget // layout fields -af "camOpField" "left" 12 -af "camOpField" "top" 12 -ac "viewAxisText" "left" 4 "camOpField" -af "viewAxisText" "top" 14 -ac "activeViewButton" "left" 26 "camOpField" -af "activeViewButton" "top" 10 -af "faceAngleSlider" "left" 12 -ac "faceAngleSlider" "top" 6 "camOpField" -af "faceAngleRadio" "left" 12 -ac "faceAngleRadio" "top" 10 "faceAngleSlider" -af "camProjField" "left" 12 -ac "camProjField" "top" 10 "faceAngleRadio" -ac "uvRemapButton" "left" 8 "camProjField" -ac "uvRemapButton" "top" 8 "faceAngleRadio" // layout buttons -ac "selectButton" "top" 12 "camProjField" -af "selectButton" "left" 4 -af "selectButton" "bottom" 4 -ac "applyButton" "top" 12 "camProjField" -ac "applyButton" "left" 4 "selectButton" -af "applyButton" "bottom" 4 -ac "closeButton" "top" 12 "camProjField" -ac "closeButton" "left" 4 "applyButton" -af "closeButton" "bottom" 4 -af "closeButton" "right" 4 $fnsLayout; showWindow $fnsWin; } global proc updateCameraDirection () { // set the camera Direction Text based on the current Camera text -e -l ("["+`grabAxis`+"]") viewAxisText; } global proc grabActiveCamera () { // sets the camera gadget to the name of camera linking to the current active viewport string $currentPane = `getPanel -wf`; string $currentCamera = `modelEditor -q -camera $currentPane`; optionMenu -e -v $currentCamera camOpField; } global proc string grabAxis () {// with the cardinal axis of a predefined camera for both Y and Z up string $currentCamera = `optionMenu -q -v camOpField`; if (`upAxis -q -axis` == "y") switch ($currentCamera) { case "front" : $viewAxis = "Z"; break; case "side" : $viewAxis = "X"; break; case "top" : $viewAxis = "Y"; break; default : $viewAxis = "P"; break; } else switch ($currentCamera) { case "front" : $viewAxis = "Y"; break; case "side" : $viewAxis = "X"; break; case "top" : $viewAxis = "Z"; break; default : $viewAxis = "P"; break; }; return $viewAxis; } global proc fnsUVremap (string $currentCamera) { // remaps the current selection based on curent setings if (`optionMenu -q -v camProjField`!="None") { string $curProj = `optionMenu -q -v camProjField`; string $curOp = `optionMenu -q -v camOpField`; string $mapDir; switch ($curOp) { case "front" : $mapDir = "z"; break; case "side" : $mapDir = "x"; break; case "top" : $mapDir = "y"; break; default : $mapDir = "b"; break; } string $newProj[] = `polyProjection -ch 1 -type $curProj -md $mapDir -sf 1`; select -addFirst $newProj; if ($curProj!="Planar" ) { // aligns Project to match camera view // need work camera setAttr ($newProj[0]+".rotateX") (90 - `getAttr ($currentCamera+".rotateX")`); setAttr ($newProj[0]+".rotateY") (-`getAttr ($currentCamera+".rotateY")`); setAttr ($newProj[0]+".projectionHorizontalSweep") 360; if ($curProj=="Spherical" ) setAttr ($newProj[0]+".projectionVerticalSweep") 180; } } } global proc selectFacebyCameraViewAngle (string $camera, int $angle, int $choice) {// selects the faces of current selected or highlighted objects // based on the angle threshold to, (or away from) the camera view vector // select hilited objects string $highlightedObjects[] = `ls -hl`; string $selectedObjects[] = `ls -sl -tr`; hilite -u $highlightedObjects; // select hilited objects select -r $highlightedObjects; // add non hilited objects that were selected select -add $selectedObjects; // grab new selection $selectedObjects = `ls -sl`; // calculate camera View Normal $viewVector = {0.0,0.0,-1.0}; $viewVector = pointMatrixMult ($viewVector,`getAttr ($camera+"Shape.worldMatrix")`); select $selectedObjects; // necessary because of bug with pointMatrixMult string $polyFaceInfo[]; float $polyFaceValue[]; for ($currentSelection in $selectedObjects) { $PolyFaceInfo = `polyInfo -fn`; string $currentFace; for ($i=0;$i 2 ) $facingAngle = abs($facingAngle); // both Face and Not if ($choice <4) if (cosd($angle)<=$facingAngle) {//select Face hilite -r $currentSelection; string $faceNumber = substring($faceNormalData[1],1,size($faceNormalData[1])-1); select -add ($currentSelection+".f["+$faceNumber+"]"); } else {} else if (cosd($angle)>$facingAngle) {//select Face hilite -r $currentSelection; string $faceNumber = substring($faceNormalData[1],1,size($faceNormalData[1])-1); select -add ($currentSelection+".f["+$faceNumber+"]"); } } } // map or remap UVs - if selected fnsUVremap($camera); }