Skip to content

Commit f5baa22

Browse files
committed
add comments + clean up implementation
- make sure that only transform curves are replaced
1 parent ba4b192 commit f5baa22

File tree

1 file changed

+53
-14
lines changed

1 file changed

+53
-14
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,6 +1793,8 @@ protected void ExportAnimationClip (AnimationClip uniAnimClip, GameObject uniRoo
17931793

17941794
var unityCurves = new Dictionary<GameObject, List<UnityCurve>> ();
17951795

1796+
// extract and store all necessary information from the curve bindings, namely the animation curves
1797+
// and their corresponding property names for each GameObject.
17961798
foreach (EditorCurveBinding uniCurveBinding in AnimationUtility.GetCurveBindings (uniAnimClip)) {
17971799
Object uniObj = AnimationUtility.GetAnimatedObject (uniRoot, uniCurveBinding);
17981800
if (!uniObj) {
@@ -1820,25 +1822,29 @@ protected void ExportAnimationClip (AnimationClip uniAnimClip, GameObject uniRoo
18201822
var animSource = ExportOptions.AnimationSource;
18211823
var animDest = ExportOptions.AnimationDest;
18221824
if (animSource && animDest) {
1823-
// list of source to dest
1824-
var transformsInHierarchy = new List<Transform> ();
1825+
// list of all transforms between source and dest, including source and dest
1826+
var transformsFromSourceToDest = new List<Transform> ();
18251827
var curr = animDest;
18261828
while (curr != animSource) {
1827-
transformsInHierarchy.Add (curr);
1829+
transformsFromSourceToDest.Add (curr);
18281830
curr = curr.parent;
18291831
}
1830-
transformsInHierarchy.Add (animSource);
1831-
transformsInHierarchy.Reverse ();
1832+
transformsFromSourceToDest.Add (animSource);
1833+
transformsFromSourceToDest.Reverse ();
18321834

1833-
while (transformsInHierarchy.Count >= 2) {
1834-
var source = transformsInHierarchy [0];
1835-
transformsInHierarchy.RemoveAt (0);
1836-
var dest = transformsInHierarchy [0];
1835+
// while there are 2 transforms in the list, transfer the animation from the
1836+
// first to the next transform.
1837+
// Then remove the first transform from the list.
1838+
while (transformsFromSourceToDest.Count >= 2) {
1839+
var source = transformsFromSourceToDest [0];
1840+
transformsFromSourceToDest.RemoveAt (0);
1841+
var dest = transformsFromSourceToDest [0];
18371842

18381843
TransferMotion (source, dest, uniAnimClip.frameRate, ref unityCurves);
18391844
}
18401845
}
18411846

1847+
// export the animation curves for each GameObject that has animation
18421848
foreach (var kvp in unityCurves) {
18431849
var uniGO = kvp.Key;
18441850
foreach (var uniCurve in kvp.Value) {
@@ -1887,10 +1893,17 @@ protected void ExportAnimationClip (AnimationClip uniAnimClip, GameObject uniRoo
18871893
}
18881894
}
18891895

1890-
1896+
/// <summary>
1897+
/// Transfers transform animation from source to dest. Replaces dest's Unity Animation Curves with updated animations.
1898+
/// NOTE: Source must be the parent of dest.
1899+
/// </summary>
1900+
/// <param name="source">Source.</param>
1901+
/// <param name="dest">Destination.</param>
1902+
/// <param name="sampleRate">Sample rate.</param>
1903+
/// <param name="unityCurves">Unity curves.</param>
18911904
private void TransferMotion(Transform source, Transform dest, float sampleRate, ref Dictionary<GameObject, List<UnityCurve>> unityCurves){
18921905
// get sample times for curves in dest + source
1893-
// at each sample time, evaluate all 18 anim curves, creating 2 transform matrices
1906+
// at each sample time, evaluate all 18 transfom anim curves, creating 2 transform matrices
18941907
// combine the matrices, get the new values, apply to the 9 new anim curves for dest
18951908
if (dest.parent != source) {
18961909
Debug.LogError ("dest must be a child of source");
@@ -1899,7 +1912,7 @@ private void TransferMotion(Transform source, Transform dest, float sampleRate,
18991912

19001913
List<UnityCurve> sourceUnityCurves;
19011914
if (!unityCurves.TryGetValue (source.gameObject, out sourceUnityCurves)) {
1902-
return; // nothing to do
1915+
return; // nothing to do, source has no animation
19031916
}
19041917

19051918
List<UnityCurve> destUnityCurves;
@@ -1979,8 +1992,34 @@ private void TransferMotion(Transform source, Transform dest, float sampleRate,
19791992
newUnityCurves.Add (scaleUniCurve);
19801993
}
19811994

1982-
unityCurves.Remove (source.gameObject);
1983-
unityCurves [dest.gameObject] = newUnityCurves;
1995+
// remove old transform curves
1996+
RemoveTransformCurves (ref sourceUnityCurves);
1997+
RemoveTransformCurves (ref destUnityCurves);
1998+
1999+
unityCurves [source.gameObject] = sourceUnityCurves;
2000+
if (destUnityCurves.Count == 0) {
2001+
unityCurves.Add (dest.gameObject, newUnityCurves);
2002+
return;
2003+
}
2004+
unityCurves [dest.gameObject].AddRange(newUnityCurves);
2005+
2006+
}
2007+
2008+
2009+
private void RemoveTransformCurves(ref List<UnityCurve> curves){
2010+
var transformCurves = new List<UnityCurve> ();
2011+
var transformPropNames = new string[]{"m_LocalPosition.", "m_LocalRotation", "localEulerAnglesRaw.", "m_LocalScale."};
2012+
foreach (var curve in curves) {
2013+
foreach (var prop in transformPropNames) {
2014+
if (curve.propertyName.StartsWith (prop)) {
2015+
transformCurves.Add (curve);
2016+
break;
2017+
}
2018+
}
2019+
}
2020+
foreach (var curve in transformCurves) {
2021+
curves.Remove (curve);
2022+
}
19842023
}
19852024

19862025
private Matrix4x4 GetTransformMatrix(float currSampleTime, Transform orig, List<UnityCurve> unityCurves){

0 commit comments

Comments
 (0)