Merge pull request #734 from hyarion:fix/unity-load-balloons.xml

PiperOrigin-RevId: 509832074
Change-Id: Ia8505b4ea83d8903052e72a12b914a8fd94a13df
This commit is contained in:
Copybara-Service
2023-02-15 07:51:34 -08:00
4 changed files with 19 additions and 6 deletions
@@ -20,17 +20,23 @@ namespace Mujoco {
// The component represents the apex of hierarchy that defines a single rigid body. // The component represents the apex of hierarchy that defines a single rigid body.
public class MjBody : MjBaseBody { public class MjBody : MjBaseBody {
[Tooltip("Gravity compensation force, specified as fraction of body weight.")]
public float GravityCompensation;
protected override void OnParseMjcf(XmlElement mjcf) { protected override void OnParseMjcf(XmlElement mjcf) {
// Transform // Transform
transform.localPosition = transform.localPosition =
MjEngineTool.UnityVector3(mjcf.GetVector3Attribute("pos", defaultValue: Vector3.zero)); MjEngineTool.UnityVector3(mjcf.GetVector3Attribute("pos", defaultValue: Vector3.zero));
transform.localRotation = MjEngineTool.UnityQuaternion( transform.localRotation = MjEngineTool.UnityQuaternion(
mjcf.GetQuaternionAttribute("quat", defaultValue: MjEngineTool.MjQuaternionIdentity)); mjcf.GetQuaternionAttribute("quat", defaultValue: MjEngineTool.MjQuaternionIdentity));
GravityCompensation = mjcf.GetFloatAttribute("gravcomp", defaultValue: 0.0f);
} }
protected override XmlElement OnGenerateMjcf(XmlDocument doc) { protected override XmlElement OnGenerateMjcf(XmlDocument doc) {
var mjcf = (XmlElement)doc.CreateElement("body"); var mjcf = (XmlElement)doc.CreateElement("body");
MjEngineTool.PositionRotationToMjcf(mjcf, this); MjEngineTool.PositionRotationToMjcf(mjcf, this);
mjcf.SetAttribute("gravcomp", MjEngineTool.MakeLocaleInvariant($"{GravityCompensation}"));
return mjcf; return mjcf;
} }
+3 -2
View File
@@ -29,7 +29,8 @@ namespace Mujoco {
// exactly what the documentation specifies: http://mujoco.org/book/XMLreference.html#option . // exactly what the documentation specifies: http://mujoco.org/book/XMLreference.html#option .
public enum IntegratorType { public enum IntegratorType {
Euler, Euler,
RK4 RK4,
@implicit
} }
public enum CollisionCheckType { public enum CollisionCheckType {
@@ -208,7 +209,7 @@ public struct MjOptionStruct {
public static MjOptionStruct Default = new MjOptionStruct() { public static MjOptionStruct Default = new MjOptionStruct() {
ImpRatio = 1.0f, ImpRatio = 1.0f,
Magnetic = Vector3.zero, Magnetic = Vector3.zero,
Wind = new Vector3(0.0f, -0.5f, 0.0f), Wind = new Vector3(0.0f, 0.0f, 0.0f),
Density = 0.0f, Density = 0.0f,
Viscosity = 0.0f, Viscosity = 0.0f,
OverrideMargin = 0.0f, OverrideMargin = 0.0f,
+8 -3
View File
@@ -28,12 +28,17 @@ namespace Mujoco {
_root = root; _root = root;
} }
public void ApplyModifiersToElement(XmlElement element) { public void ApplyModifiersToElement(XmlElement element, string elementName=null) {
// Allow overriding the element name for defaults lookup, needed for tendon
if (elementName == null) {
elementName = element.Name;
}
// Combine all defaults into one. At this stage, we want to overwrite attributes defined by // Combine all defaults into one. At this stage, we want to overwrite attributes defined by
// the previous defaults. // the previous defaults.
var aggregateDefaults = _root.CreateElement("aggregate"); var aggregateDefaults = _root.CreateElement("aggregate");
// Root default leaf should be processed only once, and handled first (so it's overriden). // Root default leaf should be processed only once, and handled first (so it's overriden).
var rootDefaultLeaf = _root.SelectSingleNode($"/mujoco/default/{element.Name}") as XmlElement; var rootDefaultLeaf = _root.SelectSingleNode($"/mujoco/default/{elementName}") as XmlElement;
if (rootDefaultLeaf != null) { if (rootDefaultLeaf != null) {
CopyAttributes(rootDefaultLeaf, aggregateDefaults); CopyAttributes(rootDefaultLeaf, aggregateDefaults);
} }
@@ -43,7 +48,7 @@ namespace Mujoco {
var defaultClassElement = var defaultClassElement =
_root.SelectSingleNode($"descendant::default[@class='{className}']") as XmlElement; _root.SelectSingleNode($"descendant::default[@class='{className}']") as XmlElement;
// Ancestry iterates up in the tree, but we want to apply changes from remote to specific. // Ancestry iterates up in the tree, but we want to apply changes from remote to specific.
var ancestors = GetDefaultAncestry(defaultClassElement, element.Name).Reverse(); var ancestors = GetDefaultAncestry(defaultClassElement, elementName).Reverse();
foreach (var defaultAncestor in ancestors) { foreach (var defaultAncestor in ancestors) {
CopyAttributesOverwriteExisting(defaultAncestor, aggregateDefaults); CopyAttributesOverwriteExisting(defaultAncestor, aggregateDefaults);
} }
+2 -1
View File
@@ -167,6 +167,7 @@ public class MjcfImporter {
if (tendonNode != null) { if (tendonNode != null) {
var tendonsParentObject = CreateGameObjectInParent("tendons", rootObject); var tendonsParentObject = CreateGameObjectInParent("tendons", rootObject);
foreach (var child in tendonNode.OfType<XmlElement>()) { foreach (var child in tendonNode.OfType<XmlElement>()) {
_modifiers.ApplyModifiersToElement(child, elementName: "tendon");
if (child.Name == "fixed") { if (child.Name == "fixed") {
CreateGameObjectWithUniqueName<MjFixedTendon>(tendonsParentObject, child); CreateGameObjectWithUniqueName<MjFixedTendon>(tendonsParentObject, child);
} else if (child.Name == "spatial") { } else if (child.Name == "spatial") {
@@ -238,7 +239,7 @@ public class MjcfImporter {
} }
} }
// Called by ParseBodyChildren for each XML node, overridable by inheriting claases. // Called by ParseBodyChildren for each XML node, overridable by inheriting classes.
private void ParseBodyChild(XmlElement child, GameObject parentObject) { private void ParseBodyChild(XmlElement child, GameObject parentObject) {
switch (child.Name) { switch (child.Name) {
case "geom": { case "geom": {