-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathmayaCommandHook.cpp
More file actions
387 lines (334 loc) · 11 KB
/
Copy pathmayaCommandHook.cpp
File metadata and controls
387 lines (334 loc) · 11 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//
// 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 "mayaCommandHook.h"
#include "abstractCommandHook.h"
#include "mayaSessionState.h"
#include <mayaUsd/undo/OpUndoItems.h>
#include <mayaUsd/utils/layerLocking.h>
#include <mayaUsd/utils/layers.h>
#include <mayaUsd/utils/util.h>
#include <mayaUsdUI/ui/undoChunkUtils.h>
#include <pxr/usd/usd/prim.h>
#include <pxr/usd/usd/primRange.h>
#include <pxr/usd/usd/stage.h>
#include <maya/MGlobal.h>
#include <maya/MString.h>
#include <ufe/hierarchy.h>
#include <ufe/path.h>
#include <ufe/pathString.h>
#include <ufe/selection.h>
#include <QtCore/QStringList>
#include <cassert>
#include <iomanip>
#include <sstream>
#include <string>
#define STR(x) std::string(x)
namespace {
std::string quoteForCommand(const std::string& string)
{
std::ostringstream oss;
oss << " " << std::quoted(string);
return oss.str();
}
std::string quoteLayerIdentifierForCommand(const PXR_NS::SdfLayerRefPtr& usdLayer)
{
if (!usdLayer) {
return "";
}
return quoteForCommand(usdLayer->GetIdentifier());
}
std::string quoteFilePathForCommand(const std::string& path)
{
// Note: C++ std::quoted() already handles backslashes.
return quoteForCommand(path);
}
std::string getProxyShapeName(const std::string& proxyShapePath)
{
std::size_t found = proxyShapePath.find_last_of("|");
if (std::string::npos != found) {
return proxyShapePath.substr(found + 1);
} else {
return proxyShapePath;
}
}
bool getBooleanAttributeOnProxyShape(
const std::string& proxyShapePath,
const std::string& attributeName)
{
if (proxyShapePath.empty()) {
return false;
}
MObject mobj;
MStatus status = PXR_NS::UsdMayaUtil::GetMObjectByName(getProxyShapeName(proxyShapePath), mobj);
if (status == MStatus::kSuccess) {
MFnDependencyNode fn;
fn.setObject(mobj);
bool attribute;
if (PXR_NS::UsdMayaUtil::getPlugValue(fn, attributeName.c_str(), &attribute)) {
return attribute;
}
}
return false;
}
} // namespace
namespace UsdLayerEditor {
std::string MayaCommandHook::proxyShapePath()
{
return dynamic_cast<MayaSessionState*>(_sessionState)->proxyShapePath();
}
void MayaCommandHook::setEditTarget(UsdLayer usdLayer)
{
std::string cmd;
cmd = STR("mayaUsdEditTarget -edit -editTarget ") + quoteLayerIdentifierForCommand(usdLayer);
cmd += quoteForCommand(proxyShapePath());
executeMel(cmd);
}
// starts a complex undo operation in the host app. Please use UndoContext class to safely
// open/close
void MayaCommandHook::openUndoBracket(const QString& name)
{
MGlobal::executeCommand(
MString("undoInfo -openChunk -chunkName ") + MayaUsdUI::cleanChunkName(name.toStdString()),
false,
false);
}
// closes a complex undo operation in the host app. Please use UndoContext class to safely
// open/close
void MayaCommandHook::closeUndoBracket()
{
MGlobal::executeCommand("undoInfo -closeChunk", false, false);
}
// insert a sub layer path at a given index
void MayaCommandHook::insertSubLayerPath(UsdLayer usdLayer, Path path, int index)
{
std::string cmd;
cmd = "mayaUsdLayerEditor -edit -insertSubPath ";
cmd += std::to_string(index);
cmd += quoteFilePathForCommand(path);
cmd += quoteLayerIdentifierForCommand(usdLayer);
executeMel(cmd);
}
// remove a sub layer by path
void MayaCommandHook::removeSubLayerPath(UsdLayer usdLayer, Path path)
{
size_t index = usdLayer->GetSubLayerPaths().Find(path);
assert(index != static_cast<size_t>(-1));
std::string cmd;
cmd = "mayaUsdLayerEditor -edit -removeSubPath ";
cmd += std::to_string(index);
cmd += quoteForCommand(proxyShapePath());
cmd += quoteLayerIdentifierForCommand(usdLayer);
executeMel(cmd);
}
void MayaCommandHook::moveSubLayerPath(
Path path,
UsdLayer oldParentUsdLayer,
UsdLayer newParentUsdLayer,
int index)
{
std::string cmd;
cmd = "mayaUsdLayerEditor -edit -moveSubPath ";
cmd += quoteFilePathForCommand(path);
cmd += quoteLayerIdentifierForCommand(newParentUsdLayer);
cmd += " " + std::to_string(index);
cmd += quoteLayerIdentifierForCommand(oldParentUsdLayer);
executeMel(cmd);
}
// replaces a path in the layer stack
void MayaCommandHook::replaceSubLayerPath(UsdLayer usdLayer, Path oldPath, Path newPath)
{
std::string cmd;
cmd = "mayaUsdLayerEditor -edit -replaceSubPath ";
cmd += quoteFilePathForCommand(oldPath);
cmd += quoteFilePathForCommand(newPath);
cmd += quoteLayerIdentifierForCommand(usdLayer);
executeMel(cmd);
}
// discard edit on a layer
void MayaCommandHook::discardEdits(UsdLayer usdLayer)
{
std::string cmd;
cmd = "mayaUsdLayerEditor -edit -discardEdits ";
cmd += quoteLayerIdentifierForCommand(usdLayer);
executeMel(cmd);
refreshLayerSystemLock(usdLayer);
}
// erases everything on a layer
void MayaCommandHook::clearLayer(UsdLayer usdLayer)
{
std::string cmd;
cmd = "mayaUsdLayerEditor -edit -clear ";
cmd += quoteLayerIdentifierForCommand(usdLayer);
executeMel(cmd);
}
// flattens a layer with its sublayers
void MayaCommandHook::flattenLayer(UsdLayer usdLayer)
{
std::string cmd;
cmd = "mayaUsdLayerEditor -edit -flatten ";
cmd += quoteLayerIdentifierForCommand(usdLayer);
executeMel(cmd);
}
// add an anon layer at the top of the stack, returns it
UsdLayer MayaCommandHook::addAnonymousSubLayer(UsdLayer usdLayer, std::string newName)
{
std::string cmd;
cmd = "mayaUsdLayerEditor -edit -addAnonymous ";
cmd += quoteFilePathForCommand(newName);
cmd += quoteLayerIdentifierForCommand(usdLayer);
std::string result = executeMel(cmd);
if (result.size() > 0)
return PXR_NS::SdfLayer::FindOrOpen(result);
else
return {};
}
// mute or unmute the given layer
void MayaCommandHook::muteSubLayer(UsdLayer usdLayer, bool muteIt)
{
std::string cmd;
cmd = "mayaUsdLayerEditor -edit -muteLayer ";
cmd += muteIt ? "1" : "0";
cmd += quoteForCommand(proxyShapePath());
cmd += quoteLayerIdentifierForCommand(usdLayer);
executeMel(cmd);
}
// lock, system-lock or unlock the given layer
void MayaCommandHook::lockLayer(
UsdLayer usdLayer,
MayaUsd::LayerLockType lockState,
bool includeSubLayers)
{
// Per design, we refuse to change the lock state of system-locked
// layers through the UI.
if (MayaUsd::isLayerSystemLocked(usdLayer))
return;
std::string cmd;
cmd = "mayaUsdLayerEditor -edit -skipSystemLocked -lockLayer ";
cmd += std::to_string(lockState);
cmd += includeSubLayers ? " 1" : " 0";
cmd += quoteForCommand(proxyShapePath());
cmd += quoteLayerIdentifierForCommand(usdLayer);
executeMel(cmd);
}
void MayaCommandHook::refreshLayerSystemLock(UsdLayer usdLayer, bool refreshSubLayers /*= false*/)
{
const std::string shapePath = proxyShapePath();
if (shapePath.empty())
return;
MObject mobj;
if (PXR_NS::UsdMayaUtil::GetMObjectByName(getProxyShapeName(shapePath), mobj)
!= MStatus::kSuccess)
return;
std::string cmd;
cmd = "mayaUsdLayerEditor -edit -refreshSystemLock ";
cmd += quoteForCommand(shapePath);
cmd += refreshSubLayers ? " 1" : " 0";
cmd += quoteLayerIdentifierForCommand(usdLayer);
executeMel(cmd, false);
}
void MayaCommandHook::stitchLayers(const std::vector<PXR_NS::SdfLayerRefPtr>& layers)
{
if (layers.empty())
return;
std::string cmd = "mayaUsdLayerEditor -edit ";
const std::string proxyShape = proxyShapePath();
for (const auto& layer : layers) {
if (!layer)
continue;
cmd += "-stitchLayers ";
cmd += quoteForCommand(proxyShape);
cmd += quoteLayerIdentifierForCommand(layer);
}
// Target layer isn't actually used, but needed for the command syntax.
cmd += quoteLayerIdentifierForCommand(layers[0]);
executeMel(cmd);
}
// Help menu callback
void MayaCommandHook::showLayerEditorHelp()
{
MGlobal::executePythonCommand(
"from mayaUsdUtils import showHelpMayaUSD; showHelpMayaUSD(\"UsdLayerEditor\");");
}
// this method is used to select the prims with spec in a layer
void MayaCommandHook::selectPrimsWithSpec(UsdLayer usdLayer)
{
Ufe::Selection sn;
for (auto prim : _sessionState->stage()->Traverse()) {
auto primSpec = usdLayer->GetPrimAtPath(prim.GetPath());
if (primSpec) {
auto ufePath
= Ufe::PathString::path(proxyShapePath() + "," + prim.GetPath().GetString());
auto ufeSceneItem = Ufe::Hierarchy::createItem(ufePath);
if (ufeSceneItem) {
sn.append(ufeSceneItem);
}
}
}
if (sn.empty()) {
return;
}
MayaUsd::UfeSelectionUndoItem::select("selectPrimsWithSpec", sn);
}
bool MayaCommandHook::isProxyShapeStageIncoming(const std::string& proxyShapePath)
{
return getBooleanAttributeOnProxyShape(proxyShapePath, "stageIncoming");
}
bool MayaCommandHook::isProxyShapeSharedStage(const std::string& proxyShapePath)
{
return getBooleanAttributeOnProxyShape(proxyShapePath, "shareStage");
}
std::string MayaCommandHook::executeMel(const std::string& commandString, bool undoable)
{
if (areCommandsDelayed()) {
_delayedCommands.push_back({ commandString, false, undoable });
} else {
// executes maya command with display and undo set to true so that it logs
MStringArray result;
MGlobal::executeCommand(
MString(commandString.c_str()),
result,
/*display*/ true,
/*undo*/ undoable);
if (result.length() > 0)
return result[0].asChar();
}
return "";
}
void MayaCommandHook::executePython(const std::string& commandString, bool undoable)
{
if (areCommandsDelayed()) {
_delayedCommands.push_back({ commandString, true, undoable });
} else {
MGlobal::executePythonCommand(commandString.c_str(), /*display*/ false, undoable);
}
}
void MayaCommandHook::executeDelayedCommands()
{
if (areCommandsDelayed())
return;
// In case the execution of commands add new commands,
// make a copy and clear the delayed commands.
std::vector<DelayedCommand> cmds = _delayedCommands;
_delayedCommands.clear();
for (const auto& cmd : cmds) {
if (cmd.isPython) {
executePython(cmd.command, cmd.isUndoable);
} else {
executeMel(cmd.command, cmd.isUndoable);
}
}
}
} // namespace UsdLayerEditor