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
477 USD_API
478 void SetCustomData(const VtDictionary &customData) const;
479
483 USD_API
484 void SetCustomDataByKey(const TfToken &keyPath, const VtValue &value) const;
485
489 USD_API
490 void ClearCustomData() const;
491
496 USD_API
497 void ClearCustomDataByKey(const TfToken &keyPath) const;
498
501 USD_API
502 bool HasCustomData() const;
503
508 USD_API
509 bool HasCustomDataKey(const TfToken &keyPath) const;
510
513 USD_API
515
520 USD_API
521 bool HasAuthoredCustomDataKey(const TfToken &keyPath) const;
522
536 USD_API
538
544 USD_API
545 VtValue GetAssetInfoByKey(const TfToken &keyPath) const;
546
549 USD_API
550 void SetAssetInfo(const VtDictionary &customData) const;
551
555 USD_API
556 void SetAssetInfoByKey(const TfToken &keyPath, const VtValue &value) const;
557
561 USD_API
562 void ClearAssetInfo() const;
563
568 USD_API
569 void ClearAssetInfoByKey(const TfToken &keyPath) const;
570
573 USD_API
574 bool HasAssetInfo() const;
575
580 USD_API
581 bool HasAssetInfoKey(const TfToken &keyPath) const;
582
585 USD_API
587
592 USD_API
593 bool HasAuthoredAssetInfoKey(const TfToken &keyPath) const;
594
598 USD_API
599 std::string GetDocumentation() const;
600
602 USD_API
603 bool SetDocumentation(const std::string& doc) const;
604
607 USD_API
608 bool ClearDocumentation() const;
609
612 USD_API
614
621 USD_API
622 std::string GetDisplayName() const;
623
632 USD_API
633 bool SetDisplayName(const std::string& name) const;
634
640 USD_API
641 bool ClearDisplayName() const;
642
648 USD_API
650
651 // --------------------------------------------------------------------- //
653 // --------------------------------------------------------------------- //
654
655 // XXX: This method can and probably should move to UsdProperty
656 static char GetNamespaceDelimiter()
657 { return SdfPathTokens->namespaceDelimiter.GetText()[0]; }
658
659private:
660 template <class T>
661 bool _GetMetadataImpl(const TfToken& key,
662 T* value,
663 const TfToken &keyPath=TfToken()) const;
664
665 bool _GetMetadataImpl(const TfToken& key,
666 VtValue* value,
667 const TfToken &keyPath=TfToken()) const;
668
669 bool _SetMetadataImpl(const TfToken& key, VtValueRef value,
670 const TfToken &keyPath=TfToken()) const;
671
672protected:
673 template <class Derived> struct _Null {};
674
675 // Private constructor for null dervied types.
676 template <class Derived>
677 explicit UsdObject(_Null<Derived>)
678 : _type(_Detail::GetObjType<Derived>::Value) {}
679
680 // Private constructor for UsdPrim.
681 UsdObject(const Usd_PrimDataHandle &prim,
682 const SdfPath &proxyPrimPath)
683 : _type(UsdTypePrim)
684 , _prim(prim)
685 , _proxyPrimPath(proxyPrimPath)
686 {
687 TF_VERIFY(!_prim || _prim->GetPath() != _proxyPrimPath);
688 }
689
690 // Private constructor for UsdAttribute/UsdRelationship.
691 UsdObject(UsdObjType objType,
692 const Usd_PrimDataHandle &prim,
693 const SdfPath &proxyPrimPath,
694 const TfToken &propName)
695 : _type(objType)
696 , _prim(prim)
697 , _proxyPrimPath(proxyPrimPath)
698 , _propName(propName)
699 {
700 TF_VERIFY(!_prim || _prim->GetPath() != _proxyPrimPath);
701 }
702
703 // Return the stage this object belongs to.
704 UsdStage *_GetStage() const { return _prim->GetStage(); }
705
706 // Return this object's defining spec type.
707 USD_API
708 SdfSpecType _GetDefiningSpecType() const;
709
710 // Helper for subclasses: return held prim data.
711 const Usd_PrimDataHandle &_Prim() const { return _prim; }
712
713 // Helper for subclasses: return held property name.
714 const TfToken &_PropName() const { return _propName; }
715
716 // Helper for subclasses: return held proxy prim path.
717 const SdfPath &_ProxyPrimPath() const { return _proxyPrimPath; }
718
719private:
720 // Helper for the above helper, and also for GetDescription()
721 std::string _GetObjectDescription(const std::string &preface) const;
722
723 friend class UsdStage;
724
725 friend UsdObjType Usd_GetObjType(const UsdObject &obj) {
726 return obj._type;
727 }
728
729 UsdObjType _type;
730 Usd_PrimDataHandle _prim;
731 SdfPath _proxyPrimPath;
732 TfToken _propName;
733
734};
735
736template<typename T>
737inline
738bool
739UsdObject::GetMetadata(const TfToken& key, T* value) const
740{
741 return _GetMetadataImpl(key, value);
742}
743
744template <typename T>
745inline
746bool
748 const TfToken &keyPath,
749 T *value) const
750{
751 return _GetMetadataImpl(key, value, keyPath);
752}
753
754template <class T>
755bool
756UsdObject::_GetMetadataImpl(const TfToken& key,
757 T* value,
758 const TfToken &keyPath) const
759{
760 return _GetStage()->_GetMetadata(
761 *this, key, keyPath, /*useFallbacks=*/true, value);
762}
763
764PXR_NAMESPACE_CLOSE_SCOPE
765
766#endif //PXR_USD_USD_OBJECT_H
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:280
bool IsEmpty() const noexcept
Returns true if this is the empty path (SdfPath::EmptyPath()).
Definition: path.h:404
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: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: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: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:747
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:739
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 ...
Definition: relationship.h:111
The outermost container for scene description, which owns and presents composed prims as a scenegraph...
Definition: stage.h:137
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:266
SdfSpecType
An enum that specifies the type of an object.
Definition: types.h:71
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