Skip to content

Commit aff4952

Browse files
committed
urdf import can import basic hierarchy (nothing else)
1 parent 22a340a commit aff4952

File tree

4 files changed

+75
-25
lines changed

4 files changed

+75
-25
lines changed

Editor/ZOImportZeroSimProcessor.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

Editor/ZOZeroSimMenu.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using UnityEngine;
22
using UnityEditor;
33
using ZO.Document;
4-
4+
using ZO.ImportExport;
55
namespace ZO.Editor {
66
public static class ZOZeroSimMenu {
77
[MenuItem("GameObject/ZeroSim/New Robot", false, 0)]
@@ -12,8 +12,8 @@ static void CreateZeroSimRobot(MenuCommand menuCommand) {
1212
GameObject baseOccurrence = new GameObject("base");
1313
baseOccurrence.transform.SetParent(documentRoot.transform);
1414
ZOSimOccurrence occurrence = baseOccurrence.AddComponent<ZOSimOccurrence>();
15-
occurrence.DocumentRoot = docRoot;
16-
15+
occurrence.DocumentRoot = docRoot;
16+
1717

1818
GameObject visuals = new GameObject("visuals");
1919
visuals.transform.SetParent(occurrence.transform);
@@ -32,11 +32,26 @@ static void CreateZeroSimRobot(MenuCommand menuCommand) {
3232
GameObject cubeCollision = new GameObject("MyExampleCollisionCube");
3333
cubeCollision.AddComponent<BoxCollider>();
3434
cubeCollision.transform.SetParent(collisions.transform);
35-
36-
35+
36+
3737

3838
Undo.RegisterCreatedObjectUndo(documentRoot, "Create ZeroSim Robot " + documentRoot.name);
3939
Selection.activeGameObject = documentRoot;
4040
}
41+
42+
43+
44+
[MenuItem("GameObject/ZeroSim/Import URDF...", false, 0)]
45+
static void ImportURDF(MenuCommand menuCommand) {
46+
string filePath = EditorUtility.OpenFilePanel("Import URDF", ".", "urdf");
47+
48+
if (filePath.Length == 0) {
49+
return;
50+
}
51+
52+
ZOImportURDF.Import(filePath);
53+
54+
}
55+
4156
}
4257
}

Runtime/Scripts/Util/ImportExport/ZOImportURDF.cs

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections;
44
using System.Collections.Generic;
55
using System.IO;
6+
using System;
67
using System.Text;
78
using ZO.Document;
89
using ZO.Physics;
@@ -16,28 +17,73 @@ namespace ZO.ImportExport {
1617

1718
public class ZOImportURDF {
1819

19-
public ZOSimDocumentRoot Import(string urdfFilePath) {
20-
using(StreamReader streamReader = new StreamReader(urdfFilePath)) {
20+
public static ZOSimDocumentRoot Import(string urdfFilePath) {
21+
using (StreamReader streamReader = new StreamReader(urdfFilePath)) {
2122
XmlDocument xmlDocument = new XmlDocument();
2223
xmlDocument.Load(streamReader);
2324
return Import(xmlDocument);
2425
}
2526
}
2627

27-
public ZOSimDocumentRoot Import(XmlDocument xmlDocument) {
28+
public static ZOSimDocumentRoot Import(XmlDocument xmlDocument) {
2829
XmlNode robot = xmlDocument.GetChildByName("robot");
2930

30-
GameObject rootObject = new GameObject(robot.Name);
31+
GameObject rootObject = new GameObject(robot.Name);
3132

3233
ZOSimDocumentRoot simDocumentRoot = rootObject.AddComponent<ZOSimDocumentRoot>();
3334

35+
// get the joints & links
36+
XmlNode[] xmlLinks = robot.GetChildrenByName("link");
37+
Dictionary<string, Tuple<XmlNode, GameObject>> goLinks = new Dictionary<string, Tuple<XmlNode, GameObject>>();
3438

35-
// create the URDF links in Unity
36-
XmlNode[] links = robot.GetChildrenByName("link");
37-
foreach(XmlNode link in links) {
38-
// process the visuals
39-
XmlNode[] visuals = link.GetChildrenByName("visual");
39+
// find root link. which is a link without an explicit parent
4040

41+
// create links
42+
foreach (XmlNode xmlLink in xmlLinks) {
43+
44+
string linkName = xmlLink.Attributes["name"].Value;
45+
GameObject goLink = new GameObject(linkName);
46+
ZOSimOccurrence occurrence = goLink.AddComponent<ZOSimOccurrence>();
47+
48+
GameObject goVisualsEmpty = new GameObject("visuals");
49+
goVisualsEmpty.transform.SetParent(goLink.transform);
50+
51+
GameObject goCollisionsEmpty = new GameObject("collisions");
52+
goCollisionsEmpty.transform.SetParent(goLink.transform);
53+
54+
goLinks[linkName] = new Tuple<XmlNode, GameObject>(xmlLink, goLink);
55+
// // process the visuals
56+
// XmlNode[] visuals = xmlLink.GetChildrenByName("visual");
57+
58+
// foreach (XmlNode visual in visuals) {
59+
60+
// }
61+
62+
// get the origin
63+
// XmlNode origin =
64+
65+
}
66+
67+
// create hierarchy from joints
68+
XmlNode[] xmlJoints = robot.GetChildrenByName("joint");
69+
foreach (var goLink in goLinks) {
70+
71+
// find link parent
72+
GameObject linkParent = rootObject;
73+
foreach (XmlNode joint in xmlJoints) {
74+
XmlNode xmlChildLink = joint.GetChildByName("child");
75+
string childName = xmlChildLink.Attributes["link"].Value;
76+
77+
if (goLink.Value.Item2.name == childName) {
78+
XmlNode xmlParentLink = joint.GetChildByName("parent");
79+
string parentName = xmlParentLink.Attributes["link"].Value;
80+
linkParent = goLinks[parentName].Item2;
81+
break;
82+
}
83+
84+
}
85+
86+
goLink.Value.Item2.transform.SetParent(linkParent.transform);
4187
}
4288

4389
return simDocumentRoot;

0 commit comments

Comments
 (0)