Loading...
Searching...
No Matches
dataSourceAttribute.h
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"
28#include "pxr/usd/usd/attributeQuery.h"
29#include "pxr/usdImaging/usdImaging/api.h"
30#include "pxr/usdImaging/usdImaging/dataSourceStageGlobals.h"
31#include "pxr/imaging/hd/dataSource.h"
32#include "pxr/imaging/hd/dataSourceTypeDefs.h"
33
34PXR_NAMESPACE_OPEN_SCOPE
35
40template <typename T>
41class UsdImagingDataSourceAttribute : public HdTypedSampledDataSource<T>
42{
43public:
44
45 HD_DECLARE_DATASOURCE(UsdImagingDataSourceAttribute<T>);
46
49 VtValue GetValue(HdSampledDataSource::Time shutterOffset) override
50 {
51 return VtValue(GetTypedValue(shutterOffset));
52 }
53
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
72 HdSampledDataSource::Time startTime,
73 HdSampledDataSource::Time endTime,
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
106private:
107
122 UsdImagingDataSourceAttribute(
123 const UsdAttribute &usdAttr,
124 const UsdImagingDataSourceStageGlobals &stageGlobals,
125 const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
126 const HdDataSourceLocator &timeVaryingFlagLocator =
128
130 UsdImagingDataSourceAttribute(
131 const UsdAttributeQuery &usdAttrQuery,
132 const UsdImagingDataSourceStageGlobals &stageGlobals,
133 const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
134 const HdDataSourceLocator &timeVaryingFlagLocator =
136
137private:
138 UsdAttributeQuery _usdAttrQuery;
139 const UsdImagingDataSourceStageGlobals &_stageGlobals;
140};
141
142// ----------------------------------------------------------------------------
143
144
148USDIMAGING_API
149HdSampledDataSourceHandle
150UsdImagingDataSourceAttributeNew(
151 const UsdAttribute &usdAttr,
152 const UsdImagingDataSourceStageGlobals &stageGlobals,
153 const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
154 const HdDataSourceLocator &timeVaryingFlagLocator =
156
158USDIMAGING_API
159HdSampledDataSourceHandle
160UsdImagingDataSourceAttributeNew(
161 const UsdAttributeQuery &usdAttrQuery,
162 const UsdImagingDataSourceStageGlobals &stageGlobals,
163 const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
164 const HdDataSourceLocator &timeVaryingFlagLocator =
166
167// ----------------------------------------------------------------------------
168
169
170template<typename T>
171inline void
172UsdImagingDataSourceAttribute_RecordObjectInStageGlobals(
173 const UsdImagingDataSourceStageGlobals *stageGlobals,
174 const SdfPath &objPath)
175{
176 // By default nothing to record.
177}
178
179template<>
180inline void
181UsdImagingDataSourceAttribute_RecordObjectInStageGlobals<SdfAssetPath>(
182 const UsdImagingDataSourceStageGlobals *stageGlobals,
183 const SdfPath &objPath)
184{
185 // Record asset path-valued attributes.
186 stageGlobals->FlagAsAssetPathDependent(objPath);
187}
188
189template<typename T>
190UsdImagingDataSourceAttribute<T>::UsdImagingDataSourceAttribute(
191 const UsdAttribute &usdAttr,
192 const UsdImagingDataSourceStageGlobals &stageGlobals,
193 const SdfPath &sceneIndexPath,
194 const HdDataSourceLocator &timeVaryingFlagLocator)
195 : UsdImagingDataSourceAttribute(
196 UsdAttributeQuery(usdAttr), stageGlobals,
197 sceneIndexPath, timeVaryingFlagLocator)
198{
199}
200
201template<typename T>
202UsdImagingDataSourceAttribute<T>::UsdImagingDataSourceAttribute(
203 const UsdAttributeQuery &usdAttrQuery,
204 const UsdImagingDataSourceStageGlobals &stageGlobals,
205 const SdfPath &sceneIndexPath,
206 const HdDataSourceLocator &timeVaryingFlagLocator)
207 : _usdAttrQuery(usdAttrQuery)
208 , _stageGlobals(stageGlobals)
209{
210 if (!timeVaryingFlagLocator.IsEmpty()) {
211 if (_usdAttrQuery.ValueMightBeTimeVarying()) {
212 _stageGlobals.FlagAsTimeVarying(
213 sceneIndexPath, timeVaryingFlagLocator);
214 }
215 }
216
217 UsdImagingDataSourceAttribute_RecordObjectInStageGlobals<T>(
218 &_stageGlobals, usdAttrQuery.GetAttribute().GetPath());
219}
220
221PXR_NAMESPACE_CLOSE_SCOPE
222
223#endif // PXR_USD_IMAGING_USD_IMAGING_DATA_SOURCE_ATTRIBUTE_H
A basic mathematical interval class.
Definition: interval.h:50
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:210
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:291
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:176
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:203
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition: timeCode.h:84
double GetValue() const
Return the numeric value for this time.
Definition: timeCode.h:151
bool IsNumeric() const
Return true if this time represents a numeric value, false otherwise.
Definition: timeCode.h:145
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:165