@@ -1793,6 +1793,8 @@ protected void ExportAnimationClip (AnimationClip uniAnimClip, GameObject uniRoo
1793
1793
1794
1794
var unityCurves = new Dictionary < GameObject , List < UnityCurve > > ( ) ;
1795
1795
1796
+ // extract and store all necessary information from the curve bindings, namely the animation curves
1797
+ // and their corresponding property names for each GameObject.
1796
1798
foreach ( EditorCurveBinding uniCurveBinding in AnimationUtility . GetCurveBindings ( uniAnimClip ) ) {
1797
1799
Object uniObj = AnimationUtility . GetAnimatedObject ( uniRoot , uniCurveBinding ) ;
1798
1800
if ( ! uniObj ) {
@@ -1820,25 +1822,29 @@ protected void ExportAnimationClip (AnimationClip uniAnimClip, GameObject uniRoo
1820
1822
var animSource = ExportOptions . AnimationSource ;
1821
1823
var animDest = ExportOptions . AnimationDest ;
1822
1824
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 > ( ) ;
1825
1827
var curr = animDest ;
1826
1828
while ( curr != animSource ) {
1827
- transformsInHierarchy . Add ( curr ) ;
1829
+ transformsFromSourceToDest . Add ( curr ) ;
1828
1830
curr = curr . parent ;
1829
1831
}
1830
- transformsInHierarchy . Add ( animSource ) ;
1831
- transformsInHierarchy . Reverse ( ) ;
1832
+ transformsFromSourceToDest . Add ( animSource ) ;
1833
+ transformsFromSourceToDest . Reverse ( ) ;
1832
1834
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 ] ;
1837
1842
1838
1843
TransferMotion ( source , dest , uniAnimClip . frameRate , ref unityCurves ) ;
1839
1844
}
1840
1845
}
1841
1846
1847
+ // export the animation curves for each GameObject that has animation
1842
1848
foreach ( var kvp in unityCurves ) {
1843
1849
var uniGO = kvp . Key ;
1844
1850
foreach ( var uniCurve in kvp . Value ) {
@@ -1887,10 +1893,17 @@ protected void ExportAnimationClip (AnimationClip uniAnimClip, GameObject uniRoo
1887
1893
}
1888
1894
}
1889
1895
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>
1891
1904
private void TransferMotion ( Transform source , Transform dest , float sampleRate , ref Dictionary < GameObject , List < UnityCurve > > unityCurves ) {
1892
1905
// 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
1894
1907
// combine the matrices, get the new values, apply to the 9 new anim curves for dest
1895
1908
if ( dest . parent != source ) {
1896
1909
Debug . LogError ( "dest must be a child of source" ) ;
@@ -1899,7 +1912,7 @@ private void TransferMotion(Transform source, Transform dest, float sampleRate,
1899
1912
1900
1913
List < UnityCurve > sourceUnityCurves ;
1901
1914
if ( ! unityCurves . TryGetValue ( source . gameObject , out sourceUnityCurves ) ) {
1902
- return ; // nothing to do
1915
+ return ; // nothing to do, source has no animation
1903
1916
}
1904
1917
1905
1918
List < UnityCurve > destUnityCurves ;
@@ -1979,8 +1992,34 @@ private void TransferMotion(Transform source, Transform dest, float sampleRate,
1979
1992
newUnityCurves . Add ( scaleUniCurve ) ;
1980
1993
}
1981
1994
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
+ }
1984
2023
}
1985
2024
1986
2025
private Matrix4x4 GetTransformMatrix ( float currSampleTime , Transform orig , List < UnityCurve > unityCurves ) {
0 commit comments