Skip to content

Commit 1d363b1

Browse files
authored
Merge pull request #189 from Unity-Technologies/usim-app-param-fix
App-params now deserialize from the correct location in USim
2 parents 7745ed8 + bfa8ed3 commit 1d363b1

File tree

6 files changed

+44
-51
lines changed

6 files changed

+44
-51
lines changed
Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,11 @@
11
using UnityEditor;
2+
using UnityEditor.Perception.Randomization;
23
using UnityEditor.UIElements;
34
using UnityEngine.Perception.Randomization.Randomizers;
45
using UnityEngine.UIElements;
56

67
namespace UnityEngine.Perception.Randomization.Editor
78
{
89
[CustomEditor(typeof(RandomizerTag), true)]
9-
public class RandomizerTagEditor : UnityEditor.Editor
10-
{
11-
public override VisualElement CreateInspectorGUI()
12-
{
13-
var rootElement = new VisualElement();
14-
CreatePropertyFields(rootElement);
15-
return rootElement;
16-
}
17-
18-
void CreatePropertyFields(VisualElement rootElement)
19-
{
20-
var iterator = serializedObject.GetIterator();
21-
iterator.NextVisible(true);
22-
do
23-
{
24-
if (iterator.name == "m_Script")
25-
continue;
26-
var propertyField = new PropertyField(iterator.Copy());
27-
propertyField.Bind(serializedObject);
28-
rootElement.Add(propertyField);
29-
} while (iterator.NextVisible(false));
30-
}
31-
}
10+
class RandomizerTagEditor : ParameterUIElementsEditor { }
3211
}

com.unity.perception/Editor/Randomization/Utilities/UIElementsEditorUtilities.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ public static void CreatePropertyFields(SerializedObject serializedObj, VisualEl
2222
var fieldType = serializedObj.targetObject.GetType();
2323
var iterator = serializedObj.GetIterator();
2424
iterator.NextVisible(true);
25-
iterator.NextVisible(false);
26-
do
25+
if (iterator.NextVisible(false))
2726
{
28-
var propertyField = CreatePropertyField(iterator, fieldType);
29-
containerElement.Add(propertyField);
30-
} while (iterator.NextVisible(false));
27+
do
28+
{
29+
var propertyField = CreatePropertyField(iterator, fieldType);
30+
containerElement.Add(propertyField);
31+
} while (iterator.NextVisible(false));
32+
}
3133
}
3234

