Skip to content

Commit

Permalink
Support package dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
gkjohnson committed Aug 19, 2018
1 parent 12ce50c commit ba80149
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
8 changes: 5 additions & 3 deletions unity/Assets/URDF-Loader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ Reads and processes the urdf at the given path, returning a `URDFRobot` that des
##### urdfpath : String
The path to the URDF file relative to the specified package directory.

##### package : String
The path representing the package:// directory to load package:// relative files.
##### package : String | Dictionary<string, string>
If the package is just a string then it replaces the `package://` portion of the path.

If it is a dictionary then it represents a list of packages by `packageName : packagePath` so that multiple packages can be specified.

##### options : URDFLoader.Options

Expand All @@ -35,7 +37,7 @@ Same function as above, but this function takes the raw contents of the urdf fil

The urdf content to parse into a robot.

##### package : String
##### package : String | Dictionary<string, string>

See `LoadURDFRobot`.

Expand Down
29 changes: 22 additions & 7 deletions unity/Assets/example/LoadRobot.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using UnityEngine;
using System.IO;
using System.Collections.Generic;
using System;

public class LoadRobot : MonoBehaviour {
enum Axis {
Expand All @@ -11,8 +13,14 @@ enum Axis {
NEG_Z = -3
}

[Serializable]
struct Package {
public string name;
public string path;
}

[SerializeField]
string _packagePath = "";
List<Package> _packages;

[SerializeField]
string _fileName = "";
Expand All @@ -23,16 +31,23 @@ enum Axis {
public URDFRobot robot;

void Awake() {
string packagePath = _packagePath;
if (packagePath.IndexOf("https://") != 0 && packagePath.IndexOf("https://") != 0) {
packagePath = Path.Combine(Application.dataPath, packagePath);

Dictionary<string, string> packages = new Dictionary<string, string>();
foreach (var pkg in _packages) {

string packagePath = pkg.path;
if (packagePath.IndexOf("https://") != 0 && packagePath.IndexOf("https://") != 0) {
packagePath = Path.Combine(Application.dataPath, packagePath);
}
packages[pkg.name] = packagePath;

}

string urdfPath = _fileName;
if (urdfPath.IndexOf("https://") != 0 && urdfPath.IndexOf("https://") != 0) {
urdfPath = Path.Combine(Application.dataPath, urdfPath);
}
robot = CreateRobot(urdfPath, packagePath);
robot = CreateRobot(urdfPath, packages);

bool positive = _upAxis > 0;
Axis axis = !positive ? (Axis)(-1 * (int)_upAxis) : _upAxis;
Expand All @@ -45,9 +60,9 @@ void Awake() {
robot.transform.rotation = Quaternion.Euler(angles);
}

virtual protected URDFRobot CreateRobot(string urdf, string package) {
virtual protected URDFRobot CreateRobot(string urdf, Dictionary<string, string> packages) {

URDFRobot ur = URDFLoader.LoadRobot(urdf, package);
URDFRobot ur = URDFLoader.LoadRobot(urdf, packages);
ur.name = urdf;

return ur;
Expand Down
9 changes: 5 additions & 4 deletions unity/Assets/example/LoadWebRobot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
using UnityEngine.Networking;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System;

public class LoadWebRobot : LoadRobot {

override protected URDFRobot CreateRobot(string urdfPath, string package) {
override protected URDFRobot CreateRobot(string urdfPath, Dictionary<string, string> packages) {

URDFRobot ujl = new GameObject("Pending Robot").AddComponent<URDFRobot>();
StartCoroutine(DownloadRobot(urdfPath, package, ujl));
StartCoroutine(DownloadRobot(urdfPath, packages, ujl));

return ujl;

}

IEnumerator DownloadRobot(string urdfPath, string package, URDFRobot ur) {
IEnumerator DownloadRobot(string urdfPath, Dictionary<string, string> packages, URDFRobot ur) {

using (UnityWebRequest www = UnityWebRequest.Get(urdfPath)) {

Expand All @@ -36,7 +37,7 @@ IEnumerator DownloadRobot(string urdfPath, string package, URDFRobot ur) {
target = ur
};

URDFLoader.BuildRobot(www.downloadHandler.text, package, opt);
URDFLoader.BuildRobot(www.downloadHandler.text, packages, opt);

}

Expand Down
Binary file modified unity/Assets/example/scene.unity
Binary file not shown.
Binary file modified unity/Assets/example/web scene.unity
Binary file not shown.

0 comments on commit ba80149

Please sign in to comment.