HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
primCompositionQuery.h
Go to the documentation of this file.
1 //
2 // Copyright 2019 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_PRIM_COMPOSITION_QUERY_H
8 #define PXR_USD_USD_PRIM_COMPOSITION_QUERY_H
9 
10 /// \file usd/primCompositionQuery.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/usd/usd/api.h"
14 #include "pxr/usd/usd/prim.h"
15 
16 #include "pxr/usd/pcp/node.h"
17 #include "pxr/usd/pcp/primIndex.h"
18 
19 #include <memory>
20 
22 
23 /// \class UsdPrimCompositionQueryArc
24 ///
25 /// This represents a composition arc that is returned by a
26 /// UsdPrimCompositionQuery. It contains the node in the composition graph that
27 /// is the target of this arc as well as access to information about how the
28 /// arc was introduced to the composition graph.
29 ///
30 /// \section UsdQueryRootArc Root Arc
31 /// If this arc's \ref GetArcType "arc type" is \ref PcpArcType "PcpArcTypeRoot",
32 /// then this arc represents the root node of the graph. The composition graph's
33 /// root arc is not an authored arc; it exists to target the root node of the
34 /// graph which represents any local opinions that may be defined for the prim
35 /// in the root layer stack.
37 {
38 public:
39  ~UsdPrimCompositionQueryArc() = default;
40 
41  /// \name Target and Introducing Nodes
42  /// These functions access either the target or the introducing nodes in
43  /// the composition graph that this arc represents. The returned node from
44  /// GetTargetNode(), along with a layer obtained from the layer stack
45  /// accessible through the node's GetLayerStack() function, can be used to
46  /// create a \ref UsdEditTarget to direct edits to the target of the arc.
47  /// The same can be done with the introducing node but there are additional
48  /// functions below that are more convenient for directly editing the
49  /// included arcs.
50  ///
51  /// It is important to be aware that the nodes returned by GetTargetNode()
52  /// and GetIntroducingNode() are only valid through the collective lifetime
53  /// of the UsdCompositionQuery and all the UsdPrimCompositionQueryArcs the
54  /// query returns. After the query and all the arcs have gone out of scope
55  /// every PcpNodeRef returned by these two functions will become immediately
56  /// invalid and its behavior will be undefined.
57  /// @{
58 
59  /// Returns the targeted node of this composition arc.
60  USD_API
61  PcpNodeRef GetTargetNode() const;
62 
63  /// Returns the node that introduces this arc into composition graph. This
64  /// is the node where the authored composition opinion exists and is not
65  /// necessarily the target node's parent. If this arc is the
66  /// \ref UsdQueryRootArc "root arc" then this function returns the same
67  /// node as GetTargetNode which is the root node of the composition graph.
68  USD_API
70 
71  /// @}
72 
73  /// \name Arc Target Details
74  /// @{
75 
76  /// Returns the root layer of the layer stack that holds the prim spec
77  /// targeted by this composition arc.
78  USD_API
79  SdfLayerHandle GetTargetLayer() const;
80 
81  /// Returns the path of the prim spec that is targeted by this composition
82  /// arc in the target layer stack.
83  USD_API
84  SdfPath GetTargetPrimPath() const;
85 
86  /// Creates and returns a resolve target that, when passed to a
87  /// UsdAttributeQuery for one of this prim's attributes, causes value
88  /// resolution to only consider node sites weaker than this arc, up to and
89  /// and including this arc's site itself.
90  ///
91  /// If \p subLayer is provided, it must be a layer in this arc's layer stack
92  /// and it will further limit value resolution to only the weaker layers up
93  /// to and including \p subLayer within this layer stack. (This is only with
94  /// respect to this arc; all layers will still be considered in the arcs
95  /// weaker than this arc).
96  USD_API
98  const SdfLayerHandle &subLayer = nullptr) const;
99 
100  /// Creates and returns a resolve target that, when passed to a
101  /// UsdAttributeQuery for one of this prim's attributes, causes value
102  /// resolution to only consider node sites stronger than this arc, not
103  /// including this arc itself (unless \p subLayer is provided).
104  ///
105  /// If \p subLayer is provided, it must be a layer in this arc's layer stack
106  /// and it will cause value resolution to additionally consider layers in
107  /// this arc but only if they are stronger than subLayer within this arc's
108  /// layer stack.
109  USD_API
111  const SdfLayerHandle &subLayer = nullptr) const;
112 
113  /// @}
114 
115  /// \name Arc Editing
116  /// This set of functions returns information about where the specific
117  /// opinions are authored that cause this arc to be included in the
118  /// composition graph. They can be used to edit the composition arcs
119  /// themselves.
120  /// @{
121 
122  /// Returns the specific layer in the layer stack that adds this arc to the
123  /// composition graph. This layer combined with the path returned from
124  /// GetIntroducingPrimPath can be used to find the prim spec which owns
125  /// the field that ultimately causes this arc to exist. If this arc is
126  /// the \ref UsdQueryRootArc "root arc" of the composition graph, it is not
127  /// an authored composition arc and this returns a null layer handle.
128  USD_API
129  SdfLayerHandle GetIntroducingLayer() const;
130 
131  /// Returns the path of the prim that introduces this arc to the composition
132  /// graph within the layer in which the composition opinion is authored.
133  /// This path combined with the layer returned from GetIntroducingLayer
134  /// can be used to find the prim spec which owns the field that ultimately
135  /// causes this arc to exist.
136  /// Note that relocate arcs are authored at the layer metadata and not at
137  /// the prim spec level, so this function will return the path to the
138  /// pseudoroot prim (see SdfLayer::GetPseudoRoot for more details). If this
139  /// arc is the \ref UsdQueryRootArc "root arc" of the composition graph, it
140  /// is not an authored composition arc and this returns an empty path.
141  USD_API
143 
144  /// Gets the list editor and authored SdfReference value that introduces
145  /// this arc to the composition graph for reference arcs. If this arc's
146  /// type is reference, \p editor will be set to the reference list editor
147  /// of the introducing prim spec and \p ref will be set to the authored
148  /// value of the SdfReference in the reference list.
149  ///
150  /// This returns true if the \ref GetArcType "arc type" is
151  /// \ref PcpArcType "reference" and there are no errors; it returns false
152  /// for all other arc types.
153  USD_API
155  SdfReference *ref) const;
156 
157  /// Gets the list editor and authored SdfPayload value that introduces
158  /// this arc to the composition graph for payload arcs. If this arc's
159  /// type is payload, \p editor will be set to the payload list editor
160  /// of the introducing prim spec and \p payload will be set to the authored
161  /// value of the SdfPayload in the payload list.
162  ///
163  /// This returns true if the \ref GetArcType "arc type" is
164  /// \ref PcpArcType "payload" and there are no errors; it returns false
165  /// for all other arc types.
166  USD_API
168  SdfPayload *payload) const;
169 
170  /// Gets the list editor and authored SdfPath value that introduced
171  /// this arc to the composition graph for class arcs. If this arc's
172  /// type is inherit or specialize, \p editor will be set to the
173  /// corresponding path list editor of the introducing prim spec and \p path
174  /// will be set to the authored value of the SdfPath in the path list.
175  ///
176  /// This returns true if the \ref GetArcType "arc type" is either
177  /// \ref PcpArcType "inherit" or \ref PcpArcType "specialize" and
178  /// there are no errors; it returns false for all other arc types.
179  USD_API
181  SdfPath *path) const;
182 
183  /// Gets the list editor and authored string value that introduces
184  /// this arc to the composition graph for variant arcs. If this arc's
185  /// type is variant, \p editor will be set to the name list editor
186  /// of the introducing prim spec and \p name will be set to the authored
187  /// value of the variant set name in the name list.
188  ///
189  /// This returns true if the \ref GetArcType "arc type" is
190  /// \ref PcpArcType "variant" and there are no errors; it returns false
191  /// for all other arc types.
192  USD_API
194  std::string *name) const;
195 
196  /// @}
197 
198  /// \name Arc classification
199  /// Queries about the arc that are useful for classifying the arc for
200  /// filtering.
201  /// @{
202 
203  /// Returns the arc type.
204  USD_API
205  PcpArcType GetArcType() const;
206 
207  /// Returns whether this arc was implicitly added to this prim meaning it
208  /// exists because of the introduction of another composition arc. These
209  /// will typically exist due to inherits or specializes that are authored
210  /// across a reference.
211  USD_API
212  bool IsImplicit() const;
213 
214  /// Returns whether this arc is ancestral, i.e. it exists because it was
215  /// composed in by a namespace parent's prim index.
216  USD_API
217  bool IsAncestral() const;
218 
219  /// Returns whether the target node of this arc contributes any local spec
220  /// opinions that are composed for the prim.
221  USD_API
222  bool HasSpecs() const;
223 
224  /// Returns whether the composition opinion that introduces this arc
225  /// is authored in the root layer stack. This returns true for any arcs
226  /// where the composition opinion can be authored in the root layer stack.
227  /// This is always true for the root arc.
228  USD_API
229  bool IsIntroducedInRootLayerStack() const;
230 
231  /// Returns whether the composition opinion that introduces this arc is
232  /// authored directly on the prim's prim spec within the root layer stack.
233  /// This is always true for the root arc.
234  USD_API
235  bool IsIntroducedInRootLayerPrimSpec() const;
236 
237  /// @}
238 
239 private:
240  // These will only be created by a UsdPrimCompositionQuery itself.
243 
244  PcpNodeRef _node;
245  PcpNodeRef _originalIntroducedNode;
246  PcpNodeRef _introducingNode;
247 
248  std::shared_ptr<PcpPrimIndex> _primIndex;
249 };
250 
251 /// \class UsdPrimCompositionQuery
252 ///
253 /// Object for making optionally filtered composition queries about a prim.
254 /// It creates a list of strength ordering UsdPrimCompositionQueryArc that
255 /// can be filtered by a combination of criteria and returned.
256 ///
257 /// \section UsdPrimCompositionQuery_Invalidation Invalidation
258 /// This object does not listen for change notification. If a consumer is
259 /// holding on to a UsdPrimCompositionQuery, it is their responsibility to
260 /// dispose of it in response to a resync change to the associated prim.
261 /// Failing to do so may result in incorrect values or crashes due to
262 /// dereferencing invalid objects.
264 {
265 public:
266  /// Choices for filtering composition arcs based on arc type
267  enum class ArcTypeFilter
268  {
269  All = 0,
270 
271  // Single arc types
272  Reference,
273  Payload,
274  Inherit,
275  Specialize,
276  Variant,
277  Relocate,
278 
279  // Related arc types
282 
283  // Inverse of related arc types
286  NotVariant,
288  };
289 
290  /// Choices for filtering composition arcs on dependency type. This can
291  /// be direct (arc introduced at the prim's level in namespace) or ancestral
292  /// (arc introduced by a namespace parent of the prim).
294  {
295  All = 0,
296 
297  Direct,
298  Ancestral
299  };
300 
301  /// Choices for filtering composition arcs based on where the arc is
302  /// introduced.
304  {
305  All = 0,
306 
307  // Indicates that we only want arcs that are authored somewhere in the
308  // root layer stack.
310 
311  // Indicates that we only want arcs that are authored directly in the
312  // in the prim's prim spec in the root layer stack.
314  };
315 
316  /// Choices for filtering composition arcs on whether the node contributes
317  /// specs to the prim.
318  enum class HasSpecsFilter
319  {
320  All = 0,
321 
322  HasSpecs,
323  HasNoSpecs
324  };
325 
326  /// Aggregate filter for filtering composition arcs by the previously
327  /// defined criteria.
328  struct Filter
329  {
330  /// Filters by arc type
332 
333  /// Filters by dependency type, direct or ancestral.
335 
336  /// Filters by where the arc is introduced
338 
339  /// Filters by whether the arc provides specs for the prim.
341 
342  Filter() {};
343 
344  bool operator==(const Filter &rhs) {
346  arcTypeFilter == rhs.arcTypeFilter &&
349  };
350 
351  bool operator!=(const Filter &rhs) {
352  return !(*this == rhs);
353  };
354  };
355 
356  /// Returns a prim composition query for the given \p prim with a preset
357  /// filter that only returns reference arcs that are not ancestral.
358  USD_API
360 
361  /// Returns a prim composition query for the given \p prim with a preset
362  /// filter that only returns inherit arcs that are not ancestral.
363  USD_API
365 
366  /// Returns a prim composition query for the given \p prim with a preset
367  /// filter that only returns direct arcs that were introduced by opinions
368  /// defined in a layer in the root layer stack.
369  USD_API
371 
372  /// Create a prim composition query for the \p with the given option
373  /// \p filter.
374  USD_API
375  UsdPrimCompositionQuery(const UsdPrim & prim,
376  const Filter &filter = Filter());
377 
378  ~UsdPrimCompositionQuery() = default;
379 
380  /// Change the filter for this query.
381  USD_API
382  void SetFilter(const Filter &filter);
383 
384  /// Return a copy of the current filter parameters.
385  USD_API
386  Filter GetFilter() const;
387 
388  /// Return a list of composition arcs for this query's prim using the
389  /// current query filter. The composition arcs are always returned in order
390  /// from strongest to weakest regardless of the filter.
391  USD_API
392  std::vector<UsdPrimCompositionQueryArc> GetCompositionArcs();
393 
394 private:
395  UsdPrim _prim;
396  Filter _filter;
397  std::shared_ptr<PcpPrimIndex> _expandedPrimIndex;
398  std::vector<UsdPrimCompositionQueryArc> _unfilteredArcs;
399 };
400 
401 
403 
404 #endif // PXR_USD_USD_PRIM_COMPOSITION_QUERY_H
405 
#define USD_API
Definition: api.h:23
USD_API bool IsImplicit() const
USD_API PcpNodeRef GetTargetNode() const
Returns the targeted node of this composition arc.
ArcIntroducedFilter arcIntroducedFilter
Filters by where the arc is introduced.
~UsdPrimCompositionQueryArc()=default
bool operator==(const Filter &rhs)
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
USD_API SdfLayerHandle GetIntroducingLayer() const
static USD_API UsdPrimCompositionQuery GetDirectRootLayerArcs(const UsdPrim &prim)
ArcTypeFilter arcTypeFilter
Filters by arc type.
USD_API SdfPath GetTargetPrimPath() const
USD_API SdfLayerHandle GetTargetLayer() const
USD_API bool IsIntroducedInRootLayerPrimSpec() const
bool operator!=(const Filter &rhs)
ArcTypeFilter
Choices for filtering composition arcs based on arc type.
USD_API PcpArcType GetArcType() const
Returns the arc type.
USD_API std::vector< UsdPrimCompositionQueryArc > GetCompositionArcs()
GLint ref
Definition: glcorearb.h:124
USD_API PcpNodeRef GetIntroducingNode() const
static USD_API UsdPrimCompositionQuery GetDirectInherits(const UsdPrim &prim)
HasSpecsFilter hasSpecsFilter
Filters by whether the arc provides specs for the prim.
USD_API UsdResolveTarget MakeResolveTargetUpTo(const SdfLayerHandle &subLayer=nullptr) const
USD_API void SetFilter(const Filter &filter)
Change the filter for this query.
USD_API bool GetIntroducingListEditor(SdfReferenceEditorProxy *editor, SdfReference *ref) const
~UsdPrimCompositionQuery()=default
Definition: prim.h:116
GLuint const GLchar * name
Definition: glcorearb.h:786
Definition: path.h:280
static USD_API UsdPrimCompositionQuery GetDirectReferences(const UsdPrim &prim)
USD_API Filter GetFilter() const
Return a copy of the current filter parameters.
USD_API UsdResolveTarget MakeResolveTargetStrongerThan(const SdfLayerHandle &subLayer=nullptr) const
USD_API SdfPath GetIntroducingPrimPath() const
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
USD_API bool IsIntroducedInRootLayerStack() const
USD_API bool HasSpecs() const
USD_API bool IsAncestral() const
DependencyTypeFilter dependencyTypeFilter
Filters by dependency type, direct or ancestral.
USD_API UsdPrimCompositionQuery(const UsdPrim &prim, const Filter &filter=Filter())
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glcorearb.h:1297
PcpArcType
Definition: types.h:27