All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
primDefinition.h
1 //
2 // Copyright 2020 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_PRIM_DEFINITION_H
25 #define PXR_USD_USD_PRIM_DEFINITION_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usd/usd/api.h"
29 #include "pxr/usd/usd/schemaRegistry.h"
30 
31 #include "pxr/usd/sdf/layer.h"
35 #include "pxr/base/tf/hash.h"
36 #include "pxr/base/tf/hashmap.h"
37 
38 PXR_NAMESPACE_OPEN_SCOPE
39 
40 class UsdPrim;
41 
48 {
49 public:
50  ~UsdPrimDefinition() = default;
51 
53  const TfTokenVector &GetPropertyNames() const { return _properties; }
54 
58  return _appliedAPISchemas;
59  }
60 
64  SdfSpecType GetSpecType(const TfToken &propName) const
65  {
66  if (const SdfPath *path = TfMapLookupPtr(_propPathMap, propName)) {
67  return _GetSchematics()->GetSpecType(*path);
68  }
69  return SdfSpecTypeUnknown;
70  }
71 
75  SdfPropertySpecHandle GetSchemaPropertySpec(const TfToken& propName) const
76  {
77  if (const SdfPath *path = TfMapLookupPtr(_propPathMap, propName)) {
78  return _GetSchematics()->GetPropertyAtPath(*path);
79  }
80  return TfNullPtr;
81  }
82 
86  SdfAttributeSpecHandle GetSchemaAttributeSpec(const TfToken& attrName) const
87  {
88  if (const SdfPath *path = TfMapLookupPtr(_propPathMap, attrName)) {
89  return _GetSchematics()->GetAttributeAtPath(*path);
90  }
91  return TfNullPtr;
92  }
93 
97  SdfRelationshipSpecHandle GetSchemaRelationshipSpec(const TfToken& relName) const
98  {
99  if (const SdfPath *path = TfMapLookupPtr(_propPathMap, relName)) {
100  return _GetSchematics()->GetRelationshipAtPath(*path);
101  }
102  return TfNullPtr;
103  }
104 
110  template <class T>
111  bool GetAttributeFallbackValue(const TfToken &attrName, T *value) const
112  {
113  return _HasField(attrName, SdfFieldKeys->Default, value);
114  }
115 
119  {
120  return _ListMetadataFields(TfToken());
121  }
122 
129  template <class T>
130  bool GetMetadata(const TfToken &key, T* value) const
131  {
133  return false;
134  }
135  return _HasField(TfToken(), key, value);
136  }
137 
146  template <class T>
147  bool GetMetadataByDictKey(const TfToken &key,
148  const TfToken &keyPath, T* value) const
149  {
151  return false;
152  }
153  return _HasFieldDictKey(TfToken(), key, keyPath, value);
154  }
155 
158  USD_API
159  std::string GetDocumentation() const;
160 
165  {
166  return propName.IsEmpty() ?
167  TfTokenVector() : _ListMetadataFields(propName);
168  }
169 
176  template <class T>
178  const TfToken &propName, const TfToken &key, T* value) const
179  {
180  if (propName.IsEmpty() || UsdSchemaRegistry::IsDisallowedField(key)) {
181  return false;
182  }
183  return _HasField(propName, key, value);
184  }
185 
194  template <class T>
196  const TfToken &propName, const TfToken &key,
197  const TfToken &keyPath, T* value) const
198  {
199  if (propName.IsEmpty() || UsdSchemaRegistry::IsDisallowedField(key)) {
200  return false;
201  }
202  return _HasFieldDictKey(propName, key, keyPath, value);
203  }
204 
207  USD_API
208  std::string GetPropertyDocumentation(const TfToken &propName) const;
209 
233  USD_API
234  bool FlattenTo(const SdfLayerHandle &layer,
235  const SdfPath &path,
236  SdfSpecifier newSpecSpecifier = SdfSpecifierOver) const;
237 
242  USD_API
243  UsdPrim FlattenTo(const UsdPrim &parent,
244  const TfToken &name,
245  SdfSpecifier newSpecSpecifier = SdfSpecifierOver) const;
246 
250  USD_API
251  UsdPrim FlattenTo(const UsdPrim &prim,
252  SdfSpecifier newSpecSpecifier = SdfSpecifierOver) const;
253 
254 
255 private:
256  // Only the UsdSchemaRegistry can construct prim definitions.
257  friend class UsdSchemaRegistry;
258 
259  // Friended private accessor for use by UsdStage when composing metadata
260  // values for value resolution. The public GetMetadata functions perform
261  // the extra step of filtering out disallowed or private metadata fields
262  // from the SdfSpecs before retrieving metadata. Value resolution does not
263  // want to pay that extra cost so uses this function instead.
264  template <class T>
265  friend bool Usd_GetFallbackValue(const UsdPrimDefinition &primDef,
266  const TfToken &propName,
267  const TfToken &fieldName,
268  const TfToken &keyPath,
269  T *value)
270  {
271  // Try to read fallback value.
272  return keyPath.IsEmpty() ?
273  primDef._HasField(propName, fieldName, value) :
274  primDef._HasFieldDictKey(propName, fieldName, keyPath, value);
275  }
276 
280  template <class T>
281  bool _HasField(const TfToken& propName,
282  const TfToken& fieldName,
283  T* value) const
284  {
285  if (const SdfPath *path = TfMapLookupPtr(_propPathMap, propName)) {
286  return _GetSchematics()->HasField(*path, fieldName, value);
287  }
288  return false;
289  }
290 
291  template <class T>
292  bool _HasFieldDictKey(const TfToken& propName,
293  const TfToken& fieldName,
294  const TfToken& keyPath,
295  T* value) const
296  {
297  if (const SdfPath *path = TfMapLookupPtr(_propPathMap, propName)) {
298  return _GetSchematics()->HasFieldDictKey(
299  *path, fieldName, keyPath, value);
300  }
301  return false;
302  }
303 
304  UsdPrimDefinition() = default;
305 
306  UsdPrimDefinition(const SdfPrimSpecHandle &primSpec, bool isAPISchema);
307 
308  // Access to the schema registry's schematics.
309  const SdfLayerRefPtr &_GetSchematics() const {
310  return UsdSchemaRegistry::GetInstance()._schematics;
311  }
312 
313  USD_API
314  TfTokenVector _ListMetadataFields(const TfToken &propName) const;
315 
316  // Helpers for constructing the prim definition.
317  USD_API
318  void _SetPrimSpec(const SdfPrimSpecHandle &primSpec,
319  bool providesPrimMetadata);
320 
321  USD_API
322  void _ApplyPropertiesFromPrimDef(const UsdPrimDefinition &primDef,
323  const std::string &propPrefix = "");
324 
325  void _AddProperty(const TfToken &name, const SdfPath &schemaPath)
326  {
327  // Adds the property name with schema path to the prim def. This makes
328  // sure we overwrite the original property path with the new path if it
329  // already exists, but makes sure we don't end up with duplicate names
330  // in the property names list.
331  auto it = _propPathMap.insert(std::make_pair(name, schemaPath));
332  if (it.second) {
333  _properties.push_back(name);
334  } else {
335  it.first->second = schemaPath;
336  }
337  }
338 
339  SdfPrimSpecHandle _primSpec;
340 
341  // Map for caching the paths to each property spec in the schematics by
342  // property name.
343  using _PrimTypePropNameToPathMap =
344  TfHashMap<TfToken, SdfPath, TfToken::HashFunctor>;
345  _PrimTypePropNameToPathMap _propPathMap;
346  TfTokenVector _appliedAPISchemas;
347 
348  // Cached list of property names.
349  TfTokenVector _properties;
350 };
351 
352 PXR_NAMESPACE_CLOSE_SCOPE
353 
354 #endif //PXR_USD_USD_PRIM_DEFINITION_H
SDF_API bool HasField(const SdfPath &path, const TfToken &fieldName, VtValue *value=NULL) const
Return whether a value exists for the given path and fieldName.
USD_API bool FlattenTo(const SdfLayerHandle &layer, const SdfPath &path, SdfSpecifier newSpecSpecifier=SdfSpecifierOver) const
Copies the contents of this prim definition to a prim spec on the given layer at the given path...
SDF_API bool HasFieldDictKey(const SdfPath &path, const TfToken &fieldName, const TfToken &keyPath, VtValue *value=NULL) const
Return whether a value exists for the given path and fieldName and keyPath.
SDF_API SdfSpecType GetSpecType(const SdfPath &path) const
Return the spec type for path.
SDF_API SdfPropertySpecHandle GetPropertyAtPath(const SdfPath &path)
Returns a property at the given path.
USD_API std::string GetPropertyDocumentation(const TfToken &propName) const
Returns the documentation metadata defined by the prim definition for the property named propName if ...
SdfAttributeSpecHandle GetSchemaAttributeSpec(const TfToken &attrName) const
This is a convenience method.
USD_API std::string GetDocumentation() const
Returns the documentation metadata defined by the prim definition for the prim itself.
SdfSpecType GetSpecType(const TfToken &propName) const
Return the SdfSpecType for propName if it is a builtin property of the prim type represented by this ...
TfTokenVector ListMetadataFields() const
Returns the list of names of metadata fields that are defined by this prim definition for the prim it...
bool GetMetadataByDictKey(const TfToken &key, const TfToken &keyPath, T *value) const
Retrieves the value at keyPath from the fallback dictionary value for the dictionary metadata field n...
bool GetPropertyMetadataByDictKey(const TfToken &propName, const TfToken &key, const TfToken &keyPath, T *value) const
Retrieves the value at keyPath from the fallback dictionary value for the dictionary metadata field n...
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
const TfTokenVector & GetPropertyNames() const
Return the list of names of builtin properties for this prim definition.
bool GetPropertyMetadata(const TfToken &propName, const TfToken &key, T *value) const
Retrieves the fallback value for the metadata field named key, that is defined by this prim definitio...
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a &quot;Prim&quot; as ...
Definition: prim.h:132
SdfSpecifier
An enum that identifies the possible specifiers for an SdfPrimSpec.
Definition: types.h:123
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
SDF_API SdfRelationshipSpecHandle GetRelationshipAtPath(const SdfPath &path)
Returns a relationship at the given path.
SdfRelationshipSpecHandle GetSchemaRelationshipSpec(const TfToken &relName) const
This is a convenience method.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:288
bool GetAttributeFallbackValue(const TfToken &attrName, T *value) const
Retrieves the fallback value for the attribute named attrName and stores it in value if possible...
static USD_API bool IsDisallowedField(const TfToken &fieldName)
Returns true if the field fieldName cannot have fallback values specified in schemas.
SdfSpecType
An enum that specifies the type of an object.
Definition: types.h:91
TfTokenVector ListPropertyMetadataFields(const TfToken &propName) const
Returns the list of names of metadata fields that are defined by this prim definition for property pr...
Class representing the builtin definition of a prim given the schemas registered in the schema regist...
Container::mapped_type * TfMapLookupPtr(Container &map, Key const &key)
Checks if an item exists in a map or TfHashMap, without copying it.
Definition: stl.h:143
SdfPropertySpecHandle GetSchemaPropertySpec(const TfToken &propName) const
Return the property spec that defines the fallback for the property named propName on prims of this p...
bool IsEmpty() const
Returns true iff this token contains the empty string &quot;&quot;.
Definition: token.h:302
const TfTokenVector & GetAppliedAPISchemas() const
Return the list of names of the API schemas that have been applied to this prim definition in order...
bool GetMetadata(const TfToken &key, T *value) const
Retrieves the fallback value for the metadata field named key, that is defined by this prim definitio...
Singleton registry that provides access to schema type information and the prim definitions for regis...
SDF_API SdfAttributeSpecHandle GetAttributeAtPath(const SdfPath &path)
Returns an attribute at the given path.