Loading...
Searching...
No Matches
object.h
Go to the documentation of this file.
1//
2// Copyright 2016 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_OBJECT_H
8#define PXR_USD_USD_OBJECT_H
9
11
12#include "pxr/pxr.h"
13#include "pxr/usd/usd/api.h"
14#include "pxr/usd/usd/common.h"
16#include "pxr/usd/usd/stage.h"
17
18#include "pxr/usd/sdf/abstractData.h"
19#include "pxr/usd/sdf/path.h"
20
21#include "pxr/base/tf/hash.h"
22
23#include <type_traits>
24
25PXR_NAMESPACE_OPEN_SCOPE
26
27
29
35{
36 // Value order matters in this enum.
37 UsdTypeObject,
38 UsdTypePrim,
39 UsdTypeProperty,
40 UsdTypeAttribute,
41 UsdTypeRelationship,
42
43 Usd_NumObjTypes
44};
45
46
47namespace _Detail {
48
49// A metafunction that takes a UsdObject class like UsdObject, UsdPrim,
50// UsdProperty, etc, and gives its corresponding UsdObjType, e.g. UsdTypeObject,
51// UsdTypePrim, UsdTypeProperty, etc. Usage: GetObjType<UsdPrim>::Value.
52template <UsdObjType Type>
53struct Const { static const UsdObjType Value = Type; };
54template <class T> struct GetObjType {
55 static_assert(std::is_base_of<UsdObject, T>::value,
56 "Type T must be a subclass of UsdObject.");
57};
58template <> struct GetObjType<UsdObject> : Const<UsdTypeObject> {};
59template <> struct GetObjType<UsdPrim> : Const<UsdTypePrim> {};
60template <> struct GetObjType<UsdProperty> : Const<UsdTypeProperty> {};
61template <> struct GetObjType<UsdAttribute> : Const<UsdTypeAttribute> {};
62template <> struct GetObjType<UsdRelationship> : Const<UsdTypeRelationship> {};
63
64} // _Detail
65
68inline bool
70 return (baseType == UsdTypeObject) || (baseType == subType) ||
71 (baseType == UsdTypeProperty && subType > UsdTypeProperty);
72}
73
76inline bool
78 return UsdIsSubtype(to, from);
79}
80
83inline bool
85 return type == UsdTypePrim ||
86 type == UsdTypeAttribute ||
87 type == UsdTypeRelationship;
88}
89
116public:
118 UsdObject() : _type(UsdTypeObject) {}
119
120 // --------------------------------------------------------------------- //
123 // --------------------------------------------------------------------- //
124
126 bool IsValid() const {
127 if (!UsdIsConcrete(_type) || !_prim)
128 return false;
129 if (_type == UsdTypePrim)
130 return true;
131 SdfSpecType specType = _GetDefiningSpecType();
132 return (_type == UsdTypeAttribute &&
133 specType == SdfSpecTypeAttribute) ||
134 (_type == UsdTypeRelationship &&
135 specType == SdfSpecTypeRelationship);
136 }
137
139 explicit operator bool() const {
140 return IsValid();
141 }
142
143public:
144
147 friend bool operator==(const UsdObject &lhs, const UsdObject &rhs) {
148 return lhs._type == rhs._type &&
149 lhs._prim == rhs._prim &&
150 lhs._proxyPrimPath == rhs._proxyPrimPath &&
151 lhs._propName == rhs._propName;
152 }
153
156 friend bool operator!=(const UsdObject &lhs, const UsdObject &rhs) {
157 return !(lhs == rhs);
158 }
159
163 friend bool operator<(const UsdObject &lhs, const UsdObject &rhs) {
164 return lhs.GetPath() < rhs.GetPath();
165 }
166
167 // hash_value overload for std/boost hash.
168 friend size_t hash_value(const UsdObject &obj) {
169 return TfHash()(obj);
170 }
171
172 // TfHash support
173 template <class HashState>
174 friend void TfHashAppend(HashState &h, const UsdObject &obj) {
175 h.Append(obj._type, obj._prim, obj._proxyPrimPath, obj._propName);
176 }
177
180 USD_API
181 UsdStageWeakPtr GetStage() const;
182
186 SdfPath GetPath() const {
187 // Allow getting expired object paths.
188 if (!_proxyPrimPath.IsEmpty()) {
189 return _type == UsdTypePrim ?
190 _proxyPrimPath : _proxyPrimPath.AppendProperty(_propName);
191 }
192 else if (Usd_PrimDataConstPtr p = get_pointer(_prim)) {
193 return _type == UsdTypePrim ?
194 p->GetPath() : p->GetPath().AppendProperty(_propName);
195 }
196 return SdfPath();
197 }
198
201 const SdfPath &GetPrimPath() const {
202 // Allow getting expired object paths.
203 if (!_proxyPrimPath.IsEmpty()) {
204 return _proxyPrimPath;
205 }
206 else if (Usd_PrimDataConstPtr p = get_pointer(_prim)) {
207 return p->GetPath();
208 }
209 return SdfPath::EmptyPath();
210 }
211
214 inline UsdPrim GetPrim() const;
215
221 const TfToken &GetName() const {
222 return _type == UsdTypePrim ? GetPrimPath().GetNameToken() : _propName;
223 }
224
228 template <class T>
229 T As() const {
230 // compile-time type assertion provided by invoking Is<T>().
231 return Is<T>() ? T(_type, _prim, _proxyPrimPath, _propName) : T();
232 }
233
239 template <class T>
240 bool Is() const {
241 static_assert(std::is_base_of<UsdObject, T>::value,
242 "Provided type T must derive from or be UsdObject");
243 return UsdIsConvertible(_type, _Detail::GetObjType<T>::Value);
244 }
245
251 USD_API
252 std::string GetDescription() const;
253
254 // --------------------------------------------------------------------- //
256 // --------------------------------------------------------------------- //
257
258
259 // --------------------------------------------------------------------- //
262 // --------------------------------------------------------------------- //
263
278 template<typename T>
279 bool GetMetadata(const TfToken& key, T* value) const;
283 USD_API
284 bool GetMetadata(const TfToken& key, VtValue* value) const;
285
292 template<typename T>
293 bool SetMetadata(const TfToken& key, const T& value) const;
295 USD_API
296 bool SetMetadata(const TfToken& key, const VtValue& value) const;
297
307 USD_API
308 bool ClearMetadata(const TfToken& key) const;
309
313 USD_API
314 bool HasMetadata(const TfToken& key) const;
315
319 USD_API
320 bool HasAuthoredMetadata(const TfToken& key) const;
321
337 template <class T>
339 const TfToken& key, const TfToken &keyPath, T *value) const;
341 USD_API
343 const TfToken& key, const TfToken &keyPath, VtValue *value) const;
344
352 template<typename T>
354 const TfToken& key, const TfToken &keyPath, const T& value) const;
356 USD_API
358 const TfToken& key, const TfToken &keyPath, const VtValue& value) const;
359
367 USD_API
369 const TfToken& key, const TfToken& keyPath) const;
370
377 USD_API
379 const TfToken& key, const TfToken &keyPath) const;
380
387 USD_API
389 const TfToken& key, const TfToken &keyPath) const;
390
397 USD_API
398 UsdMetadataValueMap GetAllMetadata() const;
399
406 USD_API
407 UsdMetadataValueMap GetAllAuthoredMetadata() const;
408
409 // --------------------------------------------------------------------- //
411 // --------------------------------------------------------------------- //
412
413 // --------------------------------------------------------------------- //
416 // --------------------------------------------------------------------- //
417
432 USD_API
433 bool IsHidden() const;
434
437 USD_API
438 bool SetHidden(bool hidden) const;
439
441 USD_API
442 bool ClearHidden() const;
443
449 USD_API
450 bool HasAuthoredHidden() const;
451
470 USD_API
472
478 USD_API
479 VtValue GetCustomDataByKey(const TfToken &keyPath) const;
480
483 USD_API
484 void SetCustomData(const VtDictionary &customData) const;
485
489 USD_API
490 void SetCustomDataByKey(const TfToken &keyPath, const VtValue &value) const;
491
495 USD_API
496 void ClearCustomData() const;
497
502 USD_API
503 void ClearCustomDataByKey(const TfToken &keyPath) const;
504
507 USD_API
508 bool HasCustomData() const;
509
514 USD_API
515 bool HasCustomDataKey(const TfToken &keyPath) const;
516
519 USD_API
521
526 USD_API
527 bool HasAuthoredCustomDataKey(const TfToken &keyPath) const;
528
542 USD_API
544
550 USD_API
551 VtValue GetAssetInfoByKey(const TfToken &keyPath) const;
552
555 USD_API
556 void SetAssetInfo(const VtDictionary &customData) const;
557
561 USD_API
562 void SetAssetInfoByKey(const TfToken &keyPath, const VtValue &value) const;
563
567 USD_API
568 void ClearAssetInfo() const;
569
574 USD_API
575 void ClearAssetInfoByKey(const TfToken &keyPath) const;
576
579 USD_API
580 bool HasAssetInfo() const;
581
586 USD_API
587 bool HasAssetInfoKey(const TfToken &keyPath) const;
588
591 USD_API
593
598 USD_API
599 bool HasAuthoredAssetInfoKey(const TfToken &keyPath) const;
600
604 USD_API
605 std::string GetDocumentation() const;
606
608 USD_API
609 bool SetDocumentation(const std::string& doc) const;
610
613 USD_API
614 bool ClearDocumentation() const;
615
618 USD_API
620
627 USD_API
628 std::string GetDisplayName() const;
629
638 USD_API
639 bool SetDisplayName(const std::string& name) const;
640
646 USD_API
647 bool ClearDisplayName() const;
648
654 USD_API
656
657 // --------------------------------------------------------------------- //
659 // --------------------------------------------------------------------- //
660
661 // XXX: This method can and probably should move to UsdProperty
662 static char GetNamespaceDelimiter()
663 { return SdfPathTokens->namespaceDelimiter.GetText()[0]; }
664
665private:
666 template <class T>
667 bool _GetMetadataImpl(const TfToken& key,
668 T* value,
669 const TfToken &keyPath=TfToken()) const;
670
671 bool _GetMetadataImpl(const TfToken& key,
672 VtValue* value,
673 const TfToken &keyPath=TfToken()) const;
674
675 template <class T>
676 bool _SetMetadataImpl(const TfToken& key,
677 const T& value,
678 const TfToken &keyPath=TfToken()) const;
679
680 bool _SetMetadataImpl(const TfToken& key,
681 const VtValue& value,
682 const TfToken &keyPath=TfToken()) const;
683
684protected:
685 template <class Derived> struct _Null {};
686
687 // Private constructor for null dervied types.
688 template <class Derived>
689 explicit UsdObject(_Null<Derived>)
690 : _type(_Detail::GetObjType<Derived>::Value) {}
691
692 // Private constructor for UsdPrim.
693 UsdObject(const Usd_PrimDataHandle &prim,
694 const SdfPath &proxyPrimPath)
695 : _type(UsdTypePrim)
696 , _prim(prim)
697 , _proxyPrimPath(proxyPrimPath)
698 {
699 TF_VERIFY(!_prim || _prim->GetPath() != _proxyPrimPath);
700 }
701
702 // Private constructor for UsdAttribute/UsdRelationship.
703 UsdObject(UsdObjType objType,
704 const Usd_PrimDataHandle &prim,
705 const SdfPath &proxyPrimPath,
706 const TfToken &propName)
707 : _type(objType)
708 , _prim(prim)
709 , _proxyPrimPath(proxyPrimPath)
710 , _propName(propName)
711 {
712 TF_VERIFY(!_prim || _prim->GetPath() != _proxyPrimPath);
713 }
714
715 // Return the stage this object belongs to.
716 UsdStage *_GetStage() const { return _prim->GetStage(); }
717
718 // Return this object's defining spec type.
719 USD_API
720 SdfSpecType _GetDefiningSpecType() const;
721
722 // Helper for subclasses: return held prim data.
723 const Usd_PrimDataHandle &_Prim() const { return _prim; }
724
725 // Helper for subclasses: return held property name.
726 const TfToken &_PropName() const { return _propName; }
727
728 // Helper for subclasses: return held proxy prim path.
729 const SdfPath &_ProxyPrimPath() const { return _proxyPrimPath; }
730
731private:
732 // Helper for the above helper, and also for GetDescription()
733 std::string _GetObjectDescription(const std::string &preface) const;
734
735 friend class UsdStage;
736
737 friend UsdObjType Usd_GetObjType(const UsdObject &obj) {
738 return obj._type;
739 }
740
741 UsdObjType _type;
742 Usd_PrimDataHandle _prim;
743 SdfPath _proxyPrimPath;
744 TfToken _propName;
745
746};
747
748template<typename T>
749inline
750bool
751UsdObject::GetMetadata(const TfToken& key, T* value) const
752{
753 return _GetMetadataImpl(key, value);
754}
755
756template<typename T>
757inline
758bool
759UsdObject::SetMetadata(const TfToken& key, const T& value) const
760{
761 return _SetMetadataImpl(key, value);
762}
763
764template <typename T>
765inline
766bool
768 const TfToken &keyPath,
769 T *value) const
770{
771 return _GetMetadataImpl(key, value, keyPath);
772}
773
774template <typename T>
775inline
776bool
778 const TfToken &keyPath,
779 const T& value) const
780{
781 return _SetMetadataImpl(key, value, keyPath);
782}
783
784template <class T>
785bool
786UsdObject::_GetMetadataImpl(const TfToken& key,
787 T* value,
788 const TfToken &keyPath) const
789{
790 return _GetStage()->_GetMetadata(
791 *this, key, keyPath, /*useFallbacks=*/true, value);
792}
793
794template <class T>
795bool
796UsdObject::_SetMetadataImpl(const TfToken& key,
797 const T& value,
798 const TfToken &keyPath) const
799{
800 return _GetStage()->_SetMetadata(*this, key, keyPath, value);
801}
802
803PXR_NAMESPACE_CLOSE_SCOPE
804
805#endif //PXR_USD_USD_OBJECT_H
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:274
bool IsEmpty() const noexcept
Returns true if this is the empty path (SdfPath::EmptyPath()).
Definition: path.h:398
static SDF_API const SdfPath & EmptyPath()
The empty path value, equivalent to SdfPath().
SDF_API const TfToken & GetNameToken() const
Returns the name of the prim, property or relational attribute identified by the path,...
SDF_API SdfPath AppendProperty(TfToken const &propName) const
Creates a path by appending an element for propName to this path.
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
Scenegraph object for authoring and retrieving numeric, string, and array valued data,...
Definition: attribute.h:183
Base class for Usd scenegraph objects, providing common API.
Definition: object.h:115
USD_API bool HasAuthoredCustomDataKey(const TfToken &keyPath) const
Return true if there are any authored opinions (excluding fallback) for the element identified by key...
USD_API bool SetMetadataByDictKey(const TfToken &key, const TfToken &keyPath, const VtValue &value) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
USD_API bool SetHidden(bool hidden) const
Sets the value of the 'hidden' metadata field.
bool SetMetadata(const TfToken &key, const T &value) const
Set metadatum key's value to value.
Definition: object.h:759
USD_API bool SetDocumentation(const std::string &doc) const
Sets this object's documentation (metadata). Returns true on success.
friend bool operator!=(const UsdObject &lhs, const UsdObject &rhs)
Inequality comparison.
Definition: object.h:156
USD_API bool HasAuthoredMetadataDictKey(const TfToken &key, const TfToken &keyPath) const
Return true if there exists any authored opinion (excluding fallbacks) for key and keyPath.
USD_API void SetAssetInfoByKey(const TfToken &keyPath, const VtValue &value) const
Author the element identified by keyPath in this object's assetInfo dictionary at the current EditTar...
USD_API bool HasCustomDataKey(const TfToken &keyPath) const
Return true if there are any authored or fallback opinions for the element identified by keyPath in t...
USD_API void ClearCustomData() const
Clear the authored opinion for this object's customData dictionary at the current EditTarget.
USD_API bool HasAuthoredMetadata(const TfToken &key) const
Returns true if the key has an authored value, false if no value was authored or the only value avail...
UsdObject()
Default constructor produces an invalid object.
Definition: object.h:118
USD_API bool GetMetadataByDictKey(const TfToken &key, const TfToken &keyPath, VtValue *value) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
USD_API bool ClearDisplayName() const
Clears this object's display name (metadata) in the current EditTarget (only).
UsdPrim GetPrim() const
Return this object if it is a prim, otherwise return this object's nearest owning prim.
Definition: prim.h:2793
USD_API bool HasAssetInfoKey(const TfToken &keyPath) const
Return true if there are any authored or fallback opinions for the element identified by keyPath in t...
USD_API VtDictionary GetAssetInfo() const
Return this object's composed assetInfo dictionary.
USD_API void ClearCustomDataByKey(const TfToken &keyPath) const
Clear the authored opinion identified by keyPath in this object's customData dictionary at the curren...
USD_API bool ClearHidden() const
Clears the opinion for "Hidden" at the current EditTarget.
USD_API bool HasAuthoredDisplayName() const
Returns true if displayName was explicitly authored and GetMetadata() will return a meaningful value ...
bool Is() const
Return true if this object is convertible to T.
Definition: object.h:240
USD_API bool HasAuthoredAssetInfoKey(const TfToken &keyPath) const
Return true if there are any authored opinions (excluding fallback) for the element identified by key...
bool SetMetadataByDictKey(const TfToken &key, const TfToken &keyPath, const T &value) const
Author value to the field identified by key and keyPath at the current EditTarget.
Definition: object.h:777
USD_API void ClearAssetInfo() const
Clear the authored opinion for this object's assetInfo dictionary at the current EditTarget.
friend bool operator==(const UsdObject &lhs, const UsdObject &rhs)
Equality comparison.
Definition: object.h:147
USD_API bool SetDisplayName(const std::string &name) const
Sets this object's display name (metadata).
USD_API bool HasAuthoredHidden() const
Returns true if hidden was explicitly authored and GetMetadata() will return a meaningful value for H...
USD_API bool HasMetadataDictKey(const TfToken &key, const TfToken &keyPath) const
Return true if there exists any authored or fallback opinion for key and keyPath.
SdfPath GetPath() const
Return the complete scene path to this object on its UsdStage, which may (UsdPrim) or may not (all ot...
Definition: object.h:186
const SdfPath & GetPrimPath() const
Return this object's path if this object is a prim, otherwise this object's nearest owning prim's pat...
Definition: object.h:201
USD_API VtValue GetCustomDataByKey(const TfToken &keyPath) const
Return the element identified by keyPath in this object's composed customData dictionary.
USD_API std::string GetDescription() const
Return a string that provides a brief summary description of the object.
USD_API bool HasAssetInfo() const
Return true if there are any authored or fallback opinions for this object's assetInfo dictionary,...
T As() const
Convert this UsdObject to another object type T if possible.
Definition: object.h:229
USD_API bool IsHidden() const
Gets the value of the 'hidden' metadata field, false if not authored.
USD_API void ClearAssetInfoByKey(const TfToken &keyPath) const
Clear the authored opinion identified by keyPath in this object's assetInfo dictionary at the current...
friend bool operator<(const UsdObject &lhs, const UsdObject &rhs)
Less-than operator.
Definition: object.h:163
USD_API bool HasAuthoredDocumentation() const
Returns true if documentation was explicitly authored and GetMetadata() will return a meaningful valu...
USD_API bool ClearDocumentation() const
Clears this object's documentation (metadata) in the current EditTarget (only).
bool GetMetadataByDictKey(const TfToken &key, const TfToken &keyPath, T *value) const
Resolve the requested dictionary sub-element keyPath of dictionary-valued metadatum named key into va...
Definition: object.h:767
USD_API bool SetMetadata(const TfToken &key, const VtValue &value) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
USD_API void SetCustomDataByKey(const TfToken &keyPath, const VtValue &value) const
Author the element identified by keyPath in this object's customData dictionary at the current EditTa...
USD_API VtDictionary GetCustomData() const
Return this object's composed customData dictionary.
const TfToken & GetName() const
Return the full name of this object, i.e.
Definition: object.h:221
USD_API void SetCustomData(const VtDictionary &customData) const
Author this object's customData dictionary to customData at the current EditTarget.
bool IsValid() const
Return true if this is a valid object, false otherwise.
Definition: object.h:126
USD_API UsdMetadataValueMap GetAllAuthoredMetadata() const
Resolve and return all user-authored metadata on this object, sorted lexicographically.
USD_API UsdStageWeakPtr GetStage() const
Return the stage that owns the object, and to whose state and lifetime this object's validity is tied...
USD_API void SetAssetInfo(const VtDictionary &customData) const
Author this object's assetInfo dictionary to assetInfo at the current EditTarget.
bool GetMetadata(const TfToken &key, T *value) const
Resolve the requested metadatum named key into value, returning true on success.
Definition: object.h:751
USD_API VtValue GetAssetInfoByKey(const TfToken &keyPath) const
Return the element identified by keyPath in this object's composed assetInfo dictionary.
USD_API bool HasMetadata(const TfToken &key) const
Returns true if the key has a meaningful value, that is, if GetMetadata() will provide a value,...
USD_API bool HasCustomData() const
Return true if there are any authored or fallback opinions for this object's customData dictionary,...
USD_API bool HasAuthoredAssetInfo() const
Return true if there are any authored opinions (excluding fallback) for this object's assetInfo dicti...
USD_API bool GetMetadata(const TfToken &key, VtValue *value) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
USD_API bool ClearMetadata(const TfToken &key) const
Clears the authored key's value at the current EditTarget, returning false on error.
USD_API std::string GetDocumentation() const
Return this object's documentation (metadata).
USD_API UsdMetadataValueMap GetAllMetadata() const
Resolve and return all metadata (including both authored and fallback values) on this object,...
USD_API bool ClearMetadataByDictKey(const TfToken &key, const TfToken &keyPath) const
Clear any authored value identified by key and keyPath at the current EditTarget.
USD_API bool HasAuthoredCustomData() const
Return true if there are any authored opinions (excluding fallback) for this object's customData dict...
USD_API std::string GetDisplayName() const
Return this object's display name (metadata).
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a "Prim" as ...
Definition: prim.h:117
Base class for UsdAttribute and UsdRelationship scenegraph objects.
Definition: property.h:38
A UsdRelationship creates dependencies between scenegraph objects by allowing a prim to target other ...
Definition: relationship.h:111
The outermost container for scene description, which owns and presents composed prims as a scenegraph...
Definition: stage.h:136
A map with string keys and VtValue values.
Definition: dictionary.h:43
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:152
#define TF_DECLARE_WEAK_PTRS(type)
Define standard weak pointer types.
Definition: declarePtrs.h:45
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:266
SdfSpecType
An enum that specifies the type of an object.
Definition: types.h:68
bool UsdIsConvertible(UsdObjType from, UsdObjType to)
Return true if from is convertible to to, false otherwise.
Definition: object.h:77
bool UsdIsSubtype(UsdObjType baseType, UsdObjType subType)
Return true if subType is the same as or a subtype of baseType, false otherwise.
Definition: object.h:69
bool UsdIsConcrete(UsdObjType type)
Return true if type is a concrete object type, namely one of Prim, Attribute, or Relationship.
Definition: object.h:84
UsdObjType
Enum values to represent the various Usd object types.
Definition: object.h:35