/* * 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 string cryMaterialGetPartitionName() { return "cryMaterials"; } global proc string cryMaterialGetDefaultGroupName() { return "newMaterialGroup1"; } global proc string cryMaterialCreateGroup( string $name ) { string $partitionName = `cryMaterialGetPartitionName`; if( !`objExists $partitionName` ) { partition -name $partitionName; } int $defaultName = 0; if( `size($name)` == 0 ) { $name = `cryMaterialGetDefaultGroupName`; $defaultName = 1; } $newSet = `sets -empty -name $name`; if( $defaultName == 0 && $newSet != $name ) { confirmDialog -title "Create Group" -message ("The group was renamed from `" + $name + "` to `" + $newSet + "` due to a name clash.") -button "OK"; } partition -add $partitionName $newSet; return $newSet; } global proc string cryMaterialCreateShader( string $name ) { string $newShader; $newShader = `shadingNode -asShader phong`; string $renamed = `rename $newShader $name`; return $renamed; } global proc string[] cryMaterialGetMaterialGroups(string $partitionName) { string $materialGroups[]; if( `objExists $partitionName` ) { if( `objectType -isType "partition" $partitionName` ) { $materialGroups = `partition -q $partitionName`; } } return $materialGroups; } global proc string[] cryMaterialGetGroupShaders( string $group) { // It seems that the sets command dosen't always return the list in the right order. // The shaders should be connected to the dnSetMembers plug. // List connections seems to get them in the right order. string $contents[]; $contents = `listConnections ($group+".dnSetMembers")`; //$contents = `sets -q -nodesOnly $group`; return $contents; } global proc cryMaterialRebuildGroup( string $group ) { // Rebuild the set's connections to remove any gaps in the dnSetMembers array string $contents[]; $contents = `cryMaterialGetGroupShaders $group`; // Remove all connections for( $shader in $contents ) { string $dests[]; $dests = `connectionInfo -destinationFromSource ($shader+".message")`; for( $dest in $dests ) { if( `gmatch ($dest) ($group+".dnSetMembers*")` ) disconnectAttr ($shader+".message") ($dest); } } // Recreate connections int $connectionCount = 0; for( $shader in $contents ) { connectAttr ($shader+".message") ($group+".dnSetMembers["+$connectionCount+"]"); $connectionCount++; } } global proc cryMaterialDeleteGroup( string $group ) { delete $group; } global proc cryMaterialMoveShaderToGroupWithPartition( string $group, string $shader, string $partitionName ) { // If it's already a member of this group just return. if( `sets -isMember $group $shader` ) { return; } // The forceElement option on the sets command dosn't seem to be working so we have // to remove the shader from any other groups. string $materialGroups[]; $materialGroups = `cryMaterialGetMaterialGroups $partitionName`; int $doAdd = 1; for( $materialGroup in $materialGroups ) { if( `sets -isMember $materialGroup $shader` ) { string $result = `confirmDialog -title "Move Shader" -message ("Shader `"+$shader+"` is already in group `"+$materialGroup+"`. Do you want to move it to group `"+$group+"`?") -button "Yes" -button "No" -defaultButton "Yes" -cancelButton "No" -dismissString "No"`; if( $result == "Yes" ) { string $shaders[]; $shaders[0] = $shader; cryMaterialRemoveShadersFromGroup $materialGroup $shaders; } else { $doAdd = 0; } } } if( $doAdd == 1 ) { print("Add shader to group `"+$shader+"` -> `"+$group+"`\n"); sets -forceElement $group $shader; cryMaterialRebuildGroup $group; } } global proc cryMaterialMoveShaderToGroup( string $group, string $shader ) { cryMaterialMoveShaderToGroupWithPartition $group $shader `cryMaterialGetPartitionName`; } global proc cryMatrialAddSelectedShadersToGroup( string $group ) { if( `objExists $group` ) { string $selected[]; $selected = `ls -sl`; for( $object in $selected ) { if( `objectType -isType "lambert" $object` || `objectType -isType "phong" $object` || `objectType -isType "blinn" $object` || `objectType -isAType "hwShader" $object` ) { cryMaterialMoveShaderToGroup $group $object; } } cryMaterialRebuildGroup $group; } } global proc cryMaterialAddShadersFromGeomToGroupRecurse( string $group, string $node, string $partitionName ) { string $shaderEngines[] = `listConnections -type shadingEngine $node`; for( $se in $shaderEngines ) { string $shaders[] = `listConnections -type lambert $se`; for( $shader in $shaders ) { cryMaterialMoveShaderToGroupWithPartition $group $shader $partitionName; } string $hwShaders[] = `listConnections -type hwShader $se`; for( $hwShader in $hwShaders ) { cryMaterialMoveShaderToGroupWithPartition $group $hwShader $partitionName; } } string $children[]; string $child; $children = `listRelatives -children -fullPath $node`; for ($child in $children) { cryMaterialAddShadersFromGeomToGroupRecurse $group $child $partitionName; } } global proc cryMaterialAddShadersFromGeomToGroup( string $group ) { if( `objExists $group` ) { string $selected[]; $selected = `ls -sl`; for( $rootNode in $selected ) { cryMaterialAddShadersFromGeomToGroupRecurse $group $rootNode `cryMaterialGetPartitionName`; } } } global proc cryMaterialRemoveShadersFromGroup( string $group, string $shaders[] ) { if( `objExists $group` ) { for( $shader in $shaders ) { if( `sets -isMember $group $shader` ) { print("Remove shader from group `"+$shader+"` <- `"+$group+"`\n"); sets -edit -remove $group $shader; } } cryMaterialRebuildGroup $group; } } global proc cryMaterialMoveShadersUp( string $group, string $inShaders[] ) { // Rebuild the group to make sure the content index and the connection index match cryMaterialRebuildGroup $group; string $shaders[]; $shaders = `cryMaterialGetGroupShaders $group`; // Sort shaders array string $sortedInShaders[]; for( $shader in $shaders ) { for( $inShader in $inShaders ) { if( $inShader == $shader ) $sortedInShaders[`size($sortedInShaders)`] = $shader; } } if( `size($sortedInShaders)` != `size($inShaders)` ) print("Error sorting shaders\n"); int $numInShaders = `size($sortedInShaders)`; int $numShaders = `size($shaders)`; for( $i = 0;$i<$numInShaders;$i++ ) { int $swapped = 0; string $inShader = $inShaders[$i]; for( $j = 1;$j<$numShaders;$j++ ) { string $shader = $shaders[$j]; if( $inShader == $shader ) { string $shaderM1 = $shaders[$j-1]; if( !`stringArrayContains $shaderM1 $sortedInShaders` ) { // Swap $shader and $shaderM1 disconnectAttr ($shader+".message") ($group+".dnSetMembers["+$j+"]"); disconnectAttr ($shaderM1+".message") ($group+".dnSetMembers["+($j-1)+"]"); connectAttr ($shader+".message") ($group+".dnSetMembers["+($j-1)+"]"); connectAttr ($shaderM1+".message") ($group+".dnSetMembers["+$j+"]"); $swapped = 1; break; } } } if( $swapped ) { $shaders = `cryMaterialGetGroupShaders $group`; } } // Rebuild again though it shouldn't be needed cryMaterialRebuildGroup $group; } global proc cryMaterialMoveShadersDown( string $group, string $inShaders[] ) { // Rebuild the group to make sure the content index and the connection index match cryMaterialRebuildGroup $group; string $shaders[]; $shaders = `cryMaterialGetGroupShaders $group`; // Sort shaders array string $sortedInShaders[]; for( $shader in $shaders ) { for( $inShader in $inShaders ) { if( $inShader == $shader ) $sortedInShaders[`size($sortedInShaders)`] = $shader; } } if( `size($sortedInShaders)` != `size($inShaders)` ) print("Error sorting shaders\n"); int $numInShaders = `size($sortedInShaders)`; int $numShaders = `size($shaders)`; for( $i = $numInShaders-1;$i>=0;$i-- ) { int $swapped = 0; string $inShader = $inShaders[$i]; for( $j = $numShaders-2;$j>=0;$j-- ) { string $shader = $shaders[$j]; if( $inShader == $shader ) { string $shaderP1 = $shaders[$j+1]; if( !`stringArrayContains $shaderP1 $sortedInShaders` ) { // Swap $shader and $shaderM1 disconnectAttr ($shader+".message") ($group+".dnSetMembers["+$j+"]"); disconnectAttr ($shaderP1+".message") ($group+".dnSetMembers["+($j+1)+"]"); connectAttr ($shader+".message") ($group+".dnSetMembers["+($j+1)+"]"); connectAttr ($shaderP1+".message") ($group+".dnSetMembers["+$j+"]"); $swapped = 1; break; } } } if( $swapped ) { $shaders = `cryMaterialGetGroupShaders $group`; } } // Rebuild again though it shouldn't be needed cryMaterialRebuildGroup $group; } ////////////////////////////////////////////////////////////////////// // Cry Material UI ////////////////////////////////////////////////////////////////////// global proc cryMaterialUICloseWindow() { deleteUI -window CRYMATERIAL_WINDOW; } global proc string cryMaterialUIShaderNameEncode( string $group, string $shader ) { string $contents[]; $contents = `cryMaterialGetGroupShaders $group`; int $shaderIndex = 0; for( $i = 0;$i 0 ) { for( $selectedNode in $selectedNodes ) { string $lowerNode = tolower( $selectedNode ); if( startsWith( $lowerNode, $lumberyardExportNodePrefix ) ) { int $start = size($lumberyardExportNodePrefix)+1; int $end = size($selectedNode); $name = `substring $selectedNode $start $end`; break; } } } string $result = `promptDialog -title "Create Group" -message "Enter group name:" -text $name -button "OK" -button "Cancel" -defaultButton "OK" -cancelButton "Cancel" -dismissString "Cancel"`; if ($result == "OK") { $name = `promptDialog -query -text`; if( `size($name)` == 0 ) $name = `cryMaterialGetDefaultGroupName`; string $newGroup = `cryMaterialCreateGroup $name`; cryMaterialAddShadersFromGeomToGroup $newGroup; cryMaterialUIRebuild; } } global proc cryMaterialUIReadMaterial() { if( `cryPluginIsLoaded` == 0 ) { confirmDialog -title "Read Material" -message ("Can't read a material as the Lumberyard plugin isn't loaded.") -button "OK"; } else { string $currentPath = `file -q -sceneName`; $currentPath = `dirname $currentPath`; string $startingFolder = $currentPath; string $absPath = `cryExportRelativeToAbsolutePath $startingFolder`; string $inFiles[] = `fileDialog2 -fileMode 1 -startingDirectory $absPath -fileFilter ("Material Files (*.mtl)")`; if( size($inFiles) > 0 ) { string $option1 = "readMaterial"; string $option2 = $inFiles[0]; if( catchQuiet( `cryMayaSupportPlugin $option1 $option2` ) ) { confirmDialog -title "Read Material" -message ("Failed to read material file `"+$inFiles[0]+"`.") -button "OK"; } cryMaterialUIRebuild; } } } global proc cryMaterialUIRenameGroup() { string $currentName = `cryMaterialUIGetSelectedGroup`; string $result = `promptDialog -title "Rename Group" -message "Enter new group name:" -text $currentName -button "OK" -button "Cancel" -defaultButton "OK" -cancelButton "Cancel" -dismissString "Cancel"`; if ($result == "OK") { string $name = `promptDialog -query -text`; if( `size($name)` != 0 ) { string $newName; $newName = `rename $currentName $name`; if( $newName != $name ) { confirmDialog -title "Rename Group" -message ("The group was renamed from `" + $name + "` to `" + $newName + "` due to a name clash.") -button "OK"; } cryMaterialUIRebuild; textScrollList -e -selectItem $name CRYMATERIAL_GROUPLIST; cryMaterialUIUpdateShaderList; } } } global proc cryMaterialUIRenameShader() { string $currentShaders[] = `cryMaterialUIGetSelectedShaders`; if( size($currentShaders) > 0 ) { string $currentName = $currentShaders[0]; string $result = `promptDialog -title "Rename Shader" -message "Enter new shader name:" -text $currentName -button "OK" -button "Cancel" -defaultButton "OK" -cancelButton "Cancel" -dismissString "Cancel"`; if ($result == "OK") { string $name = `promptDialog -query -text`; if( `size($name)` != 0 ) { rename $currentName $name; int $selectedItems[] = `textScrollList -q -selectIndexedItem CRYMATERIAL_SHADERLIST`; cryMaterialUIRebuild; cryMaterialUIUpdateShaderList; textScrollList -e -selectIndexedItem $selectedItems[0] CRYMATERIAL_SHADERLIST; } } } } global proc cryMaterialUIDeleteGroup( int $deleteAllGroups ) { string $answer = "Yes"; if( $deleteAllGroups ) $answer = `confirmDialog -title "Lumberyard Material" -message ("Delete all material groups?") -button "Yes" -button "No"`; if( $answer == "Yes" ) { string $selected[]; if( $deleteAllGroups ) $selected = `textScrollList -q -allItems CRYMATERIAL_GROUPLIST`; else $selected = `textScrollList -q -selectItem CRYMATERIAL_GROUPLIST`; for( $i = 0;$i 0 ) { select $currentShaders; cryMaterialUIOpenHyperShader; } else { confirmDialog -title "Material" -message ("No shaders are selected.") -button "OK"; } } global proc cryMaterialUIOpenGroupInHyperShade( ) { string $currentGroup = `cryMaterialUIGetSelectedGroup`; if( `size($currentGroup)` > 0 ) { select -r -ne $currentGroup; cryMaterialUIOpenHyperShader; } else { confirmDialog -title "Material" -message ("No group is selected.") -button "OK"; } } global proc cryMaterialUISelectShader() { string $currentShaders[] = `cryMaterialUIGetSelectedShaders`; if( `size($currentShaders)` == 0 ) { confirmDialog -title "Material" -message ("No shaders are selected.") -button "OK"; return; } if( `size($currentShaders)` > 1 ) { confirmDialog -title "Material" -message ("Too many shaders are selected. Select only 1.") -button "OK"; return; } select $currentShaders[0]; } global proc cryMaterialUICreateNewShader() { string $newShader = `cryMaterialCreateShader("newShader")`; string $currentGroup = `cryMaterialUIGetSelectedGroup`; cryMaterialMoveShaderToGroup $currentGroup $newShader; cryMaterialUIRebuild; } global proc cryMaterialUISelectObjectsWithMaterialGroup() { string $currentGroup = `cryMaterialUIGetSelectedGroup`; if( `size($currentGroup)` > 0 ) { string $groupShaders[] = `cryMaterialGetGroupShaders $currentGroup`; if( `size($groupShaders)` > 0 ) { select -r $groupShaders; hyperShade -objects ""; } else { confirmDialog -title "Material" -message ("The selected group has no shaders.") -button "OK"; } } else { confirmDialog -title "Material" -message ("No material group selected.") -button "OK"; } } global proc cryMaterialUISelectObjectsWithMaterial() { string $currentShaders[] = `cryMaterialUIGetSelectedShaders`; if( `size($currentShaders)` > 0 ) { select -r $currentShaders; hyperShade -objects ""; } else { confirmDialog -title "Material" -message ("No shaders are selected.") -button "OK"; } } global proc cryMaterialUIAssignMaterialToSelection() { string $currentShaders[] = `cryMaterialUIGetSelectedShaders`; if( `size($currentShaders)` == 0 ) { confirmDialog -title "Material" -message ("No shaders are selected.") -button "OK"; return; } if( `size($currentShaders)` > 1 ) { confirmDialog -title "Material" -message ("Too many shaders are selected. Select only 1.") -button "OK"; return; } hyperShade -assign $currentShaders[0]; } global proc cryMaterialUIOpenInAttributeEditor( ) { string $currentShaders[] = `cryMaterialUIGetSelectedShaders`; if( `size($currentShaders)` == 0 ) { confirmDialog -title "Material" -message ("No shaders are selected.") -button "OK"; return; } if( `size($currentShaders)` > 1 ) { confirmDialog -title "Material" -message ("Too many shaders are selected. Select only 1.") -button "OK"; return; } select $currentShaders[0]; autoUpdateAttrEd; openAEWindow; } global proc cryMaterialUIUpdateShaderList() { string $currentGroup = `cryMaterialUIGetSelectedGroup`; string $selected[]; $selected = `cryMaterialUIGetSelectedShaders`; textScrollList -e -removeAll CRYMATERIAL_SHADERLIST; if( `objExists $currentGroup` ) { string $contents[]; $contents = `cryMaterialGetGroupShaders $currentGroup`; for( $shader in $contents ) { string $shaderName = `cryMaterialUIShaderNameEncode $currentGroup $shader`; textScrollList -e -append $shaderName CRYMATERIAL_SHADERLIST; } string $newSelected[]; for( $item in $selected ) { $newSelected[size($newSelected)] = $item; } cryMaterialUIShaderListSelect $newSelected; } } global proc cryMaterialUIRebuild() { // Don't clear the shader list is here or the slection will be lost when moving the shaders up and down etc. // It should be cleared in the caller when needed. //textScrollList -e -removeAll CRYMATERIAL_SHADERLIST; $partitionName = `cryMaterialGetPartitionName`; if( `objExists $partitionName` ) { string $materialGroups[]; $materialGroups = `partition -q $partitionName`; string $selected[]; $selected = `textScrollList -q -selectItem CRYMATERIAL_GROUPLIST`; textScrollList -e -removeAll CRYMATERIAL_GROUPLIST; for( $materialGroup in $materialGroups ) { textScrollList -e -append $materialGroup CRYMATERIAL_GROUPLIST; } if( `size($materialGroups)` > 0 ) { if( `size($selected)` > 0 ) { for( $item in $selected ) { if( `stringArrayContains $item $materialGroups` ) textScrollList -e -selectItem $item CRYMATERIAL_GROUPLIST; } } else { textScrollList -e -selectIndexedItem 1 CRYMATERIAL_GROUPLIST; } } cryMaterialUIUpdateShaderList; } } proc createCryMaterialWindow() { if(!`window -ex CRYMATERIAL_WINDOW`) { if(`windowPref -exists CRYMATERIAL_WINDOW`) { windowPref -wh 500 410 -tlc `windowPref -q -topEdge CRYMATERIAL_WINDOW` `windowPref -q -leftEdge CRYMATERIAL_WINDOW` CRYMATERIAL_WINDOW; //windowPref -remove CRYMATERIAL_WINDOW; } window -titleBar true -title "Material Groups" -widthHeight 500 410 -sizeable true -mnb false -mxb false CRYMATERIAL_WINDOW; $layout1 = `formLayout -numberOfDivisions 100`; { text -height 30 -font "boldLabelFont" -label "Material Groups" CRYMATERIAL_GROUPSLABEL; textScrollList -allowMultiSelection false -selectCommand "cryMaterialUIUpdateShaderList" -deleteKeyCommand "cryMaterialUIDeleteGroup 0" -doubleClickCommand "cryMaterialUIRenameGroup" -width 20 CRYMATERIAL_GROUPLIST; popupMenu; { menuItem -label "Select Objects With MaterialGroup" -command "cryMaterialUISelectObjectsWithMaterialGroup"; menuItem -divider true; menuItem -label "Rename Group" -command "cryMaterialUIRenameGroup"; menuItem -label "View in HyperShade" -command "cryMaterialUIOpenGroupInHyperShade"; } text -height 30 -font "boldLabelFont" -label "Shaders" CRYMATERIAL_SHADERSLABEL; textScrollList -allowMultiSelection true -deleteKeyCommand "cryMaterialUIRemoveShaders" -doubleClickCommand "cryMaterialUIRenameShader" -width 20 CRYMATERIAL_SHADERLIST; popupMenu; { menuItem -label "Select Shader" -command "cryMaterialUISelectShader"; menuItem -divider true; menuItem -label "Open Shader In Attribute Editor" -command "cryMaterialUIOpenInAttributeEditor"; menuItem -label "Select Objects With Material" -command "cryMaterialUISelectObjectsWithMaterial"; menuItem -divider true; menuItem -label "Assign Material To Selection" -command "cryMaterialUIAssignMaterialToSelection"; menuItem -divider true; menuItem -label "Create New Shader" -command "cryMaterialUICreateNewShader"; } columnLayout -adjustableColumn true CRYMATERIAL_GROUPCOL; { button -label "Create Group" -command "cryMaterialUICreateGroup"; button -label "Create Group From Selection" -command "cryMaterialUICreateGroupFromSelection"; button -label "Rename Selected Group" -command "cryMaterialUIRenameGroup"; button -label "Read Material File" -command "cryMaterialUIReadMaterial"; button -label "View Selected Group in HyperShade" -command "cryMaterialUIOpenGroupInHyperShade"; } setParent ..; columnLayout -adjustableColumn true CRYMATERIAL_SHADERCOL; { button -label "Add Selected Shaders" -command "cryMaterialUIAddSelectedShaders"; button -label "Add Shaders From Selected Geom" -command "cryMaterialUIAddShadersFromGeom"; button -label "Move Shaders Up" -command "cryMaterialUIMoveShadersUp"; button -label "Move Shaders Down" -command "cryMaterialUIMoveShadersDown"; button -label "View Selected Shaders in HyperShade" -command "cryMaterialUIOpenShaderInHyperShade"; } setParent ..; } $closebutton = `button -label "Close" -command ("cryMaterialUICloseWindow")`; setParent ..; formLayout -edit -attachForm CRYMATERIAL_GROUPSLABEL "top" 5 -attachForm CRYMATERIAL_GROUPSLABEL "left" 5 -attachPosition CRYMATERIAL_GROUPSLABEL "right" 5 50 -attachNone CRYMATERIAL_GROUPSLABEL "bottom" -attachForm CRYMATERIAL_SHADERSLABEL "top" 5 -attachPosition CRYMATERIAL_SHADERSLABEL "left" 5 50 -attachForm CRYMATERIAL_SHADERSLABEL "right" 5 -attachNone CRYMATERIAL_SHADERSLABEL "bottom" -attachControl CRYMATERIAL_GROUPLIST "top" 5 CRYMATERIAL_GROUPSLABEL -attachForm CRYMATERIAL_GROUPLIST "left" 5 -attachPosition CRYMATERIAL_GROUPLIST "right" 5 50 -attachControl CRYMATERIAL_GROUPLIST "bottom" 5 CRYMATERIAL_GROUPCOL -attachControl CRYMATERIAL_SHADERLIST "top" 5 CRYMATERIAL_SHADERSLABEL -attachPosition CRYMATERIAL_SHADERLIST "left" 5 50 -attachForm CRYMATERIAL_SHADERLIST "right" 5 -attachControl CRYMATERIAL_SHADERLIST "bottom" 5 CRYMATERIAL_SHADERCOL -attachNone CRYMATERIAL_GROUPCOL "top" -attachForm CRYMATERIAL_GROUPCOL "left" 5 -attachPosition CRYMATERIAL_GROUPCOL "right" 5 50 -attachControl CRYMATERIAL_GROUPCOL "bottom" 5 $closebutton -attachNone CRYMATERIAL_SHADERCOL "top" -attachPosition CRYMATERIAL_SHADERCOL "left" 5 50 -attachForm CRYMATERIAL_SHADERCOL "right" 5 -attachControl CRYMATERIAL_SHADERCOL "bottom" 5 $closebutton -attachForm $closebutton "bottom" 5 -attachForm $closebutton "left" 5 -attachForm $closebutton "right" 5 -attachNone $closebutton "top" $layout1; } cryMaterialUIRebuild; showWindow CRYMATERIAL_WINDOW; } proc cryMaterialSourceDependencies() { eval("source cryExport.mel"); } global proc cryMaterialWin() { cryMaterialSourceDependencies; createCryMaterialWindow(); }