HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
attributeQuery.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_ATTRIBUTE_QUERY_H
8 #define PXR_USD_USD_ATTRIBUTE_QUERY_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/usd/usd/api.h"
12 #include "pxr/usd/usd/attribute.h"
13 #include "pxr/usd/usd/common.h"
14 #include "pxr/usd/usd/prim.h"
17 #include "pxr/usd/usd/timeCode.h"
18 
19 #include "pxr/base/tf/token.h"
20 
21 #include <vector>
22 
24 
25 
26 /// \class UsdAttributeQuery
27 ///
28 /// Object for efficiently making repeated queries for attribute values.
29 ///
30 /// Retrieving an attribute's value at a particular time requires determining
31 /// the source of strongest opinion for that value. Often (i.e. unless the
32 /// attribute is affected by \ref Usd_Page_ValueClips "Value Clips") this
33 /// source does not vary over time. UsdAttributeQuery uses this fact to
34 /// speed up repeated value queries by caching the source information for an
35 /// attribute. It is safe to use a UsdAttributeQuery for any attribute - if
36 /// the attribute \em is affected by Value Clips, the performance gain will
37 /// just be less.
38 ///
39 /// \section UsdAttributeQuery_Resolve_targets Resolve targets
40 /// An attribute query can also be constructed for an attribute along with a
41 /// UsdResolveTarget. A resolve target allows value resolution to consider only
42 /// a subrange of the prim stack instead of the entirety of it. All of the methods
43 /// of an attribute query created with a resolve target will perform value
44 /// resolution within that resolve target. This can be useful for finding the
45 /// value of an attribute resolved up to a particular layer or for determining
46 /// if a value authored on layer would be overridden by a stronger opinion.
47 ///
48 /// \section UsdAttributeQuery_Thread_safety Thread safety
49 /// This object provides the basic thread-safety guarantee. Multiple threads
50 /// may call the value accessor functions simultaneously.
51 ///
52 /// \section UsdAttributeQuery_Invalidation Invalidation
53 /// This object does not listen for change notification. If a consumer is
54 /// holding on to a UsdAttributeQuery, it is their responsibility to dispose
55 /// of it in response to a resync change to the associated attribute.
56 /// Failing to do so may result in incorrect values or crashes due to
57 /// dereferencing invalid objects.
58 ///
60 {
61 public:
62  /// Construct an invalid query object.
63  USD_API
65 
66  /// Copy constructor.
67  USD_API
68  UsdAttributeQuery(const UsdAttributeQuery &other);
69 
70  /// Move constructor.
71  USD_API
72  UsdAttributeQuery(UsdAttributeQuery &&other) = default;
73 
74  /// Construct a new query for the attribute \p attr.
75  USD_API
76  explicit UsdAttributeQuery(const UsdAttribute& attr);
77 
78  /// Construct a new query for the attribute named \p attrName under
79  /// the prim \p prim.
80  USD_API
81  UsdAttributeQuery(const UsdPrim& prim, const TfToken& attrName);
82 
83  /// Construct a new query for the attribute \p attr with the given
84  /// resolve target \p resolveTarget.
85  ///
86  /// Note that a UsdResolveTarget is associated with a particular prim so
87  /// only resolve targets for the attribute's owning prim are allowed.
88  USD_API
89  UsdAttributeQuery(const UsdAttribute &attr,
90  const UsdResolveTarget &resolveTarget);
91 
92  /// Construct new queries for the attributes named in \p attrNames under
93  /// the prim \p prim. The objects in the returned vector will line up
94  /// 1-to-1 with \p attrNames.
95  USD_API
96  static std::vector<UsdAttributeQuery> CreateQueries(
97  const UsdPrim& prim, const TfTokenVector& attrNames);
98 
99  // --------------------------------------------------------------------- //
100  /// \name Query information
101  // --------------------------------------------------------------------- //
102 
103  /// @{
104 
105  /// Return the attribute associated with this query.
106  USD_API
107  const UsdAttribute& GetAttribute() const;
108 
109  /// Return true if this query is valid (i.e. it is associated with a
110  /// valid attribute), false otherwise.
111  bool IsValid() const {
112  return GetAttribute().IsValid();
113  }
114 
115 public:
116  /// Returns \c true if the query object is valid, \c false otherwise.
117  explicit operator bool() const {
118  return IsValid();
119  }
120 
121  /// Copy assignment.
122  USD_API
123  UsdAttributeQuery &operator=(const UsdAttributeQuery &other);
124 
125  /// Move assignment.
126  USD_API
127  UsdAttributeQuery &operator=(UsdAttributeQuery &&other) = default;
128 
129  /// @}
130 
131  // --------------------------------------------------------------------- //
132  /// \name Value & Time-Sample Accessors
133  // --------------------------------------------------------------------- //
134 
135  /// @{
136 
137  /// Perform value resolution to fetch the value of the attribute associated
138  /// with this query at the requested UsdTimeCode \p time.
139  ///
140  /// \sa UsdAttribute::Get
141  template <typename T>
144  "T must be an SdfValueType.");
145  return _Get(value, time);
146  }
147  /// \overload
148  /// Type-erased access, often not as efficient as typed access.
149  USD_API
150  bool Get(VtValue* value, UsdTimeCode time = UsdTimeCode::Default()) const;
151 
152  /// Populates a vector with authored sample times.
153  /// Returns false only on error.
154  //
155  /// Behaves identically to UsdAttribute::GetTimeSamples()
156  ///
157  /// \sa UsdAttributeQuery::GetTimeSamplesInInterval
158  USD_API
159  bool GetTimeSamples(std::vector<double>* times) const;
160 
161  /// Populates a vector with authored sample times in \p interval.
162  /// Returns false only on an error.
163  ///
164  /// Behaves identically to UsdAttribute::GetTimeSamplesInInterval()
165  USD_API
166  bool GetTimeSamplesInInterval(const GfInterval& interval,
167  std::vector<double>* times) const;
168 
169  /// Populates the given vector, \p times with the union of all the
170  /// authored sample times on all of the given attribute-query objects,
171  /// \p attrQueries.
172  ///
173  /// Behaves identically to UsdAttribute::GetUnionedTimeSamples()
174  ///
175  /// \return false if one or more attribute-queries in \p attrQueries are
176  /// invalid or if there's an error fetching time-samples for any of
177  /// the attribute-query objects.
178  ///
179  /// \sa UsdAttribute::GetUnionedTimeSamples
180  /// \sa UsdAttributeQuery::GetUnionedTimeSamplesInInterval
181  USD_API
182  static bool GetUnionedTimeSamples(
183  const std::vector<UsdAttributeQuery> &attrQueries,
184  std::vector<double> *times);
185 
186  /// Populates the given vector, \p times with the union of all the
187  /// authored sample times in the GfInterval, \p interval on all of the
188  /// given attribute-query objects, \p attrQueries.
189  ///
190  /// Behaves identically to UsdAttribute::GetUnionedTimeSamplesInInterval()
191  ///
192  /// \return false if one or more attribute-queries in \p attrQueries are
193  /// invalid or if there's an error fetching time-samples for any of
194  /// the attribute-query objects.
195  ///
196  /// \sa UsdAttribute::GetUnionedTimeSamplesInInterval
197  USD_API
199  const std::vector<UsdAttributeQuery> &attrQueries,
200  const GfInterval &interval,
201  std::vector<double> *times);
202 
203  /// Returns the number of time samples that have been authored.
204  ///
205  /// \sa UsdAttribute::GetNumTimeSamples
206  USD_API
207  size_t GetNumTimeSamples() const;
208 
209  /// Populate \a lower and \a upper with the next greater and lesser
210  /// value relative to the \a desiredTime.
211  ///
212  /// \sa UsdAttribute::GetBracketingTimeSamples
213  USD_API
214  bool GetBracketingTimeSamples(double desiredTime,
215  double* lower,
216  double* upper,
217  bool* hasTimeSamples) const;
218 
219  /// Return true if the attribute associated with this query has an
220  /// authored default value, authored time samples or a fallback value
221  /// provided by a registered schema.
222  ///
223  /// \sa UsdAttribute::HasValue
224  USD_API
225  bool HasValue() const;
226 
227  /// \deprecated This method is deprecated because it returns `true` even when
228  /// an attribute is blocked. Please use HasAuthoredValue() instead. If
229  /// you truly need to know whether the attribute has **any** authored
230  /// value opinions, *including blocks*, you can make the following query:
231  /// `query.GetAttribute().GetResolveInfo().HasAuthoredValueOpinion()`
232  ///
233  ///
234  /// Return true if this attribute has either an authored default value or
235  /// authored time samples.
236  USD_API
237  bool HasAuthoredValueOpinion() const;
238 
239  /// Return true if this attribute has either an authored default value or
240  /// authored time samples. If the attribute has been
241  /// \ref Usd_AttributeBlocking "blocked", then return `false`
242  /// \sa UsdAttribute::HasAuthoredValue()
243  USD_API
244  bool HasAuthoredValue() const;
245 
246  /// Return true if the attribute associated with this query has a
247  /// fallback value provided by a registered schema.
248  ///
249  /// \sa UsdAttribute::HasFallbackValue
250  USD_API
251  bool HasFallbackValue() const;
252 
253  /// Return true if it is possible, but not certain, that this attribute's
254  /// value changes over time, false otherwise.
255  ///
256  /// \sa UsdAttribute::ValueMightBeTimeVarying
257  USD_API
258  bool ValueMightBeTimeVarying() const;
259 
260  /// @}
261 
262 private:
263  void _Initialize();
264 
265  void _Initialize(const UsdResolveTarget &resolveTarget);
266 
267  template <typename T>
268  USD_API
269  bool _Get(T* value, UsdTimeCode time) const;
270 
271 private:
272  UsdAttribute _attr;
273  UsdResolveInfo _resolveInfo;
274  std::unique_ptr<UsdResolveTarget> _resolveTarget;
275 };
276 
278 
279 #endif // PXR_USD_USD_ATTRIBUTE_QUERY_H
std::string upper(string_view a)
Return an all-upper case version of a (locale-independent).
Definition: strutil.h:500
bool Get(T *value, UsdTimeCode time=UsdTimeCode::Default()) const
USD_API bool HasValue() const
static constexpr UsdTimeCode Default()
Definition: timeCode.h:113
#define USD_API
Definition: api.h:23
GT_API const UT_StringHolder time
GLsizei const GLfloat * value
Definition: glcorearb.h:824
USD_API size_t GetNumTimeSamples() const
USD_API bool HasAuthoredValueOpinion() const
USD_API UsdAttributeQuery & operator=(const UsdAttributeQuery &other)
Copy assignment.
OutGridT const XformOp bool bool
USD_API UsdAttributeQuery()
Construct an invalid query object.
USD_API bool HasFallbackValue() const
Definition: token.h:70
USD_API bool ValueMightBeTimeVarying() const
Definition: prim.h:116
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
static USD_API bool GetUnionedTimeSamplesInInterval(const std::vector< UsdAttributeQuery > &attrQueries, const GfInterval &interval, std::vector< double > *times)
USD_API bool GetTimeSamples(std::vector< double > *times) const
USD_API bool GetTimeSamplesInInterval(const GfInterval &interval, std::vector< double > *times) const
static USD_API bool GetUnionedTimeSamples(const std::vector< UsdAttributeQuery > &attrQueries, std::vector< double > *times)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
bool IsValid() const
Return true if this is a valid object, false otherwise.
Definition: object.h:126
USD_API bool HasAuthoredValue() const
std::string lower(string_view a)
Return an all-upper case version of a (locale-independent).
Definition: strutil.h:493
bool IsValid() const
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
USD_API bool GetBracketingTimeSamples(double desiredTime, double *lower, double *upper, bool *hasTimeSamples) const
USD_API const UsdAttribute & GetAttribute() const
Return the attribute associated with this query.
static USD_API std::vector< UsdAttributeQuery > CreateQueries(const UsdPrim &prim, const TfTokenVector &attrNames)
Definition: value.h:146