/**@file na_dupDrivenKey.mel
@brief Tools for duplicating set driven keys
@author Nathaniel O. Anozie (ogbonnawork at gmail dot com)
@bug all functions no assert checks
@note date created: Apr 2 2012
@note date last modified: Apr 5 2012
@note source na_sdkGeneral.mel
@note source naMath.mel
@note source na_connect.mel
@note source naGeneral.mel
@see na_sdkGeneral.mel
@see naMath.mel
*/
/**transfer animatins from one animator control to another
@param string $node[] node(s)
@param string $nodeAttr[] node attribute(s) should be same number as node
@param string $anim to anim (where were putting control of new driven keys)
@param string $attr to anim attribute
@param string $fromAnim from anim
@param string $fromAttr from anim attribute (where were getting driven keys from)
@note
@see na_getKeysSDK
@see na_setSDK
@see na_findStartToGoal
@see na_getOutConnect
@see
*/
global proc
na_dupDrivenKey(string $node[], string $nodeAttr[], string $anim, string $attr, string $fromAnim, string $fromAttr)
{
na_dupDrivenKey_assert($node, $nodeAttr, $anim, $attr, $fromAnim, $fromAttr);
string $curve[] = {};
float $frame[] = {};//sdk keyframe
float $value[] = {}; //sdk value at keyframe
int $n = size($node);
string $fromControl = $fromAnim+"."+$fromAttr;
string $drv = "";
//get all possible anim curves coming out of fromControl
$allPossibleCurve = na_getOutConnect( $fromControl, "animCurve");
string $supportedTypes[] = {"joint","animCurve","blendWeighted","unitConversion"};
//get frames well use for new control
$curve = na_findStartToGoal( $node[0], $allPossibleCurve, $supportedTypes );
//assuming identical frames for all animation
$frame = na_getKeysSDK($curve[0]); //index possible error
clear($curve);
//loop joints we want to add extra control too
for( $i = 0; $i < $n; $i++ )
{
//loop each possible curve finding listRelativeConnection if joint of interest is found
$joint = $node[$i];
//this curve(s) is used by from control on our joint well use this for setting up new control
$curve = na_findStartToGoal( $joint, $allPossibleCurve, $supportedTypes );
//get only the thing we wish to be driving
$drv = $node[$i]+"."+$nodeAttr[$i];
//get values well using for new control
//this is so frames match value
if( size(na_getKeysSDK($curve[0])) == size($frame) ){
$value = na_getValuesSDK($curve[0]);
//making new control assume what the from control was doing
na_setSDK($drv,$frame,$value, $anim, $attr);
}
else{ print("skipping--"+$curve[0]+"on "+$joint+"-- different number frames"); }
clear($curve);
clear($value);
}
print("completed animator control transfer\n");
}
/**na_dupDrivenKey assert existence attribute,node,animCurve
@see na_getOutConnect
*/
global proc
na_dupDrivenKey_assert(string $node[], string $nodeAttr[], string $anim, string $attr, string $fromAnim, string $fromAttr)
{
//node exist checks
for($i = 0; $i < size($node) ; $i++){
if(`objExists $node[$i]` == 0){ error("Sorry, Cannot find--"+$node[$i]); }
}
if(`objExists $anim` == 0){ error("Sorry, Cannot find--"+$anim); }
if(`objExists $fromAnim` == 0){ error("Sorry, Cannot find--"+$fromAnim); }
//attribute existence checks
if(`attributeExists $attr $anim` == 0){ error("attribute: "+$attr+" for--"+$anim+" not found");}
if(`attributeExists $fromAttr $fromAnim` == 0){ error("attribute: "+$fromAttr+" for--"+$fromAnim+" not found");}
//size
if(size($node) == 0){error("please enter node(s)"); }
if( size($node) != size($nodeAttr) ){error("check sizes of input");}
//anim curve exists on to
string $to = $anim+"."+$attr;
if(size( na_getOutConnect($to,"animCurve") ) > 0 ){error("animCurve exists on --"+$to);}
//no anim curve exists on from
string $from = $fromAnim+"."+$attr;
if(size( na_getOutConnect($from,"animCurve") ) == 0 ){error("no animCurve exists on --"+$from);}
}
/**
@note note implemented
*/
global proc
na_dupDrivenKey_unitTest()
{
polyCube -w 1 -h 1 -d 1 -sx 1 -sy 1 -sz 1 -ax 0 1 0 -cuv 4 -ch 1;
polyCone -r 1 -h 2 -sx 8 -sy 1 -sz 0 -ax 0 1 0 -rcp 0 -cuv 3 -ch 1;
//so anim curve has keys
setDrivenKeyframe -currentDriver pCube1.translateX pCone1.translateX;
setAttr "pCube1.translateX" 3;
setAttr "pCone1.translateX" 5;
setDrivenKeyframe -currentDriver pCube1.translateX pCone1.translateX;
setAttr "pCube1.translateX" 0;
//so have anim control
spaceLocator -p 0 0 0;
addAttr -ln "anim" -at double |locator1;
setAttr -e-keyable true |locator1.anim;
setAttr -lock true -keyable false "locator1.tx";
setAttr -lock true -keyable false "locator1.ty";
setAttr -lock true -keyable false "locator1.tz";
setAttr -lock true -keyable false "locator1.rx";
setAttr -lock true -keyable false "locator1.ry";
setAttr -lock true -keyable false "locator1.rz";
setAttr -lock true -keyable false "locator1.sx";
setAttr -lock true -keyable false "locator1.sy";
setAttr -lock true -keyable false "locator1.sz";
setAttr -lock true -keyable false "locator1.v";
polyCube -w 1 -h 1 -d 1 -sx 1 -sy 1 -sz 1 -ax 0 1 0 -cuv 4 -ch 1;
setDrivenKeyframe -currentDriver pCube2.translateX pCone1.translateX;
setAttr "pCube2.translateX" 3;
setAttr "pCone1.translateX" 20;
setDrivenKeyframe -currentDriver pCube2.translateX pCone1.translateX;
setAttr "pCube2.translateX" 0;
selectKey -clear ;
selectKey -add -k pCone1_translateX ;
selectKey -add -k animCurveUL1;
keyTangent -itt linear -ott linear;
//need to make old control
//because at start pCone1 has 2 curves coming in
//na_dupDrivenKey( {"pCone1"},{"translateX"}, "locator1", "anim");
//selectKey -clear ;
//selectKey -add -k locator1_anim;
}
/**sets keys given frames, values and an animator control
@param string $drivenPlusAttr animator driven control and attribute
@param string $frame frames to set key
@param float $value values to set key on
@param string $anim animation control to be controlling sdk
@param string $attr animation control's attribute controlling sdk
@note not checking input
@note not tested
*/
global proc
na_setSDK(string $drivenPlusAttr, float $frame[], float $value[], string $anim, string $attr)
{
int $n = 0;
string $driver = "";
//setting some variables
$n = size($frame); //number of frames
$driver = $anim+"."+$attr; //full animator control with attribute
//keying with specified value for animator control input
for( $i = 0; $i < $n; $i++ )
{
setDrivenKeyframe -dv $frame[$i] -v $value[$i] -cd $driver $drivenPlusAttr ;
}
}
global proc
na_setSDK_unitTest()
{
polyCube -w 1 -h 1 -d 1 -sx 1 -sy 1 -sz 1 -ax 0 1 0 -cuv 4 -ch 1;
polyCone -r 1 -h 2 -sx 8 -sy 1 -sz 0 -ax 0 1 0 -rcp 0 -cuv 3 -ch 1;
//so anim curve has keys
na_setSDK("pCone1.translateX",{0,3},{2,5}, "pCube1", "translateX");
selectKey -clear ;
selectKey -add -k pCone1_translateX ;
keyTangent -itt linear -ott linear;
}