You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
o3de/Tools/maya/script/cryTools.mel

571 lines
18 KiB
Plaintext

/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
// Original file Copyright Crytek GMBH or its affiliates, used under license.
global proc cryToolsCreateCryExportNodeCloseWindow()
{
deleteUI -window CRYNEWEXPORTNODE_WINDOW;
}
global proc string cryToolsCreateCryExportNodeCreateCore( string $name, int $fileType, string $contents[] )
{
// check selection
for( $content in $contents )
{
string $type = `nodeType $content`;
if( $type != "transform" && $type != "joint" )
{
warning( "Selected node " + $content + " is not valid for exporting!" );
return "";
}
}
string $targetName = $contents[0];
// Create/Adjust group properly
// If there are multiple contents, need to create a group
string $groupPostfix = "_group";
if( size( $contents ) > 1 )
{
if( `confirmDialog -title "Lumberyard Exporter" -message "A group node will be created for selected nodes. Scene hierarchy will be modified."
-button "Confirm" -button "Cancel" -defaultButton "Confirm"
-cancelButton "Cancel" -dismissString "Cancel"` == "Confirm" )
{
$targetName = `group -empty -name ($name + $groupPostfix)`;
xform -objectSpace -pivots 0 0 0 $targetName;
// Add contents to group node
for( $item in $contents )
{
parent $item $targetName;
}
}
else
{
// User cancelled operation, do nothing and return
return "";
}
}
// Else if it's a group, attempt to add "_group" postfix if it lacks
else if (`nodeType $contents[0]` == "transform" && size(`listRelatives -type "mesh" -children $contents[0]`) == 0)
{
if ( !endsWith($contents[0], $groupPostfix) && !endsWith($contents[0], "_helper") )
{
string $message = "Renaming this group node with a `_group` postfix will allow a more predictable export. Would you like to rename this node to `" + $contents[0] + "_group`?";
string $action = `confirmDialog -title "Lumberyard Exporter" -message $message
-button "Rename" -button "Ignore" -button "Cancel" -defaultButton "Rename"
-cancelButton "Cancel" -dismissString "Cancel"`;
if ($action == "Rename")
{
$targetName = `rename $contents[0] ($contents[0] + $groupPostfix)`;
}
else if ($action == "Cancel")
{
// User cancels operation, do nothing and return
return "";
}
// If choose "Ignore", keep the original name
}
}
// Create LUMBERYARD_EXPORT_GROUP and LUMBERYARD_GEOMETRYEXPORT_GROUP if needed.
if( !`objExists LUMBERYARD_EXPORT_GROUP` )
{
// Create LUMBERYARD_EXPORT_GROUP
group -empty -name LUMBERYARD_EXPORT_GROUP;
}
if( !`objExists LUMBERYARD_GEOMETRYEXPORT_GROUP` )
{
// Create LUMBERYARD_GEOMETRYEXPORT_GROUP
group -empty -parent LUMBERYARD_EXPORT_GROUP -name LUMBERYARD_GEOMETRYEXPORT_GROUP;
}
// Create the new node and make sure it's transforms are zeroed
string $lumberyardExportNodePrefix = LumberyardGetExportNodeNamePrefix();
string $fullName = $lumberyardExportNodePrefix + $name;
$newExportNode = `group -empty -name $fullName -parent LUMBERYARD_GEOMETRYEXPORT_GROUP`;
xform -objectSpace -pivots 0 0 0 $newExportNode;
// Add attributes to all nodes
cryExportAddAttributes;
// Set file type
setAttr ($newExportNode+".fileType") ($fileType);
// Set export target.
string $longName[] = `ls -long $targetName`;
// Exporter plugin expects leading | stripped from long paths
$targetName = `substring $longName[0] 2 (size($longName[0]))`;
setAttr ($newExportNode+".exportTarget") -type "string" ($targetName);
cryExportUpdateExportLists;
return $newExportNode;
}
global proc cryToolsCreateCryExportNodeCreate()
{
if(`window -ex CRYNEWEXPORTNODE_WINDOW`)
{
// Create the new node and make sure it's transforms are zeroed
string $name = `textField -q -text CRYTOOLS_NEWEXPORTNODE_NAME`;
// Set the fileType. Subtract 1 as the optionMenu index is 1 based and the attr is 0 based.
int $fileType = `optionMenu -q -select CRYNEWEXPORTNODE_FILETYPE`;
$fileType -= 1;
int $groupSelection = `checkBox -q -value CRYNEWEXPORTNODE_GROUPSELECTION`;
string $selection[];
if( $groupSelection )
{
$selection = `ls -sl`;
}
cryToolsCreateCryExportNodeCreateCore $name $fileType $selection;
cryToolsCreateCryExportNodeCloseWindow;
}
}
global proc cryToolsCreateCryExportNode( int $quietMode )
{
string $newName = "New";
string $selection[] = `ls -sl`;
int $hasSelection = size($selection) > 0;
if( $hasSelection )
{
$newName = $selection[0];
}
if( $quietMode )
{
int $fileType = 0;
string $selection[] = `ls -sl`;
cryToolsCreateCryExportNodeCreateCore $newName $fileType $selection;
}
else if(!`window -ex CRYNEWEXPORTNODE_WINDOW`)
{
int $pluginLoaded = `cryPluginIsLoaded`;
if( !$pluginLoaded )
{
confirmDialog -title "Lumberyard Export" -message "The MayaCryExport plugin is not loaded." -button "OK";
return;
}
if(`windowPref -exists CRYNEWEXPORTNODE_WINDOW`)
{
windowPref -wh 400 150 -tlc `windowPref -q -topEdge CRYNEWEXPORTNODE_WINDOW` `windowPref -q -leftEdge CRYNEWEXPORTNODE_WINDOW` CRYNEWEXPORTNODE_WINDOW;
//windowPref -remove CRYNEWEXPORTNODE_WINDOW;
}
window -titleBar true -title "Create CryExportNode" -widthHeight 400 180 -sizeable false -mnb false -mxb false CRYNEWEXPORTNODE_WINDOW;
$layout1 = `formLayout -numberOfDivisions 100`;
{
columnLayout -adjustableColumn true -rowSpacing 10 CRYNEWEXPORTNODE_MAINCOL;
{
rowLayout -numberOfColumns 2 -columnAttach 1 "right" 5 -columnWidth 1 100 -columnWidth 2 200 CRYNEWEXPORTNODE_NAMEROW;
{
text -label "Name";
textField -text $newName CRYTOOLS_NEWEXPORTNODE_NAME;
}
setParent ..;
rowLayout -numberOfColumns 2 -columnAttach 1 "right" 5 -columnWidth 1 100 -columnWidth 2 200 CRYNEWEXPORTNODE_FILETYPEROW;
{
text -label "File Type";
optionMenu CRYNEWEXPORTNODE_FILETYPE;
{
string $enumString = `cryExportGetFileTypeEnumString`;
string $tokens[];
$numTokens = `tokenize $enumString ":" $tokens`;
for( $i = 0;$i<$numTokens;$i++ )
{
menuItem -label $tokens[$i];
}
}
}
setParent ..;
rowLayout -numberOfColumns 2 -columnAttach 1 "right" 5 -columnWidth 1 100 -columnWidth 2 200 CRYNEWEXPORTNODE_GROUPSELECTIONROW;
{
text "";
string $annotation = "Moves the currently selected items into the new export node.";
checkBox -annotation $annotation -value $hasSelection -label "Group Selection" CRYNEWEXPORTNODE_GROUPSELECTION;
}
setParent ..;
rowLayout -numberOfColumns 2 -columnAttach 1 "right" 5 -columnWidth 1 100 -columnWidth 2 200 CRYNEWEXPORTNODE_CREATEGROUPNODEROW;
{
text "";
string $annotation = "Create a `_group` node and put it under the new export node. If you also have `Group Selection` checked the selected items will be put under the `_group` node.";
checkBox -annotation $annotation -value true -label "Create `_group` node" CRYNEWEXPORTNODE_CREATEGROUPNODE;
}
setParent ..;
}
setParent ..;
}
$createbutton = `button -label "Create" -command ("cryToolsCreateCryExportNodeCreate")`;
$closebutton = `button -label "Close" -command ("cryToolsCreateCryExportNodeCloseWindow")`;
setParent ..;
formLayout -edit
-attachForm CRYNEWEXPORTNODE_MAINCOL "top" 15
-attachForm CRYNEWEXPORTNODE_MAINCOL "left" 5
-attachForm CRYNEWEXPORTNODE_MAINCOL "right" 5
-attachControl CRYNEWEXPORTNODE_MAINCOL "bottom" 5 $closebutton
-attachForm $createbutton "bottom" 5
-attachForm $createbutton "left" 5
-attachPosition $createbutton "right" 5 50
-attachNone $createbutton "top"
-attachForm $closebutton "bottom" 5
-attachPosition $closebutton "left" 5 50
-attachForm $closebutton "right" 5
-attachNone $closebutton "top"
$layout1;
showWindow CRYNEWEXPORTNODE_WINDOW;
}
else
{
showWindow CRYNEWEXPORTNODE_WINDOW;
}
}
global proc cryToolsCreateMultipleCryExportNodes()
{
string $result = `confirmDialog -title "Lumberyard Tools" -message "Create an export node and group for each of the selected nodes?" -button "Yes" -button "Cancel" -defaultButton "Yes" -cancelButton "Cancel" -dismissString "Cancel"`;
if( $result != "Yes" )
return;
string $newName = "New";
int $fileType = 0;
string $selection[] = `ls -sl`;
string $newExportNodes[];
for( $item in $selection )
{
string $itemArray[];
$itemArray[0] = $item;
string $newName = $item;
string $newExportNode = `cryToolsCreateCryExportNodeCreateCore $newName $fileType $itemArray`;
$newExportNodes[size($newExportNodes)] = $newExportNode;
}
select -cl;
for( $node in $newExportNodes )
{
select -add $node;
}
}
global proc cryToolsCreateCryHelperNode()
{
string $newNode = `createNode cryHelperNode`;
string $newParent = firstParentOf($newNode);
rename $newNode "cryHelperShape#";
rename $newParent "cryHelper#";
}
global proc cryToolsFixCameraPivots()
{
string $cameras[] = `ls -l -sl -type camera`;
if( size($cameras) == 0 )
{
string $message = "This will attempt to reset the pivots for all cameras in the scene.";
string $result = `confirmDialog -title "Lumberyard Tools" -message $message -button "Ok" -button "Cancel" -defaultButton "Ok" -cancelButton "Cancel" -dismissString "Cancel"`;
if( $result != "Ok" )
return;
$cameras = `ls -l -type camera`;
}
for( $camera in $cameras )
{
string $camParent = firstParentOf($camera);
if( `nodeType $camParent` == "transform" )
{
vector $rotPivTrans = `getAttr ($camParent+".rotatePivotTranslate")`;
catchQuiet( `setAttr ($camParent+".rotatePivotTranslate") 0 0 0` );
catchQuiet( `setAttr ($camParent+".scalePivotTranslate") 0 0 0` );
catchQuiet( `setAttr ($camParent+".rotatePivot") 0 0 0` );
catchQuiet( `setAttr ($camParent+".scalePivot") 0 0 0` );
catchQuiet( `xform -r -translation ($rotPivTrans.x) ($rotPivTrans.y) ($rotPivTrans.z) $camParent` );
}
}
}
// Convert units and remove the postfixes.
proc float cryToolsConvertUnit( float $inVal, string $from, string $to )
{
string $floatStr = `convertUnit -f $from -t $to (""+$inVal)`;
$floatStr = `substitute "m" $floatStr ""`;
$floatStr = `substitute "rad" $floatStr ""`;
float $f = $floatStr;
return $f;
}
global proc cryOutputAttachmentHelpers()
{
print("======== Begin Attachment Helpers ========\n");
string $selected[] = `ls -sl`;
for( $item in $selected )
{
string $useItem = "";
if( `objectType $item` == "locator" )
{
string $parents[] = `listRelatives -parent $item`;
if( size($parents) > 0 )
{
$count = stringArrayContains($parents[0], $selected);
if( $count == 0 ) // Dont process this locator if the parent node is alos in the selected list.
$useItem = $parents[0];
}
}
else
{
string $children[] = `listRelatives -children $item`;
for( $child in $children )
{
if( `objectType $child` == "locator" )
{
$useItem = $item;
break;
}
}
}
string $currentLinearUnit = `currentUnit -q -fullName -linear`;
string $currentAngleUnit = `currentUnit -q -fullName -angle`;
if( size($useItem) > 0 && `objectType $useItem` == "transform" )
{
float $trans[] = `xform -q -translation $useItem`;
float $rot[] = `xform -q -rotation $useItem`;
$trans[0] = `cryToolsConvertUnit $trans[0] $currentLinearUnit "meter"`;
$trans[1] = `cryToolsConvertUnit $trans[1] $currentLinearUnit "meter"`;
$trans[2] = `cryToolsConvertUnit $trans[2] $currentLinearUnit "meter"`;
$rot[0] = `cryToolsConvertUnit $rot[0] $currentAngleUnit "radian"`;
$rot[1] = `cryToolsConvertUnit $rot[1] $currentAngleUnit "radian"`;
$rot[2] = `cryToolsConvertUnit $rot[2] $currentAngleUnit "radian"`;
string $quatStr = `cryMayaSupportPlugin convertToQuat $rot[0] $rot[1] $rot[2]`;
string $tokens[];
$numTokens = `tokenize $quatStr " " $tokens`;
if( $numTokens == 4 )
{
float $quat[];
$quat[0] = $tokens[0];
$quat[1] = $tokens[1];
$quat[2] = $tokens[2];
$quat[3] = $tokens[3];
string $output = (" <Attachment AName=\"" + $useItem + "\"");
$output += (" Type=\"CA_BONE\"");
$output += (" Rotation=\"" + $quat[3] + "," + $quat[0] + "," + $quat[1] + "," + $quat[2] + "\"");
$output += (" Position=\"" + $trans[0] + "," + $trans[1] + "," + $trans[2] + "\"");
$output += (" BoneName=\"bone_" + $useItem + "\"");
$output += (" Binding=\"\"");
$output += (" Flags=\"0\"/>");
print ($output + "\n");
}
}
}
print("======== End Attachment Helpers ========\n");
}
//////////////////////////////////////////////////////////////////////
// Cry Tools Menu
//////////////////////////////////////////////////////////////////////
global proc cryToolsRemoveMenu()
{
if( `control -q -exists CRYTOOLS_MENU` )
{
deleteUI -menu CRYTOOLS_MENU;
}
}
proc createCryToolsMenu()
{
// Remove the menu first
cryToolsRemoveMenu;
global string $gMainWindow;
setParent $gMainWindow;
menu -l "Lumberyard Tools" -p MayaWindow -to 1 -aob true CRYTOOLS_MENU;
menuItem -label "Create LumberyardExportNode" -command "cryToolsCreateCryExportNode 0";
menuItem -label "Create CryHelperNode" -command "cryToolsCreateCryHelperNode";
menuItem -divider true;
menuItem -label "Zero Joint Rotations" -command "cryAnimZeroJointRotation";
menuItem -label "Anim Manager" -command "cryAnimManagerWin";
menuItem -label "Output Attachment Helpers" -command "cryOutputAttachmentHelpers";
menuItem -divider true;
menuItem -label "Load Plug-in" -command "cryLoadPlugin";
menuItem -label "Unload Plug-in" -command "cryUnloadPlugin";
menuItem -divider true;
menuItem -label "Reset Camera Pivots" -command "cryToolsFixCameraPivots";
}
//////////////////////////////////////////////////////////////////////
// Cry Tools UI
//////////////////////////////////////////////////////////////////////
global proc cryToolsUICloseWindow()
{
deleteUI -window CRYTOOLS_WINDOW;
}
proc createCryToolsWindow()
{
if(!`window -ex CRYTOOLS_WINDOW`)
{
if(`windowPref -exists CRYTOOLS_WINDOW`)
{
windowPref -wh 200 480 -tlc `windowPref -q -topEdge CRYTOOLS_WINDOW` `windowPref -q -leftEdge CRYTOOLS_WINDOW` CRYTOOLS_WINDOW;
//windowPref -remove CRYTOOLS_WINDOW;
}
window -titleBar true -title "Lumberyard Tools" -widthHeight 200 480 -sizeable true -mnb false -mxb false CRYTOOLS_WINDOW;
$layout1 = `formLayout -numberOfDivisions 100`;
{
columnLayout -adjustableColumn true -rowSpacing 15 CRYTOOLS_MAINCOL;
{
frameLayout -label "Node Tools" -borderStyle "out" -collapsable true -marginHeight 5 -marginWidth 5;
{
columnLayout -adjustableColumn true -rowSpacing 5;
{
button -label "Create LumberyardExportNode" -command "cryToolsCreateCryExportNode 0";
button -label "Create Multiple LumberyardExportNodes" -command "cryToolsCreateMultipleCryExportNodes";
button -label "Create CryHelperNode" -command "cryToolsCreateCryHelperNode";
}
setParent ..;
}
setParent ..;
frameLayout -label "Animation Tools" -borderStyle "out" -collapsable true -marginHeight 5 -marginWidth 5;
{
columnLayout -adjustableColumn true -rowSpacing 5;
{
button -label "Zero Joint Rotations" -command "cryAnimZeroJointRotation";
button -label "Anim Manager" -command "cryAnimManagerWin";
button -label "Output Attachment Helpers" -command "cryOutputAttachmentHelpers";
}
setParent ..;
}
setParent ..;
frameLayout -label "Plug-in Tools" -borderStyle "out" -collapsable true -marginHeight 5 -marginWidth 5;
{
columnLayout -adjustableColumn true -rowSpacing 5;
{
button -label "Load Plug-in" -command "cryLoadPlugin";
button -label "Unload Plug-in" -command "cryUnloadPlugin";
}
setParent ..;
}
setParent ..;
frameLayout -label "Helpers" -borderStyle "out" -collapsable true -marginHeight 5 -marginWidth 5;
{
columnLayout -adjustableColumn true -rowSpacing 5;
{
button -label "Reset Camera Pivots" -command "cryToolsFixCameraPivots";
}
setParent ..;
}
setParent ..;
}
setParent ..;
}
$closebutton = `button -label "Close" -command ("cryToolsUICloseWindow")`;
setParent ..;
formLayout -edit
-attachForm CRYTOOLS_MAINCOL "top" 5
-attachForm CRYTOOLS_MAINCOL "left" 5
-attachForm CRYTOOLS_MAINCOL "right" 5
-attachControl CRYTOOLS_MAINCOL "bottom" 5 $closebutton
-attachForm $closebutton "bottom" 5
-attachForm $closebutton "left" 5
-attachForm $closebutton "right" 5
-attachNone $closebutton "top"
$layout1;
}
showWindow CRYTOOLS_WINDOW;
}
//////////////////////////////////////////////////////////////////////
// Window and menu creation entry functions.
//////////////////////////////////////////////////////////////////////
proc cryToolsSourceDependencies()
{
eval("source cryExport.mel");
eval("source cryAnim.mel");
eval("source LumberyardUtilities.mel");
//eval("source cryExportSettings.mel");
}
global proc cryToolsWin()
{
cryToolsSourceDependencies;
createCryToolsWindow;
}
global proc cryToolsMenu()
{
cryToolsSourceDependencies;
createCryToolsMenu;
}
global proc cryToolsMenuRemove()
{
cryToolsRemoveMenu;
}
//////////////////////////////////////////////////////////////////////
// Shelf functions.
//////////////////////////////////////////////////////////////////////
global proc string cryToolsGetShelfNameFromScriptName( string $scriptName )
{
string $shelfName = $scriptName;
string $tokens[];
$numTokens = `tokenize $scriptName "_." $tokens`;
if( $numTokens == 3 )
{
$shelfName = $tokens[1];
}
return $shelfName;
}
global proc cryToolsLoadShelf( string $scriptName )
{
string $shelfName = `cryToolsGetShelfNameFromScriptName $scriptName`;
if (`shelfLayout -exists $shelfName`)
{
deleteUI $shelfName;
}
evalDeferred( "loadNewShelf "+$scriptName );
}