This document is for a version of USD that is under development. See this page for the current release.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 UsdTimeCode time = _stageGlobals.GetTime();
46 if (time.IsNumeric()) {
47 time = UsdTimeCode(time.GetValue() + shutterOffset);
48 }
49 _usdAttrQuery.Get<T>(&result, time);
50 return result;
51 }
52
57 HdSampledDataSource::Time startTime,
58 HdSampledDataSource::Time endTime,
59 std::vector<HdSampledDataSource::Time> *outSampleTimes) override
60 {
61 UsdTimeCode time = _stageGlobals.GetTime();
62 if (!_usdAttrQuery.ValueMightBeTimeVarying() ||
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
109protected:
113
128 UsdImagingDataSourceAttribute(
129 const UsdAttribute &usdAttr,
130 const UsdImagingDataSourceStageGlobals &stageGlobals,
131 const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
132 const HdDataSourceLocator &timeVaryingFlagLocator =
134
136 UsdImagingDataSourceAttribute(
137 const UsdAttributeQuery &usdAttrQuery,
138 const UsdImagingDataSourceStageGlobals &stageGlobals,
139 const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
140 const HdDataSourceLocator &timeVaryingFlagLocator =
142
143 UsdAttributeQuery _usdAttrQuery;
144 const UsdImagingDataSourceStageGlobals &_stageGlobals;
145};
146
147// ----------------------------------------------------------------------------
148
149
153USDIMAGING_API
154HdSampledDataSourceHandle
155UsdImagingDataSourceAttributeNew(
156 const UsdAttribute &usdAttr,
157 const UsdImagingDataSourceStageGlobals &stageGlobals,
158 const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
159 const HdDataSourceLocator &timeVaryingFlagLocator =
161
163USDIMAGING_API
164HdSampledDataSourceHandle
165UsdImagingDataSourceAttributeNew(
166 const UsdAttributeQuery &usdAttrQuery,
167 const UsdImagingDataSourceStageGlobals &stageGlobals,
168 const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
169 const HdDataSourceLocator &timeVaryingFlagLocator =
171
172// ----------------------------------------------------------------------------
173
174
175template<typename T>
176inline void
177UsdImagingDataSourceAttribute_RecordObjectInStageGlobals(
178 const UsdImagingDataSourceStageGlobals *stageGlobals,
179 const SdfPath &objPath)
180{
181 // By default nothing to record.
182}
183
184template<>
185inline void
186UsdImagingDataSourceAttribute_RecordObjectInStageGlobals<SdfAssetPath>(
187 const UsdImagingDataSourceStageGlobals *stageGlobals,
188 const SdfPath &objPath)
189{
190 // Record asset path-valued attributes.
191 stageGlobals->FlagAsAssetPathDependent(objPath);
192}
193
194template<typename T>
195UsdImagingDataSourceAttribute<T>::UsdImagingDataSourceAttribute(
196 const UsdAttribute &usdAttr,
197 const UsdImagingDataSourceStageGlobals &stageGlobals,
198 const SdfPath &sceneIndexPath,
199 const HdDataSourceLocator &timeVaryingFlagLocator)
200 : UsdImagingDataSourceAttribute(
201 UsdAttributeQuery(usdAttr), stageGlobals,
202 sceneIndexPath, timeVaryingFlagLocator)
203{
204}
205
206template<typename T>
207UsdImagingDataSourceAttribute<T>::UsdImagingDataSourceAttribute(
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
226PXR_NAMESPACE_CLOSE_SCOPE
227
228#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:167
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:147