protected void CheckAnimCurve(
            FbxObject origAnimObject, FbxObject importAnimObject,
            FbxAnimLayer origLayer, FbxAnimLayer importLayer,
            List <PropertyComponentPair> propCompPairs,
            FbxNodeAttribute origNodeAttr = null, FbxNodeAttribute importNodeAttr = null)
        {
            foreach (var pair in propCompPairs)
            {
                FbxProperty origProperty = origAnimObject.FindProperty(pair.propertyName, false);
                if (origNodeAttr != null && (origProperty == null || !origProperty.IsValid()))
                {
                    origProperty = origNodeAttr.FindProperty(pair.propertyName, false);
                }
                FbxProperty importProperty = importAnimObject.FindProperty(pair.propertyName, false);
                if (importNodeAttr != null && (importProperty == null || !importProperty.IsValid()))
                {
                    importProperty = importNodeAttr.FindProperty(pair.propertyName, false);
                }

                Assert.IsNotNull(origProperty);
                Assert.IsNotNull(importProperty);
                Assert.IsTrue(origProperty.IsValid());
                Assert.IsTrue(importProperty.IsValid());

                foreach (var component in pair.componentList)
                {
                    FbxAnimCurve origAnimCurve   = origProperty.GetCurve(origLayer, component, false);
                    FbxAnimCurve importAnimCurve = importProperty.GetCurve(importLayer, component, false);

                    Assert.IsNotNull(origAnimCurve);
                    Assert.IsNotNull(importAnimCurve);

                    Assert.AreEqual(origAnimCurve.KeyGetCount(), importAnimCurve.KeyGetCount());

                    for (int i = 0; i < origAnimCurve.KeyGetCount(); i++)
                    {
                        Assert.AreEqual(origAnimCurve.KeyGetTime(i), importAnimCurve.KeyGetTime(i));
                        Assert.AreEqual(origAnimCurve.KeyGetValue(i), importAnimCurve.KeyGetValue(i));
                    }
                }
            }
        }
        protected void CreateAnimCurves(
            FbxObject animObject, FbxAnimLayer animLayer,
            List <PropertyComponentPair> properties,
            System.Func <int, double> calcTime, // lambda function for calculating time based on index
            System.Func <int, float> calcValue, // lambda function for calculating value based on index
            FbxNodeAttribute animNodeAttr = null)
        {
            foreach (var pair in properties)
            {
                FbxProperty fbxProperty = animObject.FindProperty(pair.propertyName, false);
                if (animNodeAttr != null && (fbxProperty == null || !fbxProperty.IsValid()))
                {
                    // backup method for finding the property if we can't find it on the node itself
                    fbxProperty = animNodeAttr.FindProperty(pair.propertyName, false);
                }

                Assert.IsNotNull(fbxProperty);
                Assert.IsTrue(fbxProperty.IsValid());
                Assert.That(fbxProperty.GetFlag(FbxPropertyFlags.EFlags.eAnimatable), Is.True);

                foreach (var component in pair.componentList)
                {
                    // Create the AnimCurve on the channel
                    FbxAnimCurve fbxAnimCurve = fbxProperty.GetCurve(animLayer, component, true);

                    Assert.IsNotNull(fbxAnimCurve);

                    fbxAnimCurve.KeyModifyBegin();
                    for (int keyIndex = 0; keyIndex < m_keyCount; ++keyIndex)
                    {
                        FbxTime fbxTime = FbxTime.FromSecondDouble(calcTime(keyIndex));
                        fbxAnimCurve.KeyAdd(fbxTime);
                        fbxAnimCurve.KeySet(keyIndex, fbxTime, calcValue(keyIndex));
                    }
                    fbxAnimCurve.KeyModifyEnd();
                }
            }
        }
Esempio n. 3
0
            /// <summary>
            /// Export an AnimationCurve.
            ///
            /// This is not used for rotations, because we need to convert from
            /// quaternion to euler and various other stuff.
            /// </summary>
            protected void ExportAnimCurve(UnityEngine.Object unityObj,
                                           AnimationCurve unityAnimCurve,
                                           string unityPropertyName,
                                           FbxAnimLayer fbxAnimLayer)
            {
                FbxPropertyChannelPair fbxPair;

                if (!MapUnityPropertyNameToFbx.TryGetValue(unityPropertyName, out fbxPair))
                {
                    Debug.LogWarning(string.Format("no property-channel mapping found for {0}", unityPropertyName));
                    return;
                }

                GameObject unityGo = GetGameObject(unityObj);

                if (unityGo == null)
                {
                    Debug.LogError(string.Format("cannot convert to GameObject from {0}", unityObj.ToString()));
                    return;
                }

                FbxNode fbxNode;

                if (!MapUnityObjectToFbxNode.TryGetValue(unityGo, out fbxNode))
                {
                    Debug.LogError(string.Format("cannot find fbxNode for {0}", unityGo.ToString()));
                    return;
                }

                FbxProperty fbxProperty = null;

                // try finding unity property name on node attribute
                FbxNodeAttribute fbxNodeAttribute = fbxNode.GetNodeAttribute();

                if (fbxNodeAttribute != null)
                {
                    fbxProperty = fbxNodeAttribute.FindProperty(fbxPair.Property, false);
                }

                // try finding unity property on the node
                if (fbxProperty == null || !fbxProperty.IsValid())
                {
                    fbxProperty = fbxNode.FindProperty(fbxPair.Property, false);
                }

                if (fbxProperty == null || !fbxProperty.IsValid())
                {
                    Debug.LogError(string.Format("cannot find fbxProperty {0} on {1}", fbxPair.Property, fbxNode.GetName()));
                    return;
                }

                if (Verbose)
                {
                    Debug.Log(string.Format("Exporting animation for {0} ({1})",
                                            unityObj.ToString(),
                                            fbxPair.Property));
                }

                // Create the AnimCurve on the channel
                FbxAnimCurve fbxAnimCurve = (fbxPair.Channel != null)
                    ? fbxProperty.GetCurve(fbxAnimLayer, fbxPair.Channel, true)
                                 : fbxProperty.GetCurve(fbxAnimLayer, true);

                // copy Unity AnimCurve to FBX AnimCurve.
                fbxAnimCurve.KeyModifyBegin();

                for (int keyIndex = 0, n = unityAnimCurve.length; keyIndex < n; ++keyIndex)
                {
                    var key     = unityAnimCurve [keyIndex];
                    var fbxTime = FbxTime.FromSecondDouble(key.time);
                    fbxAnimCurve.KeyAdd(fbxTime);
                    fbxAnimCurve.KeySet(keyIndex, fbxTime, key.value);
                }

                fbxAnimCurve.KeyModifyEnd();
            }