From ba00ae16ed8320b46307954d42ab19a1b9db3c98 Mon Sep 17 00:00:00 2001 From: pixar-oss Date: Mon, 11 Jan 2021 11:27:35 -0800 Subject: [PATCH] usdGenSchema now adds a type alias to plugInfo for abstract typed schemas too. The impact of this is that abstract typed schemas now have an official USD type name that can be retrieved from UsdSchemaRegistry::GetSchemaTypeName or inputted to UsdSchemaRegistry::GetSchemaTypeFromTypeName (Internal change: 2136573) --- pxr/usd/usd/schemaRegistry.cpp | 151 +++++++++--------- .../baseline/basic/plugInfo.json | 3 + .../headerTerminatorString/plugInfo.json | 3 + .../baseline/namespace/plugInfo.json | 3 + .../baseline/nestedNamespace/plugInfo.json | 3 + pxr/usd/usd/testenv/testUsdSchemaRegistry.py | 36 ++++- .../resources/generatedSchema.usda | 15 ++ .../resources/plugInfo.json | 12 +- .../resources/schema.usda | 12 +- pxr/usd/usd/usdGenSchema.py | 2 +- pxr/usd/usdGeom/plugInfo.json | 18 +++ pxr/usd/usdLux/plugInfo.json | 3 + pxr/usd/usdRender/plugInfo.json | 3 + pxr/usd/usdVol/plugInfo.json | 6 + 14 files changed, 188 insertions(+), 82 deletions(-) diff --git a/pxr/usd/usd/schemaRegistry.cpp b/pxr/usd/usd/schemaRegistry.cpp index 78c7e56978..59c7235b9f 100644 --- a/pxr/usd/usd/schemaRegistry.cpp +++ b/pxr/usd/usd/schemaRegistry.cpp @@ -91,43 +91,42 @@ struct _TypeMapCache { const TfType schemaBaseType = TfType::Find(); auto _MapDerivedTypes = [this, &schemaBaseType]( - const TfType &baseType, bool isConcrete) + const TfType &baseType, bool isTyped) { set types; PlugRegistry::GetAllDerivedTypes(baseType, &types); for (const TfType &type : types) { // The USD type name is the type's alias under UsdSchemaBase. - // Only concrete typed and API schemas should have a type name - // alias. + // All schemas should have a type name alias. const vector aliases = schemaBaseType.GetAliases(type); if (aliases.size() == 1) { TfToken typeName(aliases.front(), TfToken::Immortal); nameToType.insert(std::make_pair( - typeName, TypeInfo(type, isConcrete))); + typeName, TypeInfo(type, isTyped))); typeToName.insert(std::make_pair( - type, TypeNameInfo(typeName, isConcrete))); + type, TypeNameInfo(typeName, isTyped))); } } }; - _MapDerivedTypes(TfType::Find(), /*isConcrete=*/true); - _MapDerivedTypes(TfType::Find(), /*isConcrete=*/false); + _MapDerivedTypes(TfType::Find(), /*isTyped=*/true); + _MapDerivedTypes(TfType::Find(), /*isTyped=*/false); } // For each type and type name mapping we also want to store if it's a // concrete prim type vs an API schema type. struct TypeInfo { TfType type; - bool isConcrete; - TypeInfo(const TfType &type_, bool isConcrete_) - : type(type_), isConcrete(isConcrete_) {} + bool isTyped; + TypeInfo(const TfType &type_, bool isTyped_) + : type(type_), isTyped(isTyped_) {} }; struct TypeNameInfo { TfToken name; - bool isConcrete; - TypeNameInfo(const TfToken &name_, bool isConcrete_) - : name(name_), isConcrete(isConcrete_) {} + bool isTyped; + TypeNameInfo(const TfToken &name_, bool isTyped_) + : name(name_), isTyped(isTyped_) {} }; TfHashMap nameToType; @@ -141,64 +140,6 @@ static const _TypeMapCache &_GetTypeMapCache() { return typeCache; } -/*static*/ -TfToken -UsdSchemaRegistry::GetSchemaTypeName(const TfType &schemaType) -{ - const _TypeMapCache & typeMapCache = _GetTypeMapCache(); - auto it = typeMapCache.typeToName.find(schemaType); - return it != typeMapCache.typeToName.end() ? it->second.name : TfToken(); -} - -/*static*/ -TfToken -UsdSchemaRegistry::GetConcreteSchemaTypeName(const TfType &schemaType) -{ - const _TypeMapCache & typeMapCache = _GetTypeMapCache(); - auto it = typeMapCache.typeToName.find(schemaType); - return it != typeMapCache.typeToName.end() && it->second.isConcrete ? - it->second.name : TfToken(); -} - -/*static*/ -TfToken -UsdSchemaRegistry::GetAPISchemaTypeName(const TfType &schemaType) -{ - const _TypeMapCache & typeMapCache = _GetTypeMapCache(); - auto it = typeMapCache.typeToName.find(schemaType); - return it != typeMapCache.typeToName.end() && !it->second.isConcrete ? - it->second.name : TfToken(); -} - -/*static*/ -TfType -UsdSchemaRegistry::GetTypeFromSchemaTypeName(const TfToken &typeName) -{ - const _TypeMapCache & typeMapCache = _GetTypeMapCache(); - auto it = typeMapCache.nameToType.find(typeName); - return it != typeMapCache.nameToType.end() ? it->second.type : TfType(); -} - -/*static*/ -TfType -UsdSchemaRegistry::GetConcreteTypeFromSchemaTypeName(const TfToken &typeName) -{ - const _TypeMapCache & typeMapCache = _GetTypeMapCache(); - auto it = typeMapCache.nameToType.find(typeName); - return it != typeMapCache.nameToType.end() && it->second.isConcrete ? - it->second.type : TfType(); -} - -/*static*/ -TfType -UsdSchemaRegistry::GetAPITypeFromSchemaTypeName(const TfToken &typeName) -{ - const _TypeMapCache & typeMapCache = _GetTypeMapCache(); - auto it = typeMapCache.nameToType.find(typeName); - return it != typeMapCache.nameToType.end() && !it->second.isConcrete ? - it->second.type : TfType(); -} - static bool _IsConcreteSchemaKind(const UsdSchemaKind schemaKind) { @@ -261,6 +202,72 @@ _GetSchemaKindFromPlugin(const TfType &schemaType) return _GetSchemaKindFromMetadata(plugin->GetMetadataForType(schemaType)); } +/*static*/ +TfToken +UsdSchemaRegistry::GetSchemaTypeName(const TfType &schemaType) +{ + const _TypeMapCache & typeMapCache = _GetTypeMapCache(); + auto it = typeMapCache.typeToName.find(schemaType); + return it != typeMapCache.typeToName.end() ? it->second.name : TfToken(); +} + +/*static*/ +TfToken +UsdSchemaRegistry::GetConcreteSchemaTypeName(const TfType &schemaType) +{ + const _TypeMapCache & typeMapCache = _GetTypeMapCache(); + auto it = typeMapCache.typeToName.find(schemaType); + if (it != typeMapCache.typeToName.end() && + it->second.isTyped && + _IsConcreteSchemaKind(_GetSchemaKindFromPlugin(schemaType))) { + return it->second.name; + } + return TfToken(); +} + +/*static*/ +TfToken +UsdSchemaRegistry::GetAPISchemaTypeName(const TfType &schemaType) +{ + const _TypeMapCache & typeMapCache = _GetTypeMapCache(); + auto it = typeMapCache.typeToName.find(schemaType); + return it != typeMapCache.typeToName.end() && !it->second.isTyped ? + it->second.name : TfToken(); +} + +/*static*/ +TfType +UsdSchemaRegistry::GetTypeFromSchemaTypeName(const TfToken &typeName) +{ + const _TypeMapCache & typeMapCache = _GetTypeMapCache(); + auto it = typeMapCache.nameToType.find(typeName); + return it != typeMapCache.nameToType.end() ? it->second.type : TfType(); +} + +/*static*/ +TfType +UsdSchemaRegistry::GetConcreteTypeFromSchemaTypeName(const TfToken &typeName) +{ + const _TypeMapCache & typeMapCache = _GetTypeMapCache(); + auto it = typeMapCache.nameToType.find(typeName); + if (it != typeMapCache.nameToType.end() && + it->second.isTyped && + _IsConcreteSchemaKind(_GetSchemaKindFromPlugin(it->second.type))) { + return it->second.type; + } + return TfType(); +} + +/*static*/ +TfType +UsdSchemaRegistry::GetAPITypeFromSchemaTypeName(const TfToken &typeName) +{ + const _TypeMapCache & typeMapCache = _GetTypeMapCache(); + auto it = typeMapCache.nameToType.find(typeName); + return it != typeMapCache.nameToType.end() && !it->second.isTyped ? + it->second.type : TfType(); +} + // This result struct is useful for handling the fact that we're going to // support backwards compatibility (for a bit) with schemas that were generated // before usdGenSchema has started putting schema kind in the plugInfo. When we @@ -498,7 +505,7 @@ _GetAppliedAPISchemaNames() const TfType &type = valuePair.first; const TfToken &typeName = valuePair.second.name; - if (!valuePair.second.isConcrete && + if (!valuePair.second.isTyped && _IsAppliedAPISchemaKind(_GetSchemaKind(type).schemaKind)) { result.insert(typeName); } diff --git a/pxr/usd/usd/testenv/testUsdSchemaGen/baseline/basic/plugInfo.json b/pxr/usd/usd/testenv/testUsdSchemaGen/baseline/basic/plugInfo.json index fe50de2a7e..15f9f097fe 100644 --- a/pxr/usd/usd/testenv/testUsdSchemaGen/baseline/basic/plugInfo.json +++ b/pxr/usd/usd/testenv/testUsdSchemaGen/baseline/basic/plugInfo.json @@ -7,6 +7,9 @@ "Info": { "Types": { "UsdContrivedBase": { + "alias": { + "UsdSchemaBase": "Base" + }, "autoGenerated": true, "bases": [ "UsdTyped" diff --git a/pxr/usd/usd/testenv/testUsdSchemaGen/baseline/headerTerminatorString/plugInfo.json b/pxr/usd/usd/testenv/testUsdSchemaGen/baseline/headerTerminatorString/plugInfo.json index fe50de2a7e..15f9f097fe 100644 --- a/pxr/usd/usd/testenv/testUsdSchemaGen/baseline/headerTerminatorString/plugInfo.json +++ b/pxr/usd/usd/testenv/testUsdSchemaGen/baseline/headerTerminatorString/plugInfo.json @@ -7,6 +7,9 @@ "Info": { "Types": { "UsdContrivedBase": { + "alias": { + "UsdSchemaBase": "Base" + }, "autoGenerated": true, "bases": [ "UsdTyped" diff --git a/pxr/usd/usd/testenv/testUsdSchemaGen/baseline/namespace/plugInfo.json b/pxr/usd/usd/testenv/testUsdSchemaGen/baseline/namespace/plugInfo.json index fe50de2a7e..15f9f097fe 100644 --- a/pxr/usd/usd/testenv/testUsdSchemaGen/baseline/namespace/plugInfo.json +++ b/pxr/usd/usd/testenv/testUsdSchemaGen/baseline/namespace/plugInfo.json @@ -7,6 +7,9 @@ "Info": { "Types": { "UsdContrivedBase": { + "alias": { + "UsdSchemaBase": "Base" + }, "autoGenerated": true, "bases": [ "UsdTyped" diff --git a/pxr/usd/usd/testenv/testUsdSchemaGen/baseline/nestedNamespace/plugInfo.json b/pxr/usd/usd/testenv/testUsdSchemaGen/baseline/nestedNamespace/plugInfo.json index fe50de2a7e..15f9f097fe 100644 --- a/pxr/usd/usd/testenv/testUsdSchemaGen/baseline/nestedNamespace/plugInfo.json +++ b/pxr/usd/usd/testenv/testUsdSchemaGen/baseline/nestedNamespace/plugInfo.json @@ -7,6 +7,9 @@ "Info": { "Types": { "UsdContrivedBase": { + "alias": { + "UsdSchemaBase": "Base" + }, "autoGenerated": true, "bases": [ "UsdTyped" diff --git a/pxr/usd/usd/testenv/testUsdSchemaRegistry.py b/pxr/usd/usd/testenv/testUsdSchemaRegistry.py index f521d2cdef..c42231d0f6 100644 --- a/pxr/usd/usd/testenv/testUsdSchemaRegistry.py +++ b/pxr/usd/usd/testenv/testUsdSchemaRegistry.py @@ -126,29 +126,53 @@ def test_RelationshipMetadata(self): self.assertEqual(relDef.GetInfo("testCustomMetadata"), "garply") def test_GetUsdSchemaTypeName(self): - testType = Tf.Type.FindByName("TestUsdSchemaRegistryMetadataTest") + abstractTest = Tf.Type.FindByName("TestUsdSchemaRegistryAbstractTest") + concreteTest = Tf.Type.FindByName("TestUsdSchemaRegistryMetadataTest") modelAPI = Tf.Type.FindByName("UsdModelAPI") collectionAPI = Tf.Type.FindByName("UsdCollectionAPI") # Test getting a schema type name from a TfType for a concrete typed # schema. self.assertEqual( - Usd.SchemaRegistry.GetSchemaTypeName(testType), + Usd.SchemaRegistry.GetSchemaTypeName(concreteTest), "MetadataTest") self.assertEqual( - Usd.SchemaRegistry.GetConcreteSchemaTypeName(testType), + Usd.SchemaRegistry.GetConcreteSchemaTypeName(concreteTest), "MetadataTest") self.assertEqual( - Usd.SchemaRegistry.GetAPISchemaTypeName(testType), + Usd.SchemaRegistry.GetAPISchemaTypeName(concreteTest), "") # Test the reverse of getting the TfType for concrete typed schema name. self.assertEqual( Usd.SchemaRegistry.GetTypeFromSchemaTypeName("MetadataTest"), - testType) + concreteTest) self.assertEqual( Usd.SchemaRegistry.GetConcreteTypeFromSchemaTypeName("MetadataTest"), - testType) + concreteTest) + self.assertEqual( + Usd.SchemaRegistry.GetAPITypeFromSchemaTypeName("MetadataTest"), + Tf.Type.Unknown) + + # Test getting a schema type name from a TfType for an abstract typed + # schema. + self.assertEqual( + Usd.SchemaRegistry.GetSchemaTypeName(abstractTest), + "AbstractTest") + self.assertEqual( + Usd.SchemaRegistry.GetConcreteSchemaTypeName(abstractTest), + "") + self.assertEqual( + Usd.SchemaRegistry.GetAPISchemaTypeName(abstractTest), + "") + + # Test the reverse of getting the TfType for abastract typed schema name. + self.assertEqual( + Usd.SchemaRegistry.GetTypeFromSchemaTypeName("AbstractTest"), + abstractTest) + self.assertEqual( + Usd.SchemaRegistry.GetConcreteTypeFromSchemaTypeName("AbstractTest"), + Tf.Type.Unknown) self.assertEqual( Usd.SchemaRegistry.GetAPITypeFromSchemaTypeName("MetadataTest"), Tf.Type.Unknown) diff --git a/pxr/usd/usd/testenv/testUsdSchemaRegistry/resources/generatedSchema.usda b/pxr/usd/usd/testenv/testUsdSchemaRegistry/resources/generatedSchema.usda index 187e284721..50cf490576 100644 --- a/pxr/usd/usd/testenv/testUsdSchemaRegistry/resources/generatedSchema.usda +++ b/pxr/usd/usd/testenv/testUsdSchemaRegistry/resources/generatedSchema.usda @@ -3,6 +3,21 @@ "WARNING: THIS FILE IS GENERATED BY usdGenSchema. DO NOT EDIT." ) +class "AbstractTest" ( + doc = "Testing documentation metadata" + hidden = true +) +{ + string testAttr = "foo" ( + allowedTokens = ["bar", "baz"] + displayGroup = "Display Group" + displayName = "Display Name" + doc = "Testing documentation metadata" + hidden = true + testCustomMetadata = "garply" + ) +} + class MetadataTest "MetadataTest" ( doc = "Testing documentation metadata" hidden = true diff --git a/pxr/usd/usd/testenv/testUsdSchemaRegistry/resources/plugInfo.json b/pxr/usd/usd/testenv/testUsdSchemaRegistry/resources/plugInfo.json index 9de24bd22f..544bb391da 100644 --- a/pxr/usd/usd/testenv/testUsdSchemaRegistry/resources/plugInfo.json +++ b/pxr/usd/usd/testenv/testUsdSchemaRegistry/resources/plugInfo.json @@ -12,13 +12,23 @@ } }, "Types": { + "TestUsdSchemaRegistryAbstractTest": { + "alias": { + "UsdSchemaBase": "AbstractTest" + }, + "autoGenerated": true, + "bases": [ + "UsdTyped" + ], + "schemaKind": "abstractTyped" + }, "TestUsdSchemaRegistryMetadataTest": { "alias": { "UsdSchemaBase": "MetadataTest" }, "autoGenerated": true, "bases": [ - "UsdTyped" + "TestUsdSchemaRegistryAbstractTest" ], "schemaKind": "concreteTyped" } diff --git a/pxr/usd/usd/testenv/testUsdSchemaRegistry/resources/schema.usda b/pxr/usd/usd/testenv/testUsdSchemaRegistry/resources/schema.usda index 29f1bae9d6..49413e4c01 100644 --- a/pxr/usd/usd/testenv/testUsdSchemaRegistry/resources/schema.usda +++ b/pxr/usd/usd/testenv/testUsdSchemaRegistry/resources/schema.usda @@ -11,16 +11,16 @@ def "GLOBAL" ( customData = { string libraryName = "testUsdSchemaRegistry" string libraryPath = "pxr/usd/usd" + bool isDynamic = true } ) { } -class MetadataTest "MetadataTest" ( +class "AbstractTest" ( inherits = doc = "Testing documentation metadata" hidden = true - testCustomMetadata = "garply" ) { string testAttr = "foo" ( @@ -31,7 +31,15 @@ class MetadataTest "MetadataTest" ( hidden = True testCustomMetadata = "garply" ) +} +class MetadataTest "MetadataTest" ( + inherits = + doc = "Testing documentation metadata" + hidden = true + testCustomMetadata = "garply" +) +{ rel testRel ( displayGroup = "Display Group" displayName = "Display Name" diff --git a/pxr/usd/usd/usdGenSchema.py b/pxr/usd/usd/usdGenSchema.py index 675b087e42..e3f653cc3f 100644 --- a/pxr/usd/usd/usdGenSchema.py +++ b/pxr/usd/usd/usdGenSchema.py @@ -1067,7 +1067,7 @@ def GeneratePlugInfo(templatePath, codeGenPath, classes, validate, env): {"apiSchemaAutoApplyTo": list(cls.apiAutoApply)}) # Write out alias/primdefs for concrete IsA schemas and API schemas - if (cls.isConcrete or cls.isApi): + if (cls.isTyped or cls.isApi): clsDict['alias'] = {'UsdSchemaBase': cls.usdPrimTypeName} types[cls.cppClassName] = clsDict diff --git a/pxr/usd/usdGeom/plugInfo.json b/pxr/usd/usdGeom/plugInfo.json index 895ac14a00..2b2ebeb240 100644 --- a/pxr/usd/usdGeom/plugInfo.json +++ b/pxr/usd/usdGeom/plugInfo.json @@ -76,6 +76,9 @@ "schemaKind": "concreteTyped" }, "UsdGeomBoundable": { + "alias": { + "UsdSchemaBase": "Boundable" + }, "autoGenerated": true, "bases": [ "UsdGeomXformable" @@ -126,6 +129,9 @@ "schemaKind": "concreteTyped" }, "UsdGeomCurves": { + "alias": { + "UsdSchemaBase": "Curves" + }, "autoGenerated": true, "bases": [ "UsdGeomPointBased" @@ -145,6 +151,9 @@ "schemaKind": "concreteTyped" }, "UsdGeomGprim": { + "alias": { + "UsdSchemaBase": "Gprim" + }, "autoGenerated": true, "bases": [ "UsdGeomBoundable" @@ -162,6 +171,9 @@ "schemaKind": "concreteTyped" }, "UsdGeomImageable": { + "alias": { + "UsdSchemaBase": "Imageable" + }, "autoGenerated": true, "bases": [ "UsdTyped" @@ -219,6 +231,9 @@ "schemaKind": "concreteTyped" }, "UsdGeomPointBased": { + "alias": { + "UsdSchemaBase": "PointBased" + }, "autoGenerated": true, "bases": [ "UsdGeomGprim" @@ -310,6 +325,9 @@ "schemaKind": "nonAppliedAPI" }, "UsdGeomXformable": { + "alias": { + "UsdSchemaBase": "Xformable" + }, "autoGenerated": true, "bases": [ "UsdGeomImageable" diff --git a/pxr/usd/usdLux/plugInfo.json b/pxr/usd/usdLux/plugInfo.json index ea13e03edc..58671ce51a 100644 --- a/pxr/usd/usdLux/plugInfo.json +++ b/pxr/usd/usdLux/plugInfo.json @@ -57,6 +57,9 @@ "schemaKind": "concreteTyped" }, "UsdLuxLight": { + "alias": { + "UsdSchemaBase": "Light" + }, "autoGenerated": true, "bases": [ "UsdGeomXformable" diff --git a/pxr/usd/usdRender/plugInfo.json b/pxr/usd/usdRender/plugInfo.json index ade9f274ab..306cfc871e 100644 --- a/pxr/usd/usdRender/plugInfo.json +++ b/pxr/usd/usdRender/plugInfo.json @@ -44,6 +44,9 @@ "schemaKind": "singleApplyAPI" }, "UsdRenderSettingsBase": { + "alias": { + "UsdSchemaBase": "RenderSettingsBase" + }, "autoGenerated": true, "bases": [ "UsdTyped" diff --git a/pxr/usd/usdVol/plugInfo.json b/pxr/usd/usdVol/plugInfo.json index 9e86ea425b..08a10ea5e1 100644 --- a/pxr/usd/usdVol/plugInfo.json +++ b/pxr/usd/usdVol/plugInfo.json @@ -17,6 +17,9 @@ "schemaKind": "concreteTyped" }, "UsdVolFieldAsset": { + "alias": { + "UsdSchemaBase": "FieldAsset" + }, "autoGenerated": true, "bases": [ "UsdVolFieldBase" @@ -24,6 +27,9 @@ "schemaKind": "abstractTyped" }, "UsdVolFieldBase": { + "alias": { + "UsdSchemaBase": "FieldBase" + }, "autoGenerated": true, "bases": [ "UsdGeomBoundable"