JIRA: Writing to an invalid prim crashes Unity
MODIFY
package/com.unity.formats.usd/Dependencies/USD.NET/serialization/Scene.cs
package/com.unity.formats.usd/Dependencies/USD.NET/serialization/Scene.cs
@@ -782,7 +782,8 @@ namespace USD.NET
// TODO(jcowles): there is a potential issue here if the cache gets out of sync with the
// underlying USD scene. The correct fix is to listen for change processing events and
// clear the cache accordingly.
- if (!m_primMap.TryGetValue(path, out prim))
+ prim = GetUsdPrim(path);
+ if (!prim.IsValid())
{
if (WriteMode == WriteModes.Define)
{
@@ -850,14 +851,20 @@ namespace USD.NET
private pxr.UsdPrim GetUsdPrim(SdfPath path)
{
UsdPrim prim;
- lock (m_stageLock) {
- if (!m_primMap.TryGetValue(path, out prim) || !prim.IsValid())
+ lock (m_stageLock)
+ {
+ var has = m_primMap.TryGetValue(path, out prim);
+ if (!has || !prim.IsValid())
{
prim = Stage.GetPrimAtPath(path);
if (prim.IsValid())
{
m_primMap[path] = prim;
}
+ else if (has)
+ {
+ m_primMap.Remove(path);
+ }
}
}
return prim;

MODIFY
package/com.unity.formats.usd/Tests/USD.NET/StageTests.cs
package/com.unity.formats.usd/Tests/USD.NET/StageTests.cs
@@ -310,5 +310,17 @@ namespace USD.NET.Tests
Assert.IsTrue(prim.IsValid() == false);
stage.Dispose();
}
+
+ [Test]
+ public void WritingInvalidPrims_ShouldNotCrash()
+ {
+ var stage = pxr.UsdStage.CreateInMemory(UsdStage.InitialLoadSet.LoadNone);
+ var scene = Scene.Open(stage);
+ scene.Write("/Foo", new CubeSample());
+ scene.Stage.RemovePrim(scene.GetSdfPath("/Foo"));
+ scene.Write("/Foo", new CubeSample());
+
+ Assert.IsTrue(scene.GetPrimAtPath("/Foo").IsValid());
+ }
}
}

JIRA: USDU-168 Writing to an invalid prim crashes Unity (#256)