HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dataSourceAttribute.h
Go to the documentation of this file.
1 //
2 // Copyright 2020 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_IMAGING_USD_IMAGING_DATA_SOURCE_ATTRIBUTE_H
8 #define PXR_USD_IMAGING_USD_IMAGING_DATA_SOURCE_ATTRIBUTE_H
9 
10 #include "pxr/usd/usd/attribute.h"
16 
17 #include <algorithm>
18 
20 
21 /// \class UsdImagingDataSourceAttribute<T>
22 ///
23 /// A data source that represents a USD Attribute
24 ///
25 template <typename T>
27 {
28 public:
29 
31 
32  /// Returns the VtValue of this attribute at a given \p shutterOffset
33  ///
34  VtValue GetValue(HdSampledDataSource::Time shutterOffset) override
35  {
36  return VtValue(GetTypedValue(shutterOffset));
37  }
38 
39  /// Returns the extracted T value of the attribute at \p shutterOffset
40  ///
41  T GetTypedValue(HdSampledDataSource::Time shutterOffset) override
42  {
43  // Zero-initialization for numerical types.
44  T result{};
45  if (!_usdAttrQuery) {
46  return result;
47  }
49  if (time.IsNumeric()) {
50  time = UsdTimeCode(time.GetValue() + shutterOffset);
51  }
52 
53  bool valueRetrieved = _usdAttrQuery.Get<T>(&result, time);
54  if (valueRetrieved) {
55  return result;
56  }
57 
58  // No value of expected type T could be retrieved. Try schema default
59  // and if that fails return zero-initialized result.
60  valueRetrieved = _usdAttrQuery.GetFallbackValue<T>(&result);
61  if (valueRetrieved) {
62  return result;
63  }
64 
65  return result;
66  }
67 
68  /// Fills the \p outSampleTimes with the times between \p startTime and
69  /// \p endTime that have valid sample data and returns \c true.
70  ///
72  HdSampledDataSource::Time startTime,
74  std::vector<HdSampledDataSource::Time> *outSampleTimes) override
75  {
76  if (!_usdAttrQuery) {
77  return false;
78  }
79 
82  !time.IsNumeric()) {
83  return false;
84  }
85 
86  GfInterval interval(
87  time.GetValue() + startTime,
88  time.GetValue() + endTime);
89  std::vector<double> timeSamples;
90 
91  // Start with the times that fall within the interval
92  _usdAttrQuery.GetTimeSamplesInInterval(interval, &timeSamples);
93  // Add bracketing sample times for the leading and trailing edges of the
94  // interval.
95  double first, ignore, last;
96  bool hasFirst, hasLast;
97  // If hasFirst/hasLast comes back false for an edge, or if both the left and
98  // right bracketing times for the edge are the same, it means there's no
99  // bracketing sample time anywhere beyond that edge, so we fall back to the
100  // interval's edge.
101  _usdAttrQuery.GetBracketingTimeSamples(interval.GetMin(), &first, &ignore, &hasFirst);
102  if (!hasFirst || first == ignore) {
103  first = interval.GetMin();
104  }
105  _usdAttrQuery.GetBracketingTimeSamples(interval.GetMax(), &ignore, &last, &hasLast);
106  if (!hasLast || last == ignore ) {
107  last = interval.GetMax();
108  }
109  // Add the bracketing sample times only if they actually fall outside the
110  // interval. This maintains ordering and uniqueness.
111  if (timeSamples.empty() || first < timeSamples.front()) {
112  timeSamples.insert(timeSamples.begin(), first);
113  }
114  if (last > timeSamples.back()) {
115  timeSamples.insert(timeSamples.end(), last);
116  }
117 
118  // We need to convert the time array because usd uses double and
119  // hydra (and prman) use float :/.
120  outSampleTimes->resize(timeSamples.size());
121  for (size_t i = 0; i < timeSamples.size(); ++i) {
122  (*outSampleTimes)[i] = timeSamples[i] - time.GetValue();
123  }
124 
125  return outSampleTimes->size() > 1;
126  }
127 
128 protected:
129  /// Constructors and member data are protected instead of private
130  /// for use by a specialized subclass for accessing SdfAssetPath
131  /// attributes which need special handling for UDIMs.
132 
133  /// Constructs a new UsdImagingDataSourceAttribute for the given \p usdAttr
134  ///
135  /// \p stageGlobals represents the context object for the UsdStage with
136  /// which to evaluate this attribute data source.
137  ///
138  /// \p timeVaryingFlagLocator represents the locator that should be dirtied
139  /// when time changes, if this attribute is time varying. An empty locator
140  /// means that this attribute isn't tracked for time varyingness. This is
141  /// distinct from the attribute name, say, because the attribute name
142  /// may not correspond to a meaningful Hydra dirty locator. It's the
143  /// responsibility of whoever is instantiating this data source to know the
144  /// meaning of this attribute to Hydra.
145  ///
146  /// Note: client code is calling the constructor via static New.
148  const UsdAttribute &usdAttr,
149  const UsdImagingDataSourceStageGlobals &stageGlobals,
150  const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
151  const HdDataSourceLocator &timeVaryingFlagLocator =
153 
154  /// Constructor override taking an attribute query.
156  const UsdAttributeQuery &usdAttrQuery,
157  const UsdImagingDataSourceStageGlobals &stageGlobals,
158  const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
159  const HdDataSourceLocator &timeVaryingFlagLocator =
161 
164 };
165 
166 // ----------------------------------------------------------------------------
167 
168 
169 /// Returns an instance of UsdImagingDataSourceAttribute with a given T
170 /// inferred from the usd attribute's sdf type
171 ///
173 HdSampledDataSourceHandle
175  const UsdAttribute &usdAttr,
176  const UsdImagingDataSourceStageGlobals &stageGlobals,
177  const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
178  const HdDataSourceLocator &timeVaryingFlagLocator =
180 
181 /// Override taking an attribute query
183 HdSampledDataSourceHandle
185  const UsdAttributeQuery &usdAttrQuery,
186  const UsdImagingDataSourceStageGlobals &stageGlobals,
187  const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
188  const HdDataSourceLocator &timeVaryingFlagLocator =
190 
191 // ----------------------------------------------------------------------------
192 
193 
194 template<typename T>
195 inline void
197  const UsdImagingDataSourceStageGlobals *stageGlobals,
198  const SdfPath &objPath)
199 {
200  // By default nothing to record.
201 }
202 
203 template<>
204 inline void
206  const UsdImagingDataSourceStageGlobals *stageGlobals,
207  const SdfPath &objPath)
208 {
209  // Record asset path-valued attributes.
210  stageGlobals->FlagAsAssetPathDependent(objPath);
211 }
212 
213 template<typename T>
215  const UsdAttribute &usdAttr,
216  const UsdImagingDataSourceStageGlobals &stageGlobals,
217  const SdfPath &sceneIndexPath,
218  const HdDataSourceLocator &timeVaryingFlagLocator)
220  UsdAttributeQuery(usdAttr), stageGlobals,
221  sceneIndexPath, timeVaryingFlagLocator)
222 {
223 }
224 
225 template<typename T>
227  const UsdAttributeQuery &usdAttrQuery,
228  const UsdImagingDataSourceStageGlobals &stageGlobals,
229  const SdfPath &sceneIndexPath,
230  const HdDataSourceLocator &timeVaryingFlagLocator)
231  : _usdAttrQuery(usdAttrQuery)
232  , _stageGlobals(stageGlobals)
233 {
234  if (!timeVaryingFlagLocator.IsEmpty()) {
235  if (_usdAttrQuery.ValueMightBeTimeVarying()) {
236  _stageGlobals.FlagAsTimeVarying(
237  sceneIndexPath, timeVaryingFlagLocator);
238  }
239  }
240 
241  UsdImagingDataSourceAttribute_RecordObjectInStageGlobals<T>(
242  &_stageGlobals, usdAttrQuery.GetAttribute().GetPath());
243 }
244 
246 
247 #endif // PXR_USD_IMAGING_USD_IMAGING_DATA_SOURCE_ATTRIBUTE_H
GLint first
Definition: glcorearb.h:405
bool Get(T *value, UsdTimeCode time=UsdTimeCode::Default()) const
T GetTypedValue(HdSampledDataSource::Time shutterOffset) override
#define USDIMAGING_API
Definition: api.h:23
double GetValue() const
Definition: timeCode.h:157
GT_API const UT_StringHolder time
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
**But if you need a result
Definition: thread.h:622
virtual UsdTimeCode GetTime() const =0
Returns the current time represented in this instance.
const UsdImagingDataSourceStageGlobals & _stageGlobals
static SDF_API const SdfPath & EmptyPath()
The empty path value, equivalent to SdfPath().
USD_API bool ValueMightBeTimeVarying() const
bool GetContributingSampleTimesForInterval(HdSampledDataSource::Time startTime, HdSampledDataSource::Time endTime, std::vector< HdSampledDataSource::Time > *outSampleTimes) override
void ignore(T const &) VULKAN_HPP_NOEXCEPT
Definition: vulkan.hpp:6508
HD_DECLARE_DATASOURCE(UsdImagingDataSourceAttribute< T >)
USDIMAGING_API HdSampledDataSourceHandle UsdImagingDataSourceAttributeNew(const UsdAttribute &usdAttr, const UsdImagingDataSourceStageGlobals &stageGlobals, const SdfPath &sceneIndexPath=SdfPath::EmptyPath(), const HdDataSourceLocator &timeVaryingFlagLocator=HdDataSourceLocator::EmptyLocator())
bool GetFallbackValue(T *value) const
Definition: path.h:280
UsdImagingDataSourceAttribute(const UsdAttribute &usdAttr, const UsdImagingDataSourceStageGlobals &stageGlobals, const SdfPath &sceneIndexPath=SdfPath::EmptyPath(), const HdDataSourceLocator &timeVaryingFlagLocator=HdDataSourceLocator::EmptyLocator())
__hostdev__ uint64_t last(uint32_t i) const
Definition: NanoVDB.h:5976
bool IsNumeric() const
Definition: timeCode.h:151
SdfPath GetPath() const
Definition: object.h:187
USD_API bool GetTimeSamplesInInterval(const GfInterval &interval, std::vector< double > *times) const
void UsdImagingDataSourceAttribute_RecordObjectInStageGlobals< SdfAssetPath >(const UsdImagingDataSourceStageGlobals *stageGlobals, const SdfPath &objPath)
void UsdImagingDataSourceAttribute_RecordObjectInStageGlobals(const UsdImagingDataSourceStageGlobals *stageGlobals, const SdfPath &objPath)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
USD_API bool GetBracketingTimeSamples(double desiredTime, double *lower, double *upper, bool *hasTimeSamples) const
static HD_API const HdDataSourceLocator & EmptyLocator()
USD_API const UsdAttribute & GetAttribute() const
Return the attribute associated with this query.
VtValue GetValue(HdSampledDataSource::Time shutterOffset) override
Definition: value.h:89