3335
/// <summary>
@@ -39,19 +41,19 @@ public static void CreatePropertyFields(SerializedObject serializedObj, VisualEl
3941
public static void CreatePropertyFields(SerializedProperty property, VisualElement containerElement)
4042
{
4143
var fieldType = StaticData.GetManagedReferenceValue(property).GetType();
42-
4344
var iterator = property.Copy();
4445
var nextSiblingProperty = property.Copy();
4546
nextSiblingProperty.NextVisible(false);
46-
4747
if (iterator.NextVisible(true))
48+
{
4849
do
4950
{
5051
if (SerializedProperty.EqualContents(iterator, nextSiblingProperty))
5152
break;
5253
var propertyField = CreatePropertyField(iterator, fieldType);
5354
containerElement.Add(propertyField);
5455
} while (iterator.NextVisible(false));
56+
}
5557
}
5658

5759
/// <summary>

com.unity.perception/Runtime/GroundTruth/Labelers/BoundingBox3DLabeler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ static Vector3 CalculateRotatedPoint(Camera cam, Vector3 start, Vector3 xDirecti
321321
return VisualizationHelper.ConvertToScreenSpace(cam, worldPoint);
322322
}
323323

324+
/// <inheritdoc/>
324325
protected override void OnVisualize()
325326
{
326327
if (m_ToReport == null) return;

com.unity.perception/Runtime/Randomization/Scenarios/Scenario.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,19 @@ static JObject SerializeSampler(ISampler sampler)
115115
/// <inheritdoc/>
116116
public override void DeserializeFromFile(string configFilePath)
117117
{
118-
if (string.IsNullOrEmpty(configFilePath))
119-
throw new ArgumentNullException();
120-
if (!File.Exists(configFilePath))
121-
throw new FileNotFoundException($"A scenario configuration file does not exist at path {configFilePath}");
118+
if (string.IsNullOrEmpty(configFilePath) || !File.Exists(configFilePath))
119+
Debug.Log($"No configuration file found at {defaultConfigFilePath}");
120+
else
121+
{
122122
#if UNITY_EDITOR
123-
Debug.Log($"Deserialized scenario configuration from <a href=\"file:///${configFilePath}\">{configFilePath}</a>. " +
124-
"Using undo in the editor will revert these changes to your scenario.");
123+
Debug.Log($"Deserialized scenario configuration from <a href=\"file:///${configFilePath}\">{configFilePath}</a>. " +
124+
"Using undo in the editor will revert these changes to your scenario.");
125125
#else
126-
Debug.Log($"Deserialized scenario configuration from <a href=\"file:///${configFilePath}\">{configFilePath}</a>");
126+
Debug.Log($"Deserialized scenario configuration from <a href=\"file:///${configFilePath}\">{configFilePath}</a>");
127127
#endif
128-
var jsonText = File.ReadAllText(configFilePath);
129-
DeserializeFromJson(jsonText);
128+
var jsonText = File.ReadAllText(configFilePath);
129+
DeserializeFromJson(jsonText);
130+
}
130131
}
131132

132133
/// <inheritdoc/>

com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private set
8585
/// <summary>
8686
/// Returns the absolute file path of the JSON serialized configuration
8787
/// </summary>
88-
public string defaultConfigFilePath =>
88+
public virtual string defaultConfigFilePath =>
8989
Application.dataPath + "/StreamingAssets/" + configFileName + ".json";
9090

9191
/// <summary>
@@ -165,6 +165,15 @@ public void DeserializeFromFile()
165165
DeserializeFromFile(defaultConfigFilePath);
166166
}
167167

168+
/// <summary>
169+
/// Resets SamplerState.randomState with a new seed value generated by hashing this Scenario's randomSeed
170+
/// with its currentIteration
171+
/// </summary>
172+
protected virtual void ResetRandomStateOnIteration()
173+
{
174+
SamplerState.randomState = SamplerUtility.IterateSeed((uint)currentIteration, genericConstants.randomSeed);
175+
}
176+
168177
/// <summary>
169178
/// This method executed directly after this scenario has been registered and initialized
170179
/// </summary>
@@ -204,11 +213,7 @@ void Start()
204213
Guid.Parse("14adb394-46c0-47e8-a3f0-99e754483b76"));
205214
DatasetCapture.ReportMetric(randomSeedMetricDefinition, new[] { genericConstants.randomSeed });
206215
#if !UNITY_EDITOR
207-
if (File.Exists(defaultConfigFilePath))
208-
DeserializeFromFile();
209-
else
210-
Debug.Log($"No configuration file found at {defaultConfigFilePath}. " +
211-
"Proceeding with built in scenario constants and randomizer settings.");
216+
DeserializeFromFile();
212217
#endif
213218
}
214219

@@ -272,8 +277,7 @@ void Update()
272277
if (currentIterationFrame == 0)
273278
{
274279
DatasetCapture.StartNewSequence();
275-
SamplerState.randomState = SamplerUtility.IterateSeed((uint)currentIteration, genericConstants.randomSeed);
276-
280+
ResetRandomStateOnIteration();
277281
DatasetCapture.ReportMetric(m_IterationMetricDefinition, new[]
278282
{
279283
new IterationMetricData()

com.unity.perception/Runtime/Randomization/Scenarios/UnitySimulationScenario.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
2+
using System.IO;
23
using Unity.Simulation;
4+
using UnityEngine.Perception.Randomization.Samplers;
35

46
namespace UnityEngine.Perception.Randomization.Scenarios
57
{
@@ -12,6 +14,12 @@ namespace UnityEngine.Perception.Randomization.Scenarios
1214
/// <inheritdoc/>
1315
public sealed override bool isScenarioComplete => currentIteration >= constants.totalIterations;
1416

17+
/// <inheritdoc/>
18+
public override string defaultConfigFilePath =>
19+
Configuration.Instance.IsSimulationRunningInCloud()
20+
? new Uri(Configuration.Instance.SimulationConfig.app_param_uri).LocalPath
21+
: base.defaultConfigFilePath;
22+
1523
/// <inheritdoc/>
1624
protected sealed override void IncrementIteration()
1725
{
@@ -21,10 +29,8 @@ protected sealed override void IncrementIteration()
2129
/// <inheritdoc/>
2230
public sealed override void DeserializeFromFile(string configFilePath)
2331
{
24-
base.DeserializeFromFile(Configuration.Instance.IsSimulationRunningInCloud()
25-
? new Uri(Configuration.Instance.SimulationConfig.app_param_uri).LocalPath
26-
: configFilePath);
27-
currentIteration = constants.instanceIndex;
32+
base.DeserializeFromFile(configFilePath);
33+
currentIteration = constants.instanceIndex * constants.instanceCount;
2834
}
2935
}
3036
}

0 commit comments

Comments
 (0)