Skip to content

Commit 9ba7eed

Browse files
committed
move IExportData out of ModelExporter
- and into a new file so it's easier to manage
1 parent 3f9ab36 commit 9ba7eed

File tree

5 files changed

+191
-166
lines changed

5 files changed

+191
-166
lines changed

Assets/com.unity.formats.fbx.tests/ExportTimelineClipTest.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using UnityEngine;
2-
using UnityEditor;
32
using NUnit.Framework;
43
using FbxExporters.Editor;
54
using UnityEngine.Timeline;
@@ -24,7 +23,7 @@ public void ExportSingleTimelineClipTest()
2423
GameObject myCube = GameObject.Find("CubeSpecial");
2524
string folderPath = GetRandomFileNamePath(extName: "");
2625
string filePath = null;
27-
var exportData = new Dictionary<GameObject, ModelExporter.IExportData>();
26+
var exportData = new Dictionary<GameObject, IExportData>();
2827

2928
PlayableDirector pd = myCube.GetComponent<PlayableDirector> ();
3029
if (pd != null) {

Packages/com.unity.formats.fbx/Editor/Scripts/ExportModelEditorWindow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ protected override bool DisableNameSelection {
407407
protected override GameObject GetGameObject()
408408
{
409409
return (IsTimelineAnim)
410-
? ModelExporter.AnimationOnlyExportData.GetGameObjectAndAnimationClip(ToExport [0]).Key
410+
? AnimationOnlyExportData.GetGameObjectAndAnimationClip(ToExport [0]).Key
411411
: ModelExporter.GetGameObject ( ToExport [0] );
412412
}
413413

Packages/com.unity.formats.fbx/Editor/Scripts/FbxExporter.cs

Lines changed: 0 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
using UnityEditor;
66
using Unity.FbxSdk;
77
using System.Linq;
8-
using UnityEngine.Playables;
9-
using UnityEngine.Timeline;
108
using FbxExporters.EditorTools;
119

1210
namespace FbxExporters
@@ -2584,167 +2582,6 @@ internal int ExportTransformHierarchy(
25842582
return numObjectsExported;
25852583
}
25862584

2587-
/// <summary>
2588-
/// Export data containing extra information required to export
2589-
/// </summary>
2590-
public interface IExportData
2591-
{
2592-
HashSet<GameObject> Objects { get; }
2593-
}
2594-
2595-
/// <summary>
2596-
/// Export data containing what to export when
2597-
/// exporting animation only.
2598-
/// </summary>
2599-
internal struct AnimationOnlyExportData : IExportData {
2600-
// map from animation clip to GameObject that has Animation/Animator
2601-
// component containing clip
2602-
public Dictionary<AnimationClip, GameObject> animationClips;
2603-
2604-
// set of all GameObjects to export
2605-
public HashSet<GameObject> goExportSet;
2606-
public HashSet<GameObject> Objects { get { return goExportSet; } }
2607-
2608-
// map from GameObject to component type to export
2609-
public Dictionary<GameObject, System.Type> exportComponent;
2610-
2611-
// first clip to export
2612-
public AnimationClip defaultClip;
2613-
2614-
public AnimationOnlyExportData(
2615-
Dictionary<AnimationClip, GameObject> animClips,
2616-
HashSet<GameObject> exportSet,
2617-
Dictionary<GameObject, System.Type> exportComponent
2618-
){
2619-
this.animationClips = animClips;
2620-
this.goExportSet = exportSet;
2621-
this.exportComponent = exportComponent;
2622-
this.defaultClip = null;
2623-
}
2624-
2625-
/// <summary>
2626-
/// collect all objects dependencies for animation clips.
2627-
/// </summary>
2628-
public void CollectDependencies(
2629-
AnimationClip[] animClips,
2630-
GameObject rootObject,
2631-
IExportOptions exportOptions
2632-
){
2633-
Debug.Assert(rootObject!=null);
2634-
Debug.Assert(exportOptions!=null);
2635-
2636-
// NOTE: the object (animationRootObject) containing the animation is not necessarily animated
2637-
// when driven by an animator or animation component.
2638-
foreach (var animClip in animClips) {
2639-
if (this.animationClips.ContainsKey(animClip)) {
2640-
// we have already exported gameobjects for this clip
2641-
continue;
2642-
}
2643-
2644-
this.animationClips.Add (animClip, rootObject);
2645-
2646-
foreach (EditorCurveBinding uniCurveBinding in AnimationUtility.GetCurveBindings (animClip)) {
2647-
Object uniObj = AnimationUtility.GetAnimatedObject (rootObject, uniCurveBinding);
2648-
if (!uniObj) {
2649-
continue;
2650-
}
2651-
2652-
GameObject unityGo = GetGameObject (uniObj);
2653-
if (!unityGo) {
2654-
continue;
2655-
}
2656-
2657-
if (!exportOptions.AnimateSkinnedMesh && unityGo.GetComponent<SkinnedMeshRenderer>()) {
2658-
continue;
2659-
}
2660-
2661-
// If we have a clip driving a camera or light then force the export of FbxNodeAttribute
2662-
// so that they point the right way when imported into Maya.
2663-
if (unityGo.GetComponent<Light>())
2664-
this.exportComponent[unityGo] = typeof(Light);
2665-
else if (unityGo.GetComponent<Camera>())
2666-
this.exportComponent[unityGo] = typeof(Camera);
2667-
2668-
this.goExportSet.Add (unityGo);
2669-
}
2670-
}
2671-
}
2672-
2673-
public static GameObject GetGameObjectBoundToEditorClip(object editorClip)
2674-
{
2675-
object clipItem = editorClip.GetType().GetProperty("item").GetValue(editorClip, null);
2676-
object parentTrack = clipItem.GetType().GetProperty("parentTrack").GetValue(clipItem, null);
2677-
AnimationTrack animTrack = parentTrack as AnimationTrack;
2678-
2679-
#if UNITY_2018_2_OR_NEWER
2680-
Object animationTrackObject = UnityEditor.Timeline.TimelineEditor.inspectedDirector.GetGenericBinding(animTrack);
2681-
#else // UNITY_2018_2_OR_NEWER
2682-
Object animationTrackObject = UnityEditor.Timeline.TimelineEditor.playableDirector.GetGenericBinding(animTrack);
2683-
#endif // UNITY_2018_2_OR_NEWER
2684-
2685-
GameObject animationTrackGO = null;
2686-
if (animationTrackObject is GameObject)
2687-
{
2688-
animationTrackGO = animationTrackObject as GameObject;
2689-
}
2690-
else if (animationTrackObject is Animator)
2691-
{
2692-
animationTrackGO = (animationTrackObject as Animator).gameObject;
2693-
}
2694-
2695-
if (animationTrackGO == null)
2696-
{
2697-
Debug.LogErrorFormat("Could not export animation track object of type {0}", animationTrackObject.GetType().Name);
2698-
return null;
2699-
}
2700-
return animationTrackGO;
2701-
}
2702-
2703-
public static KeyValuePair<GameObject, AnimationClip> GetGameObjectAndAnimationClip(Object obj)
2704-
{
2705-
if (!obj.GetType().Name.Contains("EditorClip"))
2706-
return new KeyValuePair<GameObject, AnimationClip>();
2707-
2708-
object clip = obj.GetType().GetProperty("clip").GetValue(obj, null);
2709-
TimelineClip timeLineClip = clip as TimelineClip;
2710-
2711-
var animationTrackGO = GetGameObjectBoundToEditorClip(obj);
2712-
if (animationTrackGO == null)
2713-
{
2714-
return new KeyValuePair<GameObject, AnimationClip>();
2715-
}
2716-
2717-
return new KeyValuePair<GameObject, AnimationClip>(animationTrackGO, timeLineClip.animationClip);
2718-
}
2719-
2720-
public static string GetFileName(Object obj)
2721-
{
2722-
if (obj.GetType().Name.Contains("EditorClip"))
2723-
{
2724-
object clip = obj.GetType().GetProperty("clip").GetValue(obj, null);
2725-
TimelineClip timeLineClip = clip as TimelineClip;
2726-
2727-
if (timeLineClip.displayName.Contains("@"))
2728-
{
2729-
return timeLineClip.displayName;
2730-
}
2731-
else
2732-
{
2733-
var goBound = GetGameObjectBoundToEditorClip(obj);
2734-
if(goBound == null)
2735-
{
2736-
return null;
2737-
}
2738-
return string.Format ("{0}@{1}", goBound.name, timeLineClip.displayName);
2739-
}
2740-
}
2741-
else
2742-
{
2743-
return obj.name;
2744-
}
2745-
}
2746-
}
2747-
27482585
/// <summary>
27492586
/// Exports all animation clips in the hierarchy along with
27502587
/// the minimum required GameObject information.
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
using UnityEngine.Timeline;
4+
using System.Collections.Generic;
5+
using FbxExporters.EditorTools;
6+
7+
namespace FbxExporters.Editor
8+
{
9+
/// <summary>
10+
/// Export data containing extra information required to export
11+
/// </summary>
12+
public interface IExportData
13+
{
14+
HashSet<GameObject> Objects { get; }
15+
}
16+
17+
/// <summary>
18+
/// Export data containing what to export when
19+
/// exporting animation only.
20+
/// </summary>
21+
internal struct AnimationOnlyExportData : IExportData
22+
{
23+
// map from animation clip to GameObject that has Animation/Animator
24+
// component containing clip
25+
public Dictionary<AnimationClip, GameObject> animationClips;
26+
27+
// set of all GameObjects to export
28+
public HashSet<GameObject> goExportSet;
29+
public HashSet<GameObject> Objects { get { return goExportSet; } }
30+
31+
// map from GameObject to component type to export
32+
public Dictionary<GameObject, System.Type> exportComponent;
33+
34+
// first clip to export
35+
public AnimationClip defaultClip;
36+
37+
public AnimationOnlyExportData(
38+
Dictionary<AnimationClip, GameObject> animClips,
39+
HashSet<GameObject> exportSet,
40+
Dictionary<GameObject, System.Type> exportComponent
41+
)
42+
{
43+
this.animationClips = animClips;
44+
this.goExportSet = exportSet;
45+
this.exportComponent = exportComponent;
46+
this.defaultClip = null;
47+
}
48+
49+
/// <summary>
50+
/// collect all objects dependencies for animation clips.
51+
/// </summary>
52+
public void CollectDependencies(
53+
AnimationClip[] animClips,
54+
GameObject rootObject,
55+
IExportOptions exportOptions
56+
)
57+
{
58+
Debug.Assert(rootObject != null);
59+
Debug.Assert(exportOptions != null);
60+
61+
// NOTE: the object (animationRootObject) containing the animation is not necessarily animated
62+
// when driven by an animator or animation component.
63+
foreach (var animClip in animClips)
64+
{
65+
if (this.animationClips.ContainsKey(animClip))
66+
{
67+
// we have already exported gameobjects for this clip
68+
continue;
69+
}
70+
71+
this.animationClips.Add(animClip, rootObject);
72+
73+
foreach (EditorCurveBinding uniCurveBinding in AnimationUtility.GetCurveBindings(animClip))
74+
{
75+
Object uniObj = AnimationUtility.GetAnimatedObject(rootObject, uniCurveBinding);
76+
if (!uniObj)
77+
{
78+
continue;
79+
}
80+
81+
GameObject unityGo = ModelExporter.GetGameObject(uniObj);
82+
if (!unityGo)
83+
{
84+
continue;
85+
}
86+
87+
if (!exportOptions.AnimateSkinnedMesh && unityGo.GetComponent<SkinnedMeshRenderer>())
88+
{
89+
continue;
90+
}
91+
92+
// If we have a clip driving a camera or light then force the export of FbxNodeAttribute
93+
// so that they point the right way when imported into Maya.
94+
if (unityGo.GetComponent<Light>())
95+
this.exportComponent[unityGo] = typeof(Light);
96+
else if (unityGo.GetComponent<Camera>())
97+
this.exportComponent[unityGo] = typeof(Camera);
98+
99+
this.goExportSet.Add(unityGo);
100+
}
101+
}
102+
}
103+
104+
public static GameObject GetGameObjectBoundToEditorClip(object editorClip)
105+
{
106+
object clipItem = editorClip.GetType().GetProperty("item").GetValue(editorClip, null);
107+
object parentTrack = clipItem.GetType().GetProperty("parentTrack").GetValue(clipItem, null);
108+
AnimationTrack animTrack = parentTrack as AnimationTrack;
109+
110+
#if UNITY_2018_2_OR_NEWER
111+
Object animationTrackObject = UnityEditor.Timeline.TimelineEditor.inspectedDirector.GetGenericBinding(animTrack);
112+
#else // UNITY_2018_2_OR_NEWER
113+
Object animationTrackObject = UnityEditor.Timeline.TimelineEditor.playableDirector.GetGenericBinding(animTrack);
114+
#endif // UNITY_2018_2_OR_NEWER
115+
116+
GameObject animationTrackGO = null;
117+
if (animationTrackObject is GameObject)
118+
{
119+
animationTrackGO = animationTrackObject as GameObject;
120+
}
121+
else if (animationTrackObject is Animator)
122+
{
123+
animationTrackGO = (animationTrackObject as Animator).gameObject;
124+
}
125+
126+
if (animationTrackGO == null)
127+
{
128+
Debug.LogErrorFormat("Could not export animation track object of type {0}", animationTrackObject.GetType().Name);
129+
return null;
130+
}
131+
return animationTrackGO;
132+
}
133+
134+
public static KeyValuePair<GameObject, AnimationClip> GetGameObjectAndAnimationClip(Object obj)
135+
{
136+
if (!obj.GetType().Name.Contains("EditorClip"))
137+
return new KeyValuePair<GameObject, AnimationClip>();
138+
139+
object clip = obj.GetType().GetProperty("clip").GetValue(obj, null);
140+
TimelineClip timeLineClip = clip as TimelineClip;
141+
142+
var animationTrackGO = GetGameObjectBoundToEditorClip(obj);
143+
if (animationTrackGO == null)
144+
{
145+
return new KeyValuePair<GameObject, AnimationClip>();
146+
}
147+
148+
return new KeyValuePair<GameObject, AnimationClip>(animationTrackGO, timeLineClip.animationClip);
149+
}
150+
151+
public static string GetFileName(Object obj)
152+
{
153+
if (obj.GetType().Name.Contains("EditorClip"))
154+
{
155+
object clip = obj.GetType().GetProperty("clip").GetValue(obj, null);
156+
TimelineClip timeLineClip = clip as TimelineClip;
157+
158+
if (timeLineClip.displayName.Contains("@"))
159+
{
160+
return timeLineClip.displayName;
161+
}
162+
else
163+
{
164+
var goBound = GetGameObjectBoundToEditorClip(obj);
165+
if (goBound == null)
166+
{
167+
return null;
168+
}
169+
return string.Format("{0}@{1}", goBound.name, timeLineClip.displayName);
170+
}
171+
}
172+
else
173+
{
174+
return obj.name;
175+
}
176+
}
177+
}
178+
}

Packages/com.unity.formats.fbx/Editor/Scripts/IExportData.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)