HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dataSourceSchemaBased.h
Go to the documentation of this file.
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 
30 
32 
34 
35 namespace UsdImagingDataSourceSchemaBased_Impl
36 {
37 struct _Mapping;
38 };
39 
40 /// \class UsdImagingDataSourceSchemaBased
41 ///
42 /// A container data source created from a Usd schema and optionally some
43 /// of its base classes which accesses the attributes on the underlying Usd
44 /// prim performing translation between the Usd attribute name and the key in
45 /// the container data source (for implementing Get) or the data source
46 /// locator (for implementing Invalidate).
47 ///
48 /// The translation starts by taking the non-inherited attributes from the
49 /// the given UsdSchemaType (e.g., UsdGeomSphere) and the given UsdSchemaBaseTypes
50 /// and calling Translator::UsdAttributeNameToHdName(usdAttributeName) which
51 /// can either return the corresponding hydra token or an empty token if
52 /// the usd attribute should not occur in the data source.
53 ///
54 /// UsdSchemaBaseTypes is a std::tuple of the base schema types that should
55 /// also be considered and can be std::tuple<> if there is no base schema or
56 /// no attribute of a base schema should be included.
57 ///
58 /// The data source locator (relevant for invalidation) will be created
59 /// by appending the hydra token to the data source locator returned by
60 /// Translator::GetContainerLocator().
61 ///
62 template<typename UsdSchemaType,
63  typename UsdSchemaBaseTypes,
64  typename Translator>
66 {
67 public:
69  UsdSchemaType, UsdSchemaBaseTypes, Translator>;
70 
72 
73  TfTokenVector GetNames() override;
74 
75  HdDataSourceBaseHandle Get(const TfToken &name) override;
76 
77  /// Translate usdNames to data source locators.
78  static
80  Invalidate(const TfToken &subprim, const TfTokenVector &usdNames);
81 
82 private:
83  // Private constructor, use static New() instead.
85  const SdfPath &sceneIndexPath,
86  UsdSchemaType usdSchema,
87  const UsdImagingDataSourceStageGlobals &stageGlobals);
88 
90 
91  static const std::vector<_Mapping> &_GetMappings();
92 
93 private:
94  const SdfPath _sceneIndexPath;
95  UsdSchemaType _usdSchema;
96  const UsdImagingDataSourceStageGlobals & _stageGlobals;
97 };
98 
99 /// Implementation
100 
101 namespace UsdImagingDataSourceSchemaBased_Impl
102 {
103 
104 struct _Mapping
105 {
109 };
110 
111 template<typename Translator,
112  typename ThisUsdSchemaType>
113 void _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 
130 template<typename Translator,
131  typename UsdSchemaBaseTypes>
133 
134 template<typename Translator>
135 struct _MappingsFiller<Translator, std::tuple<>>
136 {
137  static void Fill(std::vector<_Mapping> * const result)
138  {
139  }
140 };
141 
142 template<typename Translator,
143  typename UsdSchemaType,
144  typename ...UsdSchemaTypes>
145 struct _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 
154 template<typename UsdSchemaType,
155  typename UsdSchemaBasesTypes,
156  typename Translator>
157 std::vector<_Mapping>
159 {
160  std::vector<_Mapping> result;
161 
162  _FillMappings<Translator, UsdSchemaType>(&result);
164 
165  return result;
166 }
167 
169 inline _GetNames(const std::vector<_Mapping> &mappings)
170 {
172  for (const _Mapping &mapping : mappings) {
173  result.push_back(mapping.hdName);
174  }
175  return result;
176 }
177 
178 } // namespace UsdImagingDataSourceSchemaBased_Impl
179 
180 template<typename UsdSchemaType,
181  typename UsdSchemaBasesTypes,
182  typename Translator>
186 {
187  static const TfTokenVector names =
189  return names;
190 }
191 
192 template<typename UsdSchemaType,
193  typename UsdSchemaBasesTypes,
194  typename Translator>
195 HdDataSourceBaseHandle
197 Get(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
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 
224 template<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 
246 template<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 
260 template<typename UsdSchemaType,
261  typename UsdSchemaBasesTypes,
262  typename Translator>
263 const std::vector<UsdImagingDataSourceSchemaBased_Impl::_Mapping> &
265 _GetMappings()
266 {
267  static const std::vector<_Mapping> mappings =
268  UsdImagingDataSourceSchemaBased_Impl::
269  _GetMappings<UsdSchemaType, UsdSchemaBasesTypes, Translator>();
270  return mappings;
271 }
272 
273 
275 
276 #endif // PXR_USD_IMAGING_USD_IMAGING_DATA_SOURCE_SCHEMA_BASED_H
void _FillMappings(std::vector< _Mapping > *const result)
static HdDataSourceLocatorSet Invalidate(const TfToken &subprim, const TfTokenVector &usdNames)
Translate usdNames to data source locators.
#define TF_CODING_ERROR
**But if you need a result
Definition: thread.h:613
HdDataSourceBaseHandle Get(const TfToken &name) override
Definition: token.h:87
USDIMAGING_API HdSampledDataSourceHandle UsdImagingDataSourceAttributeNew(const UsdAttribute &usdAttr, const UsdImagingDataSourceStageGlobals &stageGlobals, const SdfPath &sceneIndexPath=SdfPath::EmptyPath(), const HdDataSourceLocator &timeVaryingFlagLocator=HdDataSourceLocator::EmptyLocator())
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
GLuint const GLchar * name
Definition: glcorearb.h:786
Definition: path.h:291
UsdPrim GetPrim() const
Definition: prim.h:2610
HD_API void insert(const HdDataSourceLocator &locator)
TfTokenVector _GetNames(const std::vector< _Mapping > &mappings)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
USD_API UsdAttribute GetAttribute(const TfToken &attrName) const
bool IsEmpty() const
Returns true iff this token contains the empty string "".
Definition: token.h:302