Skip to content

Commit 972f1cf

Browse files
committed
OBJ export moved into runtime so it can be used by URDF
1 parent a0fbbef commit 972f1cf

File tree

8 files changed

+290
-91
lines changed

8 files changed

+290
-91
lines changed

Editor/ZOExportOBJEditor.cs

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+

2+
using UnityEngine;
3+
using UnityEditor;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Text;
7+
using ZO.ImportExport;
8+
9+
namespace ZO.Export {
10+
11+
public class ZOExportOBJEditor : EditorWindow {
12+
public bool _exportSubMeshes = true;
13+
public bool _zeroPosition = true;
14+
private string _selectedNames;
15+
16+
[MenuItem("Zero Sim/Export OBJ...")]
17+
public static void DoOBJExport() {
18+
//EditorWindow.GetWindow<ZOExportOBJ>();
19+
ZOExportOBJEditor window = ScriptableObject.CreateInstance<ZOExportOBJEditor>();
20+
window.ShowUtility();
21+
22+
}
23+
24+
private void OnGUI() {
25+
_exportSubMeshes = EditorGUILayout.Toggle("Export Sub Meshes: ", _exportSubMeshes);
26+
_zeroPosition = EditorGUILayout.Toggle("Zero Position: ", _zeroPosition);
27+
28+
foreach (var t in Selection.objects) {
29+
_selectedNames += t.name + " ";
30+
}
31+
EditorGUILayout.LabelField("Selected Objects: ", _selectedNames);
32+
33+
_selectedNames = "";
34+
35+
EditorGUI.BeginDisabledGroup(Selection.gameObjects.Length == 0);
36+
if (GUILayout.Button("Export OBJ")) {
37+
string meshName = Selection.gameObjects[0].name;
38+
string fileDirectory = EditorUtility.OpenFolderPanel("Export .OBJ to directory", "", "");
39+
40+
DoExport(_exportSubMeshes, fileDirectory);
41+
}
42+
EditorGUI.EndDisabledGroup();
43+
44+
}
45+
46+
private void OnInspectorUpdate() {
47+
Repaint();
48+
}
49+
50+
public static void DoExport(bool makeSubmeshes, string directoryPath, bool zeroPosition = true, GameObject gameObject = null) {
51+
52+
if (gameObject == null && Selection.gameObjects.Length == 0) {
53+
Debug.Log("ERROR: Didn't Export Any Meshes; Nothing was selected!");
54+
return;
55+
}
56+
57+
if (gameObject == null) {
58+
gameObject = Selection.gameObjects[0];
59+
}
60+
ZOExportOBJ exportOBJ = new ZOExportOBJ();
61+
exportOBJ.ExportToDirectory(gameObject, directoryPath, makeSubmeshes, zeroPosition);
62+
63+
// string meshName = gameObject.name;
64+
65+
// ZOExportOBJ.Start();
66+
67+
// StringBuilder meshString = new StringBuilder();
68+
69+
// meshString.Append("#" + meshName + ".obj"
70+
// + "\n#" + System.DateTime.Now.ToLongDateString()
71+
// + "\n#" + System.DateTime.Now.ToLongTimeString()
72+
// + "\n#-------"
73+
// + "\n\n");
74+
75+
// meshString.AppendLine($"mtllib {meshName}.mtl");
76+
77+
// Transform transform = gameObject.transform;
78+
79+
// Vector3 originalPosition = transform.position;
80+
// if (zeroPosition == true) {
81+
// transform.position = Vector3.zero;
82+
// }
83+
84+
// if (!makeSubmeshes) {
85+
// meshString.Append("g ").Append(transform.name).Append("\n");
86+
// }
87+
// meshString.Append(ProcessTransform(transform, makeSubmeshes));
88+
89+
90+
// string objFilePath = Path.Combine(directoryPath, $"{meshName}.obj");
91+
92+
// WriteToFile(meshString.ToString(), objFilePath);
93+
94+
// transform.position = originalPosition;
95+
96+
97+
// // export materials and textures
98+
// StringBuilder mtlFileString = new StringBuilder();
99+
// HashSet<string> materialNames = new HashSet<string>();
100+
// MeshFilter meshFilter = transform.GetComponent<MeshFilter>();
101+
// if (meshFilter != null) {
102+
// Material[] materials = meshFilter.GetComponent<Renderer>().sharedMaterials;
103+
// foreach (Material material in materials) {
104+
// if (materialNames.Contains(material.name) == false) {
105+
// string materialString = ZOExportOBJ.MaterialToString(material);
106+
// mtlFileString.Append(materialString);
107+
// materialNames.Add(material.name);
108+
109+
// // handle texture
110+
// if (material.HasProperty("_MainTex")) {
111+
// string assetPath = AssetDatabase.GetAssetPath(material.GetTexture("_MainTex"));
112+
// if (string.IsNullOrEmpty(assetPath) == false) {
113+
// string texName = Path.GetFileName(assetPath);
114+
// File.Copy(assetPath, Path.Combine(directoryPath, texName));
115+
// }
116+
// }
117+
// }
118+
// }
119+
// }
120+
121+
122+
// // go through the children materials
123+
// for (int i = 0; i < transform.childCount; i++) {
124+
// Transform child = transform.GetChild(i);
125+
// meshFilter = child.GetComponent<MeshFilter>();
126+
127+
// if (meshFilter != null) {
128+
// // meshString.Append(ZOObjExporterScript.MeshToString(meshFilter, transform));
129+
// Material[] materials = meshFilter.GetComponent<Renderer>().sharedMaterials;
130+
// foreach (Material material in materials) {
131+
// if (materialNames.Contains(material.name) == false) {
132+
// string materialString = ZOExportOBJ.MaterialToString(material);
133+
// mtlFileString.Append(materialString);
134+
// materialNames.Add(material.name);
135+
// // handle texture
136+
// if (material.HasProperty("_MainTex")) {
137+
// string assetPath = AssetDatabase.GetAssetPath(material.GetTexture("_MainTex"));
138+
// if (string.IsNullOrEmpty(assetPath) == false) {
139+
// string texName = Path.GetFileName(assetPath);
140+
// File.Copy(assetPath, Path.Combine(directoryPath, texName));
141+
// }
142+
// }
143+
// }
144+
// }
145+
// }
146+
// }
147+
// string mtlFilePath = Path.Combine(directoryPath, $"{meshName}.mtl");
148+
// WriteToFile(mtlFileString.ToString(), mtlFilePath);
149+
150+
// ZOExportOBJ.End();
151+
// Debug.Log("Exported Mesh: " + objFilePath);
152+
}
153+
154+
// static string ProcessTransform(Transform transform, bool makeSubmeshes) {
155+
// StringBuilder meshString = new StringBuilder();
156+
157+
// meshString.Append("#" + transform.name
158+
// + "\n#-------"
159+
// + "\n");
160+
161+
// if (makeSubmeshes) {
162+
// meshString.Append("g ").Append(transform.name).Append("\n");
163+
// }
164+
165+
// MeshFilter meshFilter = transform.GetComponent<MeshFilter>();
166+
// if (meshFilter != null) {
167+
// meshString.Append(ZOExportOBJ.MeshToString(meshFilter, transform));
168+
// }
169+
170+
// for (int i = 0; i < transform.childCount; i++) {
171+
// meshString.Append(ProcessTransform(transform.GetChild(i), makeSubmeshes));
172+
// }
173+
174+
// return meshString.ToString();
175+
// }
176+
177+
// static void WriteToFile(string s, string filename) {
178+
// using (StreamWriter sw = new StreamWriter(filename)) {
179+
// sw.Write(s);
180+
// }
181+
// }
182+
}
183+
}

Editor/ZOExportOBJEditor.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Scripts/Document/ZOSimOccurrence.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using ZO.ROS.Unity;
1616
using ZO.ROS.Publisher;
1717
using ZO.Math;
18+
using ZO.ImportExport;
1819

1920
namespace ZO.Document {
2021

Runtime/Scripts/Util/ImportExport.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)