HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GU_USD.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_GU_USD_H_
25 #define _GUSD_GU_USD_H_
26 
27 #include "api.h"
28 #include "defaultArray.h"
29 #include "stageCache.h"
30 #include "USD_Traverse.h"
31 #include "USD_Utils.h"
32 #include "GU_PackedUSD.h"
33 
34 #include <GA/GA_Attribute.h>
35 #include <GA/GA_Handle.h>
36 #include <GA/GA_Names.h>
37 #include <GA/GA_Range.h>
38 #include <UT/UT_ErrorManager.h>
39 
40 #include "pxr/pxr.h"
41 
42 class GA_AttributeFilter;
43 class GT_RefineParms;
44 class GU_Detail;
45 class GU_PrimPacked;
46 
48 
49 /** The default names of the USD ref attributes.
50  @{ */
51 #define GUSD_PATH_ATTR "usdpath"
52 #define GUSD_PRIMPATH_ATTR "usdprimpath"
53 #define GUSD_FRAME_ATTR "frame"
54 #define GUSD_VARIANTS_ATTR "usdvariants"
55 #define GUSD_CONSTRAINT_ATTR "usdconstraint"
56 #define GUSD_PURPOSE_ATTR "usdpurpose"
57 #define GUSD_ACTIVE_ATTR "usdactive"
58 #define GUSD_VISIBLE_ATTR "usdvisible"
59 
60 #define GUSD_OVERTRANSFORMS_ATTR "usdovertransforms"
61 #define GUSD_OVERPOINTS_ATTR "usdoverpoints"
62 #define GUSD_OVERPRIMVARS_ATTR "usdoverprimvars"
63 #define GUSD_OVERALL_ATTR "usdoverall"
64 
65 #define GUSD_WRITESTATICTOPOLOGY_ATTR "usdwritestatictopology"
66 #define GUSD_WRITESTATICPRIMVARS_ATTR "usdwritestaticprimvars"
67 #define GUSD_WRITESTATICGEO_ATTR "usdwritestaticgeo"
68 /** @} */
69 
70 /** USD-related options for GT_RefineParms.
71  @{ */
72 #define GUSD_REFINE_ADDPATHATTRIB "usd:addPathAttribute"
73 #define GUSD_REFINE_PATHATTRIB "usd:pathAttribute"
74 #define GUSD_REFINE_ADDPRIMPATHATTRIB "usd:addPrimPathAttribute"
75 #define GUSD_REFINE_PRIMPATHATTRIB "usd:primPathAttribute"
76 #define GUSD_REFINE_ADDMATERIALPATHATTRIB "usd:addMaterialPathAttribute"
77 #define GUSD_REFINE_ADDVISIBILITYATTRIB "usd:addVisibilityAttribute"
78 #define GUSD_REFINE_IMPORTCOMPUTEDVISIBILITY "usd:importComputedVisibility"
79 #define GUSD_REFINE_ADDXFORMATTRIB "usd:addXformAttribute"
80 #define GUSD_REFINE_NONTRANSFORMINGPATTERN "usd:nonTransformingPrimvarPattern"
81 #define GUSD_REFINE_PRIMVARPATTERN "usd:primvarPattern"
82 #define GUSD_REFINE_IMPORTINHERITEDPRIMVARS "usd:importInheritedPrimvars"
83 #define GUSD_REFINE_ATTRIBUTEPATTERN "usd:attributePattern"
84 #define GUSD_REFINE_TRANSLATESTTOUV "usd:translateSTtoUV"
85 /** @} */
86 
87 /** Set of helpers for working with ranges of prims/points, etc.*/
89 {
90 public:
91  /** Compute an array of offsets from a range.*/
92  static bool OffsetArrayFromRange(const GA_Range& r,
94 
95  /** Compute an array mapping offset->range_index for the given range.*/
96  static bool ComputeRangeIndexMap(const GA_Range& r,
97  GA_OffsetArray& indexMap);
98 
99  static bool
100  BindPrims(GusdStageCacheReader& cache,
101  UT_Array<UsdPrim>& prims,
102  const GA_Detail& gd,
103  const GA_Range& rng,
104  UT_Array<SdfPath>* variants=nullptr,
105  GusdDefaultArray<GusdPurposeSet>* purposes=nullptr,
106  GusdDefaultArray<UsdTimeCode>* times=nullptr,
108 
109  /** Bind prims from references defined in the given attributes.
110  This creates an entry in @a prims for each entry in the given range,
111  mapped to the corresponding prim.
112  If @a variants is non-null, resolved variant paths are stored
113  in the given array.*/
114  static bool
115  BindPrimsFromAttrs(GusdStageCacheReader& cache,
116  UT_Array<UsdPrim>& prims,
117  const GA_Range& rng,
118  const GA_Attribute& pathAttr,
119  const GA_Attribute& primPathAttr,
120  const GA_Attribute* variantsAttr,
121  UT_Array<SdfPath>* variants=nullptr,
123 
124  static bool
125  BindPrimsFromPackedPrims(UT_Array<UsdPrim>& prims,
126  const GA_Range& rng,
127  UT_Array<SdfPath>* variants=nullptr,
128  UT_Array<GusdPurposeSet>* purposes=nullptr,
130 
131  static bool GetTimeCodesFromAttr(const GA_Range& rng,
132  const GA_Attribute& attr,
133  UT_Array<UsdTimeCode>& times);
134 
135  static bool GetTimeCodesFromPackedPrims(const GA_Range& rng,
136  UT_Array<UsdTimeCode>& times);
137 
138  /** Given a string attribute that represents prim paths,
139  return an array of actual prim paths.
140  @{ */
141  static bool GetPrimPathsFromStringAttr(const GA_Attribute& attr,
142  UT_Array<SdfPath>& paths,
144 
145  static bool GetPrimPathsFromStringAttr(const GA_Attribute& attr,
146  const GA_Range& rng,
147  UT_Array<SdfPath>& paths,
149  /** @} */
150 
151 
152  /** Givena string attribute, return an array of tokens.
153  @{ */
154  static bool GetTokensFromStringAttr(const GA_Attribute& attr,
155  UT_Array<TfToken>& tokens,
156  const char* nameSpace=nullptr);
157 
158  static bool GetTokensFromStringAttr(const GA_Attribute& attr,
159  const GA_Range& rng,
160  UT_Array<TfToken>& tokens,
161  const char* nameSpace=nullptr);
162  /** @} */
163 
164 
165  /** Append points to a detail that represent references to prims.
166  The point offsets are contiguous, and the offset of the first
167  point is returned. If any failures occur, and invalid
168  offset is returned.*/
169  static GA_Offset AppendRefPoints(
170  GU_Detail& gd,
171  const UT_Array<UsdPrim>& prims,
172  const char* pathAttrName=GUSD_PATH_ATTR,
173  const char* primPathAttrName=GUSD_PRIMPATH_ATTR);
174 
175  typedef GU_PrimPacked* (*PackedPrimBuildFunc)(
176  GU_Detail& detail,
177  const UT_StringHolder& fileName,
178  const SdfPath& primPath,
179  const UsdTimeCode& frame,
180  const char* lod,
181  const GusdPurposeSet purposes );
182 
183  /** Register a function to be used by AppendPackedPrims to build
184  a packed prim of the given type. */
185  static void RegisterPackedPrimBuildFunc( const TfToken& typeName,
186  PackedPrimBuildFunc func );
187 
188  /** Append packed prims to the given detail that reference the
189  given prims with the given variants. */
190  static bool AppendPackedPrims(
191  GU_Detail& gd,
192  const UT_Array<UsdPrim>& prims,
193  const UT_Array<SdfPath>& variants,
194  const GusdDefaultArray<UsdTimeCode>& times,
196  const GusdDefaultArray<GusdPurposeSet>& purposes,
198  /** More specialized function for appending prims to the given detail
199  from a LOP node where the variants are already chosen, and the LOP
200  node path needs to be set as the file path on the packed prim. */
201  static bool AppendPackedPrimsFromLopNode(
202  GU_Detail& gd,
203  const UT_Array<UsdPrim>& prims,
204  const GusdDefaultArray<UT_StringHolder> &stageids,
205  const GusdDefaultArray<UsdTimeCode>& times,
207  const GusdDefaultArray<GusdPurposeSet>& purposes,
209 
211 
212  /** Append prims @a prims, as an expansion of prims defined on
213  @a srcRange. The prim index pairs provide the prim found in
214  the expansion, and the index of the prim in the source range
215  whose expansion produced that prim.
216  Attributes and groups matching @a filter are copied from the source
217  to the newly created ref points.*/
218  static GA_Offset AppendExpandedRefPoints(
219  GU_Detail& gd,
220  const GA_Detail& srcGd,
221  const GA_Range& srcRng,
222  const UT_Array<PrimIndexPair>& prims,
223  const GA_AttributeFilter& filter,
224  const char* pathAttrName=GUSD_PATH_ATTR,
225  const char* primPathAttrName=GUSD_PRIMPATH_ATTR);
226 
227  static bool AppendExpandedPackedPrims(
228  GU_Detail& gd,
229  const GA_Detail& srcGd,
230  const GA_Range& srcRng,
231  const UT_Array<PrimIndexPair>& primIndexPairs,
232  const UT_Array<SdfPath>& variants,
233  const GusdDefaultArray<UsdTimeCode>& times,
234  const GA_AttributeFilter& filter,
235  bool unpackToPolygons,
236  const UT_String& primvarPattern,
237  const UT_String& attributePattern,
238  bool translateSTtoUV,
239  const UT_StringRef& nonTransformingPrimvarPattern,
241 
242  static bool AppendExpandedPackedPrimsFromLopNode(
243  GU_Detail& gd,
244  const GA_Detail& srcGd,
245  const GA_Range& srcRng,
246  const UT_Array<PrimIndexPair>& primIndexPairs,
247  const GusdDefaultArray<UsdTimeCode>& times,
248  const GA_AttributeFilter& filter,
249  bool unpackToPolygons,
250  const UT_String& primvarPattern,
251  bool importInheritedPrimvars,
252  const UT_String& attributePattern,
253  bool translateSTtoUV,
254  const UT_StringRef &nonTransformingPrimvarPattern,
256  const UT_StringHolder &filePathAttrib = GUSD_PATH_ATTR,
257  const UT_StringHolder &primPathAttrib = GUSD_PRIMPATH_ATTR,
258  const GT_RefineParms *refineParms = nullptr);
259 
260  /** Apply all variant selections in @a selections to each prim
261  in the range, storing the resulting variant path in @a variantsAttr.
262  For each source prim, this will first validate that the
263  variant selection is valid on the target prims.
264  If @a prevVariants is supplied, the variant selections are added
265  on top of any variant selections in the given paths.*/
266  static bool WriteVariantSelectionsToAttr(
267  GU_Detail& gd,
268  const GA_Range& rng,
269  const UT_Array<UsdPrim>& prims,
270  const GusdUSD_Utils::VariantSelArray& selections,
271  const char* variantsAttr=GUSD_VARIANTS_ATTR,
272  const UT_Array<SdfPath>* prevVariants=nullptr);
273 
274  static bool WriteVariantSelectionsToPackedPrims(
275  GU_Detail& gd,
276  const GA_Range& rng,
277  const UT_Array<UsdPrim>& prims,
278  const GusdUSD_Utils::VariantSelArray& selections,
279  const UT_Array<SdfPath>* prevVariants=nullptr);
280 
281  /** Append variant selections defined by @a orderedVariants and
282  @a variantIndices as an expansion of prims from @a srcRng.
283  Attributes and groups matching @a attrs filter are copied from the
284  source to the newly created ref points.*/
285  static GA_Offset AppendRefPointsForExpandedVariants(
286  GU_Detail& gd,
287  const GA_Detail& srcGd,
288  const GA_Range& srcRng,
289  const UT_Array<UT_StringHolder>& orderedVariants,
290  const GusdUSD_Utils::IndexPairArray& variantIndices,
291  const GA_AttributeFilter& filter,
292  const char* variantsAttr=GUSD_VARIANTS_ATTR);
293 
294  static GA_Offset AppendPackedPrimsForExpandedVariants(
295  GU_Detail& gd,
296  const GA_Detail& srcGd,
297  const GA_Range& srcRng,
298  const UT_Array<UT_StringHolder>& orderedVariants,
299  const GusdUSD_Utils::IndexPairArray& variantIndices,
300  const GA_AttributeFilter& filter);
301 
302  static bool GetPackedPrimStageIdsViewportLODsAndPurposes(
303  const GA_Detail& gd,
304  const GA_OffsetArray& offsets,
305  UT_StringArray& stageIds,
306  UT_StringArray& viewportLODs,
307  UT_Array<GusdPurposeSet>& purposes);
308 
309  /** Compute world transforms from attributes over an array of offsets.
310 
311  In addition to using the standard instancing attributes,
312  this supports an additional schema for non-orthonormal transforms,
313  where the basis vectors (rows) of a rotation matrix are stored
314  as normal attributes 'i', 'j', 'k'.*/
315  static bool ComputeTransformsFromAttrs(const GA_Detail& gd,
316  GA_AttributeOwner owner,
317  const GA_OffsetArray& offsets,
318  UT_Matrix4D* xforms);
319 
320  static bool ComputeTransformsFromPackedPrims(const GA_Detail& gd,
321  const GA_OffsetArray& offsets,
322  UT_Matrix4D* xforms);
323 
324  /** Support representations of attributes when authoring new transforms.*/
326  {
327  ORIENTATTR_ORIENT, //! quaternion orient.
328  ORIENTATTR_IJK, //! vec3 i,j,k (can be non-orthogonal).
329  ORIENTATTR_IGNORE
330  };
331 
333  {
334  SCALEATTR_SCALE, //! scale (vec3).
335  SCALEATTR_PSCALE, //! single pscale.
336  SCALEATTR_IGNORE
337  };
338 
339  /** Create and set transform attributes over the given range.
340  The @a indexMap maps offset->range_index, as computed by
341  ComputeRangeIndexMap().*/
342  static bool SetTransformAttrs(GU_Detail& gd,
343  const GA_Range& r,
344  const GA_OffsetArray& indexMap,
345  OrientAttrRepresentation orientRep,
346  ScaleAttrRepresentation scaleRep,
347  const UT_Matrix4D* xforms);
348 
349  static bool SetPackedPrimTransforms(GU_Detail& gd,
350  const GA_Range& r,
351  const UT_Matrix4D* xforms);
352 
353  static bool MultTransformableAttrs(GU_Detail& gd,
354  const GA_Range& r,
355  const GA_OffsetArray& indexMap,
356  const UT_Matrix4D* xforms,
357  bool keepLengths=false,
358  const GA_AttributeFilter* filter=nullptr);
359 
360  /// Imports \p prim as unpacked geometry in \p gd.
361  static bool ImportPrimUnpacked(GU_Detail& gd,
362  const UsdPrim& prim,
364  const char* lod = nullptr,
365  GusdPurposeSet purpose = GusdPurposeSet(
367  const UT_StringRef &primvarPattern = "*",
368  const UT_StringRef &attributePattern = UT_StringHolder::theEmptyString,
369  bool translateSTtoUV = true,
370  const UT_StringRef &nonTransformingPrimvarPattern = GA_Names::rest,
371  const UT_Matrix4D* xform = nullptr,
372  const GT_RefineParms* refineParms = nullptr);
373 };
374 
376 
377 #endif /*_GUSD_GU_USD_H_*/
Definition of a geometry attribute.
Definition: GA_Attribute.h:198
GT_API const UT_StringHolder time
UT_ErrorSeverity
Definition: UT_Error.h:25
quaternion orient.
Definition: GU_USD.h:328
#define GUSD_PRIMPATH_ATTR
Definition: GU_USD.h:52
A range of elements in an index-map.
Definition: GA_Range.h:42
GLuint GLsizei const GLuint const GLintptr * offsets
Definition: glcorearb.h:2621
#define GUSD_PATH_ATTR
Definition: GU_USD.h:51
GA_Size GA_Offset
Definition: GA_Types.h:641
GusdUSD_Traverse::PrimIndexPair PrimIndexPair
Definition: GU_USD.h:210
Definition: token.h:87
static const UT_StringHolder theEmptyString
Methods for USD scene traversal.
std::pair< UsdPrim, exint > PrimIndexPair
Definition: USD_Traverse.h:60
OrientAttrRepresentation
Definition: GU_USD.h:325
Definition: prim.h:135
Definition: path.h:291
GA_AttributeOwner
Definition: GA_Types.h:34
GLenum func
Definition: glcorearb.h:783
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
Container class for all geometry.
Definition: GA_Detail.h:96
#define GUSD_API
Definition: api.h:40
#define GUSD_VARIANTS_ATTR
Definition: GU_USD.h:54
ScaleAttrRepresentation
Definition: GU_USD.h:332
GLboolean r
Definition: glcorearb.h:1222
GusdPurposeSet
Definition: purpose.h:39
GA_API const UT_StringHolder rest
GLint lod
Definition: glcorearb.h:2765
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glcorearb.h:1297