HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
USD_Utils.h
Go to the documentation of this file.
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"
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 
48 
49 
50 namespace GusdUSD_Utils
51 {
52 
53 /// TODO: Would be nice to loft these TfTokens into a a shared place.
54 const TfToken kModelingVariantToken("modelingVariant");
55 const TfToken kAllVariantsToken("ALL_VARIANTS");
56 
57 
58 /// Convert a TfToken to a UT_StringHolder.
61 
62 /// Extract the numeric time from a time code.
63 /// If @a time is not numeric, returns the numeric value
64 /// from UsdTimeCode::EarliestTime().
67 
68 
69 /// Parse and construct and SdfPath from a string.
70 /// Returns true if there were no parse errors.
71 /// Any errors that occur while attempting to construct the path are reported
72 /// on the currently scoped error manager at a severity of \p sev.
74 bool CreateSdfPath(const UT_StringRef& pathStr,
75  SdfPath& path,
77 
78 
79 /// Returns an SdfPath that can be used for identify the stage's defaultPrim.
82 
83 
84 /// Get a prim from a stage.
85 /// If no prim can be found at \p path, an error is reported on the currently
86 /// scoped error manager at a severity of \p sev.
88 UsdPrim GetPrimFromStage(const UsdStagePtr& stage,
89  const SdfPath& path,
91 
92 
93 /// Helper for creating and validating schema objects.
94 /// If the prim doesn't match the expected schema type, an error is reported
95 /// on the currently scoped error manager at a severity of \p sev.
96 template <typename SchemaT>
97 SchemaT MakeSchemaObj(const UsdPrim& prim,
99 
100 
101 /// Given a string representing a list of whitespace-delimited paths,
102 /// which may or may not include variant specifications,
103 /// return an array of prim and variant paths.
104 ///
105 /// The resulting @a primPaths and @a variantPaths arrays
106 /// will be the same size. If no variants are associated with a path,
107 /// then the corresponding entry in @a variants will be an empty path.
108 /// If any errors occur while parsing the path, errors are reporeted on the
109 /// currently scoped error manager at a severity of \p sev.
110 GUSD_API
112  const char* str,
113  UT_Array<SdfPath>& primPaths,
114  UT_Array<SdfPath>& variants,
116 
117 /** Extract a prim path and variant selection from a path.*/
118 GUSD_API
119 void ExtractPrimPathAndVariants(const SdfPath& path,
120  SdfPath& primPath,
121  SdfPath& variants);
122 
123 /** Set a modeling variant on a stage given the prim and variant to set.*/
124 GUSD_API
125 void SetModelingVariant(const UsdStageRefPtr& stage,
126  const UsdPrim& prim,
127  const TfToken& variant);
128 
129 /** Clear any variant selection on a prim.*/
130 GUSD_API
131 void ClearModelingVariant(const UsdStageRefPtr& stage,
132  const UsdPrim& prim);
133 
134 /** Sort an array of prims (by path) */
135 GUSD_API
136 bool SortPrims(UT_Array<UsdPrim>& prims);
137 
138 
139 /** Traverse the tree of schema types to compute a list
140  of types matching a pattern.
141  Derived types of types that match the pattern are not added
142  to the list; the minimal set of matching types is returned
143  to simplify later type comparisons.*/
144 GUSD_API
147  bool caseSensitive=true);
148 
149 
150 /** Get the list of all model kinds matching the given pattern.
151  Derived types of types that match the pattern are not added
152  to the list; the minimal set of matching types is returned
153  to simplify later type comparisons.*/
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 {
171 };
172 
173 
174 /** Get a walkable hierarchy of the registered model kinds.
175  The root of the hierarchy is always a root node with an empty kind.*/
176 GUSD_API
178 
179 
181 {
183 };
184 
186 
187 
188 /** Helper for building up a variant-encoded prim path.
189  Appends string {vset=sel} to @a buf. If the buffer is empty,
190  the buffer is initialized to the path up to @a prim, including
191  any of the variant selections specified in @a variants.*/
192 GUSD_API
194  const SdfPath& prim,
195  const SdfPath& variants,
196  const std::string& vset,
197  const std::string& sel);
198 
199 
200 /** Given an array of prims, compute new variant path strings
201  that apply a set of variant selections.
202  Only the variants that exist on each prim are applied.
203  The @a variants array may optionally be specified to provide
204  the previous variant path of each prim.
205 
206  The resulting @a indices array provides an index per-prim into
207  the @a orderedVariants array. The indices may be -1 to indicate
208  that the entry has no variant selections.*/
209 GUSD_API
211  const VariantSelArray& selections,
212  UT_Array<UT_StringHolder>& orderedVariants,
214  const UT_Array<SdfPath>* prevVariants=NULL);
215 
216 
218 {
219  virtual ~NameMatcher() {}
220  virtual bool operator()(const std::string& name) const = 0;
221 };
222 
223 typedef std::pair<exint,exint> IndexPair;
225 
226 
227 /** Expand selections of variants that match a given match function.
228  For every prim in @a prims that has variant set @a variantSet,
229  this appends an entry in @a indices for each matching variant.
230  The first component of the pair in @a indices is the index of the
231  original prim from @a prims from which the entry was expanded.
232  The second component is an index into the @a orderedVariants array. */
233 GUSD_API
234 bool ExpandVariantSetPaths(const UT_Array<UsdPrim>& prims,
235  const std::string& variantSet,
236  const NameMatcher& matcher,
237  UT_Array<UT_StringHolder>& orderedVariants,
239  const UT_Array<SdfPath>* prevVariants=NULL);
240 
241 
242 /// Author variant selections on a layer using
243 /// variants stored in a path.
244 GUSD_API
245 void SetVariantsFromPath(const SdfPath& path,
246  const SdfLayerHandle& layer);
247 
248 
249 /** Compute a set of properties matching the namespace of a range of prims.
250  For every prim in @a prims, this appends an entry in @a indices for
251  each matching attribute. The first component of the pairt in @a indices
252  is the index of the original prim from @a prims that the attribute
253  was matched from. The second component is an index into the
254  @a orderedNames array.*/
255 GUSD_API
256 bool GetPropertyNames(const UT_Array<UsdPrim>& prims,
257  const NameMatcher& matcher,
258  UT_Array<UT_StringHolder>& orderedNames,
260  const std::string& nameSpace=std::string());
261 
262 
263 /** Query all unique variant set names for a range of prims.*/
264 GUSD_API
267 
268 
269 /** Query all unique variant names for a specific variant
270  set on all of the given prims.*/
271 GUSD_API
272 bool GetUniqueVariantNames(const UT_Array<UsdPrim>& prims,
273  const std::string& variantSet,
275 
276 
277 /** Query all unique property names for a range of prims.*/
278 GUSD_API
279 bool GetUniquePropertyNames(const UT_Array<UsdPrim>& prims,
281  const std::string& nameSpace=std::string());
282 
283 
284 inline double
286 {
287  return time.IsNumeric() ?
289 }
290 
291 
292 inline UsdTimeCode
293 ClampTimeCode(UsdTimeCode t, double start, double end, int digits)
294 {
295  if(ARCH_UNLIKELY(t.IsDefault()))
296  return t;
297  return UsdTimeCode(
298  SYSniceNumber(SYSclamp(t.GetValue(), start, end), digits));
299 }
300 
301 
302 template <typename SchemaT>
303 SchemaT
305 {
306  SchemaT obj(prim);
307  if(!obj) {
308  static const std::string typeName =
309  TfType::Find<SchemaT>().GetTypeName();
310 
311  GUSD_GENERIC_ERR(sev).Msg(
312  "Prim '%s' is not a %s.",
313  prim.GetPath().GetText(), typeName.c_str());
314  }
315  return obj;
316 }
317 
318 
319 } /*namespace GusdUSD_Utils*/
320 
322 
323 #endif /*_GUSD_USD_UTILS_H_*/
SDF_API const char * GetText() const
GUSD_API void ExtractPrimPathAndVariants(const SdfPath &path, SdfPath &primPath, SdfPath &variants)
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
GLsizei GLenum const void * indices
Definition: glcorearb.h:406
GUSD_API bool AppendVariantSelections(const UT_Array< UsdPrim > &prims, const VariantSelArray &selections, UT_Array< UT_StringHolder > &orderedVariants, UT_Array< exint > &indices, const UT_Array< SdfPath > *prevVariants=NULL)
GUSD_API const KindNode & GetModelKindHierarchy()
UT_Array< IndexPair > IndexPairArray
Definition: USD_Utils.h:224
double GetValue() const
Definition: timeCode.h:151
static constexpr UsdTimeCode EarliestTime()
Definition: timeCode.h:101
GUSD_API bool GetPrimAndVariantPathsFromPathList(const char *str, UT_Array< SdfPath > &primPaths, UT_Array< SdfPath > &variants, UT_ErrorSeverity sev=UT_ERROR_ABORT)
GT_API const UT_StringHolder time
GUSD_API bool CreateSdfPath(const UT_StringRef &pathStr, SdfPath &path, UT_ErrorSeverity sev=UT_ERROR_ABORT)
GLuint start
Definition: glcorearb.h:475
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
GUSD_API const SdfPath & GetDefaultPrimIdentifier()
Returns an SdfPath that can be used for identify the stage's defaultPrim.
GUSD_API bool GetPropertyNames(const UT_Array< UsdPrim > &prims, const NameMatcher &matcher, UT_Array< UT_StringHolder > &orderedNames, IndexPairArray &indices, const std::string &nameSpace=std::string())
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
const TfToken kModelingVariantToken("modelingVariant")
TODO: Would be nice to loft these TfTokens into a a shared place.
UT_ErrorSeverity
Definition: UT_Error.h:25
GUSD_API void GetBaseSchemaTypesMatchingPattern(const char *pattern, UT_Array< TfType > &types, bool caseSensitive=true)
GUSD_API bool GetUniqueVariantNames(const UT_Array< UsdPrim > &prims, const std::string &variantSet, UT_Array< UT_StringHolder > &names)
GUSD_API bool ExpandVariantSetPaths(const UT_Array< UsdPrim > &prims, const std::string &variantSet, const NameMatcher &matcher, UT_Array< UT_StringHolder > &orderedVariants, IndexPairArray &indices, const UT_Array< SdfPath > *prevVariants=NULL)
SYS_API fpreal64 SYSniceNumber(fpreal64 num, int digits=6)
GLenum GLuint GLint GLint layer
Definition: glcorearb.h:1299
UT_SharedPtr< KindNode > RefPtr
Definition: USD_Utils.h:168
#define ARCH_UNLIKELY(x)
Definition: hints.h:47
Definition: token.h:87
GUSD_API UT_StringHolder TokenToStringHolder(const TfToken &token)
Convert a TfToken to a UT_StringHolder.
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
GUSD_API void AppendVariantSelectionString(UT_WorkBuffer &buf, const SdfPath &prim, const SdfPath &variants, const std::string &vset, const std::string &sel)
bool IsDefault() const
Definition: timeCode.h:139
GLuint GLuint end
Definition: glcorearb.h:475
UT_Vector3T< T > SYSclamp(const UT_Vector3T< T > &v, const UT_Vector3T< T > &min, const UT_Vector3T< T > &max)
Definition: UT_Vector3.h:1057
Definition: prim.h:133
GLuint const GLchar * name
Definition: glcorearb.h:786
Definition: path.h:290
GLushort pattern
Definition: glad.h:2583
GLdouble t
Definition: glad.h:2397
const TfToken kAllVariantsToken("ALL_VARIANTS")
bool IsNumeric() const
Definition: timeCode.h:145
GUSD_API void SetVariantsFromPath(const SdfPath &path, const SdfLayerHandle &layer)
SdfPath GetPath() const
Definition: object.h:203
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
virtual bool operator()(const std::string &name) const =0
GUSD_API void GetPurposesMatchingPattern(const char *pattern, UT_Array< TfToken > &purposes, bool caseSensitive=true)
#define GUSD_GENERIC_ERR(sev)
Definition: error.h:54
SchemaT MakeSchemaObj(const UsdPrim &prim, UT_ErrorSeverity sev=UT_ERROR_ABORT)
Definition: USD_Utils.h:304
GUSD_API bool GetUniquePropertyNames(const UT_Array< UsdPrim > &prims, UT_Array< UT_StringHolder > &names, const std::string &nameSpace=std::string())
std::pair< exint, exint > IndexPair
Definition: USD_Utils.h:223
UT_Array< RefPtr > children
Definition: USD_Utils.h:170
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
UT_Array< VariantSel > VariantSelArray
Definition: USD_Utils.h:185
GUSD_API void GetBaseModelKindsMatchingPattern(const char *pattern, UT_Array< TfToken > &kinds, bool caseSensitive=true)
#define GUSD_API
Definition: api.h:40
GUSD_API bool GetUniqueVariantSetNames(const UT_Array< UsdPrim > &prims, UT_Array< UT_StringHolder > &names)
GUSD_API UsdPrim GetPrimFromStage(const UsdStagePtr &stage, const SdfPath &path, UT_ErrorSeverity sev=UT_ERROR_ABORT)
GUSD_API double GetNumericTime(UsdTimeCode time)
Definition: USD_Utils.h:285
GLsizei GLenum GLenum * types
Definition: glcorearb.h:2542
UsdTimeCode ClampTimeCode(UsdTimeCode t, double start, double end, int digits)
Definition: USD_Utils.h:293
GUSD_API bool SortPrims(UT_Array< UsdPrim > &prims)
GUSD_API void SetModelingVariant(const UsdStageRefPtr &stage, const UsdPrim &prim, const TfToken &variant)
GUSD_API void ClearModelingVariant(const UsdStageRefPtr &stage, const UsdPrim &prim)