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
42
50 return _locallyDefinedPropertyNames;
51 }
52
56 return _appliedAPISchemas;
57 }
58
59private:
60 // Forward declaration required by Property.
61 struct _LayerAndPath;
62
63public:
74 class Property {
75 public:
77 Property() = default;
78
82 USD_API
83 const TfToken &GetName() const;
84
87 explicit operator bool() const
88 {
89 return _layerAndPath;
90 }
91
94 USD_API
95 bool IsAttribute() const;
96
99 USD_API
100 bool IsRelationship() const;
101
115
117 USD_API
119
122 USD_API
124
131 template <class T>
132 bool GetMetadata(const TfToken &key, T* value) const;
133
142 template <class T>
144 const TfToken &key, const TfToken &keyPath, T* value) const;
145
147 USD_API
149
152 USD_API
153 std::string GetDocumentation() const;
154
156
157 protected:
158 // Only the prim definition can create real property accessors.
159 friend class UsdPrimDefinition;
160 Property(const TfToken &name, const _LayerAndPath *layerAndPath):
161 _name(name), _layerAndPath(layerAndPath) {}
162 Property(const _LayerAndPath *layerAndPath):
163 _layerAndPath(layerAndPath) {}
164
165 TfToken _name;
166 const _LayerAndPath *_layerAndPath = nullptr;
167 };
168
180 class Attribute : public Property {
181 public:
183 Attribute() = default;
184
186 USD_API
187 Attribute(const Property &property);
188
190 USD_API
191 Attribute(Property &&property);
192
195 explicit operator bool() const {
196 return IsAttribute();
197 }
198
212
215 USD_API
217
220 USD_API
222
228 template <class T>
229 bool GetFallbackValue(T *value) const;
230
232 };
233
246 class Relationship : public Property {
247 public:
249 Relationship() = default;
250
252 USD_API
253 Relationship(const Property &property);
254
256 USD_API
258
261 explicit operator bool() const{
262 return IsRelationship();
263 }
264 };
265
269 USD_API
270 Property GetPropertyDefinition(const TfToken& propName) const;
271
276 USD_API
278
283 USD_API
285
289 USD_API
290 SdfSpecType GetSpecType(const TfToken &propName) const;
291
297 USD_API
298 SdfPropertySpecHandle GetSchemaPropertySpec(
299 const TfToken& propName) const;
300
306 USD_API
307 SdfAttributeSpecHandle GetSchemaAttributeSpec(
308 const TfToken& attrName) const;
309
315 USD_API
316 SdfRelationshipSpecHandle GetSchemaRelationshipSpec(
317 const TfToken& relName) const;
318
324 template <class T>
325 bool GetAttributeFallbackValue(const TfToken &attrName, T *value) const
326 {
327 return _HasField(attrName, SdfFieldKeys->Default, value);
328 }
329
332 USD_API
334
341 template <class T>
342 bool GetMetadata(const TfToken &key, T* value) const
343 {
345 return false;
346 }
347 return _HasField(TfToken(), key, value);
348 }
349
358 template <class T>
360 const TfToken &keyPath, T* value) const
361 {
363 return false;
364 }
365 return _HasFieldDictKey(TfToken(), key, keyPath, value);
366 }
367
370 USD_API
371 std::string GetDocumentation() const;
372
376 USD_API
378
385 template <class T>
387 const TfToken &propName, const TfToken &key, T* value) const
388 {
389 if (Property prop = GetPropertyDefinition(propName)) {
390 return prop.GetMetadata(key, value);
391 }
392 return false;
393 }
394
403 template <class T>
405 const TfToken &propName, const TfToken &key,
406 const TfToken &keyPath, T* value) const
407 {
408 if (Property prop = GetPropertyDefinition(propName)) {
409 return prop.GetMetadataByDictKey(key, keyPath, value);
410 }
411 return false;
412 }
413
416 USD_API
417 std::string GetPropertyDocumentation(const TfToken &propName) const;
418
442 USD_API
443 bool FlattenTo(const SdfLayerHandle &layer,
444 const SdfPath &path,
445 SdfSpecifier newSpecSpecifier = SdfSpecifierOver) const;
446
451 USD_API
452 UsdPrim FlattenTo(const UsdPrim &parent,
453 const TfToken &name,
454 SdfSpecifier newSpecSpecifier = SdfSpecifierOver) const;
455
459 USD_API
461 SdfSpecifier newSpecSpecifier = SdfSpecifierOver) const;
462
463private:
464 // Only the UsdSchemaRegistry can construct prim definitions.
465 friend class UsdSchemaRegistry;
466
467 // Friended private accessor for use by UsdStage when composing metadata
468 // values for value resolution. The public GetMetadata functions perform
469 // the extra step of filtering out disallowed or private metadata fields
470 // from the SdfSpecs before retrieving metadata. Value resolution does not
471 // want to pay that extra cost so uses this function instead.
472 template <class T>
473 friend bool Usd_GetFallbackValue(const UsdPrimDefinition &primDef,
474 const TfToken &propName,
475 const TfToken &fieldName,
476 const TfToken &keyPath,
477 T *value)
478 {
479 // Try to read fallback value.
480 return keyPath.IsEmpty() ?
481 primDef._HasField(propName, fieldName, value) :
482 primDef._HasFieldDictKey(propName, fieldName, keyPath, value);
483 }
484
485 // Prim definitions store property access via a pointer to the schematics
486 // layer and a path to the property spec on that layer.
487 struct _LayerAndPath {
488 // Note that we use a raw pointer to the layer (for efficiency) as only
489 // the schema registry can create a UsdPrimDefinition and is responsible
490 // for making sure any schematics layers are alive throughout the
491 // life-time of any UsdPrimDefinition it creates.
492 const SdfLayer *layer = nullptr;
493 SdfPath path;
494
495 // Accessors for the common data we extract from the schematics, inline
496 // for efficiency during value resolution
497 template <class T>
498 bool HasField(const TfToken& fieldName, T* value) const {
499 return layer->HasField(path, fieldName, value);
500 }
501
502 template <class T>
503 bool HasFieldDictKey(
504 const TfToken& fieldName, const TfToken& keyPath, T* value) const {
505 return layer->HasFieldDictKey(path, fieldName, keyPath, value);
506 }
507
508 VtValue GetField(const TfToken& fieldName) const {
509 return layer->GetField(path, fieldName);
510 }
511
512 explicit operator bool() const noexcept {
513 return layer;
514 }
515 };
516
520 template <class T>
521 bool _HasField(const TfToken& propName,
522 const TfToken& fieldName,
523 T* value) const
524 {
525 if (const _LayerAndPath *layerAndPath =
526 _GetPropertyLayerAndPath(propName)) {
527 return layerAndPath->HasField(fieldName, value);
528 }
529 return false;
530 }
531
532 template <class T>
533 bool _HasFieldDictKey(const TfToken& propName,
534 const TfToken& fieldName,
535 const TfToken& keyPath,
536 T* value) const
537 {
538 if (const _LayerAndPath *layerAndPath =
539 _GetPropertyLayerAndPath(propName)) {
540 return layerAndPath->HasFieldDictKey(fieldName, keyPath, value);
541 }
542 return false;
543 }
544
545 UsdPrimDefinition() = default;
546 UsdPrimDefinition(const UsdPrimDefinition &) = default;
547
548 void _ApplyPropertyOrder();
549
550 USD_API
551 void _InitializeForTypedSchema(
552 const SdfLayerHandle &schematicsLayer,
553 const SdfPath &schematicsPrimPath,
554 const VtTokenArray &propertiesToIgnore);
555
556 USD_API
557 void _InitializeForAPISchema(
558 const TfToken &apiSchemaName,
559 const SdfLayerHandle &schematicsLayer,
560 const SdfPath &schematicsPrimPath,
561 const VtTokenArray &propertiesToIgnore);
562
563 // Only used by the two _Initialize methods.
564 bool _MapSchematicsPropertyPaths(
565 const VtTokenArray &propertiesToIgnore);
566
567 // Accessors for looking property spec paths by name.
568 const _LayerAndPath *_GetPropertyLayerAndPath(const TfToken& propName) const
569 {
570 return TfMapLookupPtr(_propLayerAndPathMap, propName);
571 }
572
573 _LayerAndPath *_GetPropertyLayerAndPath(const TfToken& propName)
574 {
575 return TfMapLookupPtr(_propLayerAndPathMap, propName);
576 }
577
578 // Helpers for constructing the prim definition.
579 void _ComposePropertiesFromPrimDef(
580 const UsdPrimDefinition &weakerPrimDef);
581
582 void _ComposePropertiesFromPrimDefInstance(
583 const UsdPrimDefinition &weakerPrimDef,
584 const std::string &instanceName);
585
586 void _AddOrComposeProperty(
587 const TfToken &propName,
588 const _LayerAndPath &layerAndPath);
589
590 SdfSpecHandle _FindOrCreateSpecForComposition(
591 const TfToken &propName,
592 const _LayerAndPath &srcLayerAndPath);
593
594 SdfSpecHandle _CreateComposedPrimOrPropertyIfNeeded(
595 const TfToken &propName,
596 const _LayerAndPath &strongProp,
597 const _LayerAndPath &weakProp);
598
599 USD_API
600 void _ComposeOverAndReplaceExistingProperty(
601 const TfToken &propName,
602 const SdfLayerRefPtr &overLayer,
603 const SdfPath &overPrimPath);
604
605 using _FamilyAndInstanceToVersionMap =
606 std::unordered_map<std::pair<TfToken, TfToken>, UsdSchemaVersion, TfHash>;
607
608 USD_API
609 bool _ComposeWeakerAPIPrimDefinition(
610 const UsdPrimDefinition &apiPrimDef,
611 const TfToken &instanceName,
612 _FamilyAndInstanceToVersionMap *alreadyAppliedSchemaFamilyVersions);
613
614 static bool _PropertyTypesMatch(
615 const Property &strongProp,
616 const Property &weakProp);
617
618 // Path to the prim in the schematics for this prim definition.
619 _LayerAndPath _primLayerAndPath;
620
621 // Map for caching the paths to each property spec in the schematics by
622 // property name.
623 using _PrimTypePropNameToPathMap =
624 std::unordered_map<TfToken, _LayerAndPath, TfToken::HashFunctor>;
625 _PrimTypePropNameToPathMap _propLayerAndPathMap;
626 TfTokenVector _appliedAPISchemas;
627
628 // Cached list of property names that are locally sourced from this prim
629 // definition's schema.usda. This is a subset of _properties.
630 TfTokenVector _locallyDefinedPropertyNames;
631
632 // Cached list of property names.
633 TfTokenVector _properties;
634
635 // Layer that may be created for this prim definition if it's necessary to
636 // compose any new property specs for this definition from multiple
637 // property specs from other definitions.
638 SdfLayerRefPtr _composedPropertyLayer;
639};
640
641template <class T>
642bool
644{
646 return false;
647 }
648 return _layerAndPath->HasField(key, value);
649}
650
651template <class T>
652bool
654 const TfToken &key, const TfToken &keyPath, T* value) const
655{
657 return false;
658 }
659 return _layerAndPath->HasFieldDictKey(key, keyPath, value);
660}
661
662template <class T>
663bool
665{
666 return _layerAndPath->HasField(SdfFieldKeys->Default, value);
667}
668
669
670PXR_NAMESPACE_CLOSE_SCOPE
671
672#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:281
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...
const TfTokenVector & GetLocallyDefinedPropertyNames() const
Return the list of names of "local" builtin properties for this prim definition, ordered by this prim...
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