HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
references.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_REFERENCES_H
8 #define PXR_USD_USD_REFERENCES_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/prim.h"
14 
16 #include "pxr/usd/sdf/path.h"
17 #include "pxr/usd/sdf/reference.h"
18 
20 
21 /// \class UsdReferences
22 ///
23 /// UsdReferences provides an interface to authoring and introspecting
24 /// references in Usd.
25 ///
26 /// References are the primary operator for "encapsulated aggregation" of
27 /// scene description. \em aggregation means that references let us
28 /// build up rich scenes by composing scene description recorded in a (most
29 /// often) different layer. A scene can reference the same layer many times at
30 /// different locations in a scene's namespace. Referenced scene description
31 /// can be overridden in the referencing (or stronger) layers, allowing each
32 /// instance of the reference to be directly customized/overridden.
33 /// \em Encapsulated means that regardless of how much scene description is in
34 /// the referenced layer, only the scene description under and composed from
35 /// (via other composition arcs in the referenced layer) the targeted prim
36 /// will be composed into the aggregate scene. Multiple references to the
37 /// same layer will result in the layer being opened and retained in memory
38 /// only once, although each referencing prim will compose unique
39 /// \ref PcpPrimIndex "prim indices" for the tree rooted at the referenced prim.
40 ///
41 /// \section Usd_References Important Qualities and Effective Use of References
42 ///
43 /// \li Any prim can host zero, one or multiple references
44 ///
45 /// \li References are \ref SdfListOp "list editable"; that is, they compose
46 /// differently than ordinary properties and metadata. In any given
47 /// LayerStack, each authored reference operation at the same SdfPath
48 /// location in each layer (i.e. on the same prim) will compose into an
49 /// aggregate result by adding to, removing from, or replacing "weaker"
50 /// references.
51 ///
52 /// \li References can target the same LayerStack in which they are authored,
53 /// as long as doing so does not introduce a cycle in the composition graph.
54 /// See \ref Usd_Internal_References
55 ///
56 /// \li The \c identifier component of a reference in the provided API
57 /// can be a resolvable asset-path to some external layer, empty, in which case
58 /// the reference targets the root layer of the LayerStack containing the
59 /// referencing layer, or the identifier of an existing anonymous, in-memory-only
60 /// SdfLayer. Care should be exercised in the latter case: calling Export() on
61 /// an anonymous layer to serialize it to a file will not attempt to replace
62 /// any references to anonymous layers with references to file-backed layers.
63 ///
64 /// \li Opinions brought in by reference on an ancestor prim are weaker than
65 /// opinions brought in by references on a descendant prim.
66 ///
67 /// \subsection Usd_DefaultPrim_References Expressing references without prim paths
68 ///
69 /// References may omit the target prim path if the referenced layer has the
70 /// 'defaultPrim' metadata set. In this case, the reference targets the
71 /// 'defaultPrim' in the referenced layer. A layer's defaultPrim can be
72 /// authored and accessed on a UsdStage whose root layer is the layer in
73 /// question: see UsdStage::GetDefaultPrim() and UsdStage::SetDefaultPrim().
74 /// One can also author defaultPrim directly on an SdfLayer - see
75 /// SdfLayer::GetDefaultPrim(), SdfLayer::SetDefaultPrim().
76 ///
77 /// \subsection Usd_Internal_References Expressing "internal" references to the containing LayerStack
78 ///
79 /// References may omit the identifier specifying the referenced layer. This
80 /// creates an "internal" reference. During composition, the referenced layer
81 /// will be resolved to the root layer of the LayerStack containing the
82 /// layer where the reference was authored. See AddInternalReference().
83 ///
84 /// \subsection Usd_Subroot_References Referencing sub-root prims
85 ///
86 /// References may target any prim in a layer. In the simplest and most
87 /// common case, a root prim in a layer will be referenced. However,
88 /// referencing sub-root prims can be useful in a variety of other cases;
89 /// for example, a user might organize prims into a meaningful hierarchy
90 /// in a layer for display purposes, then use sub-root references to
91 /// reference a selection from that hierarchy into a scene.
92 ///
93 /// Sub-root references have subtle behaviors with respect to opinions
94 /// and composition arcs authored on ancestors of the referenced prim.
95 /// Users should carefully consider this when deciding whether to use
96 /// sub-root references. These issues can be avoided by not authoring
97 /// any properties or metadata on ancestors of prims that are meant to
98 /// be referenced.
99 ///
100 /// Consider the following example:
101 ///
102 /// \code
103 /// * shot.usda | * asset.usda
104 /// |
105 /// #usda 1.0 | #usda 1.0
106 /// |
107 /// over "Class" | class "Class"
108 /// { | {
109 /// over "B" | }
110 /// { |
111 /// over "Model" | def "A" (
112 /// { | inherits = </Class>
113 /// int a = 3 | )
114 /// } | {
115 /// } | token purpose = "render"
116 /// } |
117 /// | def "B" (
118 /// over "A" | variantSets = "type"
119 /// { | variants = {
120 /// over "B" ( | string type = "a"
121 /// # variant selection won't be used | }
122 /// variants = { | )
123 /// string type = "b" | {
124 /// } | variantSet "type" = {
125 /// ) | "a" {
126 /// { | def "Model"
127 /// } | {
128 /// } | int a = 1
129 /// | }
130 /// def "ReferencedModel" ( | }
131 /// references = @./asset.usda@</A/B/Model> | "b" {
132 /// ) | def "Model"
133 /// { | {
134 /// } | int a = 2
135 /// | }
136 /// | }
137 /// | }
138 /// | }
139 /// | }
140 /// \endcode
141 ///
142 /// * Property and metadata opinions on the ancestors of the referenced prim
143 /// *are not* present in the composed stage and will never contribute to any
144 /// computations. In this example, the opinion for the attribute /A.purpose
145 /// in asset.usda will never be visible in the UsdStage for shot.usda.
146 ///
147 /// * Property and metadata opinions due to ancestral composition arcs
148 /// *are* present in the composed stage. In this example, the attribute
149 /// /Class/B/Model.a in shot.usda will be present in the UsdStage for
150 /// shot.usda, even though the inherit arc is authored on an ancestor
151 /// of the referenced prim.
152 ///
153 /// * A consequence of these rules is that users might not be able to
154 /// override ancestral variant selections that affect the referenced prim.
155 /// In this example, the Model prim being referenced comes from the
156 /// variant selection {type=a} on prim /A/B in asset.usda. The {type=b}
157 /// variant cannot be selected in shot.usda, even if prims with the
158 /// same hierarchy happen to exist there. There are various workarounds
159 /// for this; in this example, the {type=b} variant selection could be
160 /// authored on /Class/B/Model in shot.usda instead because of the
161 /// inherit arc that was established on prim /A.
162 ///
163 /// \subsection Usd_Failing_References Reasons why adding a reference may fail, why adding a reference may succeed but still generate errors, and what it all means
164 ///
165 /// AddReference() and SetReferences() can each fail for a number of
166 /// reasons. If one of the specified prim targets for one of the references
167 /// is not a prim, we will generate an error, fail to author any scene
168 /// description, and return \c false. If anything goes wrong in attempting
169 /// to write the reference, we also return false, and the reference will also
170 /// remain unauthored. Otherwise, if the reference was successfully
171 /// authored, we will return \c true. <b>A successful reference authoring
172 /// operation may still generate composition errors!</b> Just because the
173 /// reference you specified was syntactically correct and therefore
174 /// successfully authored, does not imply it was meaningful. If you wish to
175 /// ensure that the reference you are about to author will be meaningfully
176 /// consumable by your stage, you are strongly encouraged to <b>ensure it
177 /// will resolve to an actual file by using
178 /// UsdStage::ResolveIdentifierToEditTarget() before authoring the
179 /// reference.</b>
180 ///
181 /// When adding an internal reference, the given prim path is expected to
182 /// be in the namespace of the owning prim's stage. Sub-root prim paths
183 /// will be translated from this namespace to the namespace of the
184 /// current edit target, if necessary. If a path cannot be translated,
185 /// a coding error will be issued and no changes will be made. Non-sub-root
186 /// paths will not be translated.
187 ///
188 /// Immediately upon successful authoring of the reference (before returning
189 /// from AddReference(), RemoveReference(), ClearReferences(), or
190 /// SetReferences()), the UsdStage on which the reference was authored will
191 /// recompose the subtree rooted at the prim hosting the reference. If the
192 /// provided identifier does not resolve to a layer that is already opened or
193 /// that can be opened in the usd format, \em or if the provided primPath is
194 /// not an actual prim in that layer, the stage's recomposition will
195 /// fail, and pass on composition errors to the client.
196 ///
198  friend class UsdPrim;
199 
200  explicit UsdReferences(const UsdPrim& prim) : _prim(prim) {}
201 
202 public:
203  /// Adds a reference to the reference listOp at the current EditTarget,
204  /// in the position specified by \p position.
205  /// \sa \ref Usd_Failing_References "Why adding references may fail" for
206  /// explanation of expectations on \p ref and what return values and errors
207  /// to expect, and \ref Usd_OM_ListOps for details on list editing and
208  /// composition of listOps.
209  USD_API
210  bool AddReference(const SdfReference& ref,
212 
213  /// \overload
214  USD_API
215  bool AddReference(const std::string &identifier,
216  const SdfPath &primPath,
217  const SdfLayerOffset &layerOffset = SdfLayerOffset(),
219 
220  /// \overload
221  /// \sa \ref Usd_DefaultPrim_References "References Without Prim Paths"
222  USD_API
223  bool AddReference(const std::string &identifier,
224  const SdfLayerOffset &layerOffset = SdfLayerOffset(),
226 
227  /// Add an internal reference to the specified prim.
228  /// \sa \ref Usd_Internal_References "Internal References"
229  USD_API
230  bool AddInternalReference(const SdfPath &primPath,
231  const SdfLayerOffset &layerOffset = SdfLayerOffset(),
233 
234  /// Removes the specified reference from the references listOp at the
235  /// current EditTarget. This does not necessarily eliminate the
236  /// reference completely, as it may be added or set in another layer in
237  /// the same LayerStack as the current EditTarget.
238  /// \sa \ref Usd_OM_ListOps
239  USD_API
240  bool RemoveReference(const SdfReference& ref);
241 
242  /// Removes the authored reference listOp edits at the current EditTarget.
243  /// The same caveats for Remove() apply to Clear(). In fact, Clear() may
244  /// actually increase the number of composed references, if the listOp
245  /// being cleared contained the "remove" operator.
246  /// \sa \ref Usd_OM_ListOps
247  USD_API
248  bool ClearReferences();
249 
250  /// Explicitly set the references, potentially blocking weaker opinions
251  /// that add or remove items.
252  /// \sa \ref Usd_Failing_References "Why adding references may fail" for
253  /// explanation of expectations on \p ref and what return values and errors
254  /// to expect, and \ref Usd_OM_ListOps for details on list editing and
255  /// composition of listOps.
256  USD_API
257  bool SetReferences(const SdfReferenceVector& items);
258 
259  /// Return the prim this object is bound to.
260  const UsdPrim &GetPrim() const { return _prim; }
261 
262  /// \overload
263  UsdPrim GetPrim() { return _prim; }
264 
265  explicit operator bool() { return bool(_prim); }
266 
267 private:
268  UsdPrim _prim;
269 };
270 
272 
273 #endif // PXR_USD_USD_REFERENCES_H
#define USD_API
Definition: api.h:23
USD_API bool SetReferences(const SdfReferenceVector &items)
const UsdPrim & GetPrim() const
Return the prim this object is bound to.
Definition: references.h:260
USD_API bool AddInternalReference(const SdfPath &primPath, const SdfLayerOffset &layerOffset=SdfLayerOffset(), UsdListPosition position=UsdListPositionBackOfPrependList)
UsdPrim GetPrim()
Definition: references.h:263
OutGridT const XformOp bool bool
GLint ref
Definition: glcorearb.h:124
Definition: prim.h:116
Definition: path.h:273
USD_API bool ClearReferences()
USD_API bool AddReference(const SdfReference &ref, UsdListPosition position=UsdListPositionBackOfPrependList)
std::vector< SdfReference > SdfReferenceVector
Definition: reference.h:27
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
SIM_API const UT_StringHolder position
USD_API bool RemoveReference(const SdfReference &ref)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
UsdListPosition
Definition: common.h:71