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 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_IMAGING_USD_IMAGING_DATA_SOURCE_ATTRIBUTE_H
25 #define PXR_USD_IMAGING_USD_IMAGING_DATA_SOURCE_ATTRIBUTE_H
26 
27 #include "pxr/usd/usd/attribute.h"
33 
35 
36 /// \class UsdImagingDataSourceAttribute<T>
37 ///
38 /// A data source that represents a USD Attribute
39 ///
40 template <typename T>
42 {
43 public:
44 
46 
47  /// Returns the VtValue of this attribute at a given \p shutterOffset
48  ///
49  VtValue GetValue(HdSampledDataSource::Time shutterOffset) override
50  {
51  return VtValue(GetTypedValue(shutterOffset));
52  }
53 
54  /// Returns the extracted T value of the attribute at \p shutterOffset
55  ///
56  T GetTypedValue(HdSampledDataSource::Time shutterOffset) override
57  {
58  // Zero-initialization for numerical types.
59  T result{};
60  UsdTimeCode time = _stageGlobals.GetTime();
61  if (time.IsNumeric()) {
62  time = UsdTimeCode(time.GetValue() + shutterOffset);
63  }
64  _usdAttrQuery.Get<T>(&result, time);
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  UsdTimeCode time = _stageGlobals.GetTime();
77  if (!_usdAttrQuery.ValueMightBeTimeVarying() ||
78  !time.IsNumeric()) {
79  return false;
80  }
81 
82  GfInterval interval(
83  time.GetValue() + startTime,
84  time.GetValue() + endTime);
85  std::vector<double> timeSamples;
86  _usdAttrQuery.GetTimeSamplesInInterval(interval, &timeSamples);
87 
88  // Add boundary timesamples, if necessary.
89  if (timeSamples.empty() || timeSamples[0] > interval.GetMin()) {
90  timeSamples.insert(timeSamples.begin(), interval.GetMin());
91  }
92  if (timeSamples.back() < interval.GetMax()) {
93  timeSamples.push_back(interval.GetMax());
94  }
95 
96  // We need to convert the time array because usd uses double and
97  // hydra (and prman) use float :/.
98  outSampleTimes->resize(timeSamples.size());
99  for (size_t i = 0; i < timeSamples.size(); ++i) {
100  (*outSampleTimes)[i] = timeSamples[i] - time.GetValue();
101  }
102 
103  return true;
104  }
105 
106 private:
107 
108  /// Constructs a new UsdImagingDataSourceAttribute for the given \p usdAttr
109  ///
110  /// \p stageGlobals represents the context object for the UsdStage with
111  /// which to evaluate this attribute data source.
112  ///
113  /// \p timeVaryingFlagLocator represents the locator that should be dirtied
114  /// when time changes, if this attribute is time varying. An empty locator
115  /// means that this attribute isn't tracked for time varyingness. This is
116  /// distinct from the attribute name, say, because the attribute name
117  /// may not correspond to a meaningful Hydra dirty locator. It's the
118  /// responsibility of whoever is instantiating this data source to know the
119  /// meaning of this attribute to Hydra.
120  ///
121  /// Note: client code is calling the constructor via static New.
123  const UsdAttribute &usdAttr,
124  const UsdImagingDataSourceStageGlobals &stageGlobals,
125  const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
126  const HdDataSourceLocator &timeVaryingFlagLocator =
128 
129  /// Constructor override taking an attribute query.
131  const UsdAttributeQuery &usdAttrQuery,
132  const UsdImagingDataSourceStageGlobals &stageGlobals,
133  const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
134  const HdDataSourceLocator &timeVaryingFlagLocator =
136 
137 private:
138  UsdAttributeQuery _usdAttrQuery;
139  const UsdImagingDataSourceStageGlobals &_stageGlobals;
140 };
141 
142 // ----------------------------------------------------------------------------
143 
144 
145 /// Returns an instance of UsdImagingDataSourceAttribute with a given T
146 /// inferred from the usd attribute's sdf type
147 ///
149 HdSampledDataSourceHandle
151  const UsdAttribute &usdAttr,
152  const UsdImagingDataSourceStageGlobals &stageGlobals,
153  const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
154  const HdDataSourceLocator &timeVaryingFlagLocator =
156 
157 /// Override taking an attribute query
159 HdSampledDataSourceHandle
161  const UsdAttributeQuery &usdAttrQuery,
162  const UsdImagingDataSourceStageGlobals &stageGlobals,
163  const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
164  const HdDataSourceLocator &timeVaryingFlagLocator =
166 
167 // ----------------------------------------------------------------------------
168 
169 template<typename T>
171  const UsdAttribute &usdAttr,
172  const UsdImagingDataSourceStageGlobals &stageGlobals,
173  const SdfPath &sceneIndexPath,
174  const HdDataSourceLocator &timeVaryingFlagLocator)
175  : _usdAttrQuery(usdAttr)
176  , _stageGlobals(stageGlobals)
177 {
178  if (!timeVaryingFlagLocator.IsEmpty()) {
179  if (_usdAttrQuery.ValueMightBeTimeVarying()) {
180  _stageGlobals.FlagAsTimeVarying(
181  sceneIndexPath, timeVaryingFlagLocator);
182  }
183  }
184 }
185 
186 template<typename T>
188  const UsdAttributeQuery &usdAttrQuery,
189  const UsdImagingDataSourceStageGlobals &stageGlobals,
190  const SdfPath &sceneIndexPath,
191  const HdDataSourceLocator &timeVaryingFlagLocator)
192  : _usdAttrQuery(usdAttrQuery)
193  , _stageGlobals(stageGlobals)
194 {
195  if (!timeVaryingFlagLocator.IsEmpty()) {
196  if (_usdAttrQuery.ValueMightBeTimeVarying()) {
197  _stageGlobals.FlagAsTimeVarying(
198  sceneIndexPath, timeVaryingFlagLocator);
199  }
200  }
201 }
202 
204 
205 #endif // PXR_USD_IMAGING_USD_IMAGING_DATA_SOURCE_ATTRIBUTE_H
bool Get(T *value, UsdTimeCode time=UsdTimeCode::Default()) const
T GetTypedValue(HdSampledDataSource::Time shutterOffset) override
#define USDIMAGING_API
Definition: api.h:40
double GetValue() const
Definition: timeCode.h:151
GT_API const UT_StringHolder time
**But if you need a result
Definition: thread.h:613
virtual UsdTimeCode GetTime() const =0
Returns the current time represented in this instance.
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
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:291
bool IsNumeric() const
Definition: timeCode.h:145
USD_API bool GetTimeSamplesInInterval(const GfInterval &interval, std::vector< double > *times) const
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
virtual void FlagAsTimeVarying(const SdfPath &primPath, const HdDataSourceLocator &locator) const =0
Flags the given primPath as time varying at the given locator.
static HD_API const HdDataSourceLocator & EmptyLocator()
VtValue GetValue(HdSampledDataSource::Time shutterOffset) override
Definition: value.h:167