HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
schemaRegistry.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_USD_USD_SCHEMA_REGISTRY_H
25 #define PXR_USD_USD_SCHEMA_REGISTRY_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usd/usd/api.h"
29 
30 #include "pxr/usd/sdf/layer.h"
31 #include "pxr/usd/sdf/primSpec.h"
32 
33 #include "pxr/base/tf/hash.h"
34 #include "pxr/base/tf/singleton.h"
35 #include "pxr/base/tf/hashmap.h"
36 
38 
41 
42 class UsdPrimDefinition;
43 
44 /// \class UsdSchemaRegistry
45 ///
46 /// Singleton registry that provides access to schema type information and
47 /// the prim definitions for registered Usd "IsA" and applied API schema
48 /// types. It also contains the data from the generated schemas that is used
49 /// by prim definitions to provide properties and fallbacks.
50 ///
51 /// The data contained herein comes from the generatedSchema.usda file
52 /// (generated when a schema.usda file is processed by \em usdGenSchema)
53 /// of each schema-defining module. The registry expects each schema type to
54 /// be represented as a single prim spec with its inheritance flattened, i.e.
55 /// the prim spec contains a union of all its local and class inherited property
56 /// specs and metadata fields.
57 ///
58 /// It is used by the Usd core, via UsdPrimDefinition, to determine how to
59 /// create scene description for unauthored "built-in" properties of schema
60 /// classes, to enumerate all properties for a given schema class, and finally
61 /// to provide fallback values for unauthored built-in properties.
62 ///
63 class UsdSchemaRegistry : public TfWeakBase, hboost::noncopyable {
64 public:
65  USD_API
68  }
69 
70  /// Return the type name in the USD schema for prims or API schemas of the
71  /// given registered \p schemaType.
72  USD_API
73  static TfToken GetSchemaTypeName(const TfType &schemaType);
74 
75  /// Return the type name in the USD schema for prims or API schemas of the
76  /// given registered \p SchemaType.
77  template <class SchemaType>
78  static
80  return GetSchemaTypeName(SchemaType::_GetStaticTfType());
81  }
82 
83  /// Return the type name in the USD schema for concrete prim types only from
84  /// the given registered \p schemaType.
85  USD_API
86  static TfToken GetConcreteSchemaTypeName(const TfType &schemaType);
87 
88  /// Return the type name in the USD schema for API schema types only from
89  /// the given registered \p schemaType.
90  USD_API
91  static TfToken GetAPISchemaTypeName(const TfType &schemaType);
92 
93  /// Return the TfType of the schema corresponding to the given prim or API
94  /// schema name \p typeName. This the inverse of GetSchemaTypeName.
95  USD_API
96  static TfType GetTypeFromSchemaTypeName(const TfToken &typeName);
97 
98  /// Return the TfType of the schema corresponding to the given concrete prim
99  /// type name \p typeName. This the inverse of GetConcreteSchemaTypeName.
100  USD_API
101  static TfType GetConcreteTypeFromSchemaTypeName(const TfToken &typeName);
102 
103  /// Return the TfType of the schema corresponding to the given API schema
104  /// type name \p typeName. This the inverse of GetAPISchemaTypeNAme.
105  USD_API
106  static TfType GetAPITypeFromSchemaTypeName(const TfToken &typeName);
107 
108  /// Returns true if the field \p fieldName cannot have fallback values
109  /// specified in schemas.
110  ///
111  /// Fields are generally disallowed because their fallback values
112  /// aren't used. For instance, fallback values for composition arcs
113  /// aren't used during composition, so allowing them to be set in
114  /// schemas would be misleading.
115  USD_API
116  static bool IsDisallowedField(const TfToken &fieldName);
117 
118  /// Returns true if the prim type \p primType inherits from \ref UsdTyped.
119  USD_API
120  static bool IsTyped(const TfType& primType);
121 
122  /// Returns true if the prim type \p primType is instantiable
123  /// in scene description.
124  USD_API
125  bool IsConcrete(const TfType& primType) const;
126 
127  /// Returns true if the prim type \p primType is instantiable
128  /// in scene description.
129  USD_API
130  bool IsConcrete(const TfToken& primType) const;
131 
132  /// Returns true if \p apiSchemaType is an applied API schema type.
133  USD_API
134  bool IsAppliedAPISchema(const TfType& apiSchemaType) const;
135 
136  /// Returns true if \p apiSchemaType is an applied API schema type.
137  USD_API
138  bool IsAppliedAPISchema(const TfToken& apiSchemaType) const;
139 
140  /// Returns true if \p apiSchemaType is a multiple-apply API schema type.
141  USD_API
142  bool IsMultipleApplyAPISchema(const TfType& apiSchemaType) const;
143 
144  /// Returns true if \p apiSchemaType is a multiple-apply API schema type.
145  USD_API
146  bool IsMultipleApplyAPISchema(const TfToken& apiSchemaType) const;
147 
148  /// Finds the TfType of a schema with \p typeName
149  ///
150  /// This is primarily for when you have been provided Schema typeName
151  /// (perhaps from a User Interface or Script) and need to identify
152  /// if a prim's type inherits/is that typeName. If the type name IS known,
153  /// then using the schema class is preferred.
154  ///
155  /// \code{.py}
156  /// # This code attempts to match all prims on a stage to a given
157  /// # user specified type, making the traditional schema based idioms not
158  /// # applicable.
159  /// data = parser.parse_args()
160  /// tfType = UsdSchemaRegistry.GetTypeFromName(data.type)
161  /// matchedPrims = [p for p in stage.Traverse() if p.IsA(tfType)]
162  /// \endcode
163  ///
164  /// \note It's worth noting that
165  /// GetTypeFromName("Sphere") == GetTypeFromName("UsdGeomSphere"), as
166  /// this function resolves both the Schema's C++ class name and any
167  /// registered aliases from a libraries plugInfo.json file. However,
168  /// GetTypeFromName("Boundable") != GetTypeFromName("UsdGeomBoundable")
169  /// because type aliases don't get registered for abstract schema types.
170  USD_API
171  static TfType GetTypeFromName(const TfToken& typeName);
172 
173  /// Finds the prim definition for the given \p typeName token if
174  /// \p typeName is a registered concrete typed schema type. Returns null if
175  /// it is not.
177  const TfToken &typeName) const {
178  auto it = _concreteTypedPrimDefinitions.find(typeName);
179  return it != _concreteTypedPrimDefinitions.end() ? it->second : nullptr;
180  }
181 
182  /// Finds the prim definition for the given \p typeName token if
183  /// \p typeName is a registered applied API schema type. Returns null if
184  /// it is not.
186  const TfToken &typeName) const {
187  auto it = _appliedAPIPrimDefinitions.find(typeName);
188  return it != _appliedAPIPrimDefinitions.end() ? it->second : nullptr;
189  }
190 
191  /// Returns the empty prim definition.
193  return _emptyPrimDefinition;
194  }
195 
196  /// Composes and returns a new UsdPrimDefinition from the given \p primType
197  /// and list of \p applieSchemas. This prim definition will contain a union
198  /// of properties from the registered prim definitions of each of the
199  /// provided types.
200  USD_API
201  std::unique_ptr<UsdPrimDefinition>
203  const TfToken &primType, const TfTokenVector &appliedAPISchemas) const;
204 
205  /// Returns a dictionary mapping concrete schema prim type names to a
206  /// VtTokenArray of fallback prim type names if fallback types are defined
207  /// for the schema type in its registered schema.
208  ///
209  /// The standard use case for this to provide schema defined metadata that
210  /// can be saved with a stage to inform an older version of USD - that
211  /// may not have some schema types - as to which types it can used instead
212  /// when encountering a prim of one these types.
213  ///
214  /// \sa UsdStage::WriteFallbackPrimTypes
215  /// \sa \ref Usd_OM_FallbackPrimTypes
217  return _fallbackPrimTypes;
218  }
219 
220 private:
222 
224 
225  void _FindAndAddPluginSchema();
226 
227  void _ApplyAPISchemasToPrimDefinition(
228  UsdPrimDefinition *primDef, const TfTokenVector &appliedAPISchemas) const;
229 
230  SdfLayerRefPtr _schematics;
231  typedef TfHashMap<TfToken, UsdPrimDefinition *,
233 
234  _TypeNameToPrimDefinitionMap _concreteTypedPrimDefinitions;
235  _TypeNameToPrimDefinitionMap _appliedAPIPrimDefinitions;
236  UsdPrimDefinition *_emptyPrimDefinition;
237 
239  _multipleApplyAPISchemaNamespaces;
240 
241  VtDictionary _fallbackPrimTypes;
242 
243  friend class UsdPrimDefinition;
244 };
245 
247 
249 
250 #endif //PXR_USD_USD_SCHEMA_REGISTRY_H
static T & GetInstance()
Definition: singleton.h:137
const UsdPrimDefinition * FindAppliedAPIPrimDefinition(const TfToken &typeName) const
#define USD_API
Definition: api.h:40
static USD_API TfType GetConcreteTypeFromSchemaTypeName(const TfToken &typeName)
USD_API bool IsAppliedAPISchema(const TfType &apiSchemaType) const
Returns true if apiSchemaType is an applied API schema type.
iterator end()
Definition: hashmap.h:318
const VtDictionary & GetFallbackPrimTypes() const
const UsdPrimDefinition * GetEmptyPrimDefinition() const
Returns the empty prim definition.
static USD_API TfType GetTypeFromName(const TfToken &typeName)
Functor to use for hash maps from tokens to other things.
Definition: token.h:166
USD_API bool IsConcrete(const TfType &primType) const
Definition: token.h:87
static TfToken GetSchemaTypeName()
static USD_API bool IsTyped(const TfType &primType)
Returns true if the prim type primType inherits from UsdTyped.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:446
static USD_API TfToken GetConcreteSchemaTypeName(const TfType &schemaType)
static USD_API bool IsDisallowedField(const TfToken &fieldName)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
USD_API bool IsMultipleApplyAPISchema(const TfType &apiSchemaType) const
Returns true if apiSchemaType is a multiple-apply API schema type.
static USD_API UsdSchemaRegistry & GetInstance()
static USD_API TfToken GetAPISchemaTypeName(const TfType &schemaType)
static USD_API TfType GetAPITypeFromSchemaTypeName(const TfToken &typeName)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
Definition: type.h:64
USD_API std::unique_ptr< UsdPrimDefinition > BuildComposedPrimDefinition(const TfToken &primType, const TfTokenVector &appliedAPISchemas) const
USD_API_TEMPLATE_CLASS(TfSingleton< UsdSchemaRegistry >)
static USD_API TfType GetTypeFromSchemaTypeName(const TfToken &typeName)
PXR_NAMESPACE_OPEN_SCOPE SDF_DECLARE_HANDLES(SdfAttributeSpec)
const UsdPrimDefinition * FindConcretePrimDefinition(const TfToken &typeName) const