Skip to content

Commit d12651d

Browse files
authored
Merge pull request #408 from Unity-Technologies/UNI-47704-refactor-editorclip-export-iexportdata
Uni 47704 refactor editorclip export (IExportData)
2 parents 6fb258a + 1e38467 commit d12651d

File tree

7 files changed

+531
-425
lines changed

7 files changed

+531
-425
lines changed
Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using UnityEngine;
2-
using UnityEditor;
32
using NUnit.Framework;
43
using FbxExporters.Editor;
54
using UnityEngine.Timeline;
65
using UnityEngine.Playables;
76
using UnityEditor.SceneManagement;
7+
using System.Collections.Generic;
88

99
namespace FbxExporters.UnitTests
1010
{
@@ -22,35 +22,31 @@ public void ExportSingleTimelineClipTest()
2222
{
2323
GameObject myCube = GameObject.Find("CubeSpecial");
2424
string folderPath = GetRandomFileNamePath(extName: "");
25+
string filePath = null;
26+
var exportData = new Dictionary<GameObject, IExportData>();
2527

2628
PlayableDirector pd = myCube.GetComponent<PlayableDirector> ();
2729
if (pd != null) {
2830
foreach (PlayableBinding output in pd.playableAsset.outputs) {
2931
AnimationTrack at = output.sourceObject as AnimationTrack;
3032

3133
GameObject atObject = pd.GetGenericBinding (output.sourceObject) as GameObject;
34+
Assert.That (atObject, Is.Not.Null);
35+
3236
// One file by animation clip
3337
foreach (TimelineClip timeLineClip in at.GetClips()) {
34-
var filePath = string.Format ("{0}/{1}@{2}", folderPath, atObject.name, "Recorded.fbx");
35-
ModelExporterReflection.ExportSingleTimelineClip (timeLineClip, atObject, filePath);
36-
FileAssert.Exists (filePath);
38+
Assert.That (timeLineClip.animationClip, Is.Not.Null);
39+
40+
filePath = string.Format ("{0}/{1}@{2}", folderPath, atObject.name, "Recorded.fbx");
41+
exportData[atObject] = ModelExporterReflection.GetExportData(atObject, timeLineClip.animationClip);
42+
break;
3743
}
3844
}
3945
}
40-
}
41-
42-
[Test]
43-
public void ExportAllTimelineClipTest()
44-
{
45-
GameObject myCube = GameObject.Find("CubeSpecial");
46-
Selection.objects = new UnityEngine.GameObject[] { myCube };
47-
string folderPath = GetRandomFileNamePath(extName: "");
48-
49-
foreach(GameObject obj in Selection.objects)
50-
{
51-
ModelExporterReflection.ExportAllTimelineClips(obj, folderPath);
52-
FileAssert.Exists(string.Format("{0}/{1}@{2}", folderPath, obj.name, "Recorded.fbx"));
53-
}
46+
Assert.That (filePath, Is.Not.Null);
47+
Assert.That (exportData, Is.Not.Null);
48+
ModelExporterReflection.ExportObjects(filePath, new Object[1]{myCube}, null, exportData);
49+
FileAssert.Exists (filePath);
5450
}
5551
}
5652
}

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
using FbxExporters.Editor;
2-
using FbxExporters.EditorTools;
32
using System;
43
using System.Linq;
54
using System.Collections.Generic;
65
using System.Reflection;
76
using Unity.FbxSdk;
87
using UnityEngine;
9-
using UnityEngine.Timeline;
108

