Skip to content

Commit 95f2155

Browse files
authored
Merge pull request #4 from Unity-Technologies/Uni-20530-maintain-localpose
ensure converted model stays in same place as original
2 parents 5ef6269 + d7674e9 commit 95f2155

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class ConvertToModel : System.IDisposable
2020
{
2121
const string MenuItemName1 = "Assets/Convert To Model";
2222
const string MenuItemName2 = "GameObject/Convert To Model";
23-
23+
2424
/// <summary>
2525
/// Clean up this class on garbage collection
2626
/// </summary>
@@ -32,7 +32,7 @@ public void Dispose () { }
3232
[MenuItem (MenuItemName1, false)]
3333
public static void OnMenuItem ()
3434
{
35-
OnConvertInPlace ();
35+
OnConvertInPlace ();
3636
}
3737

3838
/// <summary>
@@ -41,21 +41,21 @@ public static void OnMenuItem ()
4141
[MenuItem (MenuItemName1, true)]
4242
public static bool OnValidateMenuItem ()
4343
{
44-
return true;
44+
return true;
4545
}
4646

4747
// Add a menu item called "Export Model..." to a GameObject's context menu.
4848
[MenuItem (MenuItemName2, false, 30)]
4949
static void OnContextItem (MenuCommand command)
5050
{
51-
OnConvertInPlace ();
51+
OnConvertInPlace ();
5252
}
5353

5454
private static List<GameObject> OnConvertInPlace ()
5555
{
5656
List<GameObject> result = new List<GameObject> ();
5757

58-
GameObject[] unityActiveGOs = Selection.GetFiltered<GameObject>(SelectionMode.Editable | SelectionMode.TopLevel);
58+
GameObject [] unityActiveGOs = Selection.GetFiltered<GameObject> (SelectionMode.Editable | SelectionMode.TopLevel);
5959

6060
// find common ancestor root & filePath;
6161
string filePath = "";
@@ -64,22 +64,22 @@ private static List<GameObject> OnConvertInPlace ()
6464
GameObject unityCommonAncestor = null;
6565
int siblingIndex = -1;
6666

67-
foreach (GameObject goObj in unityActiveGOs)
68-
{
67+
foreach (GameObject goObj in unityActiveGOs) {
6968
siblingIndex = goObj.transform.GetSiblingIndex ();
70-
unityCommonAncestor = goObj.transform.parent.gameObject;
69+
unityCommonAncestor = (goObj.transform.parent != null) ? goObj.transform.parent.gameObject : null;
7170
filePath = Path.Combine (dirPath, goObj.name + ".fbx");
7271

7372
break;
7473
}
7574

7675
string fbxFileName = FbxExporters.Editor.ModelExporter.ExportObjects (filePath, unityActiveGOs) as string;
7776

78-
if (fbxFileName!=null)
77+
if (fbxFileName != null)
7978
{
8079
// make filepath relative to project folder
81-
if (fbxFileName.StartsWith(Application.dataPath, System.StringComparison.CurrentCulture)) {
82-
fbxFileName = "Assets" + fbxFileName.Substring(Application.dataPath.Length);
80+
if (fbxFileName.StartsWith (Application.dataPath, System.StringComparison.CurrentCulture))
81+
{
82+
fbxFileName = "Assets" + fbxFileName.Substring (Application.dataPath.Length);
8383
}
8484

8585
// refresh the assetdata base so that we can query for the model
@@ -89,38 +89,47 @@ private static List<GameObject> OnConvertInPlace ()
8989
Object unityMainAsset = AssetDatabase.LoadMainAssetAtPath (fbxFileName);
9090

9191
if (unityMainAsset != null) {
92-
Transform unityParentTransform =
93-
(unityCommonAncestor==null) ? null : unityCommonAncestor.transform;
92+
Object unityObj = PrefabUtility.InstantiateAttachedAsset (unityMainAsset);
9493

95-
Object unityObj = PrefabUtility.InstantiateAttachedAsset(unityMainAsset);
96-
97-
if (unityObj!=null)
94+
if (unityObj != null)
9895
{
9996
GameObject unityGO = unityObj as GameObject;
10097

10198
// configure name
10299
const string cloneSuffix = "(Clone)";
103100

104-
if (unityGO.name.EndsWith(cloneSuffix,System.StringComparison.CurrentCulture))
105-
{
106-
unityGO.name = unityGO.name.Remove(cloneSuffix.Length-1);
101+
if (unityGO.name.EndsWith (cloneSuffix, System.StringComparison.CurrentCulture)) {
102+
unityGO.name = unityGO.name.Remove (cloneSuffix.Length - 1);
103+
}
104+
105+
// configure transform and maintain local pose
106+
if (unityCommonAncestor != null) {
107+
unityGO.transform.SetParent (unityCommonAncestor.transform, false);
107108
}
108109

109-
// configure transform
110-
unityGO.transform.parent = unityParentTransform;
111110
unityGO.transform.SetSiblingIndex (siblingIndex);
112111

113112
result.Add (unityObj as GameObject);
114113

115114
// remove (now redundant) gameobjects
116115
for (int i = 0; i < unityActiveGOs.Length; i++) {
116+
#if UNI_19965
117+
Object.DestroyImmediate (unityActiveGOs [i]);
118+
#else
119+
// rename and put under scene root in case we need to check values
117120
unityActiveGOs [i].name = "_safe_to_delete_" + unityActiveGOs [i].name;
118-
// Object.DestroyImmediate(unityActiveGOs[i]);
121+
unityActiveGOs [i].transform.parent = null;
122+
unityActiveGOs [i].SetActive (false);
123+
#endif
119124
}
125+
126+
// select the instanced Model Prefab
127+
Selection.objects = new GameObject[] {unityGO};
120128
}
121129
}
122130

123131
}
132+
124133
return result;
125134
}
126135
}

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ public FbxSurfaceMaterial ExportMaterial (Material unityMaterial, FbxScene fbxSc
163163
fbxMaterial.Diffuse.Set(GetMaterialColor(unityMaterial, "_Color"));
164164
fbxMaterial.Emissive.Set(GetMaterialColor(unityMaterial, "_EmissionColor"));
165165
fbxMaterial.Ambient.Set(new FbxDouble3 ());
166-
fbxMaterial.BumpFactor.Set (unityMaterial ? unityMaterial.GetFloat ("_BumpScale") : 0);
166+
167+
fbxMaterial.BumpFactor.Set (unityMaterial && unityMaterial.HasProperty("_BumpScale") ? unityMaterial.GetFloat ("_BumpScale") : 0);
168+
167169
if (specular) {
168170
(fbxMaterial as FbxSurfacePhong).Specular.Set(GetMaterialColor(unityMaterial, "_SpecColor"));
169171
}

0 commit comments

Comments
 (0)