Loading...
Searching...
No Matches
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
37#include <unordered_map>
38
39PXR_NAMESPACE_OPEN_SCOPE
40
41class UsdPrim;
42
49{
50public:
51 ~UsdPrimDefinition() = default;
52
54 const TfTokenVector &GetPropertyNames() const { return _properties; }
55
59 return _appliedAPISchemas;
60 }
61
62private:
63 // Forward declaration required by Property.
64 struct _LayerAndPath;
65
66public:
77 class Property {
78 public:
80 Property() = default;
81
85 USD_API
86 const TfToken &GetName() const;
87
90 explicit operator bool() const
91 {
92 return _layerAndPath;
93 }
94
97 USD_API
98 bool IsAttribute() const;
99
102 USD_API
103 bool IsRelationship() const;
104
118
120 USD_API
122
125 USD_API
127
134 template <class T>
135 bool GetMetadata(const TfToken &key, T* value) const;
136
145 template <class T>
147 const TfToken &key, const TfToken &keyPath, T* value) const;
148
150 USD_API
152
155 USD_API
156 std::string GetDocumentation() const;
157
159
160 protected:
161 // Only the prim definition can create real property accessors.
162 friend class UsdPrimDefinition;
163 Property(const TfToken &name, const _LayerAndPath *layerAndPath):
164 _name(name), _layerAndPath(layerAndPath) {}
165 Property(const _LayerAndPath *layerAndPath):
166 _layerAndPath(layerAndPath) {}
167
168 TfToken _name;
169 const _LayerAndPath *_layerAndPath = nullptr;
170 };
171
183 class Attribute : public Property {
184 public:
186 Attribute() = default;
187
189 USD_API
190 Attribute(const Property &property);
191
193 USD_API
194 Attribute(Property &&property);
195
198 explicit operator bool() const {
199 return IsAttribute();
200 }
201
215
218 USD_API
220
223 USD_API
225
231 template <class T>
232 bool GetFallbackValue(T *value) const;
233
235 };
236
249 class Relationship : public Property {
250 public:
252 Relationship() = default;
253
255 USD_API
256 Relationship(const Property &property);
257
259 USD_API
261
264 explicit operator bool() const{
265 return IsRelationship();
266 }
267 };
268
272 USD_API
273 Property GetPropertyDefinition(const TfToken& propName) const;
274
279 USD_API
281
286 USD_API
288
292 USD_API
293 SdfSpecType GetSpecType(const TfToken &propName) const;
294
300 USD_API
301 SdfPropertySpecHandle GetSchemaPropertySpec(
302 const TfToken& propName) const;
303
309 USD_API
310 SdfAttributeSpecHandle GetSchemaAttributeSpec(
311 const TfToken& attrName) const;
312
318 USD_API
319 SdfRelationshipSpecHandle GetSchemaRelationshipSpec(
320 const TfToken& relName) const;
321
327 template <class T>
328 bool GetAttributeFallbackValue(const TfToken &attrName, T *value) const
329 {
330 return _HasField(attrName, SdfFieldKeys->Default, value);
331 }
332
335 USD_API
337
344 template <class T>
345 bool GetMetadata(const TfToken &key, T* value) const
346 {
348 return false;
349 }
350 return _HasField(TfToken(), key, value);
351 }
352
361 template <class T>
363 const TfToken &keyPath, T* value) const
364 {
366 return false;
367 }
368 return _HasFieldDictKey(TfToken(), key, keyPath, value);
369 }
370
373 USD_API
374 std::string GetDocumentation() const;
375
379 USD_API
381
388 template <class T>
390 const TfToken &propName, const TfToken &key, T* value) const
391 {
392 if (Property prop = GetPropertyDefinition(propName)) {
393 return prop.GetMetadata(key, value);
394 }
395 return false;
396 }
397
406 template <class T>
408 const TfToken &propName, const TfToken &key,
409 const TfToken &keyPath, T* value) const
410 {
411 if (Property prop = GetPropertyDefinition(propName)) {
412 return prop.GetMetadataByDictKey(key, keyPath, value);
413 }
414 return false;
415 }
416
419 USD_API
420 std::string GetPropertyDocumentation(const TfToken &propName) const;
421
445 USD_API
446 bool FlattenTo(const SdfLayerHandle &layer,
447 const SdfPath &path,
448 SdfSpecifier newSpecSpecifier = SdfSpecifierOver) const;
449
454 USD_API
455 UsdPrim FlattenTo(const UsdPrim &parent,
456 const TfToken &name,
457 SdfSpecifier newSpecSpecifier = SdfSpecifierOver) const;
458
462 USD_API
464 SdfSpecifier newSpecSpecifier = SdfSpecifierOver) const;
465
466private:
467 // Only the UsdSchemaRegistry can construct prim definitions.
468 friend class UsdSchemaRegistry;
469
470 // Friended private accessor for use by UsdStage when composing metadata
471 // values for value resolution. The public GetMetadata functions perform
472 // the extra step of filtering out disallowed or private metadata fields
473 // from the SdfSpecs before retrieving metadata. Value resolution does not
474 // want to pay that extra cost so uses this function instead.
475 template <class T>
476 friend bool Usd_GetFallbackValue(const UsdPrimDefinition &primDef,
477 const TfToken &propName,
478 const TfToken &fieldName,
479 const TfToken &keyPath,
480 T *value)
481 {
482 // Try to read fallback value.
483 return keyPath.IsEmpty() ?
484 primDef._HasField(propName, fieldName, value) :
485 primDef._HasFieldDictKey(propName, fieldName, keyPath, value);
486 }
487
488 // Prim definitions store property access via a pointer to the schematics
489 // layer and a path to the property spec on that layer.
490 struct _LayerAndPath {
491 // Note that we use a raw pointer to the layer (for efficiency) as only
492 // the schema registry can create a UsdPrimDefinition and is responsible
493 // for making sure any schematics layers are alive throughout the
494 // life-time of any UsdPrimDefinition it creates.
495 const SdfLayer *layer = nullptr;
496 SdfPath path;
497
498 // Accessors for the common data we extract from the schematics, inline
499 // for efficiency during value resolution
500 template <class T>
501 bool HasField(const TfToken& fieldName, T* value) const {
502 return layer->HasField(path, fieldName, value);
503 }
504
505 template <class T>
506 bool HasFieldDictKey(
507 const TfToken& fieldName, const TfToken& keyPath, T* value) const {
508 return layer->HasFieldDictKey(path, fieldName, keyPath, value);
509 }
510 };
511
515 template <class T>
516 bool _HasField(const TfToken& propName,
517 const TfToken& fieldName,
518 T* value) const
519 {
520 if (const _LayerAndPath *layerAndPath =
521 _GetPropertyLayerAndPath(propName)) {
522 return layerAndPath->HasField(fieldName, value);
523 }
524 return false;
525 }
526
527 template <class T>
528 bool _HasFieldDictKey(const TfToken& propName,
529 const TfToken& fieldName,
530 const TfToken& keyPath,
531 T* value) const
532 {
533 if (const _LayerAndPath *layerAndPath =
534 _GetPropertyLayerAndPath(propName)) {
535 return layerAndPath->HasFieldDictKey(fieldName, keyPath, value);
536 }
537 return false;
538 }
539
540 UsdPrimDefinition() = default;
541 UsdPrimDefinition(const UsdPrimDefinition &) = default;
542
543 USD_API
544 void _IntializeForTypedSchema(
545 const SdfLayerHandle &schematicsLayer,
546 const SdfPath &schematicsPrimPath,
547 const VtTokenArray &propertiesToIgnore);
548
549 USD_API
550 void _IntializeForAPISchema(
551 const TfToken &apiSchemaName,
552 const SdfLayerHandle &schematicsLayer,
553 const SdfPath &schematicsPrimPath,
554 const VtTokenArray &propertiesToIgnore);
555
556 // Only used by the two _Initialize methods.
557 bool _MapSchematicsPropertyPaths(
558 const VtTokenArray &propertiesToIgnore);
559
560 // Accessors for looking property spec paths by name.
561 const _LayerAndPath *_GetPropertyLayerAndPath(const TfToken& propName) const
562 {
563 return TfMapLookupPtr(_propLayerAndPathMap, propName);
564 }
565
566 _LayerAndPath *_GetPropertyLayerAndPath(const TfToken& propName)
567 {
568 return TfMapLookupPtr(_propLayerAndPathMap, propName);
569 }
570
571 // Helpers for constructing the prim definition.
572 void _ComposePropertiesFromPrimDef(
573 const UsdPrimDefinition &weakerPrimDef);
574
575 void _ComposePropertiesFromPrimDefInstance(
576 const UsdPrimDefinition &weakerPrimDef,
577 const std::string &instanceName);
578
579 void _AddOrComposeProperty(
580 const TfToken &propName,
581 const _LayerAndPath &layerAndPath);
582
583 SdfPropertySpecHandle _FindOrCreatePropertySpecForComposition(
584 const TfToken &propName,
585 const _LayerAndPath &srcLayerAndPath);
586
587 SdfPropertySpecHandle _CreateComposedPropertyIfNeeded(
588 const TfToken &propName,
589 const _LayerAndPath &strongProp,
590 const _LayerAndPath &weakProp);
591
592 USD_API
593 void _ComposeOverAndReplaceExistingProperty(
594 const TfToken &propName,
595 const SdfLayerRefPtr &overLayer,
596 const SdfPath &overPrimPath);
597
598 using _FamilyAndInstanceToVersionMap =
599 std::unordered_map<std::pair<TfToken, TfToken>, UsdSchemaVersion, TfHash>;
600
601 USD_API
602 bool _ComposeWeakerAPIPrimDefinition(
603 const UsdPrimDefinition &apiPrimDef,
604 const TfToken &instanceName,
605 _FamilyAndInstanceToVersionMap *alreadyAppliedSchemaFamilyVersions);
606
607 static bool _PropertyTypesMatch(
608 const Property &strongProp,
609 const Property &weakProp);
610
611 // Path to the prim in the schematics for this prim definition.
612 _LayerAndPath _primLayerAndPath;
613
614 // Map for caching the paths to each property spec in the schematics by
615 // property name.
616 using _PrimTypePropNameToPathMap =
617 std::unordered_map<TfToken, _LayerAndPath, TfToken::HashFunctor>;
618 _PrimTypePropNameToPathMap _propLayerAndPathMap;
619 TfTokenVector _appliedAPISchemas;
620
621 // Cached list of property names.
622 TfTokenVector _properties;
623
624 // Layer that may be created for this prim definition if it's necessary to
625 // compose any new property specs for this definition from multiple
626 // property specs from other definitions.
627 SdfLayerRefPtr _composedPropertyLayer;
628};
629
630template <class T>
631bool
633{
635 return false;
636 }
637 return _layerAndPath->HasField(key, value);
638}
639
640template <class T>
641bool
643 const TfToken &key, const TfToken &keyPath, T* value) const
644{
646 return false;
647 }
648 return _layerAndPath->HasFieldDictKey(key, keyPath, value);
649}
650
651template <class T>
652bool
654{
655 return _layerAndPath->HasField(SdfFieldKeys->Default, value);
656}
657
658
659PXR_NAMESPACE_CLOSE_SCOPE
660
661#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:100
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.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:291
Represents a value type name, i.e.
Definition: valueTypeName.h:88
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:477
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:88
bool IsEmpty() const
Returns true iff this token contains the empty string "".
Definition: token.h:305
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.
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:134
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.
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:141
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:457
SdfSpecType
An enum that specifies the type of an object.
Definition: types.h:84
SdfSpecifier
An enum that identifies the possible specifiers for an SdfPrimSpec.
Definition: types.h:116
SdfVariability
An enum that identifies variability types for attributes.
Definition: types.h:172