Skip to content

Commit 4390c85

Browse files
author
Chris Elion
authored
Warn if no joints on Root Body (#5387)
1 parent 44fe48e commit 4390c85

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

com.unity.ml-agents.extensions/Editor/RigidBodySensorComponentEditor.cs

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ public override void OnInspectorGUI()
1616
so.Update();
1717

1818
var rbSensorComp = so.targetObject as RigidBodySensorComponent;
19+
if (rbSensorComp.IsTrivial())
20+
{
21+
EditorGUILayout.HelpBox(
22+
"The Root Body has no Joints, and the Virtual Root is null or the same as the " +
23+
"Root Body's GameObject. This will not generate any useful observations; they will always " +
24+
"be the identity values. Consider removing this component since it won't help the Agent.",
25+
MessageType.Warning
26+
);
27+
}
28+
1929
bool requireExtractorUpdate;
2030

2131
EditorGUI.BeginDisabledGroup(!EditorUtilities.CanUpdateModelProperties());

com.unity.ml-agents.extensions/Runtime/Sensors/RigidBodySensorComponent.cs

+18
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ internal void SetPoseEnabled(int index, bool enabled)
9191
{
9292
GetPoseExtractor().SetPoseEnabled(index, enabled);
9393
}
94+
95+
internal bool IsTrivial()
96+
{
97+
if (ReferenceEquals(RootBody, null))
98+
{
99+
// It *is* trivial, but this will happen when the sensor is being set up, so don't warn then.
100+
return false;
101+
}
102+
var joints = RootBody.GetComponentsInChildren<Joint>();
103+
if (joints.Length == 0)
104+
{
105+
if (ReferenceEquals(VirtualRoot, null) || ReferenceEquals(VirtualRoot, RootBody.gameObject))
106+
{
107+
return true;
108+
}
109+
}
110+
return false;
111+
}
94112
}
95113

96114
}

com.unity.ml-agents.extensions/Tests/Runtime/Sensors/RigidBodySensorTests.cs

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public void TestNullRootBody()
3131
var gameObj = new GameObject();
3232

3333
var sensorComponent = gameObj.AddComponent<RigidBodySensorComponent>();
34+
Assert.IsFalse(sensorComponent.IsTrivial());
3435
var sensor = sensorComponent.CreateSensors()[0];
3536
SensorTestHelper.CompareObservation(sensor, new float[0]);
3637
}
@@ -48,6 +49,7 @@ public void TestSingleRigidbody()
4849
UseLocalSpaceTranslations = true,
4950
UseLocalSpaceRotations = true
5051
};
52+
Assert.IsTrue(sensorComponent.IsTrivial());
5153

5254
var sensor = sensorComponent.CreateSensors()[0];
5355
sensor.Update();
@@ -93,6 +95,7 @@ public void TestBodiesWithJoint()
9395
UseLocalSpaceLinearVelocity = true
9496
};
9597
sensorComponent.VirtualRoot = virtualRoot;
98+
Assert.IsFalse(sensorComponent.IsTrivial());
9699

97100
var sensor = sensorComponent.CreateSensors()[0];
98101
sensor.Update();

com.unity.ml-agents/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to
1515
#### ml-agents / ml-agents-envs / gym-unity (Python)
1616
- Added a fully connected visual encoder for environments with very small image inputs. (#5351)
1717
### Bug Fixes
18+
- RigidBodySensorComponent now displays a warning if it's used in a way that won't generate useful observations. (#5387)
1819

1920

2021
## [2.0.0-exp.1] - 2021-04-22

0 commit comments

Comments
 (0)