Skip to content

Commit 73d70e6

Browse files
committed
implement exporting only animation for simple transform hierarchy
- exports animation for hierarchy without bones - TODO: don't export components
1 parent 3eb6590 commit 73d70e6

File tree

1 file changed

+89
-4
lines changed

1 file changed

+89
-4
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2176,25 +2176,110 @@ protected int ExportTransformHierarchy(
21762176
return numObjectsExported;
21772177
}
21782178

2179-
protected bool ExportAnimationOnly(GameObject unityGo, FbxScene fbxScene, FbxNode fbxNodeParent){
2179+
protected bool ExportAnimationOnly(GameObject unityGo, FbxScene fbxScene){
21802180
// gather all animation clips
21812181
var legacyAnim = unityGo.GetComponentsInChildren<Animation>();
21822182
var genericAnim = unityGo.GetComponentsInChildren<Animator> ();
21832183

2184+
// from clips determine what needs to be exported
2185+
2186+
// export nodes and their parent hierarchies
2187+
2188+
// export objects as we go, create parent hierarchies after
2189+
// store objects in a set, along with parents that need to be exported..
2190+
2191+
// also need to know if a gameobject is a bone and export accordingly
2192+
2193+
// export components
2194+
// only light and camera components, and only if that component is animated
2195+
2196+
// export animation
2197+
2198+
var animationClips = new HashSet<AnimationClip> ();
2199+
21842200
foreach (var anim in legacyAnim) {
2185-
var animClip = anim.clip;
2201+
var animClips = AnimationUtility.GetAnimationClips (anim.gameObject);
2202+
ExportAnimatedGameObjects(animClips, anim.gameObject, unityGo, fbxScene, ref animationClips);
2203+
}
2204+
2205+
foreach (var anim in genericAnim) {
2206+
// Try the animator controller (mecanim)
2207+
var controller = anim.runtimeAnimatorController;
2208+
if (controller)
2209+
{
2210+
ExportAnimatedGameObjects(controller.animationClips, anim.gameObject, unityGo, fbxScene, ref animationClips);
2211+
}
2212+
}
2213+
2214+
return false;
2215+
}
2216+
2217+
private bool ExportAnimatedGameObjects(AnimationClip[] animClips, GameObject animationRootObject, GameObject exportRoot, FbxScene fbxScene, ref HashSet<AnimationClip> clipSet){
2218+
foreach (var animClip in animClips) {
2219+
if (!clipSet.Add (animClip)) {
2220+
// we have already exported gameobjects for this clip
2221+
continue;
2222+
}
2223+
21862224
foreach (EditorCurveBinding uniCurveBinding in AnimationUtility.GetCurveBindings (animClip)) {
2187-
Object uniObj = AnimationUtility.GetAnimatedObject (anim.gameObject, uniCurveBinding);
2225+
Object uniObj = AnimationUtility.GetAnimatedObject (animationRootObject, uniCurveBinding);
21882226
if (!uniObj) {
21892227
continue;
21902228
}
2191-
Debug.LogWarning (uniObj.name + ": " + uniCurveBinding.propertyName);
2229+
//Debug.LogWarning (uniObj.name + ": " + uniCurveBinding.propertyName);
2230+
2231+
GameObject unityGo = GetGameObject (uniObj);
2232+
if (!unityGo) {
2233+
continue;
2234+
}
2235+
FbxNode fbxNode;
2236+
ExportGameObjectAndParents (unityGo, exportRoot, fbxScene, out fbxNode);
21922237
}
21932238
}
21942239

21952240
return false;
21962241
}
21972242

2243+
private bool ExportGameObjectAndParents(GameObject unityGo, GameObject rootObject, FbxScene fbxScene, out FbxNode fbxNode){
2244+
// node already exists
2245+
if (MapUnityObjectToFbxNode.TryGetValue (unityGo, out fbxNode)) {
2246+
return true;
2247+
}
2248+
2249+
if (ExportSettings.mayaCompatibleNames) {
2250+
unityGo.name = ConvertToMayaCompatibleName (unityGo.name);
2251+
}
2252+
2253+
// create an FbxNode and add it as a child of parent
2254+
fbxNode = FbxNode.Create (fbxScene, GetUniqueName (unityGo.name));
2255+
MapUnityObjectToFbxNode [unityGo] = fbxNode;
2256+
2257+
// Default inheritance type in FBX is RrSs, which causes scaling issues in Maya as
2258+
// both Maya and Unity use RSrs inheritance by default.
2259+
// Note: MotionBuilder uses RrSs inheritance by default as well, though it is possible
2260+
// to select a different inheritance type in the UI.
2261+
// Use RSrs as the scaling inhertiance instead.
2262+
fbxNode.SetTransformationInheritType (FbxTransform.EInheritType.eInheritRSrs);
2263+
2264+
ExportTransform (unityGo.transform, fbxNode, Vector3.zero, TransformExportType.Local);//newCenter, exportType);
2265+
2266+
if (unityGo.transform.parent != null || unityGo.transform.parent != rootObject.transform.parent) {
2267+
FbxNode fbxNodeParent;
2268+
if (!ExportGameObjectAndParents (unityGo.transform.parent.gameObject, rootObject, fbxScene, out fbxNodeParent)) {
2269+
Debug.LogWarningFormat ("Failed to export GameObject {0}", unityGo.transform.parent.name);
2270+
return false;
2271+
}
2272+
fbxNodeParent.AddChild (fbxNode);
2273+
}
2274+
2275+
if (unityGo == rootObject) {
2276+
Debug.Log ("over here");
2277+
fbxScene.GetRootNode ().AddChild (fbxNode);
2278+
}
2279+
2280+
return true;
2281+
}
2282+
21982283
/// <summary>
21992284
/// Export components on this game object.
22002285
/// Transform components have already been exported.

0 commit comments

Comments
 (0)