All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
USD_Utils.h
1 //
2 // Copyright 2017 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 _GUSD_USD_UTILS_H_
25 #define _GUSD_USD_UTILS_H_
26 
27 #include "api.h"
28 
29 #include "error.h"
30 
31 #include "pxr/pxr.h"
32 #include "pxr/base/arch/hints.h"
33 #include "pxr/base/tf/token.h"
34 #include "pxr/usd/usdGeom/imageable.h"
35 #include "pxr/usd/usdGeom/tokens.h"
36 
37 #include <SYS/SYS_Floor.h>
38 #include <SYS/SYS_Math.h>
39 #include <UT/UT_Array.h>
40 #include <UT/UT_Error.h>
41 #include <UT/UT_Map.h>
42 #include <UT/UT_SharedPtr.h>
43 #include <UT/UT_StringHolder.h>
44 #include <UT/UT_WorkBuffer.h>
45 
46 
47 PXR_NAMESPACE_OPEN_SCOPE
48 
49 
50 namespace GusdUSD_Utils
51 {
52 
54 const TfToken kModelingVariantToken("modelingVariant");
55 const TfToken kAllVariantsToken("ALL_VARIANTS");
56 
57 
59 GUSD_API
60 UT_StringHolder TokenToStringHolder(const TfToken& token);
61 
65 GUSD_API
66 double GetNumericTime(UsdTimeCode time);
67 
68 
73 GUSD_API
74 bool CreateSdfPath(const UT_StringRef& pathStr,
75  SdfPath& path,
76  UT_ErrorSeverity sev=UT_ERROR_ABORT);
77 
78 
80 GUSD_API
81 const SdfPath& GetDefaultPrimIdentifier();
82 
83 
87 GUSD_API
88 UsdPrim GetPrimFromStage(const UsdStagePtr& stage,
89  const SdfPath& path,
90  UT_ErrorSeverity sev=UT_ERROR_ABORT);
91 
92 
96 template <typename SchemaT>
97 SchemaT MakeSchemaObj(const UsdPrim& prim,
98  UT_ErrorSeverity sev=UT_ERROR_ABORT);
99 
100 
110 GUSD_API
111 bool GetPrimAndVariantPathsFromPathList(
112  const char* str,
113  UT_Array<SdfPath>& primPaths,
114  UT_Array<SdfPath>& variants,
115  UT_ErrorSeverity sev=UT_ERROR_ABORT);
116 
118 GUSD_API
119 void ExtractPrimPathAndVariants(const SdfPath& path,
120  SdfPath& primPath,
121  SdfPath& variants);
122 
124 GUSD_API
125 void SetModelingVariant(const UsdStageRefPtr& stage,
126  const UsdPrim& prim,
127  const TfToken& variant);
128 
130 GUSD_API
131 void ClearModelingVariant(const UsdStageRefPtr& stage,
132  const UsdPrim& prim);
133 
135 GUSD_API
136 bool SortPrims(UT_Array<UsdPrim>& prims);
137 
138 
144 GUSD_API
145 void GetBaseSchemaTypesMatchingPattern(const char* pattern,
146  UT_Array<TfType>& types,
147  bool caseSensitive=true);
148 
149 
154 GUSD_API
155 void GetBaseModelKindsMatchingPattern(const char* pattern,
156  UT_Array<TfToken>& kinds,
157  bool caseSensitive=true);
158 
159 
160 GUSD_API
161 void GetPurposesMatchingPattern(const char* pattern,
162  UT_Array<TfToken>& purposes,
163  bool caseSensitive=true);
164 
165 
166 struct KindNode
167 {
168  typedef UT_SharedPtr<KindNode> RefPtr;
169  TfToken kind;
170  UT_Array<RefPtr> children;
171 };
172 
173 
176 const KindNode& GetModelKindHierarchy();
177 
178 
179 struct VariantSel
180 {
181  std::string variantSet, variant;
182 };
183 
184 typedef UT_Array<VariantSel> VariantSelArray;
185 
186 
191 GUSD_API
192 void AppendVariantSelectionString(UT_WorkBuffer& buf,
193  const SdfPath& prim,
194  const SdfPath& variants,
195  const std::string& vset,
196  const std::string& sel);
197 
198 
208 GUSD_API
209 bool AppendVariantSelections(const UT_Array<UsdPrim>& prims,
210  const VariantSelArray& selections,
211  UT_Array<UT_StringHolder>& orderedVariants,
212  UT_Array<exint>& indices,
213  const UT_Array<SdfPath>* prevVariants=NULL);
214 
215 
216 struct NameMatcher
217 {
218  virtual ~NameMatcher() {}
219  virtual bool operator()(const std::string& name) const = 0;
220 };
221 
222 typedef std::pair<exint,exint> IndexPair;
223 typedef UT_Array<IndexPair> IndexPairArray;
224 
225 
232 GUSD_API
233 bool ExpandVariantSetPaths(const UT_Array<UsdPrim>& prims,
234  const std::string& variantSet,
235  const NameMatcher& matcher,
236  UT_Array<UT_StringHolder>& orderedVariants,
237  IndexPairArray& indices,
238  const UT_Array<SdfPath>* prevVariants=NULL);
239 
240 
243 GUSD_API
244 void SetVariantsFromPath(const SdfPath& path,
245  const SdfLayerHandle& layer);
246 
247 
254 GUSD_API
255 bool GetPropertyNames(const UT_Array<UsdPrim>& prims,
256  const NameMatcher& matcher,
257  UT_Array<UT_StringHolder>& orderedNames,
258  IndexPairArray& indices,
259  const std::string& nameSpace=std::string());
260 
261 
263 GUSD_API
264 bool GetUniqueVariantSetNames(const UT_Array<UsdPrim>& prims,
265  UT_Array<UT_StringHolder>& names);
266 
267 
270 GUSD_API
271 bool GetUniqueVariantNames(const UT_Array<UsdPrim>& prims,
272  const std::string& variantSet,
273  UT_Array<UT_StringHolder>& names);
274 
275 
277 GUSD_API
278 bool GetUniquePropertyNames(const UT_Array<UsdPrim>& prims,
279  UT_Array<UT_StringHolder>& names,
280  const std::string& nameSpace=std::string());
281 
282 
283 inline double
284 GetNumericTime(UsdTimeCode time)
285 {
286  return time.IsNumeric() ?
288 }
289 
290 
291 inline UsdTimeCode
292 ClampTimeCode(UsdTimeCode t, double start, double end, int digits)
293 {
294  if(ARCH_UNLIKELY(t.IsDefault()))
295  return t;
296  return UsdTimeCode(
297  SYSniceNumber(SYSclamp(t.GetValue(), start, end), digits));
298 }
299 
300 
301 template <typename SchemaT>
302 SchemaT
303 MakeSchemaObj(const UsdPrim& prim, UT_ErrorSeverity sev)
304 {
305  SchemaT obj(prim);
306  if(!obj) {
307  static const std::string typeName =
308  TfType::Find<SchemaT>().GetTypeName();
309 
310  GUSD_GENERIC_ERR(sev).Msg(
311  "Prim '%s' is not a %s.",
312  prim.GetPath().GetText(), typeName.c_str());
313  }
314  return obj;
315 }
316 
317 
318 } /*namespace GusdUSD_Utils*/
319 
320 PXR_NAMESPACE_CLOSE_SCOPE
321 
322 #endif /*_GUSD_USD_UTILS_H_*/
bool IsNumeric() const
Return true if this time represents a numeric value, false otherwise.
Definition: timeCode.h:146
static constexpr UsdTimeCode EarliestTime()
Produce a UsdTimeCode representing the lowest/earliest possible timeCode.
Definition: timeCode.h:102
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:193
bool IsDefault() const
Return true if this time represents the &#39;default&#39; sentinel value, false otherwise.
Definition: timeCode.h:140
double GetValue() const
Return the numeric value for this time.
Definition: timeCode.h:152
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition: timeCode.h:85
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a &quot;Prim&quot; as ...
Definition: prim.h:131
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
SDF_API const char * GetText() const
Returns the string representation of this path as a c string.