Skip to content

Commit b118215

Browse files
committed
Working exporting single timeline clip
1 parent 6c1ebaa commit b118215

File tree

1 file changed

+71
-10
lines changed

1 file changed

+71
-10
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ public class ModelExporter : System.IDisposable
8282
const string MenuItemName = "GameObject/Export Model...";
8383

8484
const string ClipMenuItemName = "GameObject/Export All Recorded Animation Clips...";
85+
const string TimelineClipMenuItemName = "GameObject/Export Selected Timeline Clip...";
86+
8587

8688
const string AnimOnlyMenuItemName = "GameObject/Export Animation Only";
8789

@@ -2497,7 +2499,7 @@ protected int GetAnimOnlyHierarchyCount(HashSet<GameObject> exportSet, Dictionar
24972499
return completeExpSet.Count;
24982500
}
24992501

2500-
protected Dictionary<GameObject, AnimationOnlyExportData> GetTimelineAnimationExportData(GameObject rootObject, AnimationTrack animationTrack)
2502+
protected Dictionary<GameObject, AnimationOnlyExportData> GetAnimationExportDataFromAnimationTrack(GameObject rootObject, AnimationTrack animationTrack)
25012503
{
25022504
// get animation clips for root object from animation track
25032505
List<AnimationClip> clips = new List<AnimationClip>();
@@ -2507,12 +2509,18 @@ protected Dictionary<GameObject, AnimationOnlyExportData> GetTimelineAnimationEx
25072509
clips.Add(myclip.animationClip);
25082510
}
25092511

2512+
return GetTimelineAnimationExportData(rootObject, clips);
2513+
}
2514+
2515+
2516+
protected Dictionary<GameObject, AnimationOnlyExportData> GetTimelineAnimationExportData(GameObject rootObject, List<AnimationClip> animationClipsList)
2517+
{
25102518
var goToExport = new HashSet<GameObject>();
25112519
var animationClips = new Dictionary<AnimationClip, GameObject>();
25122520
var exportComponent = new Dictionary<GameObject, System.Type>();
25132521

25142522
var exportData = new AnimationOnlyExportData(animationClips, goToExport, exportComponent);
2515-
exportData.ComputeObjectsInAnimationClips(clips.ToArray(), rootObject);
2523+
exportData.ComputeObjectsInAnimationClips(animationClipsList.ToArray(), rootObject);
25162524

25172525
Dictionary<GameObject, AnimationOnlyExportData> data = new Dictionary<GameObject, AnimationOnlyExportData>();
25182526
data.Add(rootObject, exportData);
@@ -3048,11 +3056,56 @@ private void ReplaceFile ()
30483056
}
30493057
}
30503058

