Loading...
Searching...
No Matches
dataSourceAttribute.h
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"
11#include "pxr/usd/usd/attributeQuery.h"
12#include "pxr/usdImaging/usdImaging/api.h"
13#include "pxr/usdImaging/usdImaging/dataSourceStageGlobals.h"
14#include "pxr/imaging/hd/dataSource.h"
15#include "pxr/imaging/hd/dataSourceTypeDefs.h"
16
17#include <algorithm>
18
19PXR_NAMESPACE_OPEN_SCOPE
20
25template <typename T>
26class UsdImagingDataSourceAttribute : public HdTypedSampledDataSource<T>
27{
28public:
29
30 HD_DECLARE_DATASOURCE(UsdImagingDataSourceAttribute<T>);
31
34 VtValue GetValue(HdSampledDataSource::Time shutterOffset) override
35 {
36 return VtValue(GetTypedValue(shutterOffset));
37 }
38
41 T GetTypedValue(HdSampledDataSource::Time shutterOffset) override
42 {
43 // Zero-initialization for numerical types.
44 T result{};
45 if (!_usdAttrQuery) {
46 return result;
47 }
48 UsdTimeCode time = _stageGlobals.GetTime();
49 if (time.IsNumeric()) {
50 time = UsdTimeCode(time.GetValue() + shutterOffset);
51 }
52 _usdAttrQuery.Get<T>(&result, time);
53 return result;
54 }
55
60 HdSampledDataSource::Time startTime,
61 HdSampledDataSource::Time endTime,
62 std::vector<HdSampledDataSource::Time> *outSampleTimes) override
63 {
64 if (!_usdAttrQuery) {
65 return false;
66 }
67
68 UsdTimeCode time = _stageGlobals.GetTime();
69 if (!_usdAttrQuery.ValueMightBeTimeVarying() ||
70 !time.IsNumeric()) {
71 return false;
72 }
73
74 GfInterval interval(
75 time.GetValue() + startTime,
76 time.GetValue() + endTime);
77 std::vector<double> timeSamples;
78
79 // Start with the times that fall within the interval
80 _usdAttrQuery.GetTimeSamplesInInterval(interval, &timeSamples);
81 // Add bracketing sample times for the leading and trailing edges of the
82 // interval.
83 double first, ignore, last;
84 bool hasFirst, hasLast;
85 // If hasFirst/hasLast comes back false for an edge, or if both the left and
86 // right bracketing times for the edge are the same, it means there's no
87 // bracketing sample time anywhere beyond that edge, so we fall back to the
88 // interval's edge.
89 _usdAttrQuery.GetBracketingTimeSamples(interval.GetMin(), &first, &ignore, &hasFirst);
90 if (!hasFirst || first == ignore) {
91 first = interval.GetMin();
92 }
93 _usdAttrQuery.GetBracketingTimeSamples(interval.GetMax(), &ignore, &last, &hasLast);
94 if (!hasLast || last == ignore ) {
95 last = interval.GetMax();
96 }
97 // Add the bracketing sample times only if they actually fall outside the
98 // interval. This maintains ordering and uniqueness.
99 if (timeSamples.empty() || first < timeSamples.front()) {
100 timeSamples.insert(timeSamples.begin(), first);
101 }
102 if (last > timeSamples.back()) {
103 timeSamples.insert(timeSamples.end(), last);
104 }
105
106 // We need to convert the time array because usd uses double and
107 // hydra (and prman) use float :/.
108 outSampleTimes->resize(timeSamples.size());
109 for (size_t i = 0; i < timeSamples.size(); ++i) {
110 (*outSampleTimes)[i] = timeSamples[i] - time.GetValue();
111 }
112
113 return outSampleTimes->size() > 1;
114 }
115
116protected:
120
135 UsdImagingDataSourceAttribute(
136 const UsdAttribute &usdAttr,
137 const UsdImagingDataSourceStageGlobals &stageGlobals,
138 const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
139 const HdDataSourceLocator &timeVaryingFlagLocator =
141
143 UsdImagingDataSourceAttribute(
144 const UsdAttributeQuery &usdAttrQuery,
145 const UsdImagingDataSourceStageGlobals &stageGlobals,
146 const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
147 const HdDataSourceLocator &timeVaryingFlagLocator =
149
150 UsdAttributeQuery _usdAttrQuery;
151 const UsdImagingDataSourceStageGlobals &_stageGlobals;
152};
153
154// ----------------------------------------------------------------------------
155
156
160USDIMAGING_API
161HdSampledDataSourceHandle
162UsdImagingDataSourceAttributeNew(
163 const UsdAttribute &usdAttr,
164 const UsdImagingDataSourceStageGlobals &stageGlobals,
165 const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
166 const HdDataSourceLocator &timeVaryingFlagLocator =
168
170USDIMAGING_API
171HdSampledDataSourceHandle
172UsdImagingDataSourceAttributeNew(
173 const UsdAttributeQuery &usdAttrQuery,
174 const UsdImagingDataSourceStageGlobals &stageGlobals,
175 const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
176 const HdDataSourceLocator &timeVaryingFlagLocator =
178
179// ----------------------------------------------------------------------------
180
181
182template<typename T>
183inline void
184UsdImagingDataSourceAttribute_RecordObjectInStageGlobals(
185 const UsdImagingDataSourceStageGlobals *stageGlobals,
186 const SdfPath &objPath)
187{
188 // By default nothing to record.
189}
190
191template<>
192inline void
193UsdImagingDataSourceAttribute_RecordObjectInStageGlobals<SdfAssetPath>(
194 const UsdImagingDataSourceStageGlobals *stageGlobals,
195 const SdfPath &objPath)
196{
197 // Record asset path-valued attributes.
198 stageGlobals->FlagAsAssetPathDependent(objPath);
199}
200
201template<typename T>
202UsdImagingDataSourceAttribute<T>::UsdImagingDataSourceAttribute(
203 const UsdAttribute &usdAttr,
204 const UsdImagingDataSourceStageGlobals &stageGlobals,
205 const SdfPath &sceneIndexPath,
206 const HdDataSourceLocator &timeVaryingFlagLocator)
207 : UsdImagingDataSourceAttribute(
208 UsdAttributeQuery(usdAttr), stageGlobals,
209 sceneIndexPath, timeVaryingFlagLocator)
210{
211}
212
213template<typename T>
214UsdImagingDataSourceAttribute<T>::UsdImagingDataSourceAttribute(
215 const UsdAttributeQuery &usdAttrQuery,
216 const UsdImagingDataSourceStageGlobals &stageGlobals,
217 const SdfPath &sceneIndexPath,
218 const HdDataSourceLocator &timeVaryingFlagLocator)
219 : _usdAttrQuery(usdAttrQuery)
220 , _stageGlobals(stageGlobals)
221{
222 if (!timeVaryingFlagLocator.IsEmpty()) {
223 if (_usdAttrQuery.ValueMightBeTimeVarying()) {
224 _stageGlobals.FlagAsTimeVarying(
225 sceneIndexPath, timeVaryingFlagLocator);
226 }
227 }
228
229 UsdImagingDataSourceAttribute_RecordObjectInStageGlobals<T>(
230 &_stageGlobals, usdAttrQuery.GetAttribute().GetPath());
231}
232
233PXR_NAMESPACE_CLOSE_SCOPE
234
235#endif // PXR_USD_IMAGING_USD_IMAGING_DATA_SOURCE_ATTRIBUTE_H
A basic mathematical interval class.
Definition: interval.h:33
Represents an object that can identify the location of a data source.
static HD_API const HdDataSourceLocator & EmptyLocator()
Returns a common empty locator.
virtual bool GetContributingSampleTimesForInterval(Time startTime, Time endTime, std::vector< Time > *outSampleTimes)=0
Given a shutter window of interest (startTime and endTime relative to the current frame),...
virtual VtValue GetValue(Time shutterOffset)=0
Returns the value of this data source at frame-relative time shutterOffset.
A datasource representing a concretely-typed sampled value.
Definition: dataSource.h:193
virtual T GetTypedValue(Time shutterOffset)=0
Returns the value of this data source at frame-relative time shutterOffset, as type T.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:274
static SDF_API const SdfPath & EmptyPath()
The empty path value, equivalent to SdfPath().
Scenegraph object for authoring and retrieving numeric, string, and array valued data,...
Definition: attribute.h:183
Object for efficiently making repeated queries for attribute values.
USD_API const UsdAttribute & GetAttribute() const
Return the attribute associated with this query.
This class is used as a context object with global stage information, that gets passed down to dataso...
virtual void FlagAsAssetPathDependent(const SdfPath &usdPath) const =0
Flags the object at usdPath as dependent on an asset path.
SdfPath GetPath() const
Return the complete scene path to this object on its UsdStage, which may (UsdPrim) or may not (all ot...
Definition: object.h:186
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition: timeCode.h:72
double GetValue() const
Return the numeric value for this time.
Definition: timeCode.h:157
bool IsNumeric() const
Return true if this time represents a numeric value, false otherwise.
Definition: timeCode.h:151
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:90