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#include "pxr/base/vt/valueRef.h"
23
24#include <type_traits>
25
26PXR_NAMESPACE_OPEN_SCOPE
27
28
30
36{
37 // Value order matters in this enum.
38 UsdTypeObject,
39 UsdTypePrim,
40 UsdTypeProperty,
41 UsdTypeAttribute,
42 UsdTypeRelationship,
43
44 Usd_NumObjTypes
45};
46
47
48namespace _Detail {
49
50// A metafunction that takes a UsdObject class like UsdObject, UsdPrim,
51// UsdProperty, etc, and gives its corresponding UsdObjType, e.g. UsdTypeObject,
52// UsdTypePrim, UsdTypeProperty, etc. Usage: GetObjType<UsdPrim>::Value.
53template <UsdObjType Type>
54struct Const { static const UsdObjType Value = Type; };
55template <class T> struct GetObjType {
56 static_assert(std::is_base_of<UsdObject, T>::value,
57 "Type T must be a subclass of UsdObject.");
58};
59template <> struct GetObjType<UsdObject> : Const<UsdTypeObject> {};
60template <> struct GetObjType<UsdPrim> : Const<UsdTypePrim> {};
61template <> struct GetObjType<UsdProperty> : Const<UsdTypeProperty> {};
62template <> struct GetObjType<UsdAttribute> : Const<UsdTypeAttribute> {};
63template <> struct GetObjType<UsdRelationship> : Const<UsdTypeRelationship> {};
64
65} // _Detail
66
69inline bool
71 return (baseType == UsdTypeObject) || (baseType == subType) ||
72 (baseType == UsdTypeProperty && subType > UsdTypeProperty);
73}
74
77inline bool
79 return UsdIsSubtype(to, from);
80}
81
84inline bool
86 return type == UsdTypePrim ||
87 type == UsdTypeAttribute ||
88 type == UsdTypeRelationship;
89}
90
117public:
119 UsdObject() : _type(UsdTypeObject) {}
120
121 // --------------------------------------------------------------------- //
124 // --------------------------------------------------------------------- //
125
127 bool IsValid() const {
128 if (!UsdIsConcrete(_type) || !_prim)
129 return false;
130 if (_type == UsdTypePrim)
131 return true;
132 SdfSpecType specType = _GetDefiningSpecType();
133 return (_type == UsdTypeAttribute &&
134 specType == SdfSpecTypeAttribute) ||
135 (_type == UsdTypeRelationship &&
136 specType == SdfSpecTypeRelationship);
137 }
138
140 explicit operator bool() const {
141 return IsValid();
142 }
143
144public:
145
148 friend bool operator==(const UsdObject &lhs, const UsdObject &rhs) {
149 return lhs._type == rhs._type &&
150 lhs._prim == rhs._prim &&
151 lhs._proxyPrimPath == rhs._proxyPrimPath &&
152 lhs._propName == rhs._propName;
153 }
154
157 friend bool operator!=(const UsdObject &lhs, const UsdObject &rhs) {
158 return !(lhs == rhs);
159 }
160
164 friend bool operator<(const UsdObject &lhs, const UsdObject &rhs) {
165 return lhs.GetPath() < rhs.GetPath();
166 }
167
168 // hash_value overload for std/boost hash.
169 friend size_t hash_value(const UsdObject &obj) {
170 return TfHash()(obj);
171 }
172
173 // TfHash support
174 template <class HashState>
175 friend void TfHashAppend(HashState &h, const UsdObject &obj) {
176 h.Append(obj._type, obj._prim, obj._proxyPrimPath, obj._propName);
177 }
178
181 USD_API
182 UsdStageWeakPtr GetStage() const;
183
187 SdfPath GetPath() const {
188 // Allow getting expired object paths.
189 if (!_proxyPrimPath.IsEmpty()) {
190 return _type == UsdTypePrim ?
191 _proxyPrimPath : _proxyPrimPath.AppendProperty(_propName);
192 }
193 else if (Usd_PrimDataConstPtr p = get_pointer(_prim)) {
194 return _type == UsdTypePrim ?
195 p->GetPath() : p->GetPath().AppendProperty(_propName);
196 }
197 return SdfPath();
198 }
199
202 const SdfPath &GetPrimPath() const {
203 // Allow getting expired object paths.
204 if (!_proxyPrimPath.IsEmpty()) {
205 return _proxyPrimPath;
206 }
207 else if (Usd_PrimDataConstPtr p = get_pointer(_prim)) {
208 return p->GetPath();
209 }
210 return SdfPath::EmptyPath();
211 }
212
215 inline UsdPrim GetPrim() const;
216
222 const TfToken &GetName() const {
223 return _type == UsdTypePrim ? GetPrimPath().GetNameToken() : _propName;
224 }
225
229 template <class T>
230 T As() const {
231 // compile-time type assertion provided by invoking Is<T>().
232 return Is<T>() ? T(_type, _prim, _proxyPrimPath, _propName) : T();
233 }
234
240 template <class T>
241 bool Is() const {
242 static_assert(std::is_base_of<UsdObject, T>::value,
243 "Provided type T must derive from or be UsdObject");
244 return UsdIsConvertible(_type, _Detail::GetObjType<T>::Value);
245 }
246
252 USD_API
253 std::string GetDescription() const;
254
255 // --------------------------------------------------------------------- //
257 // --------------------------------------------------------------------- //
258
259
260 // --------------------------------------------------------------------- //
263 // --------------------------------------------------------------------- //
264
279 template<typename T>
280 bool GetMetadata(const TfToken& key, T* value) const;
284 USD_API
285 bool GetMetadata(const TfToken& key, VtValue* value) const;
286
293 USD_API
294 bool SetMetadata(const TfToken& key, VtValueRef value) const;
295
305 USD_API
306 bool ClearMetadata(const TfToken& key) const;
307
311 USD_API
312 bool HasMetadata(const TfToken& key) const;
313
317 USD_API
318 bool HasAuthoredMetadata(const TfToken& key) const;
319
335 template <class T>
337 const TfToken& key, const TfToken &keyPath, T *value) const;
339 USD_API
341 const TfToken& key, const TfToken &keyPath, VtValue *value) const;
342
350 USD_API
352 const TfToken& key, const TfToken &keyPath, VtValueRef value) const;
353
361 USD_API
363 const TfToken& key, const TfToken& keyPath) const;
364
371 USD_API
373 const TfToken& key, const TfToken &keyPath) const;
374
381 USD_API
383 const TfToken& key, const TfToken &keyPath) const;
384
391 USD_API
392 UsdMetadataValueMap GetAllMetadata() const;
393
400 USD_API
401 UsdMetadataValueMap GetAllAuthoredMetadata() const;
402
403 // --------------------------------------------------------------------- //
405 // --------------------------------------------------------------------- //
406
407 // --------------------------------------------------------------------- //
410 // --------------------------------------------------------------------- //
411
426 USD_API
427 bool IsHidden() const;
428
431 USD_API
432 bool SetHidden(bool hidden) const;
433
435 USD_API
436 bool ClearHidden() const;
437
443 USD_API
444 bool HasAuthoredHidden() const;
445
464 USD_API
466
472 USD_API
473 VtValue GetCustomDataByKey(const TfToken &keyPath) const;
474
479 USD_API
480 void SetCustomData(const VtDictionary &customData) const;
481
485 USD_API
486 void SetCustomDataByKey(const TfToken &keyPath, const VtValue &value) const;
487
491 USD_API
492 void ClearCustomData() const;
493
498 USD_API
499 void ClearCustomDataByKey(const TfToken &keyPath) const;
500
503 USD_API
504 bool HasCustomData() const;
505
510 USD_API
511 bool HasCustomDataKey(const TfToken &keyPath) const;
512
515 USD_API
517
522 USD_API
523 bool HasAuthoredCustomDataKey(const TfToken &keyPath) const;
524
538 USD_API
540
546 USD_API
547 VtValue GetAssetInfoByKey(const TfToken &keyPath) const;
548
553 USD_API
554 void SetAssetInfo(const VtDictionary &customData) const;
555
559 USD_API
560 void SetAssetInfoByKey(const TfToken &keyPath, const VtValue &value) const;
561
565 USD_API
566 void ClearAssetInfo() const;
567
572 USD_API
573 void ClearAssetInfoByKey(const TfToken &keyPath) const;
574
577 USD_API
578 bool HasAssetInfo() const;
579
584 USD_API
585 bool HasAssetInfoKey(const TfToken &keyPath) const;
586
589 USD_API
591
596 USD_API
597 bool HasAuthoredAssetInfoKey(const TfToken &keyPath) const;
598
602 USD_API
603 std::string GetDocumentation() const;
604
606 USD_API
607 bool SetDocumentation(const std::string& doc) const;
608
611 USD_API
612 bool ClearDocumentation() const;
613
616 USD_API
618
625 USD_API
626 std::string GetDisplayName() const;
627
636 USD_API
637 bool SetDisplayName(const std::string& name) const;
638
644 USD_API
645 bool ClearDisplayName() const;
646
652 USD_API
654
655 // --------------------------------------------------------------------- //
657 // --------------------------------------------------------------------- //
658
659 // XXX: This method can and probably should move to UsdProperty
660 static char GetNamespaceDelimiter()
661 { return SdfPathTokens->namespaceDelimiter.GetText()[0]; }
662
663private:
664 template <class T>
665 bool _GetMetadataImpl(const TfToken& key,
666 T* value,
667 const TfToken &keyPath=TfToken()) const;
668
669 bool _GetMetadataImpl(const TfToken& key,
670 VtValue* value,
671 const TfToken &keyPath=TfToken()) const;
672
673 bool _SetMetadataImpl(const TfToken& key, VtValueRef value,
674 const TfToken &keyPath=TfToken()) const;
675
676protected:
677 template <class Derived> struct _Null {};
678
679 // Private constructor for null dervied types.
680 template <class Derived>
681 explicit UsdObject(_Null<Derived>)
682 : _type(_Detail::GetObjType<Derived>::Value) {}
683
684 // Private constructor for UsdPrim.
685 UsdObject(const Usd_PrimDataHandle &prim,
686 const SdfPath &proxyPrimPath)
687 : _type(UsdTypePrim)
688 , _prim(prim)
689 , _proxyPrimPath(proxyPrimPath)
690 {
691 TF_VERIFY(!_prim || _prim->GetPath() != _proxyPrimPath);
692 }
693
694 // Private constructor for UsdAttribute/UsdRelationship.
695 UsdObject(UsdObjType objType,
696 const Usd_PrimDataHandle &prim,
697 const SdfPath &proxyPrimPath,
698 const TfToken &propName)
699 : _type(objType)
700 , _prim(prim)
701 , _proxyPrimPath(proxyPrimPath)
702 , _propName(propName)
703 {
704 TF_VERIFY(!_prim || _prim->GetPath() != _proxyPrimPath);
705 }
706
707 // Return the stage this object belongs to.
708 UsdStage *_GetStage() const { return _prim->GetStage(); }
709
710 // Return this object's defining spec type.
711 USD_API
712 SdfSpecType _GetDefiningSpecType() const;
713
714 // Helper for subclasses: return held prim data.
715 const Usd_PrimDataHandle &_Prim() const { return _prim; }
716
717 // Helper for subclasses: return held property name.
718 const TfToken &_PropName() const { return _propName; }
719
720 // Helper for subclasses: return held proxy prim path.
721 const SdfPath &_ProxyPrimPath() const { return _proxyPrimPath; }
722
723private:
724 // Helper for the above helper, and also for GetDescription()
725 std::string _GetObjectDescription(const std::string &preface) const;
726
727 friend class UsdStage;
728
729 friend UsdObjType Usd_GetObjType(const UsdObject &obj) {
730 return obj._type;
731 }
732
733 UsdObjType _type;
734 Usd_PrimDataHandle _prim;
735 SdfPath _proxyPrimPath;
736 TfToken _propName;
737
738};
739
740template<typename T>
741inline
742bool
743UsdObject::GetMetadata(const TfToken& key, T* value) const
744{
745 return _GetMetadataImpl(key, value);
746}
747
748template <typename T>
749inline
750bool
752 const TfToken &keyPath,
753 T *value) const
754{
755 return _GetMetadataImpl(key, value, keyPath);
756}
757
758template <class T>
759bool
760UsdObject::_GetMetadataImpl(const TfToken& key,
761 T* value,
762 const TfToken &keyPath) const
763{
764 return _GetStage()->_GetMetadata(
765 *this, key, keyPath, /*useFallbacks=*/true, value);
766}
767
768PXR_NAMESPACE_CLOSE_SCOPE
769
770#endif //PXR_USD_USD_OBJECT_H
A path value used to locate objects in layers or scenegraphs.
Definition path.h:281
bool IsEmpty() const noexcept
Returns true if this is the empty path (SdfPath::EmptyPath()).
Definition path.h:405
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:81
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:116
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 SetHidden(bool hidden) const
Sets the value of the 'hidden' metadata field.
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:157
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 bool SetMetadata(const TfToken &key, VtValueRef value) const
Set metadatum key's value to value.
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:119
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:2806
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:241
USD_API bool HasAuthoredAssetInfoKey(const TfToken &keyPath) const
Return true if there are any authored opinions (excluding fallback) for the element identified by key...
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:148
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:187
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:202
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:230
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:164
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:751
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:222
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:127
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:743
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 SetMetadataByDictKey(const TfToken &key, const TfToken &keyPath, VtValueRef value) const
Author value to the field identified by key and keyPath at the current EditTarget.
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 ...
The outermost container for scene description, which owns and presents composed prims as a scenegraph...
Definition stage.h:135
A map with string keys and VtValue values.
Definition dictionary.h:52
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition value.h:90
A non-owning type-erased view of a value, interoperating with VtValue.
Definition valueRef.h:65
#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:267
SdfSpecType
An enum that specifies the type of an object.
Definition types.h:73
bool UsdIsConvertible(UsdObjType from, UsdObjType to)
Return true if from is convertible to to, false otherwise.
Definition object.h:78
bool UsdIsSubtype(UsdObjType baseType, UsdObjType subType)
Return true if subType is the same as or a subtype of baseType, false otherwise.
Definition object.h:70
bool UsdIsConcrete(UsdObjType type)
Return true if type is a concrete object type, namely one of Prim, Attribute, or Relationship.
Definition object.h:85
UsdObjType
Enum values to represent the various Usd object types.
Definition object.h:36