Skip to content

Add type alias support to the reverse bindings (#USDU-3 part 2) #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ static public void RegisterTypes()
(object obj) => UnityTypeConverter.Vector2ToVec2f((Vector2)obj),
(pxr.VtValue value) => UnityTypeConverter.Vec2fToVector2(value),
SdfValueTypeNames.Float2));
binder.AddTypeAlias(SdfValueTypeNames.TexCoord2f, SdfValueTypeNames.Float2);
binder.BindType(typeof(Vector3), new UsdTypeBinding(
(object obj) => UnityTypeConverter.Vector3ToVec3f((Vector3)obj),
(pxr.VtValue value) => UnityTypeConverter.Vec3fToVector3(value),
SdfValueTypeNames.Float3));
binder.AddTypeAlias(SdfValueTypeNames.TexCoord3f, SdfValueTypeNames.Float3);
binder.BindType(typeof(Vector4), new UsdTypeBinding(
(object obj) => UnityTypeConverter.Vector4ToVec4f((Vector4)obj),
(pxr.VtValue value) => UnityTypeConverter.Vec4fToVector4(value),
Expand All @@ -77,9 +79,11 @@ static public void RegisterTypes()
//
binder.BindArrayType<UnityTypeConverter>(typeof(Vector2[]), typeof(pxr.VtVec2fArray), SdfValueTypeNames.Float2Array);
binder.BindArrayType<UnityTypeConverter>(typeof(List<Vector2>), typeof(pxr.VtVec2fArray), SdfValueTypeNames.Float2Array, "List");
binder.AddTypeAlias(SdfValueTypeNames.TexCoord2fArray, SdfValueTypeNames.Float2Array);

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

binder.BindArrayType<UnityTypeConverter>(typeof(Vector4[]), typeof(pxr.VtVec4fArray), SdfValueTypeNames.Float4Array);
binder.BindArrayType<UnityTypeConverter>(typeof(List<Vector4>), typeof(pxr.VtVec4fArray), SdfValueTypeNames.Float4Array, "List");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14973,12 +14973,14 @@ namespace USD.NET
{
public static bool EnableCodeGeneration;
public TypeBinder() {}
public void AddTypeAlias(pxr.SdfValueTypeName alias, pxr.SdfValueTypeName target);
public void BindArrayType<ConverterT>(System.Type csType, System.Type vtArrayType, pxr.SdfValueTypeName sdfName, string methodNamePrefix = );
public void BindNativeType(System.Type csType, pxr.SdfValueTypeName sdfName);
public void BindType(System.Type csType, USD.NET.UsdTypeBinding binding);
public bool GetBinding(System.Type key, out USD.NET.UsdTypeBinding binding);
public bool GetReverseBinding(pxr.SdfValueTypeName key, out USD.NET.UsdTypeBinding binding);
public bool GetReverseBinding(System.Type key, out USD.NET.UsdTypeBinding binding);
public bool RemoveTypeAlias(pxr.SdfValueTypeName alias);
}

public class UsdAssetPathAttribute : System.Attribute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using pxr;

namespace USD.NET
{
Expand Down Expand Up @@ -68,6 +70,12 @@ public class TypeBinder
Dictionary<string, string> typeNameMapping = new Dictionary<string, string>();
Dictionary<Type, Dictionary<pxr.TfToken, Enum>> enumMaps = new Dictionary<Type, Dictionary<pxr.TfToken, Enum>>();

/// <summary>
/// USD types can be aliased to represent different roles float2/texcoord2, vec3/point/color and drive the interpretation
/// Add aliases if your target application need to map multiple USD types to a single type.
/// </summary>
Dictionary<string, string> typeAliases = new Dictionary<string, string>();

public TypeBinder()
{
// TODO: kill this, see above.
Expand All @@ -93,12 +101,29 @@ private bool IsCodeGenEnabled()
#endif
}

/// <summary>
/// Get a binding from a USD type
/// </summary>
/// <remarks>
/// Used the C# type needs to be derived from the USD type.
/// Example: UVs can be of type float2, float3, texcoord2f, texcoord2f
/// </remarks>
public bool GetReverseBinding(pxr.SdfValueTypeName key, out UsdTypeBinding binding)
{
// Check if the given sdf type name is an alias
// https://graphics.pixar.com/usd/docs/api/_usd__page__datatypes.html#Usd_Roles
string name;
bool match = true;
if (!typeAliases.TryGetValue(key.GetAsToken(), out name))
{
name = key.GetAsToken();
match = false;
}

// TODO: we could keep a reverse mapping, but waiting for deeper performance analysis first.
foreach (var kvp in bindings)
{
if (kvp.Value.sdfTypeName == key)
if (kvp.Value.sdfTypeName.GetAsToken() == name)
{
binding = kvp.Value;
return true;
Expand Down Expand Up @@ -414,6 +439,16 @@ object GuidToCs_String(pxr.VtValue vtValue)
return new Guid(pxr.UsdCs.VtValueTostring(vtValue));
}

public void AddTypeAlias(SdfValueTypeName alias, SdfValueTypeName target)
{
typeAliases.Add(alias.GetAsToken(), target.GetAsToken());
}

public bool RemoveTypeAlias(SdfValueTypeName alias)
{
return typeAliases.Remove(alias.GetAsToken());
}

private void RegisterIntrinsicTypes()
{
// --------------------------------------------------------------------------------------- //
Expand Down
18 changes: 18 additions & 0 deletions package/com.unity.formats.usd/Tests/USD.NET/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -659,5 +659,23 @@ public static void ReadNonExistingPrimsTest()
Assert.True(sample3.number == 0);
Assert.True(sample3.rel == null);
}

[Test]
public static void ReverseBindingForAliasedType_NotFoundByDefault()
{
UsdTypeBinding binding;
UsdIo.Bindings.GetReverseBinding(SdfValueTypeNames.Normal3f, out binding);
Assert.Null(binding.sdfTypeName);
}

[Test]
public static void ReverseBindingForAliasedType_FoundAfterAliasing()
{
UsdIo.Bindings.AddTypeAlias(SdfValueTypeNames.Normal3f, SdfValueTypeNames.Float3);
UsdTypeBinding binding;
UsdIo.Bindings.GetReverseBinding(SdfValueTypeNames.Normal3f, out binding);
Assert.AreEqual(SdfValueTypeNames.Float3, binding.sdfTypeName);
UsdIo.Bindings.RemoveTypeAlias(SdfValueTypeNames.Normal3f);
}
}
}