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{};
46  if (time.IsNumeric()) {
47  time = UsdTimeCode(time.GetValue() + shutterOffset);
48  }
50  return result;
51  }
52 
53  /// Fills the \p outSampleTimes with the times between \p startTime and
54  /// \p endTime that have valid sample data and returns \c true.
55  ///
57  HdSampledDataSource::Time startTime,
59  std::vector<HdSampledDataSource::Time> *outSampleTimes) override
60  {
63  !time.IsNumeric()) {
64  return false;
65  }
66 
67  GfInterval interval(
68  time.GetValue() + startTime,
69  time.GetValue() + endTime);
70  std::vector<double> timeSamples;
71 
72  // Start with the times that fall within the interval
73  _usdAttrQuery.GetTimeSamplesInInterval(interval, &timeSamples);
74  // Add bracketing sample times for the leading and trailing edges of the
75  // interval.
76  double first, ignore, last;
77  bool hasFirst, hasLast;
78  // If hasFirst/hasLast comes back false for an edge, or if both the left and
79  // right bracketing times for the edge are the same, it means there's no
80  // bracketing sample time anywhere beyond that edge, so we fall back to the
81  // interval's edge.
82  _usdAttrQuery.GetBracketingTimeSamples(interval.GetMin(), &first, &ignore, &hasFirst);
83  if (!hasFirst || first == ignore) {
84  first = interval.GetMin();
85  }
86  _usdAttrQuery.GetBracketingTimeSamples(interval.GetMax(), &ignore, &last, &hasLast);
87  if (!hasLast || last == ignore ) {
88  last = interval.GetMax();
89  }
90  // Add the bracketing sample times only if they actually fall outside the
91  // interval. This maintains ordering and uniqueness.
92  if (timeSamples.empty() || first < timeSamples.front()) {
93  timeSamples.insert(timeSamples.begin(), first);
94  }
95  if (last > timeSamples.back()) {
96  timeSamples.insert(timeSamples.end(), last);
97  }
98 
99  // We need to convert the time array because usd uses double and
100  // hydra (and prman) use float :/.
101  outSampleTimes->resize(timeSamples.size());
102  for (size_t i = 0; i < timeSamples.size(); ++i) {
103  (*outSampleTimes)[i] = timeSamples[i] - time.GetValue();
104  }
105 
106  return outSampleTimes->size() > 1;
107  }
108 
109 protected:
110  /// Constructors and member data are protected instead of private
111  /// for use by a specialized subclass for accessing SdfAssetPath
112  /// attributes which need special handling for UDIMs.
113 
114  /// Constructs a new UsdImagingDataSourceAttribute for the given \p usdAttr
115  ///
116  /// \p stageGlobals represents the context object for the UsdStage with
117  /// which to evaluate this attribute data source.
118  ///
119  /// \p timeVaryingFlagLocator represents the locator that should be dirtied
120  /// when time changes, if this attribute is time varying. An empty locator
121  /// means that this attribute isn't tracked for time varyingness. This is
122  /// distinct from the attribute name, say, because the attribute name
123  /// may not correspond to a meaningful Hydra dirty locator. It's the
124  /// responsibility of whoever is instantiating this data source to know the
125  /// meaning of this attribute to Hydra.
126  ///
127  /// Note: client code is calling the constructor via static New.
129  const UsdAttribute &usdAttr,
130  const UsdImagingDataSourceStageGlobals &stageGlobals,
131  const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
132  const HdDataSourceLocator &timeVaryingFlagLocator =
134 
135  /// Constructor override taking an attribute query.
137  const UsdAttributeQuery &usdAttrQuery,
138  const UsdImagingDataSourceStageGlobals &stageGlobals,
139  const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
140  const HdDataSourceLocator &timeVaryingFlagLocator =
142 
145 };
146 
147 // ----------------------------------------------------------------------------
148 
149 
150 /// Returns an instance of UsdImagingDataSourceAttribute with a given T
151 /// inferred from the usd attribute's sdf type
152 ///
154 HdSampledDataSourceHandle
156  const UsdAttribute &usdAttr,
157  const UsdImagingDataSourceStageGlobals &stageGlobals,
158  const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
159  const HdDataSourceLocator &timeVaryingFlagLocator =
161 
162 /// Override taking an attribute query
164 HdSampledDataSourceHandle
166  const UsdAttributeQuery &usdAttrQuery,
167  const UsdImagingDataSourceStageGlobals &stageGlobals,
168  const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
169  const HdDataSourceLocator &timeVaryingFlagLocator =
171 
172 // ----------------------------------------------------------------------------
173 
174 
175 template<typename T>
176 inline void
178  const UsdImagingDataSourceStageGlobals *stageGlobals,
179  const SdfPath &objPath)
180 {
181  // By default nothing to record.
182 }
183 
184 template<>
185 inline void
187  const UsdImagingDataSourceStageGlobals *stageGlobals,
188  const SdfPath &objPath)
189 {
190  // Record asset path-valued attributes.
191  stageGlobals->FlagAsAssetPathDependent(objPath);
192 }
193 
194 template<typename T>
196  const UsdAttribute &usdAttr,
197  const UsdImagingDataSourceStageGlobals &stageGlobals,
198  const SdfPath &sceneIndexPath,
199  const HdDataSourceLocator &timeVaryingFlagLocator)
201  UsdAttributeQuery(usdAttr), stageGlobals,
202  sceneIndexPath, timeVaryingFlagLocator)
203 {
204 }
205 
206 template<typename T>
208  const UsdAttributeQuery &usdAttrQuery,
209  const UsdImagingDataSourceStageGlobals &stageGlobals,
210  const SdfPath &sceneIndexPath,
211  const HdDataSourceLocator &timeVaryingFlagLocator)
212  : _usdAttrQuery(usdAttrQuery)
213  , _stageGlobals(stageGlobals)
214 {
215  if (!timeVaryingFlagLocator.IsEmpty()) {
216  if (_usdAttrQuery.ValueMightBeTimeVarying()) {
217  _stageGlobals.FlagAsTimeVarying(
218  sceneIndexPath, timeVaryingFlagLocator);
219  }
220  }
221 
222  UsdImagingDataSourceAttribute_RecordObjectInStageGlobals<T>(
223  &_stageGlobals, usdAttrQuery.GetAttribute().GetPath());
224 }
225 
227 
228 #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
**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())
Definition: path.h:273
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:186
USD_API bool GetTimeSamplesInInterval(const GfInterval &interval, std::vector< double > *times) const
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
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:146