object.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 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_OBJECT_H
25 #define PXR_USD_USD_OBJECT_H
26 
28 
29 #include "pxr/pxr.h"
30 #include "pxr/usd/usd/api.h"
31 #include "pxr/usd/usd/common.h"
32 #include "pxr/usd/usd/primData.h"
33 #include "pxr/usd/usd/stage.h"
34 
35 #include "pxr/usd/sdf/abstractData.h"
36 #include "pxr/usd/sdf/path.h"
37 
38 #include <type_traits>
39 
40 PXR_NAMESPACE_OPEN_SCOPE
41 
42 
44 
50 {
51  // Value order matters in this enum.
52  UsdTypeObject,
53  UsdTypePrim,
54  UsdTypeProperty,
55  UsdTypeAttribute,
56  UsdTypeRelationship,
57 
58  Usd_NumObjTypes
59 };
60 
61 
62 namespace _Detail {
63 
64 // A metafunction that takes a UsdObject class like UsdObject, UsdPrim,
65 // UsdProperty, etc, and gives its corresponding UsdObjType, e.g. UsdTypeObject,
66 // UsdTypePrim, UsdTypeProperty, etc. Usage: GetObjType<UsdPrim>::Value.
67 template <UsdObjType Type>
68 struct Const { static const UsdObjType Value = Type; };
69 template <class T> struct GetObjType {
70  static_assert(std::is_base_of<UsdObject, T>::value,
71  "Type T must be a subclass of UsdObject.");
72 };
73 template <> struct GetObjType<UsdObject> : Const<UsdTypeObject> {};
74 template <> struct GetObjType<UsdPrim> : Const<UsdTypePrim> {};
75 template <> struct GetObjType<UsdProperty> : Const<UsdTypeProperty> {};
76 template <> struct GetObjType<UsdAttribute> : Const<UsdTypeAttribute> {};
77 template <> struct GetObjType<UsdRelationship> : Const<UsdTypeRelationship> {};
78 
79 } // _Detail
80 
83 inline bool
84 UsdIsSubtype(UsdObjType baseType, UsdObjType subType) {
85  return (baseType == UsdTypeObject) || (baseType == subType) ||
86  (baseType == UsdTypeProperty && subType > UsdTypeProperty);
87 }
88 
91 inline bool
93  return UsdIsSubtype(to, from);
94 }
95 
98 inline bool
100  return type == UsdTypePrim ||
101  type == UsdTypeAttribute ||
102  type == UsdTypeRelationship;
103 }
104 
130 class UsdObject {
131 public:
133  UsdObject() : _type(UsdTypeObject) {}
134 
135  // --------------------------------------------------------------------- //
138  // --------------------------------------------------------------------- //
139 
141  bool IsValid() const {
142  if (!UsdIsConcrete(_type) || !_prim)
143  return false;
144  if (_type == UsdTypePrim)
145  return true;
146  SdfSpecType specType = _GetDefiningSpecType();
147  return (_type == UsdTypeAttribute &&
148  specType == SdfSpecTypeAttribute) ||
149  (_type == UsdTypeRelationship &&
150  specType == SdfSpecTypeRelationship);
151  }
152 
154  explicit operator bool() const {
155  return IsValid();
156  }
157 
158 public:
159 
162  friend bool operator==(const UsdObject &lhs, const UsdObject &rhs) {
163  return lhs._type == rhs._type &&
164  lhs._prim == rhs._prim &&
165  lhs._proxyPrimPath == rhs._proxyPrimPath &&
166  lhs._propName == rhs._propName;
167  }
168 
171  friend bool operator!=(const UsdObject &lhs, const UsdObject &rhs) {
172  return !(lhs == rhs);
173  }
174 
178  friend bool operator<(const UsdObject &lhs, const UsdObject &rhs) {
179  return lhs.GetPath() < rhs.GetPath();
180  }
181 
182  // hash_value overload for std/boost hash.
183  USD_API
184  friend size_t hash_value(const UsdObject &obj);
185 
188  USD_API
189  UsdStageWeakPtr GetStage() const;
190 
194  SdfPath GetPath() const {
195  // Allow getting expired object paths.
196  if (!_proxyPrimPath.IsEmpty()) {
197  return _type == UsdTypePrim ?
198  _proxyPrimPath : _proxyPrimPath.AppendProperty(_propName);
199  }
200  else if (Usd_PrimDataConstPtr p = get_pointer(_prim)) {
201  return _type == UsdTypePrim ?
202  p->GetPath() : p->GetPath().AppendProperty(_propName);
203  }
204  return SdfPath();
205  }
206 
209  const SdfPath &GetPrimPath() const {
210  // Allow getting expired object paths.
211  if (!_proxyPrimPath.IsEmpty()) {
212  return _proxyPrimPath;
213  }
214  else if (Usd_PrimDataConstPtr p = get_pointer(_prim)) {
215  return p->GetPath();
216  }
217  return SdfPath::EmptyPath();
218  }
219 
222  inline UsdPrim GetPrim() const;
223 
229  const TfToken &GetName() const {
230  return _type == UsdTypePrim ? GetPrimPath().GetNameToken() : _propName;
231  }
232 
236  template <class T>
237  T As() const {
238  // compile-time type assertion provided by invoking Is<T>().
239  return Is<T>() ? T(_type, _prim, _proxyPrimPath, _propName) : T();
240  }
241 
247  template <class T>
248  bool Is() const {
249  static_assert(std::is_base_of<UsdObject, T>::value,
250  "Provided type T must derive from or be UsdObject");
251  return UsdIsConvertible(_type, _Detail::GetObjType<T>::Value);
252  }
253 
259  USD_API
260  std::string GetDescription() const;
261 
262  // --------------------------------------------------------------------- //
264  // --------------------------------------------------------------------- //
265 
266 
267  // --------------------------------------------------------------------- //
270  // --------------------------------------------------------------------- //
271 
286  template<typename T>
287  bool GetMetadata(const TfToken& key, T* value) const;
291  USD_API
292  bool GetMetadata(const TfToken& key, VtValue* value) const;
293 
300  template<typename T>
301  bool SetMetadata(const TfToken& key, const T& value) const;
303  USD_API
304  bool SetMetadata(const TfToken& key, const VtValue& value) const;
305 
315  USD_API
316  bool ClearMetadata(const TfToken& key) const;
317 
321  USD_API
322  bool HasMetadata(const TfToken& key) const;
323 
327  USD_API
328  bool HasAuthoredMetadata(const TfToken& key) const;
329 
345  template <class T>
347  const TfToken& key, const TfToken &keyPath, T *value) const;
349  USD_API
351  const TfToken& key, const TfToken &keyPath, VtValue *value) const;
352 
360  template<typename T>
362  const TfToken& key, const TfToken &keyPath, const T& value) const;
364  USD_API
366  const TfToken& key, const TfToken &keyPath, const VtValue& value) const;
367 
375  USD_API
377  const TfToken& key, const TfToken& keyPath) const;
378 
385  USD_API
386  bool HasMetadataDictKey(
387  const TfToken& key, const TfToken &keyPath) const;
388 
395  USD_API
397  const TfToken& key, const TfToken &keyPath) const;
398 
405  USD_API
406  UsdMetadataValueMap GetAllMetadata() const;
407 
414  USD_API
415  UsdMetadataValueMap GetAllAuthoredMetadata() const;
416 
417  // --------------------------------------------------------------------- //
419  // --------------------------------------------------------------------- //
420 
421  // --------------------------------------------------------------------- //
424  // --------------------------------------------------------------------- //
425 
440  USD_API
441  bool IsHidden() const;
442 
445  USD_API
446  bool SetHidden(bool hidden) const;
447 
449  USD_API
450  bool ClearHidden() const;
451 
457  USD_API
458  bool HasAuthoredHidden() const;
459 
478  USD_API
479  VtDictionary GetCustomData() const;
480 
486  USD_API
487  VtValue GetCustomDataByKey(const TfToken &keyPath) const;
488 
491  USD_API
492  void SetCustomData(const VtDictionary &customData) const;
493 
497  USD_API
498  void SetCustomDataByKey(const TfToken &keyPath, const VtValue &value) const;
499 
503  USD_API
504  void ClearCustomData() const;
505 
510  USD_API
511  void ClearCustomDataByKey(const TfToken &keyPath) const;
512 
515  USD_API
516  bool HasCustomData() const;
517 
522  USD_API
523  bool HasCustomDataKey(const TfToken &keyPath) const;
524 
527  USD_API
528  bool HasAuthoredCustomData() const;
529 
534  USD_API
535  bool HasAuthoredCustomDataKey(const TfToken &keyPath) const;
536 
550  USD_API
551  VtDictionary GetAssetInfo() const;
552 
558  USD_API
559  VtValue GetAssetInfoByKey(const TfToken &keyPath) const;
560 
563  USD_API
564  void SetAssetInfo(const VtDictionary &customData) const;
565 
569  USD_API
570  void SetAssetInfoByKey(const TfToken &keyPath, const VtValue &value) const;
571 
575  USD_API
576  void ClearAssetInfo() const;
577 
582  USD_API
583  void ClearAssetInfoByKey(const TfToken &keyPath) const;
584 
587  USD_API
588  bool HasAssetInfo() const;
589 
594  USD_API
595  bool HasAssetInfoKey(const TfToken &keyPath) const;
596 
599  USD_API
600  bool HasAuthoredAssetInfo() const;
601 
606  USD_API
607  bool HasAuthoredAssetInfoKey(const TfToken &keyPath) const;
608 
612  USD_API
613  std::string GetDocumentation() const;
614 
616  USD_API
617  bool SetDocumentation(const std::string& doc) const;
618 
621  USD_API
622  bool ClearDocumentation() const;
623 
626  USD_API
627  bool HasAuthoredDocumentation() const;
628 
632  USD_API
633  std::string GetDisplayName() const;
634 
640  USD_API
641  bool SetDisplayName(const std::string& name) const;
642 
645  USD_API
646  bool ClearDisplayName() const;
647 
650  USD_API
651  bool HasAuthoredDisplayName() const;
652 
653  // --------------------------------------------------------------------- //
655  // --------------------------------------------------------------------- //
656 
657  // XXX: This method can and probably should move to UsdProperty
658  static char GetNamespaceDelimiter()
659  { return SdfPathTokens->namespaceDelimiter.GetText()[0]; }
660 
661 private:
662  template <class T>
663  bool _GetMetadataImpl(const TfToken& key,
664  T* value,
665  const TfToken &keyPath=TfToken()) const;
666 
667  bool _GetMetadataImpl(const TfToken& key,
668  VtValue* value,
669  const TfToken &keyPath=TfToken()) const;
670 
671  template <class T>
672  bool _SetMetadataImpl(const TfToken& key,
673  const T& value,
674  const TfToken &keyPath=TfToken()) const;
675 
676  bool _SetMetadataImpl(const TfToken& key,
677  const VtValue& value,
678  const TfToken &keyPath=TfToken()) const;
679 
680 protected:
681  template <class Derived> struct _Null {};
682 
683  // Private constructor for null dervied types.
684  template <class Derived>
685  explicit UsdObject(_Null<Derived>)
686  : _type(_Detail::GetObjType<Derived>::Value) {}
687 
688  // Private constructor for UsdPrim.
689  UsdObject(const Usd_PrimDataHandle &prim,
690  const SdfPath &proxyPrimPath)
691  : _type(UsdTypePrim)
692  , _prim(prim)
693  , _proxyPrimPath(proxyPrimPath)
694  {
695  TF_VERIFY(!_prim || _prim->GetPath() != _proxyPrimPath);
696  }
697 
698  // Private constructor for UsdAttribute/UsdRelationship.
699  UsdObject(UsdObjType objType,
700  const Usd_PrimDataHandle &prim,
701  const SdfPath &proxyPrimPath,
702  const TfToken &propName)
703  : _type(objType)
704  , _prim(prim)
705  , _proxyPrimPath(proxyPrimPath)
706  , _propName(propName)
707  {
708  TF_VERIFY(!_prim || _prim->GetPath() != _proxyPrimPath);
709  }
710 
711  // Return the stage this object belongs to.
712  UsdStage *_GetStage() const { return _prim->GetStage(); }
713 
714  // Return this object's defining spec type.
715  USD_API
716  SdfSpecType _GetDefiningSpecType() const;
717 
718  // Helper for subclasses: return held prim data.
719  const Usd_PrimDataHandle &_Prim() const { return _prim; }
720 
721  // Helper for subclasses: return held property name.
722  const TfToken &_PropName() const { return _propName; }
723 
724  // Helper for subclasses: return held proxy prim path.
725  const SdfPath &_ProxyPrimPath() const { return _proxyPrimPath; }
726 
727 private:
728  // Helper for the above helper, and also for GetDescription()
729  std::string _GetObjectDescription(const std::string &preface) const;
730 
731  friend class UsdStage;
732 
733  friend UsdObjType Usd_GetObjType(const UsdObject &obj) {
734  return obj._type;
735  }
736 
737  UsdObjType _type;
738  Usd_PrimDataHandle _prim;
739  SdfPath _proxyPrimPath;
740  TfToken _propName;
741 
742 };
743 
744 template<typename T>
745 inline
746 bool
747 UsdObject::GetMetadata(const TfToken& key, T* value) const
748 {
749  return _GetMetadataImpl(key, value);
750 }
751 
752 template<typename T>
753 inline
754 bool
755 UsdObject::SetMetadata(const TfToken& key, const T& value) const
756 {
757  return _SetMetadataImpl(key, value);
758 }
759 
760 template <typename T>
761 inline
762 bool
764  const TfToken &keyPath,
765  T *value) const
766 {
767  return _GetMetadataImpl(key, value, keyPath);
768 }
769 
770 template <typename T>
771 inline
772 bool
774  const TfToken &keyPath,
775  const T& value) const
776 {
777  return _SetMetadataImpl(key, value, keyPath);
778 }
779 
780 template <class T>
781 bool
782 UsdObject::_GetMetadataImpl(const TfToken& key,
783  T* value,
784  const TfToken &keyPath) const
785 {
786  return _GetStage()->_GetMetadata(
787  *this, key, keyPath, /*useFallbacks=*/true, value);
788 }
789 
790 template <class T>
791 bool
792 UsdObject::_SetMetadataImpl(const TfToken& key,
793  const T& value,
794  const TfToken &keyPath) const
795 {
796  return _GetStage()->_SetMetadata(*this, key, keyPath, value);
797 }
798 
799 PXR_NAMESPACE_CLOSE_SCOPE
800 
801 #endif //PXR_USD_USD_OBJECT_H
USD_API void ClearCustomDataByKey(const TfToken &keyPath) const
Clear the authored opinion identified by keyPath in this object's customData dictionary at the curren...
bool SetMetadata(const TfToken &key, const T &value) const
Set metadatum key's value to value.
Definition: object.h:755
T As() const
Convert this UsdObject to another object type T if possible.
Definition: object.h:237
UsdObjType
Enum values to represent the various Usd object types.
Definition: object.h:49
USD_API bool SetDocumentation(const std::string &doc) const
Sets this object's documentation (metadata). Returns true on success.
#define TF_DECLARE_WEAK_PTRS(type)
Define standard weak pointer types.
Definition: declarePtrs.h:62
USD_API bool ClearHidden() const
Clears the opinion for "Hidden" at the current EditTarget.
USD_API bool HasCustomData() const
Return true if there are any authored or fallback opinions for this object's customData dictionary,...
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 bool SetDisplayName(const std::string &name) const
Sets this object's display name (metadata).
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...
bool UsdIsSubtype(UsdObjType baseType, UsdObjType subType)
Return true if subType is the same as or a subtype of baseType, false otherwise.
Definition: object.h:84
A map with string keys and VtValue values.
Definition: dictionary.h:63
USD_API std::string GetDisplayName() const
Return this object's display name (metadata).
USD_API bool SetHidden(bool hidden) const
Sets the value of the 'hidden' metadata field.
friend bool operator<(const UsdObject &lhs, const UsdObject &rhs)
Less-than operator.
Definition: object.h:178
USD_API bool ClearDocumentation() const
Clears this object's documentation (metadata) in the current EditTarget (only).
The outermost container for scene description, which owns and presents composed prims as a scenegraph...
Definition: stage.h:147
USD_API bool HasAuthoredCustomData() const
Return true if there are any authored opinions (excluding fallback) for this object's customData dict...
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.
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:773
USD_API void SetAssetInfo(const VtDictionary &customData) const
Author this object's assetInfo dictionary to assetInfo at the current EditTarget.
Scenegraph object for authoring and retrieving numeric, string, and array valued data,...
Definition: attribute.h:176
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 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 bool ClearMetadataByDictKey(const TfToken &key, const TfToken &keyPath) const
Clear any authored value identified by key and keyPath at the current EditTarget.
bool UsdIsConcrete(UsdObjType type)
Return true if type is a concrete object type, namely one of Prim, Attribute, or Relationship.
Definition: object.h:99
USD_API std::string GetDocumentation() const
Return this object's documentation (metadata).
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 VtValue GetCustomDataByKey(const TfToken &keyPath) const
Return the element identified by keyPath in this object's composed customData dictionary.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
USD_API bool ClearMetadata(const TfToken &key) const
Clears the authored key's value at the current EditTarget, returning false on error.
USD_API void ClearAssetInfoByKey(const TfToken &keyPath) const
Clear the authored opinion identified by keyPath in this object's assetInfo dictionary at the current...
USD_API bool ClearDisplayName() const
Clears this object's display name (metadata) in the current EditTarget (only).
USD_API VtValue GetAssetInfoByKey(const TfToken &keyPath) const
Return the element identified by keyPath in this object's composed assetInfo dictionary.
USD_API VtDictionary GetAssetInfo() const
Return this object's composed assetInfo dictionary.
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.
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:283
USD_API void ClearAssetInfo() const
Clear the authored opinion for this object's assetInfo dictionary at the current EditTarget.
USD_API bool IsHidden() const
Gets the value of the 'hidden' metadata field, false if not authored.
Base class for Usd scenegraph objects, providing common API.
Definition: object.h:130
USD_API std::string GetDescription() const
Return a string that provides a brief summary description of the object.
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a "Prim" as ...
Definition: prim.h:134
USD_API bool HasAuthoredAssetInfo() const
Return true if there are any authored opinions (excluding fallback) for this object's assetInfo dicti...
SDF_API const TfToken & GetNameToken() const
Returns the name of the prim, property or relational attribute identified by the path,...
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
bool GetMetadata(const TfToken &key, T *value) const
Resolve the requested metadatum named key into value, returning true on success.
Definition: object.h:747
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 VtDictionary GetCustomData() const
Return this object's composed customData dictionary.
A UsdRelationship creates dependencies between scenegraph objects by allowing a prim to target other ...
Definition: relationship.h:128
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 HasAssetInfo() const
Return true if there are any authored or fallback opinions for this object's assetInfo dictionary,...
bool UsdIsConvertible(UsdObjType from, UsdObjType to)
Return true if from is convertible to to, false otherwise.
Definition: object.h:92
Base class for UsdAttribute and UsdRelationship scenegraph objects.
Definition: property.h:55
SdfSpecType
An enum that specifies the type of an object.
Definition: types.h:91
USD_API UsdMetadataValueMap GetAllMetadata() const
Resolve and return all metadata (including both authored and fallback values) on this object,...
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:209
const TfToken & GetName() const
Return the full name of this object, i.e.
Definition: object.h:229
UsdObject()
Default constructor produces an invalid object.
Definition: object.h:133
SDF_API SdfPath AppendProperty(TfToken const &propName) const
Creates a path by appending an element for propName to this path.
friend bool operator==(const UsdObject &lhs, const UsdObject &rhs)
Equality comparison.
Definition: object.h:162
friend bool operator!=(const UsdObject &lhs, const UsdObject &rhs)
Inequality comparison.
Definition: object.h:171
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 bool HasAuthoredHidden() const
Returns true if hidden was explicitly authored and GetMetadata() will return a meaningful value for H...
USD_API UsdMetadataValueMap GetAllAuthoredMetadata() const
Resolve and return all user-authored metadata on this object, sorted lexicographically.
USD_API bool HasAuthoredDisplayName() const
Returns true if displayName was explicitly authored and GetMetadata() will return a meaningful value ...
bool IsValid() const
Return true if this is a valid object, false otherwise.
Definition: object.h:141
USD_API bool HasAuthoredDocumentation() const
Returns true if documentation was explicitly authored and GetMetadata() will return a meaningful valu...
bool IsEmpty() const noexcept
Returns true if this is the empty path (SdfPath::EmptyPath()).
Definition: path.h:419
static SDF_API const SdfPath & EmptyPath()
The empty path value, equivalent to SdfPath().
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:763
USD_API void ClearCustomData() const
Clear the authored opinion for this object's customData dictionary at the current EditTarget.
UsdPrim GetPrim() const
Return this object if it is a prim, otherwise return this object's nearest owning prim.
Definition: prim.h:2534
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:166
USD_API void SetCustomData(const VtDictionary &customData) const
Author this object's customData dictionary to customData 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...
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:194
bool Is() const
Return true if this object is convertible to T.
Definition: object.h:248