Skip to content

Commit 22689c8

Browse files
authored
Add type alias support to the reverse bindings (#USDU-3 part 2) (#260)
* Ignore abstract prims when getting paths by type. * Update bindings binaries * Update win-10 yamato image * Add type alias suport for reverse binding * Fix test * Add wrapper method to access typeAliases
1 parent 65402ee commit 22689c8

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

package/com.unity.formats.usd/Dependencies/USD.NET.Unity/UnityTypeBindings.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ static public void RegisterTypes()
5555
(object obj) => UnityTypeConverter.Vector2ToVec2f((Vector2)obj),
5656
(pxr.VtValue value) => UnityTypeConverter.Vec2fToVector2(value),
5757
SdfValueTypeNames.Float2));
58+
binder.AddTypeAlias(SdfValueTypeNames.TexCoord2f, SdfValueTypeNames.Float2);
5859
binder.BindType(typeof(Vector3), new UsdTypeBinding(
5960
(object obj) => UnityTypeConverter.Vector3ToVec3f((Vector3)obj),
6061
(pxr.VtValue value) => UnityTypeConverter.Vec3fToVector3(value),
6162
SdfValueTypeNames.Float3));
63+
binder.AddTypeAlias(SdfValueTypeNames.TexCoord3f, SdfValueTypeNames.Float3);
6264
binder.BindType(typeof(Vector4), new UsdTypeBinding(
6365
(object obj) => UnityTypeConverter.Vector4ToVec4f((Vector4)obj),
6466
(pxr.VtValue value) => UnityTypeConverter.Vec4fToVector4(value),
@@ -77,9 +79,11 @@ static public void RegisterTypes()
7779
//
7880
binder.BindArrayType<UnityTypeConverter>(typeof(Vector2[]), typeof(pxr.VtVec2fArray), SdfValueTypeNames.Float2Array);
7981
binder.BindArrayType<UnityTypeConverter>(typeof(List<Vector2>), typeof(pxr.VtVec2fArray), SdfValueTypeNames.Float2Array, "List");
82+
binder.AddTypeAlias(SdfValueTypeNames.TexCoord2fArray, SdfValueTypeNames.Float2Array);
8083

8184
binder.BindArrayType<UnityTypeConverter>(typeof(Vector3[]), typeof(pxr.VtVec3fArray), SdfValueTypeNames.Float3Array);
8285
binder.BindArrayType<UnityTypeConverter>(typeof(List<Vector3>), typeof(pxr.VtVec3fArray), SdfValueTypeNames.Float3Array, "List");
86+
binder.AddTypeAlias(SdfValueTypeNames.TexCoord3fArray, SdfValueTypeNames.Float3Array);
8387

8488
binder.BindArrayType<UnityTypeConverter>(typeof(Vector4[]), typeof(pxr.VtVec4fArray), SdfValueTypeNames.Float4Array);
8589
binder.BindArrayType<UnityTypeConverter>(typeof(List<Vector4>), typeof(pxr.VtVec4fArray), SdfValueTypeNames.Float4Array, "List");

package/com.unity.formats.usd/Dependencies/USD.NET/USD.NET.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14973,12 +14973,14 @@ namespace USD.NET
1497314973
{
1497414974
public static bool EnableCodeGeneration;
1497514975
public TypeBinder() {}
14976+
public void AddTypeAlias(pxr.SdfValueTypeName alias, pxr.SdfValueTypeName target);
1497614977
public void BindArrayType<ConverterT>(System.Type csType, System.Type vtArrayType, pxr.SdfValueTypeName sdfName, string methodNamePrefix = );
1497714978
public void BindNativeType(System.Type csType, pxr.SdfValueTypeName sdfName);
1497814979
public void BindType(System.Type csType, USD.NET.UsdTypeBinding binding);
1497914980
public bool GetBinding(System.Type key, out USD.NET.UsdTypeBinding binding);
1498014981
public bool GetReverseBinding(pxr.SdfValueTypeName key, out USD.NET.UsdTypeBinding binding);
1498114982
public bool GetReverseBinding(System.Type key, out USD.NET.UsdTypeBinding binding);
14983+
public bool RemoveTypeAlias(pxr.SdfValueTypeName alias);
1498214984
}
1498314985

1498414986
public class UsdAssetPathAttribute : System.Attribute

package/com.unity.formats.usd/Dependencies/USD.NET/serialization/TypeBinder.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
using System;
1616
using System.Collections.Generic;
17+
using System.Runtime.CompilerServices;
18+
using pxr;
1719

1820
namespace USD.NET
1921
{
@@ -68,6 +70,12 @@ public class TypeBinder
6870
Dictionary<string, string> typeNameMapping = new Dictionary<string, string>();
6971
Dictionary<Type, Dictionary<pxr.TfToken, Enum>> enumMaps = new Dictionary<Type, Dictionary<pxr.TfToken, Enum>>();
7072

73+
/// <summary>
74+
/// USD types can be aliased to represent different roles float2/texcoord2, vec3/point/color and drive the interpretation
75+
/// Add aliases if your target application need to map multiple USD types to a single type.
76+
/// </summary>
77+
Dictionary<string, string> typeAliases = new Dictionary<string, string>();
78+
7179
public TypeBinder()
7280
{
7381
// TODO: kill this, see above.
@@ -93,12 +101,29 @@ private bool IsCodeGenEnabled()
93101
#endif
94102
}
95103

104+
/// <summary>
105+
/// Get a binding from a USD type
106+
/// </summary>
107+
/// <remarks>
108+
/// Used the C# type needs to be derived from the USD type.
109+
/// Example: UVs can be of type float2, float3, texcoord2f, texcoord2f
110+
/// </remarks>
96111
public bool GetReverseBinding(pxr.SdfValueTypeName key, out UsdTypeBinding binding)
97112
{
113+
// Check if the given sdf type name is an alias
114+
// https://graphics.pixar.com/usd/docs/api/_usd__page__datatypes.html#Usd_Roles
115+
string name;
116+
bool match = true;
117+
if (!typeAliases.TryGetValue(key.GetAsToken(), out name))
118+
{
119+
name = key.GetAsToken();
120+
match = false;
121+
}
122+
98123
// TODO: we could keep a reverse mapping, but waiting for deeper performance analysis first.
99124
foreach (var kvp in bindings)
100125
{
101-
if (kvp.Value.sdfTypeName == key)
126+
if (kvp.Value.sdfTypeName.GetAsToken() == name)
102127
{
103128
binding = kvp.Value;
104129
return true;
@@ -414,6 +439,16 @@ object GuidToCs_String(pxr.VtValue vtValue)
414439
return new Guid(pxr.UsdCs.VtValueTostring(vtValue));
415440
}
416441

442+
public void AddTypeAlias(SdfValueTypeName alias, SdfValueTypeName target)
443+
{
444+
typeAliases.Add(alias.GetAsToken(), target.GetAsToken());
445+
}
446+
447+
public bool RemoveTypeAlias(SdfValueTypeName alias)
448+
{
449+
return typeAliases.Remove(alias.GetAsToken());
450+
}
451+
417452
private void RegisterIntrinsicTypes()
418453
{
419454
// --------------------------------------------------------------------------------------- //

package/com.unity.formats.usd/Tests/USD.NET/BasicTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,5 +659,23 @@ public static void ReadNonExistingPrimsTest()
659659
Assert.True(sample3.number == 0);
660660
Assert.True(sample3.rel == null);
661661
}
662+
663+
[Test]
664+
public static void ReverseBindingForAliasedType_NotFoundByDefault()
665+
{
666+
UsdTypeBinding binding;
667+
UsdIo.Bindings.GetReverseBinding(SdfValueTypeNames.Normal3f, out binding);
668+
Assert.Null(binding.sdfTypeName);
669+
}
670+
671+
[Test]
672+
public static void ReverseBindingForAliasedType_FoundAfterAliasing()
673+
{
674+
UsdIo.Bindings.AddTypeAlias(SdfValueTypeNames.Normal3f, SdfValueTypeNames.Float3);
675+
UsdTypeBinding binding;
676+
UsdIo.Bindings.GetReverseBinding(SdfValueTypeNames.Normal3f, out binding);
677+
Assert.AreEqual(SdfValueTypeNames.Float3, binding.sdfTypeName);
678+
UsdIo.Bindings.RemoveTypeAlias(SdfValueTypeNames.Normal3f);
679+
}
662680
}
663681
}

0 commit comments

Comments
 (0)