Skip to content

Commit 04cf3da

Browse files
committed
add menu item to export anim only
1 parent 44a9cc3 commit 04cf3da

File tree

1 file changed

+51
-35
lines changed

1 file changed

+51
-35
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public class ModelExporter : System.IDisposable
8080
// being called only once regardless of what is selected.
8181
const string MenuItemName = "GameObject/Export Model...";
8282

83+
const string AnimOnlyMenuItemName = "GameObject/Export Animation Only";
84+
8385
const string FileBaseName = "Untitled";
8486

8587
const string ProgressBarTitle = "Fbx Export";
@@ -2722,7 +2724,7 @@ public enum TransformExportType { Local, Global, Reset };
27222724
///
27232725
/// This refreshes the asset database.
27242726
/// </summary>
2725-
public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
2727+
public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet, bool animOnly = false)
27262728
{
27272729
exportCancelled = false;
27282730

@@ -2809,7 +2811,15 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
28092811
// stores how many objects we have exported, -1 if export was cancelled
28102812
int exportProgress = 0;
28112813
var revisedExportSet = RemoveRedundantObjects(unityExportSet);
2812-
int count = GetHierarchyCount (revisedExportSet);
2814+
2815+
Dictionary<GameObject, AnimationOnlyExportData> exportData;
2816+
int count = 0;
2817+
if(animOnly){
2818+
count = GetAnimOnlyHierarchyCount(revisedExportSet, out exportData);
2819+
} else {
2820+
count = GetHierarchyCount (revisedExportSet);
2821+
exportData = new Dictionary<GameObject, AnimationOnlyExportData>();
2822+
}
28132823

28142824
Vector3 center = Vector3.zero;
28152825
var exportType = TransformExportType.Reset;
@@ -2819,42 +2829,25 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
28192829
}
28202830

28212831
foreach (var unityGo in revisedExportSet) {
2822-
exportProgress = this.ExportTransformHierarchy (unityGo, fbxScene, fbxRootNode,
2823-
exportProgress, count, center, exportType);
2832+
AnimationOnlyExportData data;
2833+
if(animOnly && exportData.TryGetValue(unityGo, out data)){
2834+
exportProgress = this.ExportAnimationOnly(unityGo, fbxScene, exportProgress, count, center, data, exportType);
2835+
}
2836+
else {
2837+
exportProgress = this.ExportTransformHierarchy (unityGo, fbxScene, fbxRootNode,
2838+
exportProgress, count, center, exportType);
2839+
}
28242840
if (exportCancelled || exportProgress < 0) {
28252841
Debug.LogWarning ("Export Cancelled");
28262842
return 0;
28272843
}
28282844
}
28292845

2830-
/*if(revisedExportSet.Count == 1){
2831-
foreach(var unityGo in revisedExportSet){
2832-
exportProgress = this.ExportTransformHierarchy (
2833-
unityGo, fbxScene, fbxRootNode, exportProgress,
2834-
count, Vector3.zero, TransformExportType.Reset);
2835-
if (exportCancelled || exportProgress < 0) {
2836-
Debug.LogWarning ("Export Cancelled");
2837-
return 0;
2838-
}
2839-
}
2840-
}
2841-
else{
2842-
// find the center of the export set
2843-
Vector3 center = ExportSettings.centerObjects? FindCenter(revisedExportSet) : Vector3.zero;
2844-
2845-
foreach (var unityGo in revisedExportSet) {
2846-
exportProgress = this.ExportTransformHierarchy (unityGo, fbxScene, fbxRootNode,
2847-
exportProgress, count, center, TransformExportType.Global);
2848-
if (exportCancelled || exportProgress < 0) {
2849-
Debug.LogWarning ("Export Cancelled");
2850-
return 0;
2851-
}
2846+
if(!animOnly){
2847+
if(!ExportComponents(fbxScene)){
2848+
Debug.LogWarning ("Export Cancelled");
2849+
return 0;
28522850
}
2853-
}*/
2854-
2855-
if(!ExportComponents(fbxScene)){
2856-
Debug.LogWarning ("Export Cancelled");
2857-
return 0;
28582851
}
28592852

28602853
// Set the scene's default camera.
@@ -2981,6 +2974,29 @@ public static bool OnValidateMenuItem ()
29812974
return true;
29822975
}
29832976

2977+
/// <summary>
2978+
/// Add a menu item to a GameObject's context menu.
2979+
/// </summary>
2980+
/// <param name="command">Command.</param>
2981+
[MenuItem (AnimOnlyMenuItemName, false, 30)]
2982+
static void OnAnimOnlyContextItem (MenuCommand command)
2983+
{
2984+
if (Selection.objects.Length <= 0) {
2985+
DisplayNoSelectionDialog ();
2986+
return;
2987+
}
2988+
OnExport (animOnly:true);
2989+
}
2990+
2991+
/// <summary>
2992+
// Validate the menu item defined by the function above.
2993+
/// </summary>
2994+
[MenuItem (AnimOnlyMenuItemName, true, 30)]
2995+
public static bool OnValidateAnimOnlyMenuItem ()
2996+
{
2997+
return true;
2998+
}
2999+
29843000
public static void DisplayNoSelectionDialog()
29853001
{
29863002
UnityEditor.EditorUtility.DisplayDialog (
@@ -3424,7 +3440,7 @@ private static string MakeFileName (string basename = "test", string extension =
34243440
return basename + "." + extension;
34253441
}
34263442

3427-
private static void OnExport ()
3443+
private static void OnExport (bool animOnly = false)
34283444
{
34293445
// Now that we know we have stuff to export, get the user-desired path.
34303446
var directory = string.IsNullOrEmpty (LastFilePath)
@@ -3449,7 +3465,7 @@ private static void OnExport ()
34493465
return;
34503466
}
34513467

3452-
if (ExportObjects (filePath) != null) {
3468+
if (ExportObjects (filePath, animOnly:animOnly) != null) {
34533469
// refresh the asset database so that the file appears in the
34543470
// asset folder view.
34553471
AssetDatabase.Refresh ();
@@ -3460,7 +3476,7 @@ private static void OnExport ()
34603476
/// Export a list of (Game) objects to FBX file.
34613477
/// Use the SaveFile panel to allow user to enter a file name.
34623478
/// <summary>
3463-
public static string ExportObjects (string filePath, UnityEngine.Object[] objects = null)
3479+
public static string ExportObjects (string filePath, UnityEngine.Object[] objects = null, bool animOnly = false)
34643480
{
34653481
LastFilePath = filePath;
34663482

@@ -3472,7 +3488,7 @@ public static string ExportObjects (string filePath, UnityEngine.Object[] object
34723488
objects = Selection.objects;
34733489
}
34743490

3475-
if (fbxExporter.ExportAll (objects) > 0) {
3491+
if (fbxExporter.ExportAll (objects, animOnly) > 0) {
34763492
string message = string.Format ("Successfully exported: {0}", filePath);
34773493
UnityEngine.Debug.Log (message);
34783494

0 commit comments

Comments
 (0)