Skip to content

Commit 758d43d

Browse files
committed
saving OBJ that satifies URDF orientation
1 parent 8f26e61 commit 758d43d

File tree

4 files changed

+52
-29
lines changed

4 files changed

+52
-29
lines changed

Editor/ZOExportOBJEditor.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ namespace ZO.Export {
1010

1111
public class ZOExportOBJEditor : EditorWindow {
1212
public bool _exportSubMeshes = true;
13-
public bool _zeroPosition = true;
13+
public bool _applyLocalTransform = false;
14+
public ZOExportOBJ.Orientation _orientation = ZOExportOBJ.Orientation.URDF;
1415
private string _selectedNames;
1516

1617
[MenuItem("Zero Sim/Export OBJ...")]
@@ -23,7 +24,8 @@ public static void DoOBJExport() {
2324

2425
private void OnGUI() {
2526
_exportSubMeshes = EditorGUILayout.Toggle("Export Sub Meshes: ", _exportSubMeshes);
26-
_zeroPosition = EditorGUILayout.Toggle("Zero Position: ", _zeroPosition);
27+
_applyLocalTransform = EditorGUILayout.Toggle("Apply Local Transform: ", _applyLocalTransform);
28+
_orientation = (ZOExportOBJ.Orientation)EditorGUILayout.EnumPopup("Export orientation:", _orientation);
2729

2830
foreach (var t in Selection.objects) {
2931
_selectedNames += t.name + " ";
@@ -37,7 +39,7 @@ private void OnGUI() {
3739
string meshName = Selection.gameObjects[0].name;
3840
string fileDirectory = EditorUtility.OpenFolderPanel("Export .OBJ to directory", "", "");
3941

40-
DoExport(_exportSubMeshes, fileDirectory);
42+
DoExport(_exportSubMeshes, fileDirectory, _applyLocalTransform, _orientation);
4143
}
4244
EditorGUI.EndDisabledGroup();
4345

@@ -47,7 +49,7 @@ private void OnInspectorUpdate() {
4749
Repaint();
4850
}
4951

50-
public static void DoExport(bool makeSubmeshes, string directoryPath, bool zeroPosition = true, GameObject gameObject = null) {
52+
public static void DoExport(bool makeSubmeshes, string directoryPath, bool applyLocalTransform, ZOExportOBJ.Orientation orientation, GameObject gameObject = null) {
5153

5254
if (gameObject == null && Selection.gameObjects.Length == 0) {
5355
Debug.Log("ERROR: Didn't Export Any Meshes; Nothing was selected!");
@@ -58,7 +60,7 @@ public static void DoExport(bool makeSubmeshes, string directoryPath, bool zeroP
5860
gameObject = Selection.gameObjects[0];
5961
}
6062
ZOExportOBJ exportOBJ = new ZOExportOBJ();
61-
exportOBJ.ExportToDirectory(gameObject, directoryPath, makeSubmeshes, zeroPosition);
63+
exportOBJ.ExportToDirectory(gameObject, directoryPath, makeSubmeshes, applyLocalTransform, orientation);
6264
}
6365

6466
}

Runtime/Scripts/Util/ImportExport/ZOExportOBJ.cs

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
namespace ZO.ImportExport {
99
public class ZOExportOBJ {
1010

11+
public enum Orientation {
12+
URDF,
13+
Unity // Unity Default direction
14+
}
15+
1116
protected string _objString = null;
1217
public string OBJString {
1318
get { return _objString; }
@@ -31,19 +36,25 @@ public List<string> TextureAssetPaths {
3136

3237
private int _startIndex = 0;
3338

34-
public void Start() {
39+
protected void Start() {
3540
_startIndex = 0;
3641
}
37-
public void End() {
42+
protected void End() {
3843
_startIndex = 0;
3944
}
4045

4146

42-
protected string MeshToString(MeshFilter meshFilter, Transform transform) {
47+
protected string MeshToString(MeshFilter meshFilter, Transform transform, bool applyLocalTransform, ZOExportOBJ.Orientation orientation) {
4348
Vector3 s = transform.localScale;
4449
Vector3 p = transform.localPosition;
4550
Quaternion r = transform.localRotation;
4651

52+
if (applyLocalTransform == false) {
53+
s = Vector3.one;
54+
p = Vector3.zero;
55+
r = Quaternion.identity;
56+
}
57+
4758

4859
int numVertices = 0;
4960
Mesh mesh = meshFilter.sharedMesh;
@@ -54,15 +65,26 @@ protected string MeshToString(MeshFilter meshFilter, Transform transform) {
5465

5566
StringBuilder sb = new StringBuilder();
5667

57-
foreach (Vector3 vv in mesh.vertices) {
58-
Vector3 v = transform.TransformPoint(vv);
68+
foreach (Vector3 vv in mesh.vertices) {
69+
Vector3 v = vv;
70+
if (applyLocalTransform == true) {
71+
v = transform.TransformPoint(vv);
72+
}
5973
numVertices++;
60-
sb.AppendLine($"v {v.x} {v.y} {v.z}");
74+
if (orientation == Orientation.Unity) {
75+
sb.AppendLine($"v {v.x} {v.y} {v.z}");
76+
} else if (orientation == Orientation.URDF) {
77+
sb.AppendLine($"v {v.z} {v.x} {v.y}");
78+
}
6179
}
6280
sb.AppendLine();
6381
foreach (Vector3 nn in mesh.normals) {
6482
Vector3 v = r * nn;
65-
sb.AppendLine($"vn {v.x} {v.y} {v.z}");
83+
if (orientation == Orientation.Unity) {
84+
sb.AppendLine($"vn {v.x} {v.y} {v.z}");
85+
} else if (orientation == Orientation.URDF) {
86+
sb.AppendLine($"vn {v.z} {v.x} {v.y}");
87+
}
6688
}
6789
sb.AppendLine();
6890
foreach (Vector3 v in mesh.uv) {
@@ -106,7 +128,7 @@ protected string MaterialToString(Material material) {
106128
return sb.ToString();
107129
}
108130

109-
protected string ProcessTransform(Transform transform, bool makeSubmeshes) {
131+
protected string ProcessTransform(Transform transform, bool makeSubmeshes, bool applyLocalTransform, ZOExportOBJ.Orientation orientation) {
110132
StringBuilder meshString = new StringBuilder();
111133

112134
meshString.Append("#" + transform.name
@@ -119,18 +141,18 @@ protected string ProcessTransform(Transform transform, bool makeSubmeshes) {
119141

120142
MeshFilter meshFilter = transform.GetComponent<MeshFilter>();
121143
if (meshFilter != null) {
122-
meshString.Append(MeshToString(meshFilter, transform));
144+
meshString.Append(MeshToString(meshFilter, transform, applyLocalTransform, orientation));
123145
}
124146

125147
for (int i = 0; i < transform.childCount; i++) {
126-
meshString.Append(ProcessTransform(transform.GetChild(i), makeSubmeshes));
148+
meshString.Append(ProcessTransform(transform.GetChild(i), makeSubmeshes, applyLocalTransform, orientation));
127149
}
128150

129151
return meshString.ToString();
130152
}
131153

132154

133-
public void BuildExportData(GameObject gameObject, bool makeSubmeshes, bool zeroPosition = true) {
155+
public void BuildExportData(GameObject gameObject, bool makeSubmeshes, bool applyLocalTransform, ZOExportOBJ.Orientation orientation) {
134156
Debug.Assert(gameObject != null, "ERROR: invalid GameObject in BuildExport");
135157

136158
string meshName = gameObject.name;
@@ -149,19 +171,19 @@ public void BuildExportData(GameObject gameObject, bool makeSubmeshes, bool zero
149171

150172
Transform transform = gameObject.transform;
151173

152-
Vector3 originalPosition = transform.position;
153-
if (zeroPosition == true) {
154-
transform.position = Vector3.zero;
155-
}
174+
// Vector3 originalPosition = transform.position;
175+
// if (zeroPosition == true) {
176+
// transform.position = Vector3.zero;
177+
// }
156178

157179
if (!makeSubmeshes) {
158180
meshString.Append("g ").Append(transform.name).Append("\n");
159181
}
160-
meshString.Append(ProcessTransform(transform, makeSubmeshes));
182+
meshString.Append(ProcessTransform(transform, makeSubmeshes, applyLocalTransform, orientation));
161183

162184
OBJString = meshString.ToString();
163185

164-
transform.position = originalPosition;
186+
// transform.position = originalPosition;
165187

166188
// export materials and textures
167189
StringBuilder mtlFileString = new StringBuilder();
@@ -222,8 +244,8 @@ public void BuildExportData(GameObject gameObject, bool makeSubmeshes, bool zero
222244

223245
}
224246

225-
public void ExportToDirectory(GameObject gameObject, string directoryPath, bool makeSubmeshes, bool zeroPosition = true) {
226-
BuildExportData(gameObject, makeSubmeshes, zeroPosition);
247+
public void ExportToDirectory(GameObject gameObject, string directoryPath, bool makeSubmeshes, bool applyLocalTransform, ZOExportOBJ.Orientation orientation) {
248+
BuildExportData(gameObject, makeSubmeshes, applyLocalTransform, orientation);
227249

228250
// write out obj file
229251
string objFilePath = Path.Combine(directoryPath, $"{gameObject.name}.obj");

Runtime/Scripts/Util/ImportExport/ZOExportURDF.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public List<Transform> CollisionMeshesToExport {
6262
get { return _collisionMeshesToExport; }
6363
}
6464

65-
static protected float _meshScale = 10.0f;
6665

6766
public static void ExportToDirectory(ZOSimDocumentRoot documentRoot, string directoryPath) {
6867
ZOExportURDF exportURDF = new ZOExportURDF();
@@ -73,12 +72,12 @@ public static void ExportToDirectory(ZOSimDocumentRoot documentRoot, string dire
7372
// save out visual and collision meshes
7473
foreach(Transform meshTransform in exportURDF.VisualMeshesToExport) {
7574
ZOExportOBJ exportOBJ = new ZOExportOBJ();
76-
exportOBJ.ExportToDirectory(meshTransform.gameObject, directoryPath, true, true);
75+
exportOBJ.ExportToDirectory(meshTransform.gameObject, directoryPath, true, false, ZOExportOBJ.Orientation.URDF);
7776
}
7877

7978
foreach(Transform meshTransform in exportURDF.CollisionMeshesToExport) {
8079
ZOExportOBJ exportOBJ = new ZOExportOBJ();
81-
exportOBJ.ExportToDirectory(meshTransform.gameObject, directoryPath, true, true);
80+
exportOBJ.ExportToDirectory(meshTransform.gameObject, directoryPath, true, false, ZOExportOBJ.Orientation.URDF);
8281
}
8382

8483
Debug.Log($"INFO: ZOExportURDF Saved URDF: {urdfFilePath}");
@@ -334,7 +333,7 @@ protected void BuildURDFVisuals(Transform visualTransform, XElement link, Vector
334333
} else { // regular mesh so export meshes as OBJ
335334
XElement mesh = new XElement("mesh");
336335
mesh.SetAttributeValue("filename", $"{visualTransform.name}.obj");
337-
Vector3 scale = visualTransform.localScale * _meshScale;
336+
Vector3 scale = visualTransform.localScale;
338337
mesh.SetAttributeValue("scale", scale.ToXMLString());
339338
geometry.Add(mesh);
340339

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.fsstudio.zerosim",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"displayName": "ZeroSim",
55
"description": "ZeroSim ROS robotic simulator in Unity.",
66
"unity": "2020.1",

0 commit comments

Comments
 (0)