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