Skip to content

Commit f0e7a20

Browse files
committed
quick test - ConvertToModel
1 parent f5db3e1 commit f0e7a20

File tree

2 files changed

+161
-21
lines changed

2 files changed

+161
-21
lines changed

Assets/Editor/ConvertToModel.cs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// ***********************************************************************
2+
// Copyright (c) 2017 Unity Technologies. All rights reserved.
3+
//
4+
// Licensed under the ##LICENSENAME##.
5+
// See LICENSE.md file in the project root for full license information.
6+
// ***********************************************************************
7+
8+
using System.IO;
9+
using System.Collections.Generic;
10+
using UnityEngine;
11+
using UnityEngine.SceneManagement;
12+
using UnityEditor;
13+
using FbxSdk;
14+
15+
namespace FbxExporters
16+
{
17+
namespace Editor
18+
{
19+
public class ConvertToModel : System.IDisposable
20+
{
21+
const string MenuItemName1 = "Assets/Convert To Model";
22+
const string MenuItemName2 = "GameObject/Convert To Model";
23+
24+
/// <summary>
25+
/// Clean up this class on garbage collection
26+
/// </summary>
27+
public void Dispose () { }
28+
29+
/// <summary>
30+
/// create menu item in the File menu
31+
/// </summary>
32+
[MenuItem (MenuItemName1, false)]
33+
public static void OnMenuItem ()
34+
{
35+
OnConvertInPlace ();
36+
}
37+
38+
/// <summary>
39+
// Validate the menu item defined by the function above.
40+
/// </summary>
41+
[MenuItem (MenuItemName1, true)]
42+
public static bool OnValidateMenuItem ()
43+
{
44+
return true;
45+
}
46+
47+
// Add a menu item called "Export Model..." to a GameObject's context menu.
48+
[MenuItem (MenuItemName2, false, 30)]
49+
static void OnContextItem (MenuCommand command)
50+
{
51+
OnConvertInPlace ();
52+
}
53+
54+
private static List<GameObject> OnConvertInPlace ()
55+
{
56+
List<GameObject> result = new List<GameObject> ();
57+
58+
GameObject[] unityActiveGOs = Selection.GetFiltered<GameObject>(SelectionMode.Editable | SelectionMode.TopLevel);
59+
60+
// find common ancestor root & filePath;
61+
string filePath = "";
62+
string dirPath = Path.Combine (Application.dataPath, "Objects");
63+
64+
GameObject unityCommonAncestor = null;
65+
int siblingIndex = -1;
66+
67+
foreach (GameObject goObj in unityActiveGOs)
68+
{
69+
siblingIndex = goObj.transform.GetSiblingIndex ();
70+
unityCommonAncestor = goObj.transform.parent.gameObject;
71+
filePath = Path.Combine (dirPath, goObj.name + ".fbx");
72+
73+
break;
74+
}
75+
76+
string fbxFileName = FbxExporters.Editor.ModelExporter.ExportObjects (filePath, unityActiveGOs) as string;
77+
78+
if (fbxFileName!=null)
79+
{
80+
// make filepath relative to project folder
81+
if (fbxFileName.StartsWith(Application.dataPath, System.StringComparison.CurrentCulture)) {
82+
fbxFileName = "Assets" + fbxFileName.Substring(Application.dataPath.Length);
83+
}
84+
85+
// refresh the assetdata base so that we can query for the model
86+
AssetDatabase.Refresh ();
87+
88+
// replace w Model asset
89+
Object unityMainAsset = AssetDatabase.LoadMainAssetAtPath (fbxFileName);
90+
91+
if (unityMainAsset != null) {
92+
Transform unityParentTransform =
93+
(unityCommonAncestor==null) ? null : unityCommonAncestor.transform;
94+
95+
Object unityObj = PrefabUtility.InstantiateAttachedAsset(unityMainAsset);
96+
97+
if (unityObj!=null)
98+
{
99+
GameObject unityGO = unityObj as GameObject;
100+
101+
// configure name
102+
const string cloneSuffix = "(Clone)";
103+
104+
if (unityGO.name.EndsWith(cloneSuffix,System.StringComparison.CurrentCulture))
105+
{
106+
unityGO.name = unityGO.name.Remove(cloneSuffix.Length-1);
107+
}
108+
109+
// configure transform
110+
unityGO.transform.parent = unityParentTransform;
111+
unityGO.transform.SetSiblingIndex (siblingIndex);
112+
113+
result.Add (unityObj as GameObject);
114+
115+
// remove (now redundant) gameobjects
116+
for (int i = 0; i < unityActiveGOs.Length; i++) {
117+
unityActiveGOs [i].name = "_safe_to_delete_" + unityActiveGOs [i].name;
118+
// Object.DestroyImmediate(unityActiveGOs[i]);
119+
}
120+
}
121+
}
122+
123+
}
124+
return result;
125+
}
126+
}
127+
}
128+
}

