-
Notifications
You must be signed in to change notification settings - Fork 208
/
Copy pathUsdUndoInsertChildCommand.cpp
370 lines (312 loc) · 14 KB
/
UsdUndoInsertChildCommand.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
//
// Copyright 2020 Autodesk
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "UsdUndoInsertChildCommand.h"
#include <usdUfe/base/tokens.h>
#include <usdUfe/ufe/UfeNotifGuard.h>
#include <usdUfe/ufe/Utils.h>
#include <usdUfe/undo/UsdUndoBlock.h>
#include <usdUfe/utils/editRouter.h>
#include <usdUfe/utils/editRouterContext.h>
#include <usdUfe/utils/layers.h>
#include <usdUfe/utils/loadRules.h>
#include <usdUfe/utils/mergePrims.h>
#include <usdUfe/utils/usdUtils.h>
#include <pxr/base/tf/token.h>
#include <pxr/usd/sdf/copyUtils.h>
#ifdef USD_HAS_NAMESPACE_EDIT
#include <pxr/usd/sdf/namespaceEdit.h>
#endif
#include <pxr/usd/usd/editContext.h>
#include <pxr/usd/usd/stage.h>
#include <pxr/usd/usdGeom/gprim.h>
#include <pxr/usd/usdShade/material.h>
#include <pxr/usd/usdShade/nodeGraph.h>
#include <pxr/usd/usdShade/shader.h>
#include <ufe/log.h>
#include <ufe/pathString.h>
#include <ufe/scene.h>
#include <ufe/sceneNotification.h>
#define UFE_ENABLE_ASSERTS
#include <ufe/ufeAssert.h>
namespace {
// shared_ptr requires public ctor, dtor, so derive a class for it.
template <class T> struct MakeSharedEnabler : public T
{
MakeSharedEnabler(
const UsdUfe::UsdSceneItem::Ptr& parent,
const UsdUfe::UsdSceneItem::Ptr& child,
const UsdUfe::UsdSceneItem::Ptr& pos)
: T(parent, child, pos)
{
}
};
} // namespace
namespace USDUFE_NS_DEF {
USDUFE_VERIFY_CLASS_SETUP(Ufe::InsertChildCommand, UsdUndoInsertChildCommand);
UsdUndoInsertChildCommand::UsdUndoInsertChildCommand(
const UsdSceneItem::Ptr& parent,
const UsdSceneItem::Ptr& child,
const UsdSceneItem::Ptr& /* pos */)
: Ufe::InsertChildCommand()
, _ufeDstItem(nullptr)
, _ufeSrcPath(child->path())
, _ufeParentPath(parent->path())
, _usdSrcPath(child->prim().GetPath())
{
const auto& childPrim = child->prim();
const auto& parentPrim = parent->prim();
// Don't allow parenting to a Gprim.
// USD strongly discourages parenting of one gprim to another.
// https://graphics.pixar.com/usd/docs/USD-Glossary.html#USDGlossary-Gprim
if (parentPrim.IsA<UsdGeomGprim>()) {
std::string err = TfStringPrintf(
"Parenting geometric prim [%s] under geometric prim [%s] is not allowed "
"Please parent geometric prims under separate XForms and reparent between XForms.",
childPrim.GetName().GetString().c_str(),
parentPrim.GetName().GetString().c_str());
throw std::runtime_error(err);
}
// UsdShadeShader can only have UsdShadeNodeGraph and UsdShadeMaterial as parent.
if (childPrim.IsA<UsdShadeShader>() && !parentPrim.IsA<UsdShadeNodeGraph>()) {
std::string err = TfStringPrintf(
"Parenting Shader prim [%s] under %s prim [%s] is not allowed. "
"Shader prims can only be parented under NodeGraphs and Materials.",
childPrim.GetName().GetString().c_str(),
parentPrim.GetTypeName().GetString().c_str(),
parentPrim.GetName().GetString().c_str());
throw std::runtime_error(err);
}
// UsdShadeNodeGraph can only have a UsdShadeNodeGraph and UsdShadeMaterial as parent.
if (childPrim.IsA<UsdShadeNodeGraph>() && !childPrim.IsA<UsdShadeMaterial>()
&& !parentPrim.IsA<UsdShadeNodeGraph>()) {
std::string err = TfStringPrintf(
"Parenting NodeGraph prim [%s] under %s prim [%s] is not allowed. "
"NodeGraph prims can only be parented under NodeGraphs and Materials.",
childPrim.GetName().GetString().c_str(),
parentPrim.GetTypeName().GetString().c_str(),
parentPrim.GetName().GetString().c_str());
throw std::runtime_error(err);
}
// UsdShadeMaterial cannot have UsdShadeShader, UsdShadeNodeGraph or UsdShadeMaterial as parent.
if (childPrim.IsA<UsdShadeMaterial>()
&& (parentPrim.IsA<UsdShadeShader>() || parentPrim.IsA<UsdShadeNodeGraph>())) {
std::string err = TfStringPrintf(
"Parenting Material prim [%s] under %s prim [%s] is not allowed.",
childPrim.GetName().GetString().c_str(),
parentPrim.GetTypeName().GetString().c_str(),
parentPrim.GetName().GetString().c_str());
throw std::runtime_error(err);
}
// Reparenting directly under an instance prim is disallowed
if (parentPrim.IsInstance()) {
std::string err = TfStringPrintf(
"Parenting geometric prim [%s] under instance prim [%s] is not allowed.",
childPrim.GetName().GetString().c_str(),
parentPrim.GetName().GetString().c_str());
throw std::runtime_error(err);
}
// Apply restriction rules
UsdUfe::applyCommandRestriction(childPrim, "reparent");
// Note: the parent is only receiving the prim, so it can be declared
// in a weaker layer.
const bool allowStronger = true;
UsdUfe::applyCommandRestriction(parentPrim, "reparent", allowStronger);
}
#ifdef UFE_V4_FEATURES_AVAILABLE
std::string UsdUndoInsertChildCommand::commandString() const
{
return std::string("InsertChild ") + Ufe::PathString::string(_ufeSrcPath) + " "
+ Ufe::PathString::string(_ufeParentPath);
}
#endif
/*static*/
UsdUndoInsertChildCommand::Ptr UsdUndoInsertChildCommand::create(
const UsdSceneItem::Ptr& parent,
const UsdSceneItem::Ptr& child,
const UsdSceneItem::Ptr& pos)
{
if (!parent || !child) {
return nullptr;
}
// Error if requested parent is currently a child of requested child.
if (parent->path().startsWith(child->path())) {
return nullptr;
}
return std::make_shared<MakeSharedEnabler<UsdUndoInsertChildCommand>>(parent, child, pos);
}
static void doInsertion(
const SdfPath& srcUsdPath,
const Ufe::Path& srcUfePath,
const SdfPath& dstUsdPath,
const Ufe::Path& dstUfePath)
{
UsdUfe::InAddOrDeleteOperation ad;
UsdPrim srcPrim = ufePathToPrim(srcUfePath);
UsdStageRefPtr stage = srcPrim.GetStage();
// Enforce the edit routing for the insert-child command in order to find
// the target layer. The edit router context sets the edit target of the
// stage of the given prim, if it gets routed.
OperationEditRouterContext ctx(UsdUfe::EditRoutingTokens->RouteParent, srcPrim);
const SdfLayerHandle& dstLayer = stage->GetEditTarget().GetLayer();
enforceMutedLayer(srcPrim, "reparent");
// TODO: the replication of extra information is missing in UsdUfe.
// In MayaUsd, MayaUsd::ufe::ReplicateExtrasToUSD duplicates
// the Maya display layer information. This is not supported
// in UsdUfe at the moment. See the UsdUndoDuplicateCommand,
// which is still in MayaUsd for an example of how it is done.
// Note: the USD duplication code below is similar to the one in
// UsdUndoDuplicateCommand, except it targets an arbitrary
// destination location. This is why the duplicate command
// could not be used directly as a sub-command.
// Make sure all necessary parents exist in the target layer, at least as over,
// otherwise SdfCopySepc will fail.
SdfJustCreatePrimInLayer(dstLayer, dstUsdPath.GetParentPath());
// Retrieve the local layers around where the prim is defined and order them
// from weak to strong. That weak-to-strong order allows us to copy the weakest
// opinions first, so that they will get over-written by the stronger opinions.
SdfPrimSpecHandleVector authLayerAndPaths = UsdUfe::getDefiningPrimStack(srcPrim);
std::reverse(authLayerAndPaths.begin(), authLayerAndPaths.end());
// If no local layers were affected, then it means the prim is not local.
// It probably is inside a reference and we do not support reparent from within
// reference at this point. Report the error and abort the command.
if (0 == authLayerAndPaths.size()) {
const std::string error = TfStringPrintf(
"Cannot reparent prim \"%s\" because we found no local layer containing it.",
srcPrim.GetPath().GetText());
TF_WARN("%s", error.c_str());
throw std::runtime_error(error);
}
#ifdef USD_HAS_NAMESPACE_EDIT
// Try to use a single-layer renaming namespace edit.
// This only works correctly if there is a single layer and the destination layer
// is the same as the source layer. If it fails we will fall through to the other
// algorithm below.
if (1 == authLayerAndPaths.size()) {
SdfBatchNamespaceEdit edits;
const auto parentPath = dstUsdPath.GetParentPath();
edits.Add(SdfNamespaceEdit::Reparent(srcUsdPath, parentPath, SdfNamespaceEdit::Same));
if (dstLayer->Apply(edits)) {
return;
}
}
#endif
UsdUfe::MergePrimsOptions options;
options.verbosity = UsdUfe::MergeVerbosity::None;
options.mergeChildren = true;
bool isFirst = true;
for (const SdfPrimSpecHandle& layerAndPath : authLayerAndPaths) {
const auto layer = layerAndPath->GetLayer();
const auto path = layerAndPath->GetPath();
const bool result = isFirst
? SdfCopySpec(layer, path, dstLayer, dstUsdPath)
: UsdUfe::mergePrims(stage, layer, path, stage, dstLayer, dstUsdPath, options);
if (!result) {
const std::string error = TfStringPrintf(
"Insert child command: moving prim \"%s\" to \"%s\" failed in layer \"%s\".",
srcUsdPath.GetString().c_str(),
dstUsdPath.GetString().c_str(),
layer->GetDisplayName().c_str());
TF_WARN("%s", error.c_str());
throw std::runtime_error(error);
}
isFirst = false;
}
// Remove all scene descriptions for the source path and its subtree in the source layer.
// Note: is the layer targeting really needed? We are removing the prim entirely.
PrimLayerFunc removeFunc
= [stage, srcUsdPath](const UsdPrim& prim, const PXR_NS::SdfLayerRefPtr& layer) {
UsdEditContext ctx(stage, layer);
if (!stage->RemovePrim(srcUsdPath)) {
const std::string error = TfStringPrintf(
"Insert child command: removing prim \"%s\" in layer \"%s\" failed.",
srcUsdPath.GetString().c_str(),
layer->GetDisplayName().c_str());
TF_WARN("%s", error.c_str());
throw std::runtime_error(error);
}
};
applyToAllLayersWithOpinions(srcPrim, removeFunc);
}
static void
preserveLoadRules(const Ufe::Path& srcUfePath, const SdfPath& srcUsdPath, const SdfPath& dstUsdPath)
{
UsdPrim srcPrim = ufePathToPrim(srcUfePath);
const UsdStagePtr stage = srcPrim.GetStage();
// Make sure the load state of the reparented prim will be preserved.
// We copy all rules that applied to it specifically and remove the rules
// that applied to it specifically.
duplicateLoadRules(*stage, srcUsdPath, dstUsdPath);
removeRulesForPath(*stage, srcUsdPath);
}
static const UsdSceneItem::Ptr
sendReparentNotification(const Ufe::Path& srcUfePath, const Ufe::Path& dstUfePath)
{
UsdPrim dstPrim = ufePathToPrim(dstUfePath);
const UsdSceneItem::Ptr ufeDstItem = UsdSceneItem::create(dstUfePath, dstPrim);
sendNotification<Ufe::ObjectReparent>(ufeDstItem, srcUfePath);
return ufeDstItem;
}
void UsdUndoInsertChildCommand::execute()
{
InPathChange pc;
if (_usdDstPath.IsEmpty()) {
const auto& parentPrim = ufePathToPrim(_ufeParentPath);
// First, check if we need to rename the child.
const auto childName = uniqueChildName(parentPrim, _ufeSrcPath.back().string());
// Create a new segment if parent and child are in different run-times.
// parenting a USD node to the proxy shape node implies two different run-times
auto cRtId = _ufeSrcPath.runTimeId();
if (_ufeParentPath.runTimeId() == cRtId) {
_ufeDstPath = _ufeParentPath + childName;
} else {
auto cSep = _ufeSrcPath.getSegments().back().separator();
_ufeDstPath
= _ufeParentPath + Ufe::PathSegment(Ufe::PathComponent(childName), cRtId, cSep);
}
_usdDstPath = parentPrim.GetPath().AppendChild(TfToken(childName));
}
// Load rules must be duplicated before the prim is moved to be able
// to access the existing rules.
preserveLoadRules(_ufeSrcPath, _usdSrcPath, _usdDstPath);
// We need to keep the generated item to be able to return it to the caller
// via the insertedChild() member function.
{
UsdUndoBlock undoBlock(&_undoableItem);
doInsertion(_usdSrcPath, _ufeSrcPath, _usdDstPath, _ufeDstPath);
}
_ufeDstItem = sendReparentNotification(_ufeSrcPath, _ufeDstPath);
}
void UsdUndoInsertChildCommand::undo()
{
InPathChange pc;
// Load rules must be duplicated before the prim is moved to be able
// to access the existing rules.
// Note: the arguments passed are the opposite of those in execute and redo().
preserveLoadRules(_ufeDstPath, _usdDstPath, _usdSrcPath);
_undoableItem.undo();
// Note: the arguments passed are the opposite of those in execute and redo().
sendReparentNotification(_ufeDstPath, _ufeSrcPath);
}
void UsdUndoInsertChildCommand::redo()
{
InPathChange pc;
// Load rules must be duplicated before the prim is moved to be able
// to access the existing rules.
preserveLoadRules(_ufeSrcPath, _usdSrcPath, _usdDstPath);
_undoableItem.redo();
_ufeDstItem = sendReparentNotification(_ufeSrcPath, _ufeDstPath);
}
} // namespace USDUFE_NS_DEF