XSI JScript - Get FCurve

アニメートされたオブジェクトからFカーブを取得するだけのもの

オブジェクトが選択されてない場合、ピックさせるようにする処理を追加

var HogeFCurve = function() { this.init(); }

/////////////////////////////////////////////////////////
// initialize class
/////////////////////////////////////////////////////////
HogeFCurve.prototype.init = function() {
 this.oPSet = XSIFactory.CreateObject("CustomProperty");
 this.oPSet.Name = "Test FCurve Param";
 this.oSel = Application.Selection(0);
 this.oParam = this.oPSet.AddFCurveParameter("TestFCurve");
 this.oFc = this.oParam.Value;
 this.oKey = this.oFc.Keys;
 try {
  this._checkSelection();
  var fcx = this.oSel.posx.Source;
  var fcy = this.oSel.posy.Source;
  var fcz = this.oSel.posz.Source;
  
  var datax = this.GetFCurveFromSource(fcx);
  var datay = this.GetFCurveFromSource(fcy);
  var dataz = this.GetFCurveFromSource(fcz);
  
  this.oFc.SetKeys(datax);
  
  LogMessage("x = " + datax + ", y = " + datay + ", z = " + dataz);
  
  //this.printInfo(fcx);
  //this.printInfo(fcy);
  //this.printInfo(fcz);
 } catch(err) {
  LogMessage(err.message);
  return false;
 }
}

HogeFCurve.prototype._checkSelection = function() {
 if(ClassName(this.oSel) != "X3DObject") {
  var oPick = PickObject("Select Object");
  var oRet = oPick.Value("ButtonPressed");
  LogMessage("Result = " + oRet);
  if(oRet) this.oSel = oPick.Value("PickedElement");
 }
 if(!this.oSel.IsClassOf(siX3DObjectID)) throw "Select or Pick Object.";
}

/////////////////////////////////////////////////////////
// show PPG
/////////////////////////////////////////////////////////
HogeFCurve.prototype.show = function() {
 try {
  this._checkSelection();
  InspectObj(this.oPSet, null, "", siRecycle, false);
 } catch(err) {
  LogMessage(err.message);
 }
 return this;
}

/////////////////////////////////////////////////////////
// 
// @param in_fc Parameter - Source property of KinematicState
// @return Array - 
/////////////////////////////////////////////////////////
HogeFCurve.prototype.GetFCurveFromSource = function(in_fc) {
 if(ClassName(in_fc) != "FCurve") throw "You passed me an a(n) " + ClassName(in_fc) + "istead of an FCurve.";
 data = []
 fckeys = in_fc.Keys;
 for(var i = 0, j = 0, k = 1; i < fckeys.Count; ++i, j+=2, k+=2) {
  data[j] = fckeys(i).Time;
  data[k] = fckeys(i).Value;
 }
 return data;
}

HogeFCurve.prototype.printInfo = function(in_fc) {
 if(ClassName(in_fc) != "FCurve") throw "You passed me an a(n) " + ClassName(in_fc) + "istead of an FCurve.";
 LogMessage("FCurve for parameter:" + in_fc.Parent);
 LogMessage("Number of keys:" + in_fc.GetNumKeys());
 LogMessage("Is the FCurve begin editing? ..." + in_fc.IsEditing());
 
 data = []
 fckeys = in_fc.Keys;
 for(var i = 0; i < fckeys.Count; ++i) {
  data[i] = fckeys(i).Value;
  LogMessage("Key[" + i + "] set at frame " + fckeys(i).Time + " = " + fckeys(i).Value);
 }
 LogMessage("data = " + data)
}

/////////////////////////////////////////////////////////
// Get current frame of scene.
// @return Double - current frame number.
/////////////////////////////////////////////////////////
HogeFCurve.prototype.currentFrame = function() {
 var remote = Dictionary.GetObject("PlayControl");
 var cur = remote.Parameters("Current");
 return cur.Value;
}

HogeFCurve.prototype.play = function(in_start, in_loop) {
 var is_start = in_start || false;
 var is_loop = in_loop || false;
 if(is_start) PlayForwardsFromStart();
 else PlayForwards();
 if(is_loop) SetValue("PlayControl.Loop", true, null);
 else SetValue("PlayControl.Loop", false, null);
 return this;
}

new HogeFCurve().show().play();

No comments:

Post a Comment