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
40{
41public:
44 struct EditOptions {
45
52 };
53
54 // Structure returned by CanApplyEdits that stores errors and warnings
55 // encountered while processing the edits. This struct evaluates to false
56 // if any errors were found, and true otherwise (even if there are warnings).
57 struct CanApplyResult
58 {
59 std::vector<std::string> errors;
60 std::vector<std::string> warnings;
61
62 operator bool() const
63 {
64 return errors.empty();
65 }
66 };
67
68 USD_API
69 explicit UsdNamespaceEditor(const UsdStageRefPtr &stage);
70
71 USD_API
73 const UsdStageRefPtr &stage,
74 EditOptions &&editOptions);
75
76 USD_API
78 const UsdStageRefPtr &stage,
79 const EditOptions &editOptions);
80
100
102 USD_API
103 void AddDependentStage(const UsdStageRefPtr &stage);
104
106 USD_API
107 void RemoveDependentStage(const UsdStageRefPtr &stage);
108
111 USD_API
112 void SetDependentStages(const UsdStageRefPtrVector &stages);
113
115
121 USD_API
123 const SdfPath &path);
124
130 USD_API
132 const SdfPath &path,
133 const SdfPath &newPath);
134
141 USD_API
143 const UsdPrim &prim);
144
152 USD_API
154 const UsdPrim &prim,
155 const TfToken &newName);
156
164 USD_API
166 const UsdPrim &prim,
167 const UsdPrim &newParent);
168
177 USD_API
179 const UsdPrim &prim,
180 const UsdPrim &newParent,
181 const TfToken &newName);
182
188 USD_API
190 const SdfPath &path);
191
198 USD_API
200 const SdfPath &path,
201 const SdfPath &newPath);
202
209 USD_API
211 const UsdProperty &property);
212
220 USD_API
222 const UsdProperty &property,
223 const TfToken &newName);
224
232 USD_API
234 const UsdProperty &property,
235 const UsdPrim &newParent);
236
245 USD_API
247 const UsdProperty &property,
248 const UsdPrim &newParent,
249 const TfToken &newName);
250
258 USD_API
260
268 USD_API
269 bool CanApplyEdits(std::string *whyNot) const;
270
280 USD_API
281 CanApplyResult CanApplyEdits() const;
282
286 USD_API
287 SdfLayerHandleVector GetLayersToEdit();
288
289private:
290
291 // The type of edit that an edit description is describing.
292 enum class _EditType {
293 Invalid,
294
295 Delete,
296 Rename,
297 Reparent
298 };
299
300 // Description of an edit added to this namespace editor.
301 struct _EditDescription {
302 // Path to the existing object.
303 SdfPath oldPath;
304 // New path of the object after the edit is performed. An empty path
305 // indicates that the edit operation will delete the object.
306 SdfPath newPath;
307
308 // Type of the edit as determined by the oldPath and the newPath.
309 _EditType editType = _EditType::Invalid;
310
311 // Whether this describes a property edit or, otherwise, a prim edit.
312 bool IsPropertyEdit() const { return oldPath.IsPrimPropertyPath(); }
313 };
314
315 // Struct representing the Sdf layer edits necessary to apply an edit
316 // description to the stage. We need this to gather all the information we
317 // can about what layer edits need to be performed before we start editing
318 // any specs so that we can avoid partial edits when a composed stage level
319 // namespace would fail.
320 struct _ProcessedEdit
321 {
322 // List of errors encountered that would prevent the overall namespace
323 // edit of the composed stage object from being completed successfully.
324 std::vector<std::string> errors;
325
326 // List of warnings encountered that will not prevent the main edit from
327 // being completed but indicate that some supporting operations may not
328 // have been performed.
329 std::vector<std::string> warnings;
330
331 // The edit description of the primary edit.
332 _EditDescription editDescription;
333
334 // The list of layers that have specs that need to have the Sdf
335 // namespace edit applied.
336 SdfLayerHandleVector layersToEdit;
337
338 // Whether performing the edit will author new relocates.
339 bool willAuthorRelocates = false;
340
341 // Layer edits that need to be performed to update path-bearing fields
342 // (like connection and relationship targets) in order to keep them
343 // referring to the same object after applying this processed edit.
344 struct PathBearingFieldEdit {
345 // Prim or property spec to author the new value to. Note that we
346 // store the spec handle for the object as the object's path could
347 // change if it is moved or deleted by the primary namespace edit.
348 SdfSpecHandle spec;
349
350 // Name of the path-bearing field.
351 TfToken fieldName;
352
353 // Updated field value to set.
354 VtValue newFieldValue;
355 };
356 std::vector<PathBearingFieldEdit> pathBearingFieldEdits;
357
358 // Full set of namespace edits that need to be performed for all the
359 // dependent stages of this editor as a result of dependencies on the
360 // initial spec move edits.
361 PcpDependentNamespaceEdits dependentStageNamespaceEdits;
362
363 // Applies this processed edit, performing the individual edits
364 // necessary to each layer that needs to be updated.
365 bool Apply();
366
367 // Returns whether this processed edit can be applied and any errors
368 // or warnings that it would produce.
369 UsdNamespaceEditor::CanApplyResult CanApply() const;
370 };
371
372 // Adds an edit description for a prim delete operation.
373 bool _AddPrimDelete(const SdfPath &oldPath);
374
375 // Adds an edit description for a prim rename or reparent operation.
376 bool _AddPrimMove(const SdfPath &oldPath, const SdfPath &newPath);
377
378 // Adds an edit description for a property delete operation.
379 bool _AddPropertyDelete(const SdfPath &oldPath);
380
381 // Adds an edit description for a property rename or reparent operation.
382 bool _AddPropertyMove(const SdfPath &oldPath, const SdfPath &newPath);
383
384 // Clears the current procesed edits.
385 void _ClearProcessedEdits();
386
387 // Processes and caches the layer edits necessary for the current edit
388 // operation if there is no cached processecd edit.
389 void _ProcessEditsIfNeeded() const;
390
391 // Helper class for _ProcessEditsIfNeeded. Defined entirely in
392 // implementation. Declared here for private access to the editor
393 // structures.
394 class _EditProcessor;
395
396 UsdStageRefPtr _stage;
397 // Dependent stage order should be arbitrary but we want don't want
398 // duplicates which can cause unnecessary work.
399 using _StageSet = std::unordered_set<UsdStageRefPtr, TfHash>;
400 _StageSet _dependentStages;
401 EditOptions _editOptions;
402 _EditDescription _editDescription;
403 mutable std::optional<_ProcessedEdit> _processedEdit;
404};
405
406PXR_NAMESPACE_CLOSE_SCOPE
407
408#endif // PXR_USD_USD_NAMESPACE_EDITOR_H
409
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
Provides namespace editing operations, where a namespace edit is an operation that removes or changes...
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
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition value.h:90
@ Invalid
Invalid or unknown schema kind.