Loading...
Searching...
No Matches
dataSourceSchemaBased.h
1//
2// Copyright 2022 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
25#ifndef PXR_USD_IMAGING_USD_IMAGING_DATA_SOURCE_SCHEMA_BASED_H
26#define PXR_USD_IMAGING_USD_IMAGING_DATA_SOURCE_SCHEMA_BASED_H
27
28#include "pxr/usdImaging/usdImaging/dataSourceGprim.h"
29#include "pxr/usdImaging/usdImaging/dataSourceStageGlobals.h"
30
31#include "pxr/imaging/hd/dataSource.h"
32
33PXR_NAMESPACE_OPEN_SCOPE
34
36{
37struct _Mapping;
38};
39
62template<typename UsdSchemaType,
63 typename UsdSchemaBaseTypes,
64 typename Translator>
66{
67public:
69 UsdSchemaType, UsdSchemaBaseTypes, Translator>;
70
71 HD_DECLARE_DATASOURCE(This);
72
73 TfTokenVector GetNames() override;
74
75 HdDataSourceBaseHandle Get(const TfToken &name) override;
76
78 static
80 Invalidate(const TfToken &subprim, const TfTokenVector &usdNames);
81
82private:
83 // Private constructor, use static New() instead.
85 const SdfPath &sceneIndexPath,
86 UsdSchemaType usdSchema,
87 const UsdImagingDataSourceStageGlobals &stageGlobals);
88
89 using _Mapping = UsdImagingDataSourceSchemaBased_Impl::_Mapping;
90
91 static const std::vector<_Mapping> &_GetMappings();
92
93private:
94 const SdfPath _sceneIndexPath;
95 UsdSchemaType _usdSchema;
96 const UsdImagingDataSourceStageGlobals & _stageGlobals;
97};
98
100
102{
103
104struct _Mapping
105{
106 TfToken usdAttributeName;
107 TfToken hdName;
108 HdDataSourceLocator locator;
109};
110
111template<typename Translator,
112 typename ThisUsdSchemaType>
113void _FillMappings(std::vector<_Mapping> * const result)
114{
115 for (const TfToken &usdAttributeName :
116 ThisUsdSchemaType::GetSchemaAttributeNames(
117 /* includeInherited = */ false))
118 {
119 const TfToken hdName =
120 Translator::UsdAttributeNameToHdName(usdAttributeName);
121 if (!hdName.IsEmpty()) {
122 result->push_back(
123 { usdAttributeName,
124 hdName,
125 Translator::GetContainerLocator().Append(hdName) });
126 }
127 }
128}
129
130template<typename Translator,
131 typename UsdSchemaBaseTypes>
132struct _MappingsFiller;
133
134template<typename Translator>
135struct _MappingsFiller<Translator, std::tuple<>>
136{
137 static void Fill(std::vector<_Mapping> * const result)
138 {
139 }
140};
141
142template<typename Translator,
143 typename UsdSchemaType,
144 typename ...UsdSchemaTypes>
145struct _MappingsFiller<Translator, std::tuple<UsdSchemaType, UsdSchemaTypes...>>
146{
147 static void Fill(std::vector<_Mapping> * const result)
148 {
149 _FillMappings<Translator, UsdSchemaType>(result);
150 _MappingsFiller<Translator, std::tuple<UsdSchemaTypes...>>::Fill(result);
151 }
152};
153
154template<typename UsdSchemaType,
155 typename UsdSchemaBasesTypes,
156 typename Translator>
157std::vector<_Mapping>
158_GetMappings()
159{
160 std::vector<_Mapping> result;
161
162 _FillMappings<Translator, UsdSchemaType>(&result);
163 _MappingsFiller<Translator, UsdSchemaBasesTypes>::Fill(&result);
164
165 return result;
166}
167
169inline _GetNames(const std::vector<_Mapping> &mappings)
170{
171 TfTokenVector result;
172 for (const _Mapping &mapping : mappings) {
173 result.push_back(mapping.hdName);
174 }
175 return result;
176}
177
178} // namespace UsdImagingDataSourceSchemaBased_Impl
179
180template<typename UsdSchemaType,
181 typename UsdSchemaBasesTypes,
182 typename Translator>
185GetNames()
186{
187 static const TfTokenVector names =
188 UsdImagingDataSourceSchemaBased_Impl::_GetNames(_GetMappings());
189 return names;
190}
191
192template<typename UsdSchemaType,
193 typename UsdSchemaBasesTypes,
194 typename Translator>
195HdDataSourceBaseHandle
197Get(const TfToken &name)
198{
199 for (const _Mapping &mapping : _GetMappings()) {
200 if (mapping.hdName == name) {
201 if (UsdAttribute attr =
202 _usdSchema.GetPrim().GetAttribute(
203 mapping.usdAttributeName)) {
204 return
205 UsdImagingDataSourceAttributeNew(
206 attr,
207 _stageGlobals,
208 _sceneIndexPath,
209 mapping.locator);
210 } else {
211 // Has(name) has returned true, but we return
212 // nullptr - an inconsistency.
214 "Could not get usd attribute '%s' even though "
215 "it is on the schema.",
216 mapping.usdAttributeName.GetText());
217 return nullptr;
218 }
219 }
220 }
221 return nullptr;
222}
223
224template<typename UsdSchemaType,
225 typename UsdSchemaBasesTypes,
226 typename Translator>
230 const TfToken &subprim,
231 const TfTokenVector &usdNames)
232{
233 HdDataSourceLocatorSet locators;
234
235 for (const TfToken &usdName : usdNames) {
236 for (const _Mapping &mapping : _GetMappings()) {
237 if (mapping.usdAttributeName == usdName) {
238 locators.insert(mapping.locator);
239 }
240 }
241 }
242
243 return locators;
244}
245
246template<typename UsdSchemaType,
247 typename UsdSchemaBasesTypes,
248 typename Translator>
251 const SdfPath &sceneIndexPath,
252 UsdSchemaType usdSchema,
253 const UsdImagingDataSourceStageGlobals &stageGlobals)
254 : _sceneIndexPath(sceneIndexPath)
255 , _usdSchema(usdSchema)
256 , _stageGlobals(stageGlobals)
257{
258}
259
260template<typename UsdSchemaType,
261 typename UsdSchemaBasesTypes,
262 typename Translator>
263const std::vector<UsdImagingDataSourceSchemaBased_Impl::_Mapping> &
266{
267 static const std::vector<_Mapping> mappings =
268 UsdImagingDataSourceSchemaBased_Impl::
269 _GetMappings<UsdSchemaType, UsdSchemaBasesTypes, Translator>();
270 return mappings;
271}
272
273
274PXR_NAMESPACE_CLOSE_SCOPE
275
276#endif // PXR_USD_IMAGING_USD_IMAGING_DATA_SOURCE_SCHEMA_BASED_H
A datasource representing structured (named, hierarchical) data, for example a geometric primitive or...
Definition: dataSource.h:116
Represents an object that can identify the location of a data source.
Represents a set of data source locators closed under descendancy.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:291
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:88
bool IsEmpty() const
Returns true iff this token contains the empty string "".
Definition: token.h:305
Scenegraph object for authoring and retrieving numeric, string, and array valued data,...
Definition: attribute.h:176
A container data source created from a Usd schema and optionally some of its base classes which acces...
static HdDataSourceLocatorSet Invalidate(const TfToken &subprim, const TfTokenVector &usdNames)
Translate usdNames to data source locators.
TfTokenVector GetNames() override
Returns the list of names for which Get(...) is expected to return a non-null value.
HdDataSourceBaseHandle Get(const TfToken &name) override
Returns the child datasource of the given name.
This class is used as a context object with global stage information, that gets passed down to dataso...
UsdPrim GetPrim() const
Return this object if it is a prim, otherwise return this object's nearest owning prim.
Definition: prim.h:2804
USD_API UsdAttribute GetAttribute(const TfToken &attrName) const
Return a UsdAttribute with the name attrName.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Definition: diagnostic.h:85
STL namespace.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:457