Assets/Editor/FbxExporter.cs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//#define UNI_15935
21
// ***********************************************************************
32
// Copyright (c) 2017 Unity Technologies. All rights reserved.
43
//
@@ -31,7 +30,7 @@ public class ModelExporter : System.IDisposable
3130
const string Comments =
3231
@"";
3332

34-
const string MenuItemName = "Assets/Export Model... %e";
33+
const string MenuItemName = "Assets/Export Model...";
3534

3635
const string FileBaseName = "Untitled";
3736

@@ -348,9 +347,6 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
348347
}
349348
}
350349

351-
//
352-
// Create a simple user interface (menu items)
353-
//
354350
/// <summary>
355351
/// create menu item in the File menu
356352
/// </summary>
@@ -360,13 +356,6 @@ public static void OnMenuItem ()
360356
OnExport();
361357
}
362358

363-
// Add a menu item called "Export Model..." to a GameObject's context menu.
364-
[MenuItem ("GameObject/Export Model... %e", false, 30)]
365-
static void OnContextItem (MenuCommand command)
366-
{
367-
OnExport ();
368-
}
369-
370359
/// <summary>
371360
// Validate the menu item defined by the function above.
372361
/// </summary>
@@ -376,6 +365,13 @@ public static bool OnValidateMenuItem ()
376365
return true;
377366
}
378367

368+
// Add a menu item called "Export Model..." to a GameObject's context menu.
369+
[MenuItem ("GameObject/Export Model... %e", false, 30)]
370+
static void OnContextItem (MenuCommand command)
371+
{
372+
OnExport ();
373+
}
374+
379375
//
380376
// export mesh info from Unity
381377
//
@@ -591,44 +587,60 @@ private static string MakeFileName(string basename = "test", string extension =
591587
return basename + "." + extension;
592588
}
593589

594-
// use the SaveFile panel to allow user to enter a file name
595-
private static void OnExport(object objects = null)
590+
private static void OnExport ()
596591
{
597592
// Now that we know we have stuff to export, get the user-desired path.
598593
var directory = string.IsNullOrEmpty (LastFilePath)
599-
? Application.dataPath
600-
: System.IO.Path.GetDirectoryName (LastFilePath);
594+
? Application.dataPath
595+
: System.IO.Path.GetDirectoryName (LastFilePath);
601596

602597
var filename = string.IsNullOrEmpty (LastFilePath)
603-
? MakeFileName(basename: FileBaseName, extension: Extension)
604-
: System.IO.Path.GetFileName (LastFilePath);
598+
? MakeFileName (basename: FileBaseName, extension: Extension)
599+
: System.IO.Path.GetFileName (LastFilePath);
605600

606601
var title = string.Format ("Export Model FBX ({0})", FileBaseName);
607602

608603
var filePath = EditorUtility.SaveFilePanel (title, directory, filename, "");
609604

610605
if (string.IsNullOrEmpty (filePath)) {
611-
return;
606+
return ;
607+
}
608+
609+
if (ExportObjects (filePath)!=null)
610+
{
611+
// refresh the asset database so that the file appears in the
612+
// asset folder view.
613+
AssetDatabase.Refresh ();
612614
}
615+
}
613616

617+
/// <summary>
618+
/// Export a list of (Game) objects to FBX file.
619+
/// Use the SaveFile panel to allow user to enter a file name.
620+
/// <summary>
621+
public static object ExportObjects(string filePath, UnityEngine.Object[] objects = null)
622+
{
614623
LastFilePath = filePath;
615624

616625
using (var fbxExporter = Create())
617626
{
618627
// ensure output directory exists
619628
EnsureDirectory (filePath);
620629

621-
if (objects==null)
630+
if (objects == null)
622631
{
623632
objects = Selection.objects;
624633
}
625634

626-
if (fbxExporter.ExportAll(objects as IEnumerable<UnityEngine.Object>) > 0)
635+
if (fbxExporter.ExportAll(objects) > 0)
627636
{
628637
string message = string.Format ("Successfully exported: {0}", filePath);
629638
UnityEngine.Debug.Log (message);
639+
640+
return filePath;
630641
}
631642
}
643+
return null;
632644
}
633645

634646
private static void EnsureDirectory(string path)

0 commit comments

Comments
 (0)