HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
primvarsAPI.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef USDGEOM_GENERATED_PRIMVARSAPI_H
8 #define USDGEOM_GENERATED_PRIMVARSAPI_H
9 
10 /// \file usdGeom/primvarsAPI.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/usd/usdGeom/api.h"
15 #include "pxr/usd/usd/prim.h"
16 #include "pxr/usd/usd/stage.h"
17 
18 #include "pxr/usd/usdGeom/primvar.h"
19 
20 #include "pxr/base/vt/value.h"
21 
22 #include "pxr/base/gf/vec3d.h"
23 #include "pxr/base/gf/vec3f.h"
24 #include "pxr/base/gf/matrix4d.h"
25 
26 #include "pxr/base/tf/token.h"
27 #include "pxr/base/tf/type.h"
28 
30 
31 class SdfAssetPath;
32 
33 // -------------------------------------------------------------------------- //
34 // PRIMVARSAPI //
35 // -------------------------------------------------------------------------- //
36 
37 /// \class UsdGeomPrimvarsAPI
38 ///
39 /// UsdGeomPrimvarsAPI encodes geometric "primitive variables",
40 /// as UsdGeomPrimvar, which interpolate across a primitive's topology,
41 /// can override shader inputs, and inherit down namespace.
42 ///
43 /// \section usdGeom_PrimvarFetchingAPI Which Method to Use to Retrieve Primvars
44 ///
45 /// While creating primvars is unambiguous (CreatePrimvar()), there are quite
46 /// a few methods available for retrieving primvars, making it potentially
47 /// confusing knowing which one to use. Here are some guidelines:
48 ///
49 /// \li If you are populating a GUI with the primvars already available for
50 /// authoring values on a prim, use GetPrimvars().
51 /// \li If you want all of the "useful" (e.g. to a renderer) primvars
52 /// available at a prim, including those inherited from ancestor prims, use
53 /// FindPrimvarsWithInheritance(). Note that doing so individually for many
54 /// prims will be inefficient.
55 /// \li To find a particular primvar defined directly on a prim, which may
56 /// or may not provide a value, use GetPrimvar().
57 /// \li To find a particular primvar defined on a prim or inherited from
58 /// ancestors, which may or may not provide a value, use
59 /// FindPrimvarWithInheritance().
60 /// \li To *efficiently* query for primvars using the overloads of
61 /// FindPrimvarWithInheritance() and FindPrimvarsWithInheritance(), one
62 /// must first cache the results of FindIncrementallyInheritablePrimvars() for
63 /// each non-leaf prim on the stage.
64 ///
66 {
67 public:
68  /// Compile time constant representing what kind of schema this class is.
69  ///
70  /// \sa UsdSchemaKind
72 
73  /// Construct a UsdGeomPrimvarsAPI on UsdPrim \p prim .
74  /// Equivalent to UsdGeomPrimvarsAPI::Get(prim.GetStage(), prim.GetPath())
75  /// for a \em valid \p prim, but will not immediately throw an error for
76  /// an invalid \p prim
77  explicit UsdGeomPrimvarsAPI(const UsdPrim& prim=UsdPrim())
78  : UsdAPISchemaBase(prim)
79  {
80  }
81 
82  /// Construct a UsdGeomPrimvarsAPI on the prim held by \p schemaObj .
83  /// Should be preferred over UsdGeomPrimvarsAPI(schemaObj.GetPrim()),
84  /// as it preserves SchemaBase state.
85  explicit UsdGeomPrimvarsAPI(const UsdSchemaBase& schemaObj)
86  : UsdAPISchemaBase(schemaObj)
87  {
88  }
89 
90  /// Destructor.
92  virtual ~UsdGeomPrimvarsAPI();
93 
94  /// Return a vector of names of all pre-declared attributes for this schema
95  /// class and all its ancestor classes. Does not include attributes that
96  /// may be authored by custom/extended methods of the schemas involved.
98  static const TfTokenVector &
99  GetSchemaAttributeNames(bool includeInherited=true);
100 
101  /// Return a UsdGeomPrimvarsAPI holding the prim adhering to this
102  /// schema at \p path on \p stage. If no prim exists at \p path on
103  /// \p stage, or if the prim at that path does not adhere to this schema,
104  /// return an invalid schema object. This is shorthand for the following:
105  ///
106  /// \code
107  /// UsdGeomPrimvarsAPI(stage->GetPrimAtPath(path));
108  /// \endcode
109  ///
111  static UsdGeomPrimvarsAPI
112  Get(const UsdStagePtr &stage, const SdfPath &path);
113 
114 
115 protected:
116  /// Returns the kind of schema this class belongs to.
117  ///
118  /// \sa UsdSchemaKind
120  UsdSchemaKind _GetSchemaKind() const override;
121 
122 private:
123  // needs to invoke _GetStaticTfType.
124  friend class UsdSchemaRegistry;
126  static const TfType &_GetStaticTfType();
127 
128  static bool _IsTypedSchema();
129 
130  // override SchemaBase virtuals.
132  const TfType &_GetTfType() const override;
133 
134 public:
135  // ===================================================================== //
136  // Feel free to add custom code below this line, it will be preserved by
137  // the code generator.
138  //
139  // Just remember to:
140  // - Close the class declaration with };
141  // - Close the namespace with PXR_NAMESPACE_CLOSE_SCOPE
142  // - Close the include guard with #endif
143  // ===================================================================== //
144  // --(BEGIN CUSTOM CODE)--
145 
146  /// Author scene description to create an attribute on this prim that
147  /// will be recognized as Primvar (i.e. will present as a valid
148  /// UsdGeomPrimvar).
149  ///
150  /// The name of the created attribute may or may not be the specified
151  /// \p name, due to the possible need to apply property namespacing
152  /// for primvars. See \ref Usd_Creating_and_Accessing_Primvars
153  /// for more information. Creation may fail and return an invalid
154  /// Primvar if \p name contains a reserved keyword, such as the
155  /// "indices" suffix we use for indexed primvars.
156  ///
157  /// The behavior with respect to the provided \p typeName
158  /// is the same as for UsdAttributes::Create(), and
159  /// \p interpolation and \p elementSize are as described in
160  /// UsdGeomPrimvar::GetInterpolation() and UsdGeomPrimvar::GetElementSize().
161  ///
162  /// If \p interpolation and/or \p elementSize are left unspecified, we
163  /// will author no opinions for them, which means any (strongest) opinion
164  /// already authored in any contributing layer for these fields will
165  /// become the Primvar's values, or the fallbacks if no opinions
166  /// have been authored.
167  ///
168  /// \return an invalid UsdGeomPrimvar if we failed to create a valid
169  /// attribute, a valid UsdGeomPrimvar otherwise. It is not an
170  /// error to create over an existing, compatible attribute.
171  ///
172  /// \sa UsdPrim::CreateAttribute(), UsdGeomPrimvar::IsPrimvar()
175  const SdfValueTypeName &typeName,
176  const TfToken& interpolation = TfToken(),
177  int elementSize = -1) const;
178 
179  /// Author scene description to create an attribute and authoring a \p value
180  /// on this prim that will be recognized as a Primvar (i.e. will present as
181  /// a valid UsdGeomPrimvar). Note that unlike CreatePrimvar using this API
182  /// explicitly authors a block for the indices attr associated with the
183  /// primvar, thereby blocking any indices set in any weaker layers.
184  ///
185  /// \return an invalid UsdGeomPrimvar on error, a valid UsdGeomPrimvar
186  /// otherwise. It is fine to call this method multiple times, and in
187  /// different UsdEditTargets, even if there is an existing primvar of the
188  /// same name, indexed or not.
189  ///
190  /// \sa CreatePrimvar(), CreateIndexedPrimvar(), UsdPrim::CreateAttribute(),
191  /// UsdGeomPrimvar::IsPrimvar()
192  template <typename T>
194  const TfToken& name,
195  const SdfValueTypeName &typeName,
196  const T &value,
197  const TfToken &interpolation = TfToken(),
198  int elementSize = -1,
200  {
201  UsdGeomPrimvar primvar =
202  CreatePrimvar(name, typeName, interpolation, elementSize);
203 
204  primvar.GetAttr().Set(value, time);
205  primvar.BlockIndices();
206  return primvar;
207  }
208 
209  /// Author scene description to create an attribute and authoring a \p value
210  /// on this prim that will be recognized as an indexed Primvar with \p
211  /// indices appropriately set (i.e. will present as a valid UsdGeomPrimvar).
212  ///
213  /// \return an invalid UsdGeomPrimvar on error, a valid UsdGeomPrimvar
214  /// otherwise. It is fine to call this method multiple times, and in
215  /// different UsdEditTargets, even if there is an existing primvar of the
216  /// same name, indexed or not.
217  ///
218  /// \sa CreatePrimvar(), CreateNonIndexedPrimvar(),
219  /// UsdPrim::CreateAttribute(), UsdGeomPrimvar::IsPrimvar()
220  template <typename T>
222  const TfToken& name,
223  const SdfValueTypeName &typeName,
224  const T &value,
225  const VtIntArray &indices,
226  const TfToken &interpolation = TfToken(),
227  int elementSize = -1,
229  {
230  UsdGeomPrimvar primvar =
231  CreatePrimvar(name, typeName, interpolation, elementSize);
232 
233  primvar.GetAttr().Set(value, time);
234  primvar.SetIndices(indices, time);
235  return primvar;
236  }
237 
238  /// Author scene description to delete an attribute on this prim that
239  /// was recognized as Primvar (i.e. will present as a valid UsdGeomPrimvar),
240  /// <em>in the current UsdEditTarget</em>.
241  ///
242  /// Because this method can only remove opinions about the primvar
243  /// from the current EditTarget, you may generally find it more useful to
244  /// use BlockPrimvar() which will ensure that all values from the EditTarget
245  /// and weaker layers for the primvar and its indices will be ignored.
246  ///
247  /// Removal may fail and return false if \p name contains a reserved
248  /// keyword, such as the "indices" suffix we use for indexed primvars.
249  ///
250  /// Note this will also remove the indices attribute associated with an
251  /// indiced primvar.
252  ///
253  /// \return true if UsdGeomPrimvar and indices attribute was successfully
254  /// removed, false otherwise.
255  ///
256  /// \sa UsdPrim::RemoveProperty())
258  bool RemovePrimvar(const TfToken& name);
259 
260  /// Remove all time samples on the primvar and its associated indices attr,
261  /// and author a *block* \c default value. This will cause authored opinions
262  /// in weaker layers to be ignored.
263  ///
264  /// \sa UsdAttribute::Block(), UsdGeomPrimvar::BlockIndices
266  void BlockPrimvar(const TfToken& name);
267 
268  /// Return the Primvar object named by \p name, which will
269  /// be valid if a Primvar attribute definition already exists.
270  ///
271  /// Name lookup will account for Primvar namespacing, which means
272  /// that this method will succeed in some cases where
273  /// \code
274  /// UsdGeomPrimvar(prim->GetAttribute(name))
275  /// \endcode
276  /// will not, unless \p name is properly namespace prefixed.
277  ///
278  /// \note Just because a Primvar is valid and defined, and *even if* its
279  /// underlying UsdAttribute (GetAttr()) answers HasValue() affirmatively,
280  /// one must still check the return value of Get(), due to the potential
281  /// of time-varying value blocks (see \ref Usd_AttributeBlocking).
282  ///
283  /// \sa HasPrimvar(), \ref usdGeom_PrimvarFetchingAPI
285  UsdGeomPrimvar GetPrimvar(const TfToken &name) const;
286 
287  /// Return valid UsdGeomPrimvar objects for all defined Primvars on
288  /// this prim, similarly to UsdPrim::GetAttributes().
289  ///
290  /// The returned primvars may not possess any values, and therefore not
291  /// be useful to some clients. For the primvars useful for inheritance
292  /// computations, see GetPrimvarsWithAuthoredValues(), and for primvars
293  /// useful for direct consumption, see GetPrimvarsWithValues().
294  ///
295  /// \sa \ref usdGeom_PrimvarFetchingAPI
297  std::vector<UsdGeomPrimvar> GetPrimvars() const;
298 
299  /// Like GetPrimvars(), but include only primvars that have some
300  /// authored scene description (though not necessarily a value).
301  ///
302  /// \sa \ref usdGeom_PrimvarFetchingAPI
304  std::vector<UsdGeomPrimvar> GetAuthoredPrimvars() const;
305 
306  /// Like GetPrimvars(), but include only primvars that have some
307  /// value, whether it comes from authored scene description or a schema
308  /// fallback.
309  ///
310  /// For most purposes, this method is more useful than GetPrimvars().
311  ///
312  /// \sa \ref usdGeom_PrimvarFetchingAPI
314  std::vector<UsdGeomPrimvar> GetPrimvarsWithValues() const;
315 
316  /// Like GetPrimvars(), but include only primvars that have an **authored**
317  /// value.
318  ///
319  /// This is the query used when computing inheritable primvars, and is
320  /// generally more useful than GetAuthoredPrimvars().
321  ///
322  /// \sa \ref usdGeom_PrimvarFetchingAPI
324  std::vector<UsdGeomPrimvar> GetPrimvarsWithAuthoredValues() const;
325 
326  /// Compute the primvars that can be inherited from this prim by its
327  /// child prims, including the primvars that **this** prim inherits from
328  /// ancestor prims. Inherited primvars will be bound to attributes on
329  /// the corresponding ancestor prims.
330  ///
331  /// Only primvars with **authored**, **non-blocked**,
332  /// **constant interpolation** values are inheritable;
333  /// fallback values are not inherited. The order of the returned
334  /// primvars is undefined.
335  ///
336  /// It is not generally useful to call this method on UsdGeomGprim leaf
337  /// prims, and furthermore likely to be expensive since *most* primvars
338  /// are defined on Gprims.
339  ///
340  /// \sa \ref usdGeom_PrimvarFetchingAPI
342  std::vector<UsdGeomPrimvar> FindInheritablePrimvars() const;
343 
344  /// Compute the primvars that can be inherited from this prim by its
345  /// child prims, starting from the set of primvars inherited from
346  /// this prim's ancestors. If this method returns an empty vector, then
347  /// this prim's children should inherit the same set of primvars available
348  /// to this prim, i.e. the input `inheritedFromAncestors` .
349  ///
350  /// As opposed to FindInheritablePrimvars(), which always recurses up
351  /// through all of the prim's ancestors, this method allows more
352  /// efficient computation of inheritable primvars by starting with the
353  /// list of primvars inherited from this prim's ancestors, and returning
354  /// a newly allocated vector only when this prim makes a change to the
355  /// set of inherited primvars. This enables O(n) inherited primvar
356  /// computation for all prims on a Stage, with potential to share
357  /// computed results that are identical (i.e. when this method returns an
358  /// empty vector, its parent's result can (and must!) be reused for all
359  /// of the prim's children.
360  ///
361  /// \sa \ref usdGeom_PrimvarFetchingAPI
363  std::vector<UsdGeomPrimvar> FindIncrementallyInheritablePrimvars(
364  const std::vector<UsdGeomPrimvar> &inheritedFromAncestors) const;
365 
366  /// Like GetPrimvar(), but if the named primvar does not exist or has no
367  /// authored value on this prim, search for the named, value-producing
368  /// primvar on ancestor prims.
369  ///
370  /// The returned primvar will be bound to the attribute on the
371  /// corresponding ancestor prim on which it was found (if any). If neither
372  /// this prim nor any ancestor contains a value-producing primvar, then
373  /// the returned primvar will be the same as that returned by GetPrimvar().
374  ///
375  /// This is probably the method you want to call when needing to consume
376  /// a primvar of a particular name.
377  ///
378  /// \sa \ref usdGeom_PrimvarFetchingAPI
381 
382  /// \overload
383  ///
384  /// This version of FindPrimvarWithInheritance() takes the pre-computed
385  /// set of primvars inherited from this prim's ancestors, as computed
386  /// by FindInheritablePrimvars() or FindIncrementallyInheritablePrimvars()
387  /// on the prim's parent.
388  ///
389  /// \sa \ref usdGeom_PrimvarFetchingAPI
392  const std::vector<UsdGeomPrimvar> &inheritedFromAncestors) const;
393 
394  /// Find all of the value-producing primvars either defined on this prim,
395  /// or inherited from ancestor prims.
396  ///
397  /// \sa \ref usdGeom_PrimvarFetchingAPI
399  std::vector<UsdGeomPrimvar> FindPrimvarsWithInheritance() const;
400 
401  /// \overload
402  ///
403  /// This version of FindPrimvarsWithInheritance() takes the pre-computed
404  /// set of primvars inherited from this prim's ancestors, as computed
405  /// by FindInheritablePrimvars() or FindIncrementallyInheritablePrimvars()
406  /// on the prim's parent.
407  ///
408  /// \sa \ref usdGeom_PrimvarFetchingAPI
410  std::vector<UsdGeomPrimvar> FindPrimvarsWithInheritance(
411  const std::vector<UsdGeomPrimvar> &inheritedFromAncestors) const;
412 
413  /// Is there a defined Primvar \p name on this prim?
414  ///
415  /// Name lookup will account for Primvar namespacing.
416  ///
417  /// Like GetPrimvar(), a return value of `true` for HasPrimvar() does not
418  /// guarantee the primvar will produce a value.
420  bool HasPrimvar(const TfToken &name) const;
421 
422  /// Is there a Primvar named \p name with an authored value on this
423  /// prim or any of its ancestors?
424  ///
425  /// This is probably the method you want to call when wanting to know
426  /// whether or not the prim "has" a primvar of a particular name.
427  ///
428  /// \sa FindPrimvarWithInheritance()
430  bool HasPossiblyInheritedPrimvar(const TfToken &name) const;
431 
432  /// Test whether a given \p name contains the "primvars:" prefix
433  ///
435  static bool CanContainPropertyName(const TfToken& name);
436 };
437 
439 
440 #endif
USDGEOM_API UsdSchemaKind _GetSchemaKind() const override
USDGEOM_API std::vector< UsdGeomPrimvar > GetPrimvarsWithAuthoredValues() const
UsdGeomPrimvar CreateIndexedPrimvar(const TfToken &name, const SdfValueTypeName &typeName, const T &value, const VtIntArray &indices, const TfToken &interpolation=TfToken(), int elementSize=-1, UsdTimeCode time=UsdTimeCode::Default()) const
Definition: primvarsAPI.h:221
USDGEOM_API UsdGeomPrimvar GetPrimvar(const TfToken &name) const
Non-applied API schema.
GLsizei GLenum const void * indices
Definition: glcorearb.h:406
UsdGeomPrimvarsAPI(const UsdPrim &prim=UsdPrim())
Definition: primvarsAPI.h:77
USDGEOM_API std::vector< UsdGeomPrimvar > FindIncrementallyInheritablePrimvars(const std::vector< UsdGeomPrimvar > &inheritedFromAncestors) const
static constexpr UsdTimeCode Default()
Definition: timeCode.h:113
USDGEOM_API std::vector< UsdGeomPrimvar > GetAuthoredPrimvars() const
USDGEOM_API std::vector< UsdGeomPrimvar > FindPrimvarsWithInheritance() const
GT_API const UT_StringHolder time
GLsizei const GLfloat * value
Definition: glcorearb.h:824
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
USDGEOM_API UsdGeomPrimvar CreatePrimvar(const TfToken &name, const SdfValueTypeName &typeName, const TfToken &interpolation=TfToken(), int elementSize=-1) const
UsdGeomPrimvar CreateNonIndexedPrimvar(const TfToken &name, const SdfValueTypeName &typeName, const T &value, const TfToken &interpolation=TfToken(), int elementSize=-1, UsdTimeCode time=UsdTimeCode::Default()) const
Definition: primvarsAPI.h:193
USDGEOM_API std::vector< UsdGeomPrimvar > FindInheritablePrimvars() const
virtual USDGEOM_API ~UsdGeomPrimvarsAPI()
Destructor.
Definition: token.h:70
static USDGEOM_API const TfTokenVector & GetSchemaAttributeNames(bool includeInherited=true)
static USDGEOM_API bool CanContainPropertyName(const TfToken &name)
Definition: prim.h:116
USDGEOM_API bool HasPrimvar(const TfToken &name) const
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
USDGEOM_API void BlockPrimvar(const TfToken &name)
GLuint const GLchar * name
Definition: glcorearb.h:786
bool Set(const T &value, UsdTimeCode time=UsdTimeCode::Default()) const
Definition: attribute.h:495
Definition: path.h:273
UsdSchemaKind
Definition: common.h:112
USDGEOM_API bool RemovePrimvar(const TfToken &name)
static const UsdSchemaKind schemaKind
Definition: primvarsAPI.h:71
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
USDGEOM_API std::vector< UsdGeomPrimvar > GetPrimvars() const
UsdGeomPrimvarsAPI(const UsdSchemaBase &schemaObj)
Definition: primvarsAPI.h:85
UsdAttribute const & GetAttr() const
Explicit UsdAttribute extractor.
Definition: primvar.h:386
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
USDGEOM_API bool HasPossiblyInheritedPrimvar(const TfToken &name) const
Definition: type.h:47
USDGEOM_API bool SetIndices(const VtIntArray &indices, UsdTimeCode time=UsdTimeCode::Default()) const
#define USDGEOM_API
Definition: api.h:23
USDGEOM_API std::vector< UsdGeomPrimvar > GetPrimvarsWithValues() const
static USDGEOM_API UsdGeomPrimvarsAPI Get(const UsdStagePtr &stage, const SdfPath &path)
USDGEOM_API UsdGeomPrimvar FindPrimvarWithInheritance(const TfToken &name) const
USDGEOM_API void BlockIndices() const