// =========================================================================== // Copyright 2018 Autodesk, Inc. All rights reserved. // // Use of this software is subject to the terms of the Autodesk license // agreement provided at the time of installation or download, or which // otherwise accompanies this software in either electronic or hard copy form. // =========================================================================== global proc bakeCustomToolPivot(int $pos, int $ori) { // Must have an object(s) selected string $objects[] = `ls -sl -transforms`; string $shapes[] = `ls -sl -shapes`; if (size($shapes) > 0) { string $transforms[] = `listRelatives -path -parent -type transform`; appendStringArray($objects, $transforms, size($transforms)); } if (size($objects) == 0) { error((uiRes("m_bakeCustomToolPivot.kNoObjectsSelectedError"))); return; } float $customOri[]; int $otherToolActive = 0; int $pivotModeActive = 0; int $customModeActive = 0; string $currentCtx = `currentCtx`; if ($currentCtx == "moveSuperContext" || $currentCtx == "manipMoveContext") { // Move tool $customOri = `manipMoveContext -q -orientAxes Move`; $pivotModeActive = `manipMoveContext -q -editPivotMode Move`; $customModeActive = (`manipMoveContext -q -mode Move` == /*custom*/6); } else if ($currentCtx == "RotateSuperContext" || $currentCtx == "manipRotateContext") { // Rotate tool $customOri = `manipRotateContext -q -orientAxes Rotate`; $pivotModeActive = `manipRotateContext -q -editPivotMode Rotate`; $customModeActive = (`manipRotateContext -q -mode Rotate` == /*custom*/3); } else if ($currentCtx == "scaleSuperContext" || $currentCtx == "manipScaleContext") { // Scale tool $customOri = `manipScaleContext -q -orientAxes Scale`; $pivotModeActive = `manipScaleContext -q -editPivotMode Scale`; $customModeActive = (`manipScaleContext -q -mode Scale` == /*custom*/6); } else { // Some other tool - use the move tool orientation $customOri = `manipMoveContext -q -orientAxes Move`; $pivotModeActive = `manipMoveContext -q -editPivotMode Move`; $customModeActive = (`manipMoveContext -q -mode Move` == /*custom*/6); $otherToolActive = 1; } if ($ori && !$pos && !$customModeActive) { if ($otherToolActive) { error((uiRes("m_bakeCustomToolPivot.kWrongAxisOriToolError"))); } else { error((uiRes("m_bakeCustomToolPivot.kWrongAxisOriModeError"))); } return; } // Get custom orientation if ($ori && $customModeActive) { $customOri[0] = rad_to_deg($customOri[0]); $customOri[1] = rad_to_deg($customOri[1]); $customOri[2] = rad_to_deg($customOri[2]); // Set object(s) rotation to the custom one (preserving child transform positions and geometry positions) rotate -a -pcp -pgp -ws -fo $customOri[0] $customOri[1] $customOri[2] $objects; } if ($pos) { for ($object in $objects) { // Get pivot in parent space float $old[3]; float $m[] = `xform -q -m $object`; float $p[] = `xform -q -os -sp $object`; $old[0] = ($p[0]*$m[0] + $p[1]*$m[4]+ $p[2]*$m[8] + $m[12]); $old[1] = ($p[0]*$m[1] + $p[1]*$m[5]+ $p[2]*$m[9] + $m[13]); $old[2] = ($p[0]*$m[2] + $p[1]*$m[6]+ $p[2]*$m[10] + $m[14]); // Zero out pivots xform -zeroTransformPivots $object; // Translate object(s) back to previous pivot (preserving child transform positions and geometry positions) float $new[] = getAttr($object + ".translate"); move -pcp -pgp -ls -r ($old[0]-$new[0]) ($old[1]-$new[1]) ($old[2]-$new[2]) $object; } } // Exit pivot mode if ($pivotModeActive) ctxEditMode; // Set the axis orientation mode back to object if ($ori && $customModeActive) { if ($currentCtx == "moveSuperContext" || $currentCtx == "manipMoveContext") { manipPivot -moveToolOri 0; } else if ($currentCtx == "RotateSuperContext" || $currentCtx == "manipRotateContext") { manipPivot -rotateToolOri 0; } else if ($currentCtx == "scaleSuperContext" || $currentCtx == "manipScaleContext") { manipPivot -scaleToolOri 0; } else { // Some other tool // Set move tool to object mode and clear the custom ori. (so the tool won't restore it when activated) manipPivot -moveToolOri 0; manipPivot -ro; } } }