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
243private:
244
245 // The type of edit that an edit description is describing.
246 enum class _EditType {
247 Invalid,
248
249 Delete,
250 Rename,
251 Reparent
252 };
253
254 // Description of an edit added to this namespace editor.
255 struct _EditDescription {
256 // Path to the existing object.
257 SdfPath oldPath;
258 // New path of the object after the edit is performed. An empty path
259 // indicates that the edit operation will delete the object.
260 SdfPath newPath;
261
262 // Type of the edit as determined by the oldPath and the newPath.
263 _EditType editType = _EditType::Invalid;
264
265 // Whether this describes a property edit or, otherwise, a prim edit.
266 bool IsPropertyEdit() const { return oldPath.IsPrimPropertyPath(); }
267 };
268
269 // Struct representing the Sdf layer edits necessary to apply an edit
270 // description to the stage. We need this to gather all the information we
271 // can about what layer edits need to be performed before we start editing
272 // any specs so that we can avoid partial edits when a composed stage level
273 // namespace would fail.
274 struct _ProcessedEdit
275 {
276 // List of errors encountered that would prevent the overall namespace
277 // edit of the composed stage object from being completed successfully.
278 std::vector<std::string> errors;
279
280 // The edit description of the primary edit.
281 _EditDescription editDescription;
282
283 // The list of layers that have specs that need to have the Sdf
284 // namespace edit applied.
285 SdfLayerHandleVector layersToEdit;
286
287 // Whether performing the edit will author new relocates.
288 bool willAuthorRelocates = false;
289
290 // Layer edits that need to be performed to update connection and
291 // relationship targets of other properties in order to keep them
292 // targeting the same object after applying this processed edit.
293 struct TargetPathListOpEdit {
294 // Property spec to author the new targets value to. Note that we
295 // store the spec handle for the property as the property spec's
296 // path could change if the property is moved or deleted by the
297 // primary namespace edit.
298 SdfPropertySpecHandle propertySpec;
299
300 // Name of the field that holds the path targets for the property
301 // which differs for attributes vs relationships.
302 TfToken fieldName;
303
304 // Updated list op value to set for the property spec.
305 SdfPathListOp newFieldValue;
306 };
307 std::vector<TargetPathListOpEdit> targetPathListOpEdits;
308
309 // Full set of namespace edits that need to be performed for all the
310 // dependent stages of this editor as a result of dependencies on the
311 // initial spec move edits.
312 PcpDependentNamespaceEdits dependentStageNamespaceEdits;
313
314 // List of errors encountered that would prevent connection and
315 // relationship target edits from being performed in response to the
316 // namespace edits.
317 std::vector<std::string> targetPathListOpErrors;
318
319 // Applies this processed edit, performing the individual edits
320 // necessary to each layer that needs to be updated.
321 bool Apply();
322
323 // Returns whether this processed edit can be applied.
324 bool CanApply(std::string *whyNot) const;
325 };
326
327 // Adds an edit description for a prim delete operation.
328 bool _AddPrimDelete(const SdfPath &oldPath);
329
330 // Adds an edit description for a prim rename or reparent operation.
331 bool _AddPrimMove(const SdfPath &oldPath, const SdfPath &newPath);
332
333 // Adds an edit description for a property delete operation.
334 bool _AddPropertyDelete(const SdfPath &oldPath);
335
336 // Adds an edit description for a property rename or reparent operation.
337 bool _AddPropertyMove(const SdfPath &oldPath, const SdfPath &newPath);
338
339 // Clears the current procesed edits.
340 void _ClearProcessedEdits();
341
342 // Processes and caches the layer edits necessary for the current edit
343 // operation if there is no cached processecd edit.
344 void _ProcessEditsIfNeeded() const;
345
346 // Helper class for _ProcessEditsIfNeeded. Defined entirely in
347 // implementation. Declared here for private access to the editor
348 // structures.
349 class _EditProcessor;
350
351 UsdStageRefPtr _stage;
352 // Dependent stage order should be arbitrary but we want don't want
353 // duplicates which can cause unnecessary work.
354 using _StageSet = std::unordered_set<UsdStageRefPtr, TfHash>;
355 _StageSet _dependentStages;
356 EditOptions _editOptions;
357 _EditDescription _editDescription;
358 mutable std::optional<_ProcessedEdit> _processedEdit;
359};
360
361PXR_NAMESPACE_CLOSE_SCOPE
362
363#endif // PXR_USD_USD_NAMESPACE_EDITOR_H
364
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:274
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 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.