Loading...
Searching...
No Matches
namespaceEditor.h
Go to the documentation of this file.
1//
2// Copyright 2023 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 PXR_USD_USD_NAMESPACE_EDITOR_H
25#define PXR_USD_USD_NAMESPACE_EDITOR_H
26
28
29#include "pxr/pxr.h"
30#include "pxr/usd/usd/api.h"
31#include "pxr/usd/usd/common.h"
32#include "pxr/usd/usd/stage.h"
34
35
36PXR_NAMESPACE_OPEN_SCOPE
37
44{
45public:
46 USD_API
47 explicit UsdNamespaceEditor(const UsdStageRefPtr &stage);
48
54 USD_API
56 const SdfPath &path);
57
63 USD_API
65 const SdfPath &path,
66 const SdfPath &newPath);
67
74 USD_API
76 const UsdPrim &prim);
77
85 USD_API
87 const UsdPrim &prim,
88 const TfToken &newName);
89
97 USD_API
99 const UsdPrim &prim,
100 const UsdPrim &newParent);
101
110 USD_API
112 const UsdPrim &prim,
113 const UsdPrim &newParent,
114 const TfToken &newName);
115
121 USD_API
123 const SdfPath &path);
124
131 USD_API
133 const SdfPath &path,
134 const SdfPath &newPath);
135
142 USD_API
144 const UsdProperty &property);
145
153 USD_API
155 const UsdProperty &property,
156 const TfToken &newName);
157
165 USD_API
167 const UsdProperty &property,
168 const UsdPrim &newParent);
169
178 USD_API
180 const UsdProperty &property,
181 const UsdPrim &newParent,
182 const TfToken &newName);
183
191 USD_API
193
200 USD_API
201 bool CanApplyEdits(std::string *whyNot = nullptr) const;
202
203private:
204
205 // The type of edit that an edit description is describing.
206 enum class _EditType {
207 Invalid,
208
209 Delete,
210 Rename,
211 Reparent
212 };
213
214 // Description of an edit added to this namespace editor.
215 struct _EditDescription {
216 // Path to the existing object.
217 SdfPath oldPath;
218 // New path of the object after the edit is performed. An empty path
219 // indicates that the edit operation will delete the object.
220 SdfPath newPath;
221
222 // Type of the edit as determined by the oldPath and the newPath.
223 _EditType editType = _EditType::Invalid;
224
225 // Whether this describes a property edit or, otherwise, a prim edit.
226 bool IsPropertyEdit() const { return oldPath.IsPrimPropertyPath(); }
227 };
228
229 // Struct representing the Sdf layer edits necessary to apply an edit
230 // description to the stage. We need this to gather all the information we
231 // can about what layer edits need to be performed before we start editing
232 // any specs so that we can avoid partial edits when a composed stage level
233 // namespace would fail.
234 struct _ProcessedEdit
235 {
236 // List of errors encountered that would prevent the overall namespace
237 // edit of the composed stage object from being completed successfully.
238 std::vector<std::string> errors;
239
240 // The Sdf batch namespace edit that needs to be applied to each layer
241 // with specs.
243
244 // The list of layers that have specs that need to have the Sdf
245 // namespace edit applied.
246 SdfLayerHandleVector layersToEdit;
247
248 // Layer edits that need to be performed to update connection and
249 // relationship targets of other properties in order to keep them
250 // targeting the same object after applying this processed edit.
251 struct TargetPathListOpEdit {
252 // Property spec to author the new targets value to. Note that we
253 // store the spec handle for the property as the property spec's
254 // path could change if the property is moved or deleted by the
255 // primary namespace edit.
256 SdfPropertySpecHandle propertySpec;
257
258 // Name of the field that holds the path targets for the property
259 // which differs for attributes vs relationships.
260 TfToken fieldName;
261
262 // Updated list op value to set for the property spec.
263 SdfPathListOp newFieldValue;
264 };
265 std::vector<TargetPathListOpEdit> targetPathListOpEdits;
266
267 // List of errors encountered that would prevent connection and
268 // relationship target edits from being performed in response to the
269 // namespace edits.
270 std::vector<std::string> targetPathListOpErrors;
271
272 // Reparent edits may require overs to be created for the new parent if
273 // a layer doesn't have any specs for the parent yet. This specifies the
274 // path of the parent specs to create if need.
275 SdfPath createParentSpecIfNeededPath;
276
277 // Some edits want to remove inert ancestor overs after a prim is
278 // removed from its parent spec in a layer.
279 bool removeInertAncestorOvers = false;
280
281 // Whether the edit would require relocates (or deactivation for delete)
282 bool requiresRelocates = false;
283
284 // Applies this processed edit, performing the individual edits
285 // necessary to each layer that needs to be updated.
286 bool Apply();
287
288 // Returns whether this processed edit can be applied.
289 bool CanApply(std::string *whyNot) const;
290 };
291
292 // Adds an edit description for a prim delete operation.
293 bool _AddPrimDelete(const SdfPath &oldPath);
294
295 // Adds an edit description for a prim rename or reparent operation.
296 bool _AddPrimMove(const SdfPath &oldPath, const SdfPath &newPath);
297
298 // Adds an edit description for a property delete operation.
299 bool _AddPropertyDelete(const SdfPath &oldPath);
300
301 // Adds an edit description for a property rename or reparent operation.
302 bool _AddPropertyMove(const SdfPath &oldPath, const SdfPath &newPath);
303
304 // Clears the current procesed edits.
305 void _ClearProcessedEdits();
306
307 // Processes and caches the layer edits necessary for the current edit
308 // operation if there is no cached processecd edit.
309 void _ProcessEditsIfNeeded() const;
310
311 // Helper class for _ProcessEditsIfNeeded. Defined entirely in
312 // implementation. Declared here for private access to the editor
313 // structures.
314 class _EditProcessor;
315
316 UsdStageRefPtr _stage;
317 _EditDescription _editDescription;
318 mutable std::optional<_ProcessedEdit> _processedEdit;
319};
320
321PXR_NAMESPACE_CLOSE_SCOPE
322
323#endif // PXR_USD_USD_NAMESPACE_EDITOR_H
324
A description of an arbitrarily complex namespace edit.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
SDF_API bool IsPrimPropertyPath() const
Returns whether the path identifies a prim's property.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:88
USD_API bool MovePrimAtPath(const SdfPath &path, const SdfPath &newPath)
Adds an edit operation to move the composed prim at the given path on this namespace editor's stage t...
USD_API bool DeletePrimAtPath(const SdfPath &path)
Adds an edit operation to delete the composed prim at the given path from this namespace editor's sta...
USD_API bool RenameProperty(const UsdProperty &property, const TfToken &newName)
Adds an edit operation to rename the composed property at the path of property on this namespace edit...
USD_API bool ApplyEdits()
Applies all the added namespace edits stored in this to namespace editor to its stage by authoring al...
USD_API bool ReparentProperty(const UsdProperty &property, const UsdPrim &newParent)
Adds an edit operation to reparent the composed property at the path of property on this namespace ed...
USD_API bool DeleteProperty(const UsdProperty &property)
Adds an edit operation to delete the composed property at the path of property from this namespace ed...
USD_API bool ReparentPrim(const UsdPrim &prim, const UsdPrim &newParent)
Adds an edit operation to reparent the composed prim at the path of prim on this namespace editor's s...
USD_API bool MovePropertyAtPath(const SdfPath &path, const SdfPath &newPath)
Adds an edit operation to move the composed property at the given path on this namespace editor's sta...
USD_API bool ReparentProperty(const UsdProperty &property, const UsdPrim &newParent, const TfToken &newName)
Adds an edit operation to reparent the composed property at the path of property on this namespace ed...
USD_API bool ReparentPrim(const UsdPrim &prim, const UsdPrim &newParent, const TfToken &newName)
Adds an edit operation to reparent the composed prim at the path of prim on this namespace editor's s...
USD_API bool CanApplyEdits(std::string *whyNot=nullptr) const
Returns whether all the added namespace edits stored in this to namespace editor can be applied to it...
USD_API bool RenamePrim(const UsdPrim &prim, const TfToken &newName)
Adds an edit operation to rename the composed prim at the path of prim on this namespace editor's sta...
USD_API bool DeletePrim(const UsdPrim &prim)
Adds an edit operation to delete the composed prim at the path of prim from this namespace editor's s...
USD_API bool DeletePropertyAtPath(const SdfPath &path)
Adds an edit operation to delete the composed property at the given path from this namespace editor's...
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a "Prim" as ...
Definition: prim.h:134
Base class for UsdAttribute and UsdRelationship scenegraph objects.
Definition: property.h:55
@ Invalid
Invalid or unknown schema kind.