119
namespace FbxExporters.UnitTests
1210
{
@@ -94,6 +92,23 @@ public static bool ExportMaterial(ModelExporter instance, Material unityMaterial
9492
return (bool)InvokeMethod("ExportMaterial", new object[] {unityMaterial,fbxScene,fbxNode},instance);
9593
}
9694

95+
public static string ExportObjects(string filePath,
96+
UnityEngine.Object[] objects = null,
97+
EditorTools.IExportOptions exportOptions = null,
98+
Dictionary<GameObject, IExportData> exportData = null)
99+
{
100+
return (string)InvokeMethodOverload("ExportObjects",
101+
new object[] { filePath, objects, exportOptions, exportData },
102+
new Type[] { typeof(string), typeof(UnityEngine.Object[]), typeof(EditorTools.IExportOptions), typeof(Dictionary<GameObject, IExportData>) });
103+
}
104+
105+
public static IExportData GetExportData(GameObject rootObject, AnimationClip animationClip, EditorTools.IExportOptions exportOptions = null)
106+
{
107+
return (IExportData)InvokeMethodOverload("GetExportData",
108+
new object[] { rootObject, animationClip, exportOptions },
109+
new Type[] { typeof(GameObject), typeof(AnimationClip), typeof(EditorTools.IExportOptions) });
110+
}
111+
97112
// Redefinition of the internal delegate. There might be a way to re-use the one in ModelExporter
98113
public static void RegisterMeshObjectCallback(ModelExporter.GetMeshForObject callback)
99114
{
@@ -161,18 +176,6 @@ public static bool ExportMesh(ModelExporter instance, Mesh mesh, FbxNode fbxNode
161176

162177
}
163178

164-
public static void ExportSingleTimelineClip(TimelineClip timelineClipSelected, GameObject animationTrackGObject, string filePath = null)
165-
{
166-
InvokeMethod("ExportSingleTimelineClip",
167-
new object[] {timelineClipSelected, animationTrackGObject, filePath});
168-
}
169-
170-
public static void ExportAllTimelineClips(GameObject objectWithPlayableDirector, string folderPath, IExportOptions exportOptions = null)
171-
{
172-
InvokeMethod("ExportAllTimelineClips",
173-
new object[] {objectWithPlayableDirector, folderPath, exportOptions});
174-
}
175-
176179
/////////// Helpers ///////////
177180
private static object InvokeMethod(string methodName, object[] argsToPass, ModelExporter instance = null)
178181
{

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

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ protected bool IsInSameHierarchy(Transform t1, Transform t2){
188188
return (IsAncestor (t1, t2) || IsAncestor (t2, t1));
189189
}
190190

191+
protected virtual GameObject GetGameObject()
192+
{
193+
return ModelExporter.GetGameObject ( ToExport [0] );
194+
}
191195

192196
protected bool TransferAnimationSourceIsValid(Transform newValue){
193197
if (!newValue) {
@@ -199,7 +203,7 @@ protected bool TransferAnimationSourceIsValid(Transform newValue){
199203
return false;
200204
}
201205

202-
var selectedGO = ModelExporter.GetGameObject(ToExport[0]);
206+
var selectedGO = GetGameObject();
203207

204208
// source must be ancestor to dest
205209
if (TransferAnimationDest && !IsAncestor(newValue, TransferAnimationDest)) {
@@ -224,7 +228,7 @@ protected bool TransferAnimationDestIsValid(Transform newValue){
224228
return false;
225229
}
226230

227-
var selectedGO = ModelExporter.GetGameObject(ToExport[0]);
231+
var selectedGO = GetGameObject();
228232

229233
// source must be ancestor to dest
230234
if (TransferAnimationSource && !IsAncestor(TransferAnimationSource, newValue)) {
@@ -396,15 +400,23 @@ public class ExportModelEditorWindow : ExportOptionsEditorWindow
396400
protected override float MinWindowHeight { get { return 310; } } // determined by trial and error
397401
protected override bool DisableNameSelection {
398402
get {
399-
return IsPlayableDirector;
403+
return false;
400404
}
401405
}
406+
407+
protected override GameObject GetGameObject()
408+
{
409+
return (IsTimelineAnim)
410+
? AnimationOnlyExportData.GetGameObjectAndAnimationClip(ToExport [0]).Key
411+
: ModelExporter.GetGameObject ( ToExport [0] );
412+
}
413+
402414
protected override bool DisableTransferAnim {
403415
get {
404416
// don't transfer animation if we are exporting more than one hierarchy, the timeline clips from
405417
// a playable director, or if only the model is being exported
406418
// if we are on the timeline then export length can be more than 1
407-
return ToExport == null || ToExport.Length == 0 || (!IsTimelineAnim && ToExport.Length > 1) || IsPlayableDirector || SettingsObject.ModelAnimIncludeOption == ExportSettings.Include.Model;
419+
return ToExport == null || ToExport.Length == 0 || (!IsTimelineAnim && ToExport.Length > 1) || SettingsObject.ModelAnimIncludeOption == ExportSettings.Include.Model;
408420
}
409421
}
410422

@@ -441,26 +453,17 @@ protected bool SingleHierarchyExport {
441453
}
442454
}
443455

444-
private bool m_isPlayableDirector = false;
445-
protected bool IsPlayableDirector {
446-
get { return m_isPlayableDirector; }
447-
set {
448-
m_isPlayableDirector = value;
449-
}
450-
}
451-
452456
protected override ExportOptionsSettingsSerializeBase SettingsObject
453457
{
454458
get { return ExportSettings.instance.exportModelSettings.info; }
455459
}
456460

457461
private ExportSettings.Include m_previousInclude = ExportSettings.Include.ModelAndAnim;
458462

459-
public static void Init (IEnumerable<UnityEngine.Object> toExport, string filename = "", bool isTimelineAnim = false, bool isPlayableDirector = false)
463+
public static void Init (IEnumerable<UnityEngine.Object> toExport, string filename = "", bool isTimelineAnim = false)
460464
{
461465
ExportModelEditorWindow window = CreateWindow<ExportModelEditorWindow> ();
462466
window.IsTimelineAnim = isTimelineAnim;
463-
window.IsPlayableDirector = isPlayableDirector;
464467

465468
int numObjects = window.SetGameObjectsToExport (toExport);
466469
if (string.IsNullOrEmpty (filename)) {
@@ -473,19 +476,21 @@ public static void Init (IEnumerable<UnityEngine.Object> toExport, string filena
473476

474477
protected int SetGameObjectsToExport(IEnumerable<UnityEngine.Object> toExport){
475478
ToExport = toExport.ToArray ();
479+
if (ToExport.Length==0) return 0;
476480

477481
TransferAnimationSource = null;
478482
TransferAnimationDest = null;
479483

480484
// if only one object selected, set transfer source/dest to this object
481-
if (ToExport.Length == 1 || (IsTimelineAnim && ToExport.Length > 0)) {
482-
var go = ModelExporter.GetGameObject (ToExport [0]);
483-
if (go) {
485+
if (ToExport.Length == 1 || (IsTimelineAnim && ToExport.Length > 0))
486+
{
487+
GameObject go = GetGameObject();
488+
if (go)
489+
{
484490
TransferAnimationSource = go.transform;
485491
TransferAnimationDest = go.transform;
486492
}
487493
}
488-
489494
return ToExport.Length;
490495
}
491496

@@ -543,20 +548,6 @@ protected override bool Export(){
543548
return false;
544549
}
545550

546-
if (IsPlayableDirector) {
547-
foreach (var obj in ToExport) {
548-
var go = ModelExporter.GetGameObject (obj);
549-
if (!go) {
550-
continue;
551-
}
552-
ModelExporter.ExportAllTimelineClips (go, folderPath, SettingsObject);
553-
}
554-
// refresh the asset database so that the file appears in the
555-
// asset folder view.
556-
AssetDatabase.Refresh ();
557-
return true;
558-
}
559-
560551
if (ModelExporter.ExportObjects (filePath, ToExport, SettingsObject) != null) {
561552
// refresh the asset database so that the file appears in the
562553
// asset folder view.

0 commit comments

Comments
 (0)