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 
33 PXR_NAMESPACE_OPEN_SCOPE
34 
52 template<typename UsdSchemaType,
53  typename Translator>
55 {
56 public:
58 
59  HD_DECLARE_DATASOURCE(This);
60 
61  TfTokenVector GetNames() override {
62  static const TfTokenVector names = _GetNamesUncached();
63  return names;
64  }
65 
66  HdDataSourceBaseHandle Get(const TfToken &name) override {
67  for (const _NameInfo &info : _GetNameInfos()) {
68  if (info.hdName == name) {
69  if (UsdAttribute attr =
70  _usdSchema.GetPrim().GetAttribute(
71  info.usdAttributeName)) {
72  return
73  UsdImagingDataSourceAttributeNew(
74  attr,
75  _stageGlobals,
76  _sceneIndexPath,
77  info.locator);
78  } else {
79  // Has(name) has returned true, but we return
80  // nullptr - an inconsistency.
82  "Could not get usd attribute '%s' even though "
83  "it is on the schema.",
84  info.usdAttributeName.GetText());
85  return nullptr;
86  }
87  }
88  }
89  return nullptr;
90  }
91 
93  static
95  Invalidate(const TfToken &subprim, const TfTokenVector &usdNames) {
96  HdDataSourceLocatorSet locators;
97 
98  for (const TfToken &usdName : usdNames) {
99  for (const _NameInfo &info : _GetNameInfos()) {
100  if (info.usdAttributeName == usdName) {
101  locators.insert(info.locator);
102  }
103  }
104  }
105 
106  return locators;
107  }
108 
109 private:
110  // Private constructor, use static New() instead.
112  const SdfPath &sceneIndexPath,
113  UsdSchemaType usdSchema,
114  const UsdImagingDataSourceStageGlobals &stageGlobals)
115  : _sceneIndexPath(sceneIndexPath)
116  , _usdSchema(usdSchema)
117  , _stageGlobals(stageGlobals)
118  {
119  }
120 
121  struct _NameInfo
122  {
123  TfToken hdName;
124  TfToken usdAttributeName;
125  HdDataSourceLocator locator;
126  };
127 
128  static
129  std::vector<_NameInfo>
130  _GetNameInfosUncached()
131  {
132  std::vector<_NameInfo> result;
133  for (const TfToken &usdAttributeName :
134  UsdSchemaType::GetSchemaAttributeNames(
135  /* includeInherited = */ false))
136  {
137  const TfToken hdName = Translator::UsdAttributeNameToHdName(
138  usdAttributeName);
139  if (!hdName.IsEmpty()) {
140  result.push_back(
141  { hdName,
142  usdAttributeName,
143  Translator::GetContainerLocator().Append(hdName) });
144  }
145  }
146  return result;
147  }
148 
149  static
150  const std::vector<_NameInfo> &
151  _GetNameInfos()
152  {
153  static const std::vector<_NameInfo> result = _GetNameInfosUncached();
154  return result;
155  }
156 
157  static
159  _GetNamesUncached()
160  {
161  TfTokenVector result;
162  for (const _NameInfo &info : _GetNameInfos()) {
163  result.push_back(info.hdName);
164  }
165  return result;
166  }
167 
168 private:
169  const SdfPath _sceneIndexPath;
170  UsdSchemaType _usdSchema;
171  const UsdImagingDataSourceStageGlobals & _stageGlobals;
172 };
173 
174 PXR_NAMESPACE_CLOSE_SCOPE
175 
176 #endif // PXR_USD_IMAGING_USD_IMAGING_DATA_SOURCE_IMPLICITS_IMPL_H
bool IsEmpty() const
Returns true iff this token contains the empty string "".
Definition: token.h:302
static HdDataSourceLocatorSet Invalidate(const TfToken &subprim, const TfTokenVector &usdNames)
Translate usdNames to data source locators.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Definition: diagnostic.h:85
A datasource representing structured (named, hierarchical) data, for example a geometric primitive or...
Definition: dataSource.h:110
Scenegraph object for authoring and retrieving numeric, string, and array valued data,...
Definition: attribute.h:176
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
Represents an object that can identify the location of a data source.
TfTokenVector GetNames() override
Returns the list of names for which Get(...) is expected to return a non-null value.
This class is used as a context object with global stage information, that gets passed down to dataso...
Represents a set of data source locators closed under descendancy.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
A container data source created from a Usd schema which accesses the attributes on the underlying Usd...
HdDataSourceBaseHandle Get(const TfToken &name) override
Returns the child datasource of the given name.