AnimationSourceItem.2.js Example
Applies To
AnimationSourceItem object
Description
This example demonstrates how to use the AnimationSourceItem object.
Code
/* This example illustrates how to create an empty ActionSource using Model.AddActionSource, populate it with 2 static values and 1 fcurve using ActionSource.AddSourceItem, and then replace the fcurve source with a static value for the 2nd and change the static value for the 3rd using AnimationSourceItem.SetAsStatic. */ NewScene( null, false ); // Get the scene root var root = Application.ActiveSceneRoot; // Create a null and get pointers to the pos parameters var n = root.AddNull( "null"); var posx = n.posx; var rposx = GetRelativeName(posx); var posy = n.posy; var rposy = GetRelativeName(posy); var posz = n.posz; var rposz = GetRelativeName(posz); // Create an empty actionsource and then populate it with static values var src = root.AddActionSource( "StaticActionSource" ); var fc = MakeAnFCurve( n, "posy" ); src.AddSourceItem( rposx, 2.0 ); // static value src.AddSourceItem( rposy, fc ); // fcurve src.AddSourceItem( rposz ); // default static value = 0.0 // Now change the value on the posz parameter using SetAsStatic and change // the posy parameter source to an fcurve var animsrcitems = src.SourceItems; for ( var i=0; i<animsrcitems.Count; i++ ) { var srcitem = animsrcitems(i); // Get a pointer to the source item (it can be either a StaticSource or an FCurve) var datasrc = srcitem.Source; if ( datasrc.IsClassOf(siFCurveID) ) { // Print the FCurve type Application.LogMessage( "AnimationSourceItem[" + i + "] (" + srcitem.Target + ") has " + datasrc.GetNumKeys() + " keys" ); } if ( datasrc.IsClassOf(siStaticSourceID) ) { // Print the current value Application.LogMessage( "AnimationSourceItem[" + i + "] (" + srcitem.Target + ") has this static value: " + datasrc.Value ); } // AnimationSourceItem.Target returns the RelativeName if ( srcitem.Target == rposy ) { srcitem.SetAsStatic( 1.0 ); // Check for the new value (we know it's a StaticSource because we just set it) var datasrc = srcitem.Source; // refresh Application.LogMessage( "New static value = " + datasrc.Value ); } if ( srcitem.Target == rposz ) { srcitem.SetAsStatic( 1.5 ); // Check for the new value (we know it's a StaticSource because we just set it) var datasrc = srcitem.Source; // refresh Application.LogMessage( "New static value = " + datasrc.Value ); } } //INFO : AnimationSourceItem[0] (null.kine.local.posx) has this static value: 2 //INFO : AnimationSourceItem[1] (null.kine.local.posy) has 4 keys //INFO : New static value = 1 //INFO : AnimationSourceItem[2] (null.kine.local.posz) has this static value: 0 //INFO : New static value = 1.5 // Function to remove the name of the model from the FullName of the specified parameter. // This is necessary when setting up a source that will later be used to instantiate a // clip when the parameter lives under a model other than the Scene_Root. function GetRelativeName( in_param ) { var mdlname = in_param.Model.FullName; if ( mdlname == "Scene_Root" ) { return in_param.FullName; } else { var tmp = in_param.FullName; var re = new RegExp( mdlname + ".", "i" ); return tmp.replace( re, "" ); } } // Convenience function function MakeAnFCurve( in_obj, in_param ) { var p = in_obj.Kinematics.Local.Parameters( in_param ); var fc = p.AddFCurve( siStandardFCurve ); fc.AddKey( 1.0, 1.25 ); fc.AddKey( 25.0, 2.0 ); fc.AddKey( 40.0, 2.75 ); fc.AddKey( 85.0, 0.0 ); return fc; }
Related Examples
Keywords
AnimationSourceItem SetAsFCurve SetAsStatic Active Source Target
Autodesk Softimage 2011