HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
variantSets.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 PXR_USD_USD_VARIANT_SETS_H
8 #define PXR_USD_USD_VARIANT_SETS_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/usd/usd/api.h"
12 #include "pxr/usd/usd/common.h"
13 #include "pxr/usd/usd/editTarget.h"
14 #include "pxr/usd/usd/prim.h"
15 
17 
18 #include <string>
19 #include <vector>
20 
22 
23 
27 
28 class SdfPath;
29 
30 /// \class UsdVariantSet
31 ///
32 /// A UsdVariantSet represents a single VariantSet in USD
33 /// (e.g. modelingVariant or shadingVariant), which can have multiple
34 /// variations that express different sets of opinions about the scene
35 /// description rooted at the prim that defines the VariantSet.
36 ///
37 /// (More detailed description of variants to follow)
38 ///
40 public:
41  /// Author a variant spec for \a variantName in this VariantSet at the
42  /// stage's current EditTarget, in the position specified by
43  /// \p position. Return true if the spec was successfully authored,
44  /// false otherwise.
45  ///
46  /// This will create the VariantSet itself, if necessary, so as long as
47  /// UsdPrim "prim" is valid, the following should always work:
48  /// \code
49  /// UsdVariantSet vs = prim.GetVariantSet("myVariantSet");
50  /// vs.AddVariant("myFirstVariation");
51  /// vs.SetVariantSelection("myFirstVariation");
52  /// {
53  /// UsdEditContext ctx(vs.GetVariantEditContext());
54  /// // Now all of our subsequent edits will go "inside" the
55  /// // 'myFirstVariation' variant of 'myVariantSet'
56  /// }
57  /// \endcode
58  USD_API
59  bool AddVariant(const std::string& variantName,
61 
62  /// Return the composed variant names for this VariantSet, ordered
63  /// lexicographically.
64  USD_API
65  std::vector<std::string> GetVariantNames() const;
66 
67  /// Returns true if this VariantSet already possesses a variant
68  // named \p variantName in any layer.
69  USD_API
70  bool HasAuthoredVariant(const std::string& variantName) const;
71 
72  /// Return the variant selection for this VariantSet. If there is
73  /// no selection, return the empty string.
74  USD_API
75  std::string GetVariantSelection() const;
76 
77  /// Returns true if there is a selection authored for this VariantSet
78  /// in any layer.
79  ///
80  /// If requested, the variant selection (if any) will be returned in
81  /// \p value .
82  USD_API
83  bool HasAuthoredVariantSelection(std::string *value = nullptr) const;
84 
85  /// Author a variant selection for this VariantSet, setting it to
86  /// \a variantName in the stage's current EditTarget. If \p variantName
87  /// is empty, clear the variant selection (see ClearVariantSelection).
88  /// Call BlockVariantSelection to explicitly set an empty variant
89  /// selection.
90  ///
91  /// Return true if the selection was successfully authored or cleared,
92  /// false otherwise.
93  USD_API
94  bool SetVariantSelection(const std::string &variantName);
95 
96  /// Clear any selection for this VariantSet from the current EditTarget.
97  /// Return true on success, false otherwise.
98  USD_API
99  bool ClearVariantSelection();
100 
101  /// Block any weaker selections for this VariantSet by authoring an
102  /// empty string at the stage's current EditTarget.
103  /// Return true on success, false otherwise.
104  USD_API
105  bool BlockVariantSelection();
106 
107  /// Return a \a UsdEditTarget that edits the currently selected variant in
108  /// this VariantSet in \a layer. If there is no currently
109  /// selected variant in this VariantSet, return an invalid EditTarget.
110  ///
111  /// If \a layer is unspecified, then we will use the layer of our prim's
112  /// stage's current UsdEditTarget.
113  ///
114  /// Currently, we require \a layer to be in the stage's local LayerStack
115  /// (see UsdStage::HasLocalLayer()), and will issue an error and return
116  /// an invalid EditTarget if \a layer is not. We may relax this
117  /// restriction in the future, if need arises, but it introduces several
118  /// complications in specification and behavior.
119  USD_API
121  GetVariantEditTarget(const SdfLayerHandle &layer = SdfLayerHandle()) const;
122 
123  /// Helper function for configuring a UsdStage's EditTarget to author
124  /// into the currently selected variant. Returns configuration for a
125  /// UsdEditContext
126  ///
127  /// To begin editing into VariantSet \em varSet's currently selected
128  /// variant:
129  ///
130  /// In C++, we would use the following pattern:
131  /// \code
132  /// {
133  /// UsdEditContext ctxt(varSet.GetVariantEditContext());
134  ///
135  /// // All Usd mutation of the UsdStage on which varSet sits will
136  /// // now go "inside" the currently selected variant of varSet
137  /// }
138  /// \endcode
139  ///
140  /// In python, the pattern is:
141  /// \code{.py}
142  /// with varSet.GetVariantEditContext():
143  /// # Now sending mutations to current variant
144  /// \endcode
145  ///
146  /// See GetVariantEditTarget() for discussion of \p layer parameter
147  USD_API
148  std::pair<UsdStagePtr, UsdEditTarget>
149  GetVariantEditContext(const SdfLayerHandle &layer = SdfLayerHandle()) const;
150 
151 
152  /// Return this VariantSet's held prim.
153  UsdPrim const &GetPrim() const { return _prim; }
154 
155 
156  /// Return this VariantSet's name
157  std::string const &GetName() const { return _variantSetName; }
158 
159 
160  /// Is this UsdVariantSet object usable? If not, calling any of
161  /// its other methods is likely to crash.
162  bool IsValid() const {
163  return static_cast<bool>(_prim);
164  }
165 
166  /// Equivalent to IsValid().
167  explicit operator bool() const {
168  return IsValid();
169  }
170 
171 private:
172  UsdVariantSet(const UsdPrim &prim,
173  const std::string &variantSetName)
174  : _prim(prim)
175  , _variantSetName(variantSetName)
176  {
177  }
178 
179  SdfPrimSpecHandle _CreatePrimSpecForEditing();
180  SdfVariantSetSpecHandle _AddVariantSet(UsdListPosition position);
181 
182  UsdPrim _prim;
183  std::string _variantSetName;
184 
185  friend class UsdPrim;
186  friend class UsdVariantSets;
187 };
188 
189 
190 // TODO:
191 // VariantSet Names are stored as SdListOps, but a VariantSet is an actual spec
192 // (like a Prim). Is it important to make that distinction here?
193 
194 /// \class UsdVariantSets
195 ///
196 /// UsdVariantSets represents the collection of
197 /// \ref UsdVariantSet "VariantSets" that are present on a UsdPrim.
198 ///
199 /// A UsdVariantSets object, retrieved from a prim via
200 /// UsdPrim::GetVariantSets(), provides the API for interrogating and modifying
201 /// the composed list of VariantSets active defined on the prim, and also
202 /// the facility for authoring a VariantSet \em selection for any of those
203 /// VariantSets.
204 ///
206 public:
207 
208  /// Find an existing, or create a new VariantSet on the originating UsdPrim,
209  /// named \p variantSetName.
210  ///
211  /// This step is not always necessary, because if this UsdVariantSets
212  /// object is valid, then
213  /// \code
214  /// varSetsObj.GetVariantSet(variantSetName).AddVariant(variantName);
215  /// \endcode
216  /// will always succeed, creating the VariantSet first, if necessary. This
217  /// method exists for situations in which you want to create a VariantSet
218  /// without necessarily populating it with variants.
219  USD_API
220  UsdVariantSet AddVariantSet(const std::string& variantSetName,
222 
223  // TODO: don't we want remove and reorder, clear, etc. also?
224 
225  /// Compute the list of all VariantSets authored on the originating
226  /// UsdPrim. Always return true. Clear the contents of \p names and store
227  /// the result there.
228  USD_API
229  bool GetNames(std::vector<std::string>* names) const;
230 
231  /// Return a list of all VariantSets authored on the originating UsdPrim.
232  USD_API
233  std::vector<std::string> GetNames() const;
234 
235  UsdVariantSet operator[](const std::string& variantSetName) const {
236  return GetVariantSet(variantSetName);
237  }
238 
239  /// Return a UsdVariantSet object for \p variantSetName. This always
240  /// succeeds, although the returned VariantSet will be invalid if
241  /// the originating prim is invalid
242  USD_API
243  UsdVariantSet GetVariantSet(const std::string& variantSetName) const;
244 
245  /// Returns true if a VariantSet named \p variantSetName exists on
246  /// the originating prim.
247  USD_API
248  bool HasVariantSet(const std::string& variantSetName) const;
249 
250  /// Return the composed variant selection for the VariantSet named
251  /// \a variantSetName. If there is no selection, (or \p variantSetName
252  /// does not exist) return the empty string.
253  USD_API
254  std::string GetVariantSelection(const std::string& variantSetName) const;
255 
256  USD_API
257  bool SetSelection(const std::string& variantSetName,
258  const std::string& variantName);
259 
260  /// Returns the composed map of all variant selections authored on the
261  /// the originating UsdPrim, regardless of whether a corresponding
262  /// variant set exists.
263  USD_API
265 
266 
267 private:
268  explicit UsdVariantSets(const UsdPrim& prim)
269  : _prim(prim)
270  {
271  /* NOTHING */
272  }
273 
274  UsdPrim _prim;
275 
276  friend class UsdPrim;
277 };
278 
279 
281 
282 #endif //PXR_USD_USD_VARIANT_SETS_H
USD_API UsdVariantSet GetVariantSet(const std::string &variantSetName) const
USD_API bool SetSelection(const std::string &variantSetName, const std::string &variantName)
Definition: layer.h:81
USD_API bool ClearVariantSelection()
#define USD_API
Definition: api.h:23
std::string const & GetName() const
Return this VariantSet's name.
Definition: variantSets.h:157
USD_API bool BlockVariantSelection()
GLsizei const GLfloat * value
Definition: glcorearb.h:824
UsdVariantSet operator[](const std::string &variantSetName) const
Definition: variantSets.h:235
std::map< std::string, std::string > SdfVariantSelectionMap
A map of reference variant set names to variants in those sets.
Definition: types.h:258
UsdPrim const & GetPrim() const
Return this VariantSet's held prim.
Definition: variantSets.h:153
USD_API bool HasAuthoredVariant(const std::string &variantName) const
Returns true if this VariantSet already possesses a variant.
OutGridT const XformOp bool bool
GLenum GLuint GLint GLint layer
Definition: glcorearb.h:1299
bool IsValid() const
Definition: variantSets.h:162
USD_API std::string GetVariantSelection(const std::string &variantSetName) const
USD_API std::string GetVariantSelection() const
Definition: prim.h:116
Definition: path.h:273
USD_API std::vector< std::string > GetNames() const
Return a list of all VariantSets authored on the originating UsdPrim.
USD_API bool HasAuthoredVariantSelection(std::string *value=nullptr) const
PXR_NAMESPACE_OPEN_SCOPE SDF_DECLARE_HANDLES(SdfLayer)
USD_API bool HasVariantSet(const std::string &variantSetName) const
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
USD_API bool SetVariantSelection(const std::string &variantName)
USD_API UsdVariantSet AddVariantSet(const std::string &variantSetName, UsdListPosition position=UsdListPositionBackOfPrependList)
SIM_API const UT_StringHolder position
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
USD_API std::vector< std::string > GetVariantNames() const
USD_API SdfVariantSelectionMap GetAllVariantSelections() const
UsdListPosition
Definition: common.h:71
USD_API bool AddVariant(const std::string &variantName, UsdListPosition position=UsdListPositionBackOfPrependList)
USD_API UsdEditTarget GetVariantEditTarget(const SdfLayerHandle &layer=SdfLayerHandle()) const
USD_API std::pair< UsdStagePtr, UsdEditTarget > GetVariantEditContext(const SdfLayerHandle &layer=SdfLayerHandle()) const