Loading...
Searching...
No Matches
primDefinition.h
1//
2// Copyright 2020 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_USD_USD_PRIM_DEFINITION_H
8#define PXR_USD_USD_PRIM_DEFINITION_H
9
10#include "pxr/pxr.h"
11#include "pxr/usd/usd/api.h"
12#include "pxr/usd/usd/schemaRegistry.h"
13
14#include "pxr/usd/sdf/layer.h"
18#include "pxr/base/tf/hash.h"
19
20#include <unordered_map>
21
22PXR_NAMESPACE_OPEN_SCOPE
23
24class UsdPrim;
25
32{
33public:
34 ~UsdPrimDefinition() = default;
35
38 const TfTokenVector &GetPropertyNames() const { return _properties; }
39
43 return _appliedAPISchemas;
44 }
45
46private:
47 // Forward declaration required by Property.
48 struct _LayerAndPath;
49
50public:
61 class Property {
62 public:
64 Property() = default;
65
69 USD_API
70 const TfToken &GetName() const;
71
74 explicit operator bool() const
75 {
76 return _layerAndPath;
77 }
78
81 USD_API
82 bool IsAttribute() const;
83
86 USD_API
87 bool IsRelationship() const;
88
102
104 USD_API
106
109 USD_API
111
118 template <class T>
119 bool GetMetadata(const TfToken &key, T* value) const;
120
129 template <class T>
131 const TfToken &key, const TfToken &keyPath, T* value) const;
132
134 USD_API
136
139 USD_API
140 std::string GetDocumentation() const;
141
143
144 protected:
145 // Only the prim definition can create real property accessors.
146 friend class UsdPrimDefinition;
147 Property(const TfToken &name, const _LayerAndPath *layerAndPath):
148 _name(name), _layerAndPath(layerAndPath) {}
149 Property(const _LayerAndPath *layerAndPath):
150 _layerAndPath(layerAndPath) {}
151
152 TfToken _name;
153 const _LayerAndPath *_layerAndPath = nullptr;
154 };
155
167 class Attribute : public Property {
168 public:
170 Attribute() = default;
171
173 USD_API
174 Attribute(const Property &property);
175
177 USD_API
178 Attribute(Property &&property);
179
182 explicit operator bool() const {
183 return IsAttribute();
184 }
185
199
202 USD_API
204
207 USD_API
209
215 template <class T>
216 bool GetFallbackValue(T *value) const;
217
219 };
220
233 class Relationship : public Property {
234 public:
236 Relationship() = default;
237
239 USD_API
240 Relationship(const Property &property);
241
243 USD_API
245
248 explicit operator bool() const{
249 return IsRelationship();
250 }
251 };
252
256 USD_API
257 Property GetPropertyDefinition(const TfToken& propName) const;
258
263 USD_API
265
270 USD_API
272
276 USD_API
277 SdfSpecType GetSpecType(const TfToken &propName) const;
278
284 USD_API
285 SdfPropertySpecHandle GetSchemaPropertySpec(
286 const TfToken& propName) const;
287
293 USD_API
294 SdfAttributeSpecHandle GetSchemaAttributeSpec(
295 const TfToken& attrName) const;
296
302 USD_API
303 SdfRelationshipSpecHandle GetSchemaRelationshipSpec(
304 const TfToken& relName) const;
305
311 template <class T>
312 bool GetAttributeFallbackValue(const TfToken &attrName, T *value) const
313 {
314 return _HasField(attrName, SdfFieldKeys->Default, value);
315 }
316
319 USD_API
321
328 template <class T>
329 bool GetMetadata(const TfToken &key, T* value) const
330 {
332 return false;
333 }
334 return _HasField(TfToken(), key, value);
335 }
336
345 template <class T>
347 const TfToken &keyPath, T* value) const
348 {
350 return false;
351 }
352 return _HasFieldDictKey(TfToken(), key, keyPath, value);
353 }
354
357 USD_API
358 std::string GetDocumentation() const;
359
363 USD_API
365
372 template <class T>
374 const TfToken &propName, const TfToken &key, T* value) const
375 {
376 if (Property prop = GetPropertyDefinition(propName)) {
377 return prop.GetMetadata(key, value);
378 }
379 return false;
380 }
381
390 template <class T>
392 const TfToken &propName, const TfToken &key,
393 const TfToken &keyPath, T* value) const
394 {
395 if (Property prop = GetPropertyDefinition(propName)) {
396 return prop.GetMetadataByDictKey(key, keyPath, value);
397 }
398 return false;
399 }
400
403 USD_API
404 std::string GetPropertyDocumentation(const TfToken &propName) const;
405
429 USD_API
430 bool FlattenTo(const SdfLayerHandle &layer,
431 const SdfPath &path,
432 SdfSpecifier newSpecSpecifier = SdfSpecifierOver) const;
433
438 USD_API
439 UsdPrim FlattenTo(const UsdPrim &parent,
440 const TfToken &name,
441 SdfSpecifier newSpecSpecifier = SdfSpecifierOver) const;
442
446 USD_API
448 SdfSpecifier newSpecSpecifier = SdfSpecifierOver) const;
449
450private:
451 // Only the UsdSchemaRegistry can construct prim definitions.
452 friend class UsdSchemaRegistry;
453
454 // Friended private accessor for use by UsdStage when composing metadata
455 // values for value resolution. The public GetMetadata functions perform
456 // the extra step of filtering out disallowed or private metadata fields
457 // from the SdfSpecs before retrieving metadata. Value resolution does not
458 // want to pay that extra cost so uses this function instead.
459 template <class T>
460 friend bool Usd_GetFallbackValue(const UsdPrimDefinition &primDef,
461 const TfToken &propName,
462 const TfToken &fieldName,
463 const TfToken &keyPath,
464 T *value)
465 {
466 // Try to read fallback value.
467 return keyPath.IsEmpty() ?
468 primDef._HasField(propName, fieldName, value) :
469 primDef._HasFieldDictKey(propName, fieldName, keyPath, value);
470 }
471
472 // Prim definitions store property access via a pointer to the schematics
473 // layer and a path to the property spec on that layer.
474 struct _LayerAndPath {
475 // Note that we use a raw pointer to the layer (for efficiency) as only
476 // the schema registry can create a UsdPrimDefinition and is responsible
477 // for making sure any schematics layers are alive throughout the
478 // life-time of any UsdPrimDefinition it creates.
479 const SdfLayer *layer = nullptr;
480 SdfPath path;
481
482 // Accessors for the common data we extract from the schematics, inline
483 // for efficiency during value resolution
484 template <class T>
485 bool HasField(const TfToken& fieldName, T* value) const {
486 return layer->HasField(path, fieldName, value);
487 }
488
489 template <class T>
490 bool HasFieldDictKey(
491 const TfToken& fieldName, const TfToken& keyPath, T* value) const {
492 return layer->HasFieldDictKey(path, fieldName, keyPath, value);
493 }
494
495 VtValue GetField(const TfToken& fieldName) const {
496 return layer->GetField(path, fieldName);
497 }
498
499 explicit operator bool() const noexcept {
500 return layer;
501 }
502 };
503
507 template <class T>
508 bool _HasField(const TfToken& propName,
509 const TfToken& fieldName,
510 T* value) const
511 {
512 if (const _LayerAndPath *layerAndPath =
513 _GetPropertyLayerAndPath(propName)) {
514 return layerAndPath->HasField(fieldName, value);
515 }
516 return false;
517 }
518
519 template <class T>
520 bool _HasFieldDictKey(const TfToken& propName,
521 const TfToken& fieldName,
522 const TfToken& keyPath,
523 T* value) const
524 {
525 if (const _LayerAndPath *layerAndPath =
526 _GetPropertyLayerAndPath(propName)) {
527 return layerAndPath->HasFieldDictKey(fieldName, keyPath, value);
528 }
529 return false;
530 }
531
532 UsdPrimDefinition() = default;
533 UsdPrimDefinition(const UsdPrimDefinition &) = default;
534
535 void _ApplyPropertyOrder();
536
537 USD_API
538 void _IntializeForTypedSchema(
539 const SdfLayerHandle &schematicsLayer,
540 const SdfPath &schematicsPrimPath,
541 const VtTokenArray &propertiesToIgnore);
542
543 USD_API
544 void _IntializeForAPISchema(
545 const TfToken &apiSchemaName,
546 const SdfLayerHandle &schematicsLayer,
547 const SdfPath &schematicsPrimPath,
548 const VtTokenArray &propertiesToIgnore);
549
550 // Only used by the two _Initialize methods.
551 bool _MapSchematicsPropertyPaths(
552 const VtTokenArray &propertiesToIgnore);
553
554 // Accessors for looking property spec paths by name.
555 const _LayerAndPath *_GetPropertyLayerAndPath(const TfToken& propName) const
556 {
557 return TfMapLookupPtr(_propLayerAndPathMap, propName);
558 }
559
560 _LayerAndPath *_GetPropertyLayerAndPath(const TfToken& propName)
561 {
562 return TfMapLookupPtr(_propLayerAndPathMap, propName);
563 }
564
565 // Helpers for constructing the prim definition.
566 void _ComposePropertiesFromPrimDef(
567 const UsdPrimDefinition &weakerPrimDef);
568
569 void _ComposePropertiesFromPrimDefInstance(
570 const UsdPrimDefinition &weakerPrimDef,
571 const std::string &instanceName);
572
573 void _AddOrComposeProperty(
574 const TfToken &propName,
575 const _LayerAndPath &layerAndPath);
576
577 SdfSpecHandle _FindOrCreateSpecForComposition(
578 const TfToken &propName,
579 const _LayerAndPath &srcLayerAndPath);
580
581 SdfSpecHandle _CreateComposedPrimOrPropertyIfNeeded(
582 const TfToken &propName,
583 const _LayerAndPath &strongProp,
584 const _LayerAndPath &weakProp);
585
586 USD_API
587 void _ComposeOverAndReplaceExistingProperty(
588 const TfToken &propName,
589 const SdfLayerRefPtr &overLayer,
590 const SdfPath &overPrimPath);
591
592 using _FamilyAndInstanceToVersionMap =
593 std::unordered_map<std::pair<TfToken, TfToken>, UsdSchemaVersion, TfHash>;
594
595 USD_API
596 bool _ComposeWeakerAPIPrimDefinition(
597 const UsdPrimDefinition &apiPrimDef,
598 const TfToken &instanceName,
599 _FamilyAndInstanceToVersionMap *alreadyAppliedSchemaFamilyVersions);
600
601 static bool _PropertyTypesMatch(
602 const Property &strongProp,
603 const Property &weakProp);
604
605 // Path to the prim in the schematics for this prim definition.
606 _LayerAndPath _primLayerAndPath;
607
608 // Map for caching the paths to each property spec in the schematics by
609 // property name.
610 using _PrimTypePropNameToPathMap =
611 std::unordered_map<TfToken, _LayerAndPath, TfToken::HashFunctor>;
612 _PrimTypePropNameToPathMap _propLayerAndPathMap;
613 TfTokenVector _appliedAPISchemas;
614
615 // Cached list of property names.
616 TfTokenVector _properties;
617
618 // Layer that may be created for this prim definition if it's necessary to
619 // compose any new property specs for this definition from multiple
620 // property specs from other definitions.
621 SdfLayerRefPtr _composedPropertyLayer;
622};
623
624template <class T>
625bool
627{
629 return false;
630 }
631 return _layerAndPath->HasField(key, value);
632}
633
634template <class T>
635bool
637 const TfToken &key, const TfToken &keyPath, T* value) const
638{
640 return false;
641 }
642 return _layerAndPath->HasFieldDictKey(key, keyPath, value);
643}
644
645template <class T>
646bool
648{
649 return _layerAndPath->HasField(SdfFieldKeys->Default, value);
650}
651
652
653PXR_NAMESPACE_CLOSE_SCOPE
654
655#endif //PXR_USD_USD_PRIM_DEFINITION_H
A scene description container that can combine with other such containers to form simple component as...
Definition: layer.h:84
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 bool HasField(const SdfPath &path, const TfToken &fieldName, VtValue *value=NULL) const
Return whether a value exists for the given path and fieldName.
SDF_API VtValue GetField(const SdfPath &path, const TfToken &fieldName) const
Return the value for the given path and fieldName.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:280
Represents a value type name, i.e.
Definition: valueTypeName.h:72
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:472
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
bool IsEmpty() const
Returns true iff this token contains the empty string "".
Definition: token.h:288
Accessor to a attribute's definition in the prim definition.
USD_API Attribute(const Property &property)
Copy constructor from a Property to allow implicit conversion.
USD_API Attribute(Property &&property)
Move constructor from a Property to allow implicit conversion.
Attribute()=default
Default constructor returns an invalid attribute.
USD_API SdfValueTypeName GetTypeName() const
Returns the value type name of this attribute in the prim definition.
USD_API TfToken GetTypeNameToken() const
Returns the token value of the type name of this attribute in the prim definition.
bool GetFallbackValue(T *value) const
Retrieves the fallback value of type T for this attribute and stores it in value if possible.
Accessor to a property's definition in the prim definition.
USD_API const TfToken & GetName() const
Returns the name of the requested property.
Property()=default
Default constructor returns an invalid property.
USD_API SdfSpecType GetSpecType() const
Returns the spec type of this property in the prim definition.
USD_API bool IsRelationship() const
Return true if the property is a valid is a valid property in the prim definition and is a relationsh...
USD_API SdfVariability GetVariability() const
Returns the variability of this property in the prim definition.
bool GetMetadataByDictKey(const TfToken &key, const TfToken &keyPath, T *value) const
Retrieves the value at keyPath from the dictionary value for the dictionary metadata field named key,...
USD_API bool IsAttribute() const
Return true if the property is a valid is a valid property in the prim definition and is an attribute...
bool GetMetadata(const TfToken &key, T *value) const
Retrieves the fallback value for the metadata field named key, that is defined for this property in t...
USD_API TfTokenVector ListMetadataFields() const
Returns the list of names of metadata fields that are defined for this property in the prim definitio...
USD_API std::string GetDocumentation() const
Returns the documentation metadata defined by the prim definition for this property.
Accessor to a relationship's definition in the prim definition.
Relationship()=default
Default constructor returns an invalid relationship.
USD_API Relationship(Property &&property)
Move constructor from a Property to allow implicit conversion.
USD_API Relationship(const Property &property)
Copy constructor from a Property to allow implicit conversion.
Class representing the builtin definition of a prim given the schemas registered in the schema regist...
USD_API SdfRelationshipSpecHandle GetSchemaRelationshipSpec(const TfToken &relName) const
USD_API TfTokenVector ListPropertyMetadataFields(const TfToken &propName) const
Returns the list of names of metadata fields that are defined by this prim definition for property pr...
USD_API SdfSpecType GetSpecType(const TfToken &propName) const
Return the SdfSpecType for propName if it is a builtin property of the prim type represented by this ...
USD_API Attribute GetAttributeDefinition(const TfToken &attrName) const
Returns an attribute accessor the property named attrName if it is defined by this this prim definiti...
USD_API SdfPropertySpecHandle GetSchemaPropertySpec(const TfToken &propName) const
const TfTokenVector & GetPropertyNames() const
Return the list of names of builtin properties for this prim definition, ordered by this prim definit...
USD_API Relationship GetRelationshipDefinition(const TfToken &relName) const
Returns a relationship accessor the property named relName if it is defined by this this prim definit...
USD_API Property GetPropertyDefinition(const TfToken &propName) const
Returns a property accessor the property named propName if it is defined by this this prim definition...
USD_API UsdPrim FlattenTo(const UsdPrim &prim, SdfSpecifier newSpecSpecifier=SdfSpecifierOver) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
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...
USD_API UsdPrim FlattenTo(const UsdPrim &parent, const TfToken &name, SdfSpecifier newSpecSpecifier=SdfSpecifierOver) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
USD_API SdfAttributeSpecHandle GetSchemaAttributeSpec(const TfToken &attrName) const
bool GetAttributeFallbackValue(const TfToken &attrName, T *value) const
Retrieves the fallback value for the attribute named attrName and stores it in value if possible.
const TfTokenVector & GetAppliedAPISchemas() const
Return the list of names of the API schemas that have been applied to this prim definition in order.
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...
USD_API std::string GetPropertyDocumentation(const TfToken &propName) const
Returns the documentation metadata defined by the prim definition for the property named propName if ...
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 GetMetadata(const TfToken &key, T *value) const
Retrieves the fallback value for the metadata field named key, that is defined by this prim definitio...
USD_API TfTokenVector ListMetadataFields() const
Returns the list of names of metadata fields that are defined by this prim definition for the prim it...
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.
USD_API std::string GetDocumentation() const
Returns the documentation metadata defined by the prim definition for the prim itself.
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a "Prim" as ...
Definition: prim.h:117
Singleton registry that provides access to schema type information and the prim definitions for regis...
static USD_API bool IsDisallowedField(const TfToken &fieldName)
Returns true if the field fieldName cannot have fallback values specified in schemas.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:90
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:124
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
SdfSpecType
An enum that specifies the type of an object.
Definition: types.h:71
SdfSpecifier
An enum that identifies the possible specifiers for an SdfPrimSpec.
Definition: types.h:103
SdfVariability
An enum that identifies variability types for attributes.
Definition: types.h:159