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.*/
177 
178 
180 {
182 };
183 
185 
186 
187 /** Helper for building up a variant-encoded prim path.
188  Appends string {vset=sel} to @a buf. If the buffer is empty,
189  the buffer is initialized to the path up to @a prim, including
190  any of the variant selections specified in @a variants.*/
191 GUSD_API
193  const SdfPath& prim,
194  const SdfPath& variants,
195  const std::string& vset,
196  const std::string& sel);
197 
198 
199 /** Given an array of prims, compute new variant path strings
200  that apply a set of variant selections.
201  Only the variants that exist on each prim are applied.
202  The @a variants array may optionally be specified to provide
203  the previous variant path of each prim.
204 
205  The resulting @a indices array provides an index per-prim into
206  the @a orderedVariants array. The indices may be -1 to indicate
207  that the entry has no variant selections.*/
208 GUSD_API
210  const VariantSelArray& selections,
211  UT_Array<UT_StringHolder>& orderedVariants,
213  const UT_Array<SdfPath>* prevVariants=NULL);
214 
215 
217 {
218  virtual ~NameMatcher() {}
219  virtual bool operator()(const std::string& name) const = 0;
220 };
221 
222 typedef std::pair<exint,exint> IndexPair;
224 
225 
226 /** Expand selections of variants that match a given match function.
227  For every prim in @a prims that has variant set @a variantSet,
228  this appends an entry in @a indices for each matching variant.
229  The first component of the pair in @a indices is the index of the
230  original prim from @a prims from which the entry was expanded.
231  The second component is an index into the @a orderedVariants array. */
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,
238  const UT_Array<SdfPath>* prevVariants=NULL);
239 
240 
241 /// Author variant selections on a layer using
242 /// variants stored in a path.
243 GUSD_API
244 void SetVariantsFromPath(const SdfPath& path,
245  const SdfLayerHandle& layer);
246 
247 
248 /** Compute a set of properties matching the namespace of a range of prims.
249  For every prim in @a prims, this appends an entry in @a indices for
250  each matching attribute. The first component of the pairt in @a indices
251  is the index of the original prim from @a prims that the attribute
252  was matched from. The second component is an index into the
253  @a orderedNames array.*/
254 GUSD_API
255 bool GetPropertyNames(const UT_Array<UsdPrim>& prims,
256  const NameMatcher& matcher,
257  UT_Array<UT_StringHolder>& orderedNames,
259  const std::string& nameSpace=std::string());
260 
261 
262 /** Query all unique variant set names for a range of prims.*/
263 GUSD_API
266 
267 
268 /** Query all unique variant names for a specific variant
269  set on all of the given prims.*/
270 GUSD_API
271 bool GetUniqueVariantNames(const UT_Array<UsdPrim>& prims,
272  const std::string& variantSet,
274 
275 
276 /** Query all unique property names for a range of prims.*/
277 GUSD_API
278 bool GetUniquePropertyNames(const UT_Array<UsdPrim>& prims,
280  const std::string& nameSpace=std::string());
281 
282 
283 inline double
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
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 
321 
322 #endif /*_GUSD_USD_UTILS_H_*/
SDF_API const char * GetText() const
Returns the string representation of this path as a c string.
GUSD_API void ExtractPrimPathAndVariants(const SdfPath &path, SdfPath &primPath, SdfPath &variants)
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)
GLuint const GLchar * name
Definition: glew.h:1814
UT_Array< IndexPair > IndexPairArray
Definition: USD_Utils.h:223
double GetValue() const
Definition: timeCode.h:152
static constexpr UsdTimeCode EarliestTime()
Definition: timeCode.h:102
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)
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())
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)
GLhandleARB obj
Definition: glew.h:6236
SYS_API fpreal64 SYSniceNumber(fpreal64 num, int digits=6)
GLsizei GLenum GLenum * types
Definition: glew.h:3954
UT_SharedPtr< KindNode > RefPtr
Definition: USD_Utils.h:168
#define ARCH_UNLIKELY(x)
Definition: hints.h:47
Definition: token.h:89
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:28
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:140
GLuint GLuint GLsizei GLenum const void * indices
Definition: glew.h:1253
UT_Vector3T< T > SYSclamp(const UT_Vector3T< T > &v, const UT_Vector3T< T > &min, const UT_Vector3T< T > &max)
Definition: UT_Vector3.h:820
GLuint GLuint end
Definition: glew.h:1253
GLuint const GLuint * names
Definition: glew.h:2690
Definition: prim.h:131
Definition: path.h:287
GLuint start
Definition: glew.h:1253
GLsizei const GLchar *const * path
Definition: glew.h:6461
const TfToken kAllVariantsToken("ALL_VARIANTS")
bool IsNumeric() const
Definition: timeCode.h:146
GUSD_API void SetVariantsFromPath(const SdfPath &path, const SdfLayerHandle &layer)
GLsizei const GLchar *const * string
Definition: glew.h:1844
SdfPath GetPath() const
Definition: object.h:193
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1245
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:303
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:222
GLint GLboolean GLint layer
Definition: glew.h:3601
UT_Array< RefPtr > children
Definition: USD_Utils.h:170
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:83
UT_Array< VariantSel > VariantSelArray
Definition: USD_Utils.h:184
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:284
GLubyte * pattern
Definition: glew.h:5711
UsdTimeCode ClampTimeCode(UsdTimeCode t, double start, double end, int digits)
Definition: USD_Utils.h:292
GUSD_API bool SortPrims(UT_Array< UsdPrim > &prims)
GUSD_API void SetModelingVariant(const UsdStageRefPtr &stage, const UsdPrim &prim, const TfToken &variant)
GLenum GLuint GLsizei const GLchar * buf
Definition: glew.h:2580
GLdouble GLdouble t
Definition: glew.h:1398
const KindNode & GetModelKindHierarchy()
GUSD_API void ClearModelingVariant(const UsdStageRefPtr &stage, const UsdPrim &prim)