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 USD_SCHEMAREGISTRY_H
25 #define USD_SCHEMAREGISTRY_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 UsdSchemaRegistry
43 ///
44 /// Singleton registry that provides access to prim and property definition
45 /// information for registered Usd "IsA" schema types.
46 ///
47 /// The data contained herein comes from the processed (by \em usdGenSchema)
48 /// schema.usda files of each schema-defining module. The data is returned
49 /// in the form of SdfSpec's of the appropriate subtype.
50 ///
51 /// It is used by the Usd core to determine how to create scene description
52 /// for un-instantiated "builtin" properties of schema classes, and also
53 /// to enumerate all properties for a given schema class, and finally to
54 /// provide fallback values for unauthored builtin properties.
55 ///
56 class UsdSchemaRegistry : public TfSingleton<UsdSchemaRegistry> {
57 public:
58  USD_API
61  }
62 
63  static const SdfLayerRefPtr & GetSchematics() {
64  return GetInstance()._schematics;
65  }
66 
67  /// Return the PrimSpec that contains all the builtin metadata and
68  /// properties for the given \a primType. Return null if there is no such
69  /// prim definition.
70  USD_API
71  static SdfPrimSpecHandle GetPrimDefinition(const TfToken &primType);
72 
73  /// Return the PrimSpec that contains all the bulitin metadata and
74  /// properties for the given \a primType. Return null if there is no such
75  /// prim definition.
76  USD_API
77  static SdfPrimSpecHandle GetPrimDefinition(const TfType &primType);
78 
79  /// Return the PrimSpec that contains all the builtin metadata and
80  /// properties for the given \p SchemaType. Return null if there is no such
81  /// prim definition.
82  template <class SchemaType>
83  static SdfPrimSpecHandle GetPrimDefinition() {
84  return GetPrimDefinition(SchemaType::_GetStaticTfType());
85  }
86 
87  /// Return the property spec that defines the fallback for the property
88  /// named \a propName on prims of type \a primType. Return null if there is
89  /// no such property definition.
90  USD_API
91  static SdfPropertySpecHandle
92  GetPropertyDefinition(const TfToken& primType,
93  const TfToken& propName);
94 
95  /// This is a convenience method. It is shorthand for
96  /// TfDynamic_cast<SdfAttributeSpecHandle>(
97  /// GetPropertyDefinition(primType, attrName));
98  USD_API
99  static SdfAttributeSpecHandle
100  GetAttributeDefinition(const TfToken& primType,
101  const TfToken& attrName);
102 
103  /// This is a convenience method. It is shorthand for
104  /// TfDynamic_cast<SdfRelationshipSpecHandle>(
105  /// GetPropertyDefinition(primType, relName));
106  USD_API
107  static SdfRelationshipSpecHandle
108  GetRelationshipDefinition(const TfToken& primType, const TfToken& relName);
109 
110  /// Return the SdfSpecType for \p primType and \p propName if those identify
111  /// a builtin property. Otherwise return SdfSpecTypeUnknown.
112  static SdfSpecType GetSpecType(const TfToken &primType,
113  const TfToken &propName) {
114  const UsdSchemaRegistry &self = GetInstance();
115  const SdfPath &path = self._GetPath(primType, propName);
116  return path.IsEmpty() ? SdfSpecTypeUnknown :
117  self._schematics->GetSpecType(path);
118  }
119 
120  /// Return in \p value the field for the property named \p propName
121  /// under the prim for type \p primType or for the prim if \p propName
122  /// is empty. Returns \c true if the value exists, \c false otherwise.
123  // XXX: Getting these fields via the methods that return spec
124  // handles will be slower than using this method. It's
125  // questionable if those methods should exist at all.
126  template <class T>
127  static bool HasField(const TfToken& primType,
128  const TfToken& propName,
129  const TfToken& fieldName, T* value)
130  {
131  const UsdSchemaRegistry &self = GetInstance();
132  const SdfPath &path = self._GetPath(primType, propName);
133  return !path.IsEmpty() &&
134  self._schematics->HasField(path, fieldName, value);
135  }
136 
137  template <class T>
138  static bool HasFieldDictKey(const TfToken& primType,
139  const TfToken& propName,
140  const TfToken& fieldName,
141  const TfToken& keyPath,
142  T* value)
143  {
144  const UsdSchemaRegistry &self = GetInstance();
145  const SdfPath &path = self._GetPath(primType, propName);
146  return !path.IsEmpty() &&
147  self._schematics->HasFieldDictKey(path, fieldName, keyPath, value);
148  }
149 
150  /// Returns list of fields that cannot have fallback values
151  /// specified in schemas.
152  ///
153  /// Fields are generally in this list because their fallback values
154  /// aren't used. For instance, fallback values for composition arcs
155  /// aren't used during composition, so allowing them to be set in
156  /// schemas would be misleading.
157  USD_API
158  static std::vector<TfToken> GetDisallowedFields();
159 
160  /// Returns true if the prim type \p primType inherits from \ref UsdTyped.
161  USD_API
162  static bool IsTyped(const TfType& primType);
163 
164  /// Returns true if the prim type \p primType is instantiable
165  /// in scene description.
166  USD_API
167  static bool IsConcrete(const TfType& primType);
168 
169  /// Returns true if \p apiSchemaType is an applied API schema type.
170  USD_API
171  bool IsAppliedAPISchema(const TfType& apiSchemaType);
172 
173  /// Returns true if \p apiSchemaType is a multiple-apply API schema type.
174  USD_API
175  bool IsMultipleApplyAPISchema(const TfType& apiSchemaType);
176 
177 
178  /// Finds the TfType of a schema with \p typeName
179  ///
180  /// This is primarily for when you have been provided Schema typeName
181  /// (perhaps from a User Interface or Script) and need to identify
182  /// if a prim's type inherits/is that typeName. If the type name IS known,
183  /// then using the schema class is preferred.
184  ///
185  /// \code{.py}
186  /// # This code attempts to match all prims on a stage to a given
187  /// # user specified type, making the traditional schema based idioms not
188  /// # applicable.
189  /// data = parser.parse_args()
190  /// tfType = UsdSchemaRegistry.GetTypeFromName(data.type)
191  /// matchedPrims = [p for p in stage.Traverse() if p.IsA(tfType)]
192  /// \endcode
193  ///
194  /// \note It's worth noting that
195  /// GetTypeFromName("Sphere") == GetTypeFromName("UsdGeomSphere"), as
196  /// this function resolves both the Schema's C++ class name and any
197  /// registered aliases from a libraries plugInfo.json file. However,
198  /// GetTypeFromName("Boundable") != GetTypeFromName("UsdGeomBoundable")
199  /// because type aliases don't get registered for abstract schema types.
200  USD_API
201  static TfType GetTypeFromName(const TfToken& typeName);
202 
203 private:
205 
207 
208  // Helper for template GetPrimDefinition.
209  static SdfPrimSpecHandle
210  _GetPrimDefinitionAtPath(const SdfPath &path);
211 
212  // Helper for looking up the prim definition path for a given primType.
213  const SdfPath& _GetSchemaPrimPath(const TfToken &primType) const;
214 
215  // Helper for looking up the prim definition path for a given primType.
216  const SdfPath& _GetSchemaPrimPath(const TfType &primType) const;
217 
218  USD_API
219  const SdfPath &_GetPath(const TfToken &primType,
220  const TfToken &propName) const;
221 
222  void _FindAndAddPluginSchema();
223 
224  void _BuildPrimTypePropNameToPathMap(const TfToken &typeName,
225  const SdfPath &primPath);
226 
227  SdfLayerRefPtr _schematics;
228 
229  // Registered map of schema class type -> definition prim path.
230  // XXX: Should drop this in favor of _TypeNameToPathMap but
231  // TfType should have a GetTypeNameToken() method so we
232  // don't have to construct a TfToken from a std::string.
234  _TypeToPathMap _typeToPathMap;
235 
238  _TypeNameToPathMap _typeNameToPathMap;
239 
240  struct _TokenPairHash {
241  inline size_t operator()(const std::pair<TfToken, TfToken> &p) const {
242  size_t hash = p.first.Hash();
243  hboost::hash_combine(hash, p.second);
244  return hash;
245  }
246  };
247 
248  // Cache of primType/propName to specId.
250  _TokenPairHash> _PrimTypePropNameToPathMap;
251  _PrimTypePropNameToPathMap _primTypePropNameToPathMap;
252 
253  TfToken::HashSet _appliedAPISchemaNames;
254  TfToken::HashSet _multipleApplyAPISchemaNames;
255 };
256 
258 
260 
261 #endif //USD_SCHEMAREGISTRY_H
static SdfPrimSpecHandle GetPrimDefinition()
static T & GetInstance()
Definition: singleton.h:137
#define USD_API
Definition: api.h:40
static const SdfLayerRefPtr & GetSchematics()
static USD_API TfType GetTypeFromName(const TfToken &typeName)
bool IsEmpty() const noexcept
Returns true if this is the empty path (SdfPath::EmptyPath()).
Definition: path.h:409
static USD_API bool IsConcrete(const TfType &primType)
static USD_API std::vector< TfToken > GetDisallowedFields()
static USD_API SdfAttributeSpecHandle GetAttributeDefinition(const TfToken &primType, const TfToken &attrName)
Definition: token.h:89
static USD_API bool IsTyped(const TfType &primType)
Returns true if the prim type primType inherits from UsdTyped.
static bool HasField(const TfToken &primType, const TfToken &propName, const TfToken &fieldName, T *value)
static USD_API SdfRelationshipSpecHandle GetRelationshipDefinition(const TfToken &primType, const TfToken &relName)
Definition: path.h:287
GLsizei const GLchar *const * path
Definition: glew.h:6461
GLfloat GLfloat p
Definition: glew.h:16321
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1245
SdfSpecType
Definition: types.h:91
static USD_API UsdSchemaRegistry & GetInstance()
static SdfSpecType GetSpecType(const TfToken &primType, const TfToken &propName)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:83
static USD_API SdfPropertySpecHandle GetPropertyDefinition(const TfToken &primType, const TfToken &propName)
Definition: type.h:70
static bool HasFieldDictKey(const TfToken &primType, const TfToken &propName, const TfToken &fieldName, const TfToken &keyPath, T *value)
USD_API bool IsMultipleApplyAPISchema(const TfType &apiSchemaType)
Returns true if apiSchemaType is a multiple-apply API schema type.
GLsizei const GLfloat * value
Definition: glew.h:1849
USD_API_TEMPLATE_CLASS(TfSingleton< UsdSchemaRegistry >)
USD_API bool IsAppliedAPISchema(const TfType &apiSchemaType)
Returns true if apiSchemaType is an applied API schema type.
PXR_NAMESPACE_OPEN_SCOPE SDF_DECLARE_HANDLES(SdfAttributeSpec)