// =========================================================================== // 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 int $gClothPlayScriptJob = -1; global int $gClothPlaySelScriptJob = -1; global string $gClothPlayObjects[]; global string $gNucleusPlayObjects[]; global int $gClothPlayStartFrame[]; global int $gClothPlayTime = -1; global int $gClothPlayEndTime = -1; global string $gClothPlayStopButton = ""; global float $gClothPlayDamp[]; global int $gClothPlayGravity[]; global proc setNClothPlayObjects(float $damp, int $ignoreGravity) { global float $gClothPlayDamp[]; global int $gClothPlayGravity[]; global string $gClothPlayObjects[]; global string $gNucleusPlayObjects[]; global int $gClothPlayStartFrame[]; string $meshes[] = `ls -sl -dag -ni -o -visible -type mesh`; string $mesh; for($mesh in $meshes){ string $nodes[] = `listHistory -f off $mesh`; if( size( $nodes) > 0 ){ $nodes = `ls -type nBase $nodes`; if( size( $nodes) > 0 ){ string $cloth = $nodes[0]; string $nucleusCons[] = `listConnections -type nucleus ($cloth + ".currentState")`; if( size( $nucleusCons) > 0 ){ int $i = size( $gClothPlayObjects ); $gClothPlayObjects[ $i ] = $cloth; if( nodeType( $cloth ) == "nCloth" ){ $gClothPlayDamp[ $i ] = getAttr( $cloth + ".damp"); setAttr ($cloth + ".damp") $damp; $gClothPlayGravity[ $i ] = getAttr( $cloth + ".ignoreSolverGravity"); if( !$gClothPlayGravity[$i] && $ignoreGravity){ setAttr ($cloth + ".ignoreSolverGravity") $ignoreGravity; } } int $j = size( $gNucleusPlayObjects ); $gNucleusPlayObjects[ $j ] = $nucleusCons[0]; } } } } $gNucleusPlayObjects = stringArrayRemoveDuplicates( $gNucleusPlayObjects ); string $obj; for( $obj in $gClothPlayObjects ){ disconnectAttr time1.outTime ($obj + ".currentTime"); } int $i = 0; for( $obj in $gNucleusPlayObjects ){ $gClothPlayStartFrame[$i] = getAttr( $obj + ".startFrame" ); disconnectAttr time1.outTime ($obj + ".currentTime"); $i++; } } global proc nClothPlayAdvance() { global string $gMainProgressBar; // This is defined on maya startup global string $gClothPlayObjects[]; global int $gClothPlayTime; global int $gClothPlayEndTime; // $gClothPlayTime++; string $obj; for( $obj in $gClothPlayObjects ){ setAttr ($obj+".currentTime") $gClothPlayTime; getAttr ($obj+".forceDynamics"); } $gClothPlayTime++; if(`progressBar -query -isCancelled $gMainProgressBar` || $gClothPlayTime >= $gClothPlayEndTime ){ evalDeferred "nClothPlayStop()"; } progressBar -edit -step 1 $gMainProgressBar; } global proc nClothPlayRemoveObjects() { global float $gClothPlayDamp[]; global int $gClothPlayGravity[]; global string $gClothPlayObjects[]; global string $gNucleusPlayObjects[]; global int $gClothPlayStartFrame[]; string $obj; int $i = 0; for( $obj in $gClothPlayObjects ){ if( nodeType( $obj ) == "nCloth" ){ nBase -e -stuffStart $obj; setAttr ($obj + ".damp") $gClothPlayDamp[$i]; setAttr ($obj + ".ignoreSolverGravity") $gClothPlayGravity[$i]; } connectAttr time1.outTime ($obj + ".currentTime"); $i++; } $i = 0; for( $obj in $gNucleusPlayObjects ){ if( nodeType( $obj ) == "nucleus" ){ setAttr ($obj + ".startFrame") ($gClothPlayStartFrame[$i]); } connectAttr time1.outTime ($obj + ".currentTime"); $i++; } $gClothPlayObjects = {}; $gNucleusPlayObjects = {}; } proc setupPlayStopButton() { global string $gMainProgressBar; // This is defined on maya startup global string $gClothPlayStopButton; global int $gClothPlayTime; global int $gClothPlayEndTime; progressBar -edit -beginProgress -isInterruptable true -status "Relaxing Initial State ..." -maxValue ($gClothPlayEndTime - $gClothPlayTime) $gMainProgressBar; if( $gClothPlayStopButton == "" ){ $gClothPlayStopButton = "NClothRelaxStopButton"; hudButton -section 8 -b 2 -ao 1 -vis 1 -l "Stop Relaxation" -bw 120 -rc "nClothPlayStop()" -bsh "rectangle" $gClothPlayStopButton; } else { hudButton -e -vis true $gClothPlayStopButton; } } proc hidePlayStopButton() { global string $gClothPlayStopButton; global string $gMainProgressBar; // This is defined on maya startup if( $gClothPlayStopButton != "" ){ hudButton -e -vis false $gClothPlayStopButton; } progressBar -edit -endProgress $gMainProgressBar; } global proc nClothPlayStop() { hidePlayStopButton(); global int $gClothPlayScriptJob; if( $gClothPlayScriptJob != -1 ){ scriptJob -kill $gClothPlayScriptJob; $gClothPlayScriptJob = -1; } nClothPlayRemoveObjects(); } global proc relaxInitialStateWithStopButton( int $numSteps ) { // This routine sets up a stop button and allows interaction // during the relaxation because it uses idle callbacks. However // undo does not work well with this because each step is one undo and // worse.. undo will also break time connections to nodes, disabling solving. global int $gClothPlayScriptJob; global int $gClothPlayTime; global int $gClothPlayEndTime; $gClothPlayTime = getAttr( "time1.outTime" ); $gClothPlayEndTime = $gClothPlayTime + $numSteps; setupPlayStopButton(); if( $gClothPlayScriptJob == -1 ){ setNClothPlayObjects(0.0,false); $gClothPlayScriptJob = `scriptJob -idleEvent "nClothPlayAdvance()"`; } else { nClothPlayStop(); } } global proc relaxInitialState( int $numSteps, float $damp, int $ignoreGravity ) { // int $setInitialState = true; global string $gMainProgressBar; // This is defined on maya startup global string $gClothPlayObjects[]; global string $gNucleusPlayObjects[]; global int $gClothPlayTime; global int $gClothPlayEndTime; progressBar -edit -beginProgress -isInterruptable true -status (uiRes("m_relaxInitialState.kRelaxInitialState")) -maxValue $numSteps $gMainProgressBar; $gClothPlayTime = getAttr( "time1.outTime" ); string $obj; setNClothPlayObjects($damp, $ignoreGravity); if( size( $gClothPlayObjects ) < 1 ){ $numSteps = 0; warning (uiRes("m_relaxInitialState.kNoObjectsToRelax")); } int $i; for( $i = 0; $i < $numSteps; $i++ ){ $gClothPlayTime++; for( $obj in $gNucleusPlayObjects ){ setAttr ($obj+".currentTime") $gClothPlayTime; } for( $obj in $gClothPlayObjects ){ setAttr ($obj+".currentTime") $gClothPlayTime; getAttr ($obj+".forceDynamics"); } refresh -f; if(`progressBar -query -isCancelled $gMainProgressBar` ){ break; } progressBar -edit -step 1 $gMainProgressBar; } nClothPlayRemoveObjects(); progressBar -edit -endProgress $gMainProgressBar; }