From c2bef232b6e15144ec5ca33a9736105539f86856 Mon Sep 17 00:00:00 2001 From: Balint-H Date: Thu, 14 Mar 2024 12:38:56 +0000 Subject: [PATCH] Handle the implicit auto value for limits now written by the compiler to the xml --- .../Components/Joints/MjJointSettings.cs | 9 +------- unity/Runtime/Components/MjActuator.cs | 11 ++++------ unity/Runtime/Tools/XmlElementExtensions.cs | 22 +++++++++++++++++++ 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/unity/Runtime/Components/Joints/MjJointSettings.cs b/unity/Runtime/Components/Joints/MjJointSettings.cs index 322d6568..46da927c 100644 --- a/unity/Runtime/Components/Joints/MjJointSettings.cs +++ b/unity/Runtime/Components/Joints/MjJointSettings.cs @@ -165,14 +165,7 @@ namespace Mujoco { ImpFriction.FromMjcf(mjcf, "solimpfriction"); FrictionLoss = mjcf.GetFloatAttribute("frictionloss", 0.0f); - bool defaultLimited = false; - if ((mjcf.OwnerDocument.GetElementsByTagName("compiler")[0]?["compiler"]) - ?.GetBoolAttribute("autolimits", true) ?? - true) { - defaultLimited = mjcf.HasAttribute("range"); - } - - Limited = mjcf.GetBoolAttribute("limited", defaultLimited); + Limited = mjcf.GetLimitedAttribute("limited", mjcf.HasAttribute("range")); Margin = mjcf.GetFloatAttribute("margin", defaultValue: 0.0f); } } diff --git a/unity/Runtime/Components/MjActuator.cs b/unity/Runtime/Components/MjActuator.cs index 9aacf2bf..e44b8579 100644 --- a/unity/Runtime/Components/MjActuator.cs +++ b/unity/Runtime/Components/MjActuator.cs @@ -82,13 +82,10 @@ public class MjActuator : MjComponent { LengthRange = mjcf.GetVector2Attribute("lengthrange", defaultValue: Vector2.zero); Gear = mjcf.GetFloatArrayAttribute("gear", defaultValue: new float[] { 1.0f }).ToList(); - bool autolimits = (mjcf.OwnerDocument.GetElementsByTagName("compiler")[0]?["compiler"]) - ?.GetBoolAttribute("autolimits", true) ?? - false; - CtrlLimited = mjcf.GetBoolAttribute("ctrllimited", - defaultValue: autolimits ? CtrlRange != Vector2.zero : false); - ForceLimited = mjcf.GetBoolAttribute("forcelimited", - defaultValue: autolimits ? ForceRange != Vector2.zero : false); + CtrlLimited = mjcf.GetLimitedAttribute("ctrllimited", + rangeDefined: mjcf.HasAttribute("ctrlrange")); + ForceLimited = mjcf.GetLimitedAttribute("forcelimited", + rangeDefined: mjcf.HasAttribute("forcerange")); } } diff --git a/unity/Runtime/Tools/XmlElementExtensions.cs b/unity/Runtime/Tools/XmlElementExtensions.cs index 246f3d34..9dd8996a 100644 --- a/unity/Runtime/Tools/XmlElementExtensions.cs +++ b/unity/Runtime/Tools/XmlElementExtensions.cs @@ -37,6 +37,28 @@ public static class XmlElementExtensions { } } + public static bool GetLimitedAttribute( + this XmlElement element, string name, bool rangeDefined) { + var strValue = element.GetStringAttribute(name, "auto"); + if (strValue == "auto" && rangeDefined && element.GetAutolimitsEnabled()) return true; + if (strValue == "auto") return false; + + bool parsedValue; + if (bool.TryParse(strValue, out parsedValue)) { + return parsedValue; + } else { + throw new ArgumentException($"'{strValue}' is not a bool."); + } + } + + public static bool GetAutolimitsEnabled( + this XmlElement element) { + bool autolimits = (element.OwnerDocument?.GetElementsByTagName("compiler")[0]?["compiler"]) + ?.GetBoolAttribute("autolimits", true) ?? + true; + return autolimits; + } + public static float GetFloatAttribute( this XmlElement element, string name, float defaultValue = 0.0f) { if (!element.HasAttribute(name)) {