Revert "redid the global settings according to suggested guidelines"

This reverts commit 655b961c89.
This commit is contained in:
Joan Llobera
2025-10-17 09:42:40 +02:00
parent 655b961c89
commit 0001fe221c
+53 -95
View File
@@ -13,59 +13,57 @@
// limitations under the License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml;
using UnityEngine;
using UnityEngine.Profiling;
namespace Mujoco
{
namespace Mujoco {
// The MuJoCo parser is case-sensitive. The case format of the entries in the enums below is
// exactly what the documentation specifies: http://mujoco.org/book/XMLreference.html#option .
public enum IntegratorType
{
public enum IntegratorType {
Euler,
RK4,
@implicit,
@implicitfast
}
public enum FrictionConeType
{
public enum FrictionConeType {
pyramidal,
elliptic
}
public enum JacobianType
{
public enum JacobianType {
dense,
sparse,
auto
}
public enum ConstraintSolverType
{
public enum ConstraintSolverType {
PGS,
CG,
Newton
}
public enum EnableDisableFlag
{
public enum EnableDisableFlag {
enable,
disable
}
[Serializable]
public struct MjcfOptionFlag
{
public struct MjcfOptionFlag {
public EnableDisableFlag Constraint;
public EnableDisableFlag Equality;
public EnableDisableFlag FrictionLoss;
public EnableDisableFlag Limit;
public EnableDisableFlag Contact;
public EnableDisableFlag Spring;
public EnableDisableFlag Damper;
public EnableDisableFlag Passive;
public EnableDisableFlag Gravity;
public EnableDisableFlag ClampCtrl;
public EnableDisableFlag WarmStart;
@@ -76,15 +74,13 @@ namespace Mujoco
public EnableDisableFlag Energy;
public EnableDisableFlag FwdInv;
public EnableDisableFlag MultiCCD;
public static MjcfOptionFlag Default = new MjcfOptionFlag()
{
public static MjcfOptionFlag Default = new MjcfOptionFlag() {
Constraint = EnableDisableFlag.enable,
Equality = EnableDisableFlag.enable,
FrictionLoss = EnableDisableFlag.enable,
Limit = EnableDisableFlag.enable,
Contact = EnableDisableFlag.enable,
Spring = EnableDisableFlag.enable,
Damper = EnableDisableFlag.enable,
Passive = EnableDisableFlag.enable,
Gravity = EnableDisableFlag.enable,
ClampCtrl = EnableDisableFlag.enable,
WarmStart = EnableDisableFlag.enable,
@@ -97,8 +93,7 @@ namespace Mujoco
MultiCCD = EnableDisableFlag.disable
};
public void FromMjcf(XmlElement mjcf)
{
public void FromMjcf(XmlElement mjcf) {
var localDefault = MjcfOptionFlag.Default;
Constraint = mjcf.GetEnumAttribute<EnableDisableFlag>("constraint", localDefault.Constraint);
Equality = mjcf.GetEnumAttribute<EnableDisableFlag>("equality", localDefault.Equality);
@@ -106,8 +101,7 @@ namespace Mujoco
localDefault.FrictionLoss);
Limit = mjcf.GetEnumAttribute<EnableDisableFlag>("limit", localDefault.Limit);
Contact = mjcf.GetEnumAttribute<EnableDisableFlag>("contact", localDefault.Contact);
Spring = mjcf.GetEnumAttribute<EnableDisableFlag>("spring", localDefault.Spring);
Damper = mjcf.GetEnumAttribute<EnableDisableFlag>("damper", localDefault.Damper);
Passive = mjcf.GetEnumAttribute<EnableDisableFlag>("passive", localDefault.Passive);
Gravity = mjcf.GetEnumAttribute<EnableDisableFlag>("gravity", localDefault.Gravity);
ClampCtrl = mjcf.GetEnumAttribute<EnableDisableFlag>("clampctrl", localDefault.ClampCtrl);
WarmStart = mjcf.GetEnumAttribute<EnableDisableFlag>("warmstart", localDefault.WarmStart);
@@ -121,15 +115,13 @@ namespace Mujoco
MultiCCD = mjcf.GetEnumAttribute<EnableDisableFlag>("multiccd", localDefault.MultiCCD);
}
public void ToMjcf(XmlElement mjcf)
{
public void ToMjcf(XmlElement mjcf) {
mjcf.SetAttribute("constraint", Constraint.ToString());
mjcf.SetAttribute("equality", Equality.ToString());
mjcf.SetAttribute("frictionloss", FrictionLoss.ToString());
mjcf.SetAttribute("limit", Limit.ToString());
mjcf.SetAttribute("contact", Contact.ToString());
mjcf.SetAttribute("spring", Spring.ToString());
mjcf.SetAttribute("damper", Damper.ToString());
// mjcf.SetAttribute("passive", Passive.ToString());
mjcf.SetAttribute("gravity", Gravity.ToString());
mjcf.SetAttribute("clampctrl", ClampCtrl.ToString());
mjcf.SetAttribute("warmstart", WarmStart.ToString());
@@ -144,31 +136,27 @@ namespace Mujoco
}
[Serializable]
public struct MjSizeStruct
{
public struct MjSizeStruct {
public String Memory;
public static MjSizeStruct Default = new MjSizeStruct()
{
public static MjSizeStruct Default = new MjSizeStruct() {
Memory = "-1"
};
public void ParseMjcf(XmlElement mjcf)
{
public void ParseMjcf(XmlElement mjcf) {
Memory = mjcf.GetStringAttribute("memory", "-1");
}
public XmlElement ToMjcf(XmlElement mjcf)
{
public XmlElement ToMjcf(XmlElement mjcf) {
mjcf.SetAttribute("memory", $"{Memory}");
return mjcf;
}
}
[Serializable]
public struct MjOptionStruct
{
public struct MjOptionStruct {
// "timestep" and "gravity" come from global settings.
// "apirate" is only relevant to HAPTIX.
[Tooltip("Ratio of frictional-to-normal constraint impedance.")]
public float ImpRatio;
[Tooltip("Global magnetic flux used by magnetometer sensors.")]
@@ -209,8 +197,7 @@ namespace Mujoco
public MjcfOptionFlag Flag;
// Default values copied from the "XML reference" page in the MuJoCo documentation.
public static MjOptionStruct Default = new MjOptionStruct()
{
public static MjOptionStruct Default = new MjOptionStruct() {
ImpRatio = 1.0f,
Magnetic = Vector3.zero,
Wind = new Vector3(0.0f, 0.0f, 0.0f),
@@ -232,21 +219,17 @@ namespace Mujoco
Flag = MjcfOptionFlag.Default
};
public void ParseMjcf(XmlElement mjcf)
{
if (mjcf.HasAttribute("timestep"))
{
public void ParseMjcf(XmlElement mjcf) {
if (mjcf.HasAttribute("timestep")) {
var mjTimestep = mjcf.GetFloatAttribute("timestep", Time.fixedDeltaTime);
if (mjTimestep != Time.fixedDeltaTime)
{
if (mjTimestep != Time.fixedDeltaTime) {
Debug.unityLogger.LogWarning(
"MuJoCo",
$"MuJoCo's timestep ({mjTimestep}s) ignored, please set fixedDeltaTime manually.",
null);
}
}
if (mjcf.HasAttribute("gravity"))
{
if (mjcf.HasAttribute("gravity")) {
var mjGravity = mjcf.GetVector3Attribute("gravity", Vector3.zero);
Debug.unityLogger.LogWarning(
"MuJoCo",
@@ -279,22 +262,17 @@ namespace Mujoco
CcdTolerance = mjcf.GetFloatAttribute("ccd_tolerance", localDefault.CcdTolerance);
var flagElements = mjcf.GetElementsByTagName("flag");
if (flagElements.Count == 1)
{
if (flagElements.Count == 1) {
Flag.FromMjcf(flagElements[0] as XmlElement);
}
else if (flagElements.Count > 1)
{
} else if (flagElements.Count > 1) {
throw new ArgumentException("More than one <flag> node is not supported.");
}
}
public XmlElement ToMjcf(XmlElement mjcf)
{
public XmlElement ToMjcf(XmlElement mjcf) {
mjcf.SetAttribute("impratio", MjEngineTool.MakeLocaleInvariant($"{ImpRatio}"));
mjcf.SetAttribute("magnetic",
MjEngineTool.MakeLocaleInvariant($"{Magnetic.x} {Magnetic.y} {Magnetic.z}"));
mjcf.SetAttribute("magnetic", MjEngineTool.MakeLocaleInvariant($"{Magnetic.x} {Magnetic.y} {Magnetic.z}"));
mjcf.SetAttribute("wind", MjEngineTool.MakeLocaleInvariant($"{Wind.x} {Wind.y} {Wind.z}"));
mjcf.SetAttribute("density", MjEngineTool.MakeLocaleInvariant($"{Density}"));
@@ -324,15 +302,13 @@ namespace Mujoco
}
[Serializable]
public class NumericEntry
{
public class NumericEntry {
public String Name;
[Tooltip("Space-separated list of floats.")]
public String Data;
}
public class MjGlobalSettings : MonoBehaviour
{
public class MjGlobalSettings : MonoBehaviour {
[Tooltip("Filename for the generated scene XML.")]
public string DebugFileName;
@@ -349,20 +325,14 @@ namespace Mujoco
public List<NumericEntry> CustomNumeric = new List<NumericEntry>() {};
public static MjGlobalSettings Instance
{
get
{
if (_instance == null)
{
public static MjGlobalSettings Instance {
get {
if (_instance == null) {
var instances = FindObjectsOfType<MjGlobalSettings>();
if (instances.Length > 1)
{
if (instances.Length > 1) {
throw new InvalidOperationException(
"Only one MjGlobalSettings instance is allowed - please resolve manually.");
}
else if (instances.Length == 1)
{
} else if (instances.Length == 1) {
_instance = instances[0];
}
}
@@ -372,41 +342,31 @@ namespace Mujoco
private static MjGlobalSettings _instance = null;
public void Awake()
{
if (_instance == null)
{
public void Awake() {
if (_instance == null) {
_instance = this;
}
else if (_instance != this)
{
} else if (_instance != this) {
throw new InvalidOperationException(
"At most one MjGlobalSettings should be present.");
}
}
public void ParseGlobalMjcfSections(XmlElement mujocoNode)
{
public void ParseGlobalMjcfSections(XmlElement mujocoNode) {
var optionNode = mujocoNode.SelectSingleNode("option") as XmlElement;
var sizeNode = mujocoNode.SelectSingleNode("size") as XmlElement;
var customNode = mujocoNode.SelectSingleNode("custom") as XmlElement;
if (optionNode != null)
{
if (optionNode != null) {
GlobalOptions.ParseMjcf(optionNode);
}
if (sizeNode != null)
{
if (sizeNode != null) {
GlobalSizes.ParseMjcf(sizeNode);
}
if (customNode != null)
{
foreach (var childNode in customNode.ChildNodes)
{
if (customNode != null) {
foreach (var childNode in customNode.ChildNodes) {
var child = childNode as XmlElement;
if (child.Name == "numeric")
{
if (child.Name == "numeric") {
var numeric = new NumericEntry();
numeric.Name = child.GetAttribute("name");
numeric.Data = child.GetAttribute("data");
@@ -416,16 +376,14 @@ namespace Mujoco
}
}
public void GlobalsToMjcf(XmlElement mjcf)
{
public void GlobalsToMjcf(XmlElement mjcf) {
var doc = mjcf.OwnerDocument;
var optionMjcf = (XmlElement)mjcf.AppendChild(doc.CreateElement("option"));
GlobalOptions.ToMjcf(optionMjcf);
var sizeMjcf = (XmlElement)mjcf.AppendChild(doc.CreateElement("size"));
GlobalSizes.ToMjcf(sizeMjcf);
var customMjcf = (XmlElement)mjcf.AppendChild(doc.CreateElement("custom"));
foreach (var numeric in CustomNumeric)
{
foreach (var numeric in CustomNumeric) {
var numericMjcf = (XmlElement)customMjcf.AppendChild(doc.CreateElement("numeric"));
numericMjcf.SetAttribute("name", numeric.Name);
// TODO: add validation that data is a space-separated list of floating numbers?