Allow mocap bodies to be loaded by the Unity plugin.

PiperOrigin-RevId: 558735711
Change-Id: Ibc9a81cbf8ec4b979d5b325c822c1bffc1408509
This commit is contained in:
Joss Moore
2023-08-21 03:27:56 -07:00
committed by Copybara-Service
parent ed4bb2aab5
commit ef699d4b6d
4 changed files with 22 additions and 7 deletions
@@ -19,7 +19,11 @@ using UnityEngine;
namespace Mujoco { namespace Mujoco {
public class MjMocapBody : MjBaseBody { public class MjMocapBody : MjBaseBody {
protected override void OnParseMjcf(XmlElement mjcf) { protected override void OnParseMjcf(XmlElement mjcf) {
throw new Exception("parsing mocap bodies isn't supported."); // Transform
transform.localPosition =
MjEngineTool.UnityVector3(mjcf.GetVector3Attribute("pos", defaultValue: Vector3.zero));
transform.localRotation = MjEngineTool.UnityQuaternion(
mjcf.GetQuaternionAttribute("quat", defaultValue: MjEngineTool.MjQuaternionIdentity));
} }
protected override unsafe void OnBindToRuntime(MujocoLib.mjModel_* model, MujocoLib.mjData_* data) { protected override unsafe void OnBindToRuntime(MujocoLib.mjModel_* model, MujocoLib.mjData_* data) {
@@ -20,7 +20,7 @@ using UnityEngine;
namespace Mujoco { namespace Mujoco {
public class MjBodyQuaternionSensor : MjBaseSensor { public class MjBodyQuaternionSensor : MjBaseSensor {
public MjBody Body; public MjBaseBody Body;
[Tooltip("Should the Frame sensors use the inertial or the regular frame of reference.")] [Tooltip("Should the Frame sensors use the inertial or the regular frame of reference.")]
public bool UseInertialFrame; public bool UseInertialFrame;
@@ -39,7 +39,7 @@ public class MjBodyQuaternionSensor : MjBaseSensor {
protected override void FromMjcf(XmlElement mjcf) { protected override void FromMjcf(XmlElement mjcf) {
UseInertialFrame = mjcf.HasAttribute("body"); UseInertialFrame = mjcf.HasAttribute("body");
Body = mjcf.GetObjectReferenceAttribute<MjBody>("objname"); Body = mjcf.GetObjectReferenceAttribute<MjBaseBody>("objname");
if (Body == null) { if (Body == null) {
throw new NullReferenceException("Missing a reference to a MjBody."); throw new NullReferenceException("Missing a reference to a MjBody.");
} }
@@ -36,7 +36,7 @@ public class MjBodyVectorSensor : MjBaseSensor {
FrameAngAcc, FrameAngAcc,
} }
public AvailableSensors SensorType; public AvailableSensors SensorType;
public MjBody Body; public MjBaseBody Body;
[Tooltip("Should the Frame sensors use the inertial or the regular frame of reference.")] [Tooltip("Should the Frame sensors use the inertial or the regular frame of reference.")]
public bool UseInertialFrame; public bool UseInertialFrame;
@@ -64,9 +64,9 @@ public class MjBodyVectorSensor : MjBaseSensor {
} }
if (mjcf.Name.Contains("frame")) { if (mjcf.Name.Contains("frame")) {
UseInertialFrame = mjcf.HasAttribute("body"); // as opposed to xbody UseInertialFrame = mjcf.HasAttribute("body"); // as opposed to xbody
Body = mjcf.GetObjectReferenceAttribute<MjBody>("objname"); Body = mjcf.GetObjectReferenceAttribute<MjBaseBody>("objname");
} else { } else {
Body = mjcf.GetObjectReferenceAttribute<MjBody>("body"); Body = mjcf.GetObjectReferenceAttribute<MjBaseBody>("body");
} }
} }
+12 -1
View File
@@ -252,7 +252,18 @@ public class MjcfImporter {
break; break;
} }
case "body": { case "body": {
var childObject = CreateGameObjectWithUniqueName<MjBody>(parentObject, child); GameObject childObject = null;
const string mocapAttribute = "mocap";
if (child.HasAttribute(mocapAttribute)) {
var mocapValueStr = child.GetAttribute(mocapAttribute);
var mocapValue = bool.Parse(mocapValueStr);
if (mocapValue) {
childObject = CreateGameObjectWithUniqueName<MjMocapBody>(parentObject, child);
}
}
if (childObject == null) {
childObject = CreateGameObjectWithUniqueName<MjBody>(parentObject, child);
}
ParseBodyChildren(childObject, child); ParseBodyChildren(childObject, child);
break; break;
} }