Loading...
Searching...
No Matches
namespaceEditor.h
Go to the documentation of this file.
1//
2// Copyright 2023 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_NAMESPACE_EDITOR_H
8#define PXR_USD_USD_NAMESPACE_EDITOR_H
9
11
12#include "pxr/pxr.h"
13#include "pxr/usd/usd/api.h"
14#include "pxr/usd/usd/common.h"
15#include "pxr/usd/usd/stage.h"
16#include "pxr/usd/pcp/dependentNamespaceEditUtils.h"
18
19PXR_NAMESPACE_OPEN_SCOPE
20
27{
28public:
31 struct EditOptions {
32
39 };
40
41 USD_API
42 explicit UsdNamespaceEditor(const UsdStageRefPtr &stage);
43
44 USD_API
46 const UsdStageRefPtr &stage,
47 EditOptions &&editOptions);
48
49 USD_API
51 const UsdStageRefPtr &stage,
52 const EditOptions &editOptions);
53
73
75 USD_API
76 void AddDependentStage(const UsdStageRefPtr &stage);
77
79 USD_API
80 void RemoveDependentStage(const UsdStageRefPtr &stage);
81
84 USD_API
85 void SetDependentStages(const UsdStageRefPtrVector &stages);
86
88
94 USD_API
96 const SdfPath &path);
97
103 USD_API
105 const SdfPath &path,
106 const SdfPath &newPath);
107
114 USD_API
116 const UsdPrim &prim);
117
125 USD_API
127 const UsdPrim &prim,
128 const TfToken &newName);
129
137 USD_API
139 const UsdPrim &prim,
140 const UsdPrim &newParent);
141
150 USD_API
152 const UsdPrim &prim,
153 const UsdPrim &newParent,
154 const TfToken &newName);
155
161 USD_API
163 const SdfPath &path);
164
171 USD_API
173 const SdfPath &path,
174 const SdfPath &newPath);
175
182 USD_API
184 const UsdProperty &property);
185
193 USD_API
195 const UsdProperty &property,
196 const TfToken &newName);
197
205 USD_API
207 const UsdProperty &property,
208 const UsdPrim &newParent);
209
218 USD_API
220 const UsdProperty &property,
221 const UsdPrim &newParent,
222 const TfToken &newName);
223
231 USD_API
233
240 USD_API
241 bool CanApplyEdits(std::string *whyNot = nullptr) const;
242
246 USD_API
247 SdfLayerHandleVector GetLayersToEdit();
248
249private:
250
251 // The type of edit that an edit description is describing.
252 enum class _EditType {
253 Invalid,
254
255 Delete,
256 Rename,
257 Reparent
258 };
259
260 // Description of an edit added to this namespace editor.
261 struct _EditDescription {
262 // Path to the existing object.
263 SdfPath oldPath;
264 // New path of the object after the edit is performed. An empty path
265 // indicates that the edit operation will delete the object.
266 SdfPath newPath;
267
268 // Type of the edit as determined by the oldPath and the newPath.
269 _EditType editType = _EditType::Invalid;
270
271 // Whether this describes a property edit or, otherwise, a prim edit.
272 bool IsPropertyEdit() const { return oldPath.IsPrimPropertyPath(); }
273 };
274
275 // Struct representing the Sdf layer edits necessary to apply an edit
276 // description to the stage. We need this to gather all the information we
277 // can about what layer edits need to be performed before we start editing
278 // any specs so that we can avoid partial edits when a composed stage level
279 // namespace would fail.
280 struct _ProcessedEdit
281 {
282 // List of errors encountered that would prevent the overall namespace
283 // edit of the composed stage object from being completed successfully.
284 std::vector<std::string> errors;
285
286 // The edit description of the primary edit.
287 _EditDescription editDescription;
288
289 // The list of layers that have specs that need to have the Sdf
290 // namespace edit applied.
291 SdfLayerHandleVector layersToEdit;
292
293 // Whether performing the edit will author new relocates.
294 bool willAuthorRelocates = false;
295
296 // Layer edits that need to be performed to update connection and
297 // relationship targets of other properties in order to keep them
298 // targeting the same object after applying this processed edit.
299 struct TargetPathListOpEdit {
300 // Property spec to author the new targets value to. Note that we
301 // store the spec handle for the property as the property spec's
302 // path could change if the property is moved or deleted by the
303 // primary namespace edit.
304 SdfPropertySpecHandle propertySpec;
305
306 // Name of the field that holds the path targets for the property
307 // which differs for attributes vs relationships.
308 TfToken fieldName;
309
310 // Updated list op value to set for the property spec.
311 SdfPathListOp newFieldValue;
312 };
313 std::vector<TargetPathListOpEdit> targetPathListOpEdits;
314
315 // Full set of namespace edits that need to be performed for all the
316 // dependent stages of this editor as a result of dependencies on the
317 // initial spec move edits.
318 PcpDependentNamespaceEdits dependentStageNamespaceEdits;
319
320 // List of errors encountered that would prevent connection and
321 // relationship target edits from being performed in response to the
322 // namespace edits.
323 std::vector<std::string> targetPathListOpErrors;
324
325 // Applies this processed edit, performing the individual edits
326 // necessary to each layer that needs to be updated.
327 bool Apply();
328
329 // Returns whether this processed edit can be applied.
330 bool CanApply(std::string *whyNot) const;
331 };
332
333 // Adds an edit description for a prim delete operation.
334 bool _AddPrimDelete(const SdfPath &oldPath);
335
336 // Adds an edit description for a prim rename or reparent operation.
337 bool _AddPrimMove(const SdfPath &oldPath, const SdfPath &newPath);
338
339 // Adds an edit description for a property delete operation.
340 bool _AddPropertyDelete(const SdfPath &oldPath);
341
342 // Adds an edit description for a property rename or reparent operation.
343 bool _AddPropertyMove(const SdfPath &oldPath, const SdfPath &newPath);
344
345 // Clears the current procesed edits.
346 void _ClearProcessedEdits();
347
348 // Processes and caches the layer edits necessary for the current edit
349 // operation if there is no cached processecd edit.
350 void _ProcessEditsIfNeeded() const;
351
352 // Helper class for _ProcessEditsIfNeeded. Defined entirely in
353 // implementation. Declared here for private access to the editor
354 // structures.
355 class _EditProcessor;
356
357 UsdStageRefPtr _stage;
358 // Dependent stage order should be arbitrary but we want don't want
359 // duplicates which can cause unnecessary work.
360 using _StageSet = std::unordered_set<UsdStageRefPtr, TfHash>;
361 _StageSet _dependentStages;
362 EditOptions _editOptions;
363 _EditDescription _editDescription;
364 mutable std::optional<_ProcessedEdit> _processedEdit;
365};
366
367PXR_NAMESPACE_CLOSE_SCOPE
368
369#endif // PXR_USD_USD_NAMESPACE_EDITOR_H
370
Structure for bundling all the edits that need to be performed in order to perform a namespace edit a...
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:280
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:71
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 SdfLayerHandleVector GetLayersToEdit()
Returns the list of layers that will be edited if ApplyEdits() is called.
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 void AddDependentStage(const UsdStageRefPtr &stage)
Adds the given stage as a dependent stage of this namespace editor.
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 void SetDependentStages(const UsdStageRefPtrVector &stages)
Sets the list of dependent stages for this namespace editor to stages.
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...
bool allowRelocatesAuthoring
Whether the namespace editor will allow the authoring of relocates in order to perform edits that wou...
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...
USD_API void RemoveDependentStage(const UsdStageRefPtr &stage)
Removes the given stage as a dependent stage of this namespace editor.
Structure for holding the options for how the namespace editor will behave when trying to perform edi...
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a "Prim" as ...
Definition: prim.h:117
Base class for UsdAttribute and UsdRelationship scenegraph objects.
Definition: property.h:38
@ Invalid
Invalid or unknown schema kind.