3059+
[MenuItem(TimelineClipMenuItemName, false, 31)]
3060+
static void OnClipContextClick(MenuCommand command)
3061+
{
3062+
// Now that we know we have stuff to export, get the user-desired path.
3063+
string directory = string.IsNullOrEmpty(LastFilePath)
3064+
? Application.dataPath
3065+
: System.IO.Path.GetDirectoryName(LastFilePath);
3066+
3067+
string title = "Select the folder in which the animation files from the timeline will be exported";
3068+
string folderPath = EditorUtility.SaveFolderPanel(title, directory, "");
3069+
3070+
if (string.IsNullOrEmpty(folderPath))
3071+
{
3072+
return;
3073+
}
3074+
Debug.Log(folderPath);
3075+
3076+
3077+
var selectedObjects = Selection.objects;
3078+
foreach (var obj in selectedObjects)
3079+
{
3080+
if (obj.GetType().Name.Contains("EditorClip"))
3081+
{
3082+
var selClip = obj.GetType().GetProperty("clip").GetValue(obj, null);
3083+
UnityEngine.Timeline.TimelineClip timeLineClip = selClip as UnityEngine.Timeline.TimelineClip;
3084+
3085+
var selClipItem = obj.GetType().GetProperty("item").GetValue(obj, null);
3086+
var selClipItemParentTrack = selClipItem.GetType().GetProperty("parentTrack").GetValue(selClipItem, null);
3087+
AnimationTrack editorClipAnimationTrack = selClipItemParentTrack as AnimationTrack;
3088+
3089+
GameObject animationTrackGObject = UnityEditor.Timeline.TimelineEditor.playableDirector.GetGenericBinding (editorClipAnimationTrack) as GameObject;
3090+
3091+
Debug.Log("obj name: " + obj.name + " /clip name: " + editorClipAnimationTrack.name + " /timelineAssetName: " + animationTrackGObject.name);
3092+
3093+
string filePath = folderPath + "/" + animationTrackGObject.name + "@" + timeLineClip.animationClip.name + ".fbx";
3094+
Debug.Log("filepath: " + filePath);
3095+
UnityEngine.Object[] myArray = new UnityEngine.Object[] { animationTrackGObject, timeLineClip.animationClip };
3096+
3097+
ExportObjects(filePath, myArray, ExportType.timelineAnimationClip);
3098+
}
3099+
}
3100+
}
3101+
3102+
3103+
30513104
/// <summary>
30523105
/// Add an option "Update from FBX" in the contextual GameObject menu.
30533106
/// </summary>
30543107
[MenuItem(ClipMenuItemName, false, 31)]
3055-
static void OnClipContextClick(MenuCommand command)
3108+
static void OnGameObjectWithTimelineContextClick(MenuCommand command)
30563109
{
30573110
// Now that we know we have stuff to export, get the user-desired path.
30583111
string directory = string.IsNullOrEmpty(LastFilePath)
@@ -3102,12 +3155,11 @@ static void OnClipContextClick(MenuCommand command)
31023155
Debug.Log("filepath: " + filePath);
31033156
UnityEngine.Object[] myArray = new UnityEngine.Object[] { atObject, at};
31043157

3105-
ExportObjects(filePath, myArray, ExportType.timelineAnimation);
3158+
ExportObjects(filePath, myArray, ExportType.timelineAnimationTrack);
31063159
}
31073160
}
31083161
}
31093162
}
3110-
31113163

31123164
/// <summary>
31133165
/// Validate the menu item defined by the function above.
@@ -3124,9 +3176,10 @@ public static bool ValidateClipContextClick()
31243176
return false;
31253177
}
31263178

3127-
foreach (GameObject obj in selection)
3179+
foreach (Object obj in selection)
31283180
{
3129-
if (obj.GetComponent<PlayableDirector>() != null)
3181+
GameObject gameObj = obj as GameObject;
3182+
if (gameObj !=null && gameObj.GetComponent<PlayableDirector>() != null)
31303183
{
31313184
return true;
31323185
}
@@ -3622,7 +3675,8 @@ public void Dispose ()
36223675
const string Extension = "fbx";
36233676

36243677
public enum ExportType{
3625-
timelineAnimation,
3678+
timelineAnimationClip,
3679+
timelineAnimationTrack,
36263680
componentAnimation,
36273681
all
36283682
}
@@ -3685,10 +3739,17 @@ public static string ExportObjects (string filePath, UnityEngine.Object[] object
36853739
Dictionary<GameObject, AnimationOnlyExportData> animationExportData = null;
36863740
switch (exportType)
36873741
{
3688-
case ExportType.timelineAnimation:
3742+
case ExportType.timelineAnimationClip:
36893743
GameObject rootObject = ModelExporter.GetGameObject(objects[0]);
3744+
AnimationClip timelineClip = objects[1] as AnimationClip;
3745+
List<AnimationClip> clipList = new List<AnimationClip>();
3746+
clipList.Add(timelineClip);
3747+
animationExportData = fbxExporter.GetTimelineAnimationExportData(rootObject, clipList);
3748+
break;
3749+
case ExportType.timelineAnimationTrack:
3750+
GameObject rootObject2 = ModelExporter.GetGameObject(objects[0]);
36903751
AnimationTrack timelineTrack = objects[1] as AnimationTrack;
3691-
animationExportData = fbxExporter.GetTimelineAnimationExportData(rootObject, timelineTrack);
3752+
animationExportData = fbxExporter.GetAnimationExportDataFromAnimationTrack(rootObject2, timelineTrack);
36923753
break;
36933754
case ExportType.componentAnimation:
36943755
HashSet<GameObject> gos = new HashSet<GameObject>();

0 commit comments

Comments
 (0)