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 // Structure returned by CanApplyEdits that stores errors and warnings
42 // encountered while processing the edits. This struct evaluates to false
43 // if any errors were found, and true otherwise (even if there are warnings).
44 struct CanApplyResult
45 {
46 std::vector<std::string> errors;
47 std::vector<std::string> warnings;
48
49 operator bool() const
50 {
51 return errors.empty();
52 }
53 };
54
55 USD_API
56 explicit UsdNamespaceEditor(const UsdStageRefPtr &stage);
57
58 USD_API
60 const UsdStageRefPtr &stage,
61 EditOptions &&editOptions);
62
63 USD_API
65 const UsdStageRefPtr &stage,
66 const EditOptions &editOptions);
67
87
89 USD_API
90 void AddDependentStage(const UsdStageRefPtr &stage);
91
93 USD_API
94 void RemoveDependentStage(const UsdStageRefPtr &stage);
95
98 USD_API
99 void SetDependentStages(const UsdStageRefPtrVector &stages);
100
102
108 USD_API
110 const SdfPath &path);
111
117 USD_API
119 const SdfPath &path,
120 const SdfPath &newPath);
121
128 USD_API
130 const UsdPrim &prim);
131
139 USD_API
141 const UsdPrim &prim,
142 const TfToken &newName);
143
151 USD_API
153 const UsdPrim &prim,
154 const UsdPrim &newParent);
155
164 USD_API
166 const UsdPrim &prim,
167 const UsdPrim &newParent,
168 const TfToken &newName);
169
175 USD_API
177 const SdfPath &path);
178
185 USD_API
187 const SdfPath &path,
188 const SdfPath &newPath);
189
196 USD_API
198 const UsdProperty &property);
199
207 USD_API
209 const UsdProperty &property,
210 const TfToken &newName);
211
219 USD_API
221 const UsdProperty &property,
222 const UsdPrim &newParent);
223
232 USD_API
234 const UsdProperty &property,
235 const UsdPrim &newParent,
236 const TfToken &newName);
237
245 USD_API
247
255 USD_API
256 bool CanApplyEdits(std::string *whyNot) const;
257
267 USD_API
268 CanApplyResult CanApplyEdits() const;
269
273 USD_API
274 SdfLayerHandleVector GetLayersToEdit();
275
276private:
277
278 // The type of edit that an edit description is describing.
279 enum class _EditType {
280 Invalid,
281
282 Delete,
283 Rename,
284 Reparent
285 };
286
287 // Description of an edit added to this namespace editor.
288 struct _EditDescription {
289 // Path to the existing object.
290 SdfPath oldPath;
291 // New path of the object after the edit is performed. An empty path
292 // indicates that the edit operation will delete the object.
293 SdfPath newPath;
294
295 // Type of the edit as determined by the oldPath and the newPath.
296 _EditType editType = _EditType::Invalid;
297
298 // Whether this describes a property edit or, otherwise, a prim edit.
299 bool IsPropertyEdit() const { return oldPath.IsPrimPropertyPath(); }
300 };
301
302 // Struct representing the Sdf layer edits necessary to apply an edit
303 // description to the stage. We need this to gather all the information we
304 // can about what layer edits need to be performed before we start editing
305 // any specs so that we can avoid partial edits when a composed stage level
306 // namespace would fail.
307 struct _ProcessedEdit
308 {
309 // List of errors encountered that would prevent the overall namespace
310 // edit of the composed stage object from being completed successfully.
311 std::vector<std::string> errors;
312
313 // List of warnings encountered that will not prevent the main edit from
314 // being completed but indicate that some supporting operations may not
315 // have been performed.
316 std::vector<std::string> warnings;
317
318 // The edit description of the primary edit.
319 _EditDescription editDescription;
320
321 // The list of layers that have specs that need to have the Sdf
322 // namespace edit applied.
323 SdfLayerHandleVector layersToEdit;
324
325 // Whether performing the edit will author new relocates.
326 bool willAuthorRelocates = false;
327
328 // Layer edits that need to be performed to update connection and
329 // relationship targets of other properties in order to keep them
330 // targeting the same object after applying this processed edit.
331 struct TargetPathListOpEdit {
332 // Property spec to author the new targets value to. Note that we
333 // store the spec handle for the property as the property spec's
334 // path could change if the property is moved or deleted by the
335 // primary namespace edit.
336 SdfPropertySpecHandle propertySpec;
337
338 // Name of the field that holds the path targets for the property
339 // which differs for attributes vs relationships.
340 TfToken fieldName;
341
342 // Updated list op value to set for the property spec.
343 SdfPathListOp newFieldValue;
344 };
345 std::vector<TargetPathListOpEdit> targetPathListOpEdits;
346
347 // Full set of namespace edits that need to be performed for all the
348 // dependent stages of this editor as a result of dependencies on the
349 // initial spec move edits.
350 PcpDependentNamespaceEdits dependentStageNamespaceEdits;
351
352 // Applies this processed edit, performing the individual edits
353 // necessary to each layer that needs to be updated.
354 bool Apply();
355
356 // Returns whether this processed edit can be applied and any errors
357 // or warnings that it would produce.
358 UsdNamespaceEditor::CanApplyResult CanApply() const;
359 };
360
361 // Adds an edit description for a prim delete operation.
362 bool _AddPrimDelete(const SdfPath &oldPath);
363
364 // Adds an edit description for a prim rename or reparent operation.
365 bool _AddPrimMove(const SdfPath &oldPath, const SdfPath &newPath);
366
367 // Adds an edit description for a property delete operation.
368 bool _AddPropertyDelete(const SdfPath &oldPath);
369
370 // Adds an edit description for a property rename or reparent operation.
371 bool _AddPropertyMove(const SdfPath &oldPath, const SdfPath &newPath);
372
373 // Clears the current procesed edits.
374 void _ClearProcessedEdits();
375
376 // Processes and caches the layer edits necessary for the current edit
377 // operation if there is no cached processecd edit.
378 void _ProcessEditsIfNeeded() const;
379
380 // Helper class for _ProcessEditsIfNeeded. Defined entirely in
381 // implementation. Declared here for private access to the editor
382 // structures.
383 class _EditProcessor;
384
385 UsdStageRefPtr _stage;
386 // Dependent stage order should be arbitrary but we want don't want
387 // duplicates which can cause unnecessary work.
388 using _StageSet = std::unordered_set<UsdStageRefPtr, TfHash>;
389 _StageSet _dependentStages;
390 EditOptions _editOptions;
391 _EditDescription _editDescription;
392 mutable std::optional<_ProcessedEdit> _processedEdit;
393};
394
395PXR_NAMESPACE_CLOSE_SCOPE
396
397#endif // PXR_USD_USD_NAMESPACE_EDITOR_H
398
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:281
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 CanApplyResult CanApplyEdits() const
Returns whether all the added namespace edits stored in this to namespace editor can be applied to it...
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 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...
USD_API bool CanApplyEdits(std::string *whyNot) const
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.