-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathCompositionEditorCmd.cpp
More file actions
332 lines (283 loc) · 10.2 KB
/
Copy pathCompositionEditorCmd.cpp
File metadata and controls
332 lines (283 loc) · 10.2 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
//
// Copyright 2026 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 "CompositionEditorCmd.h"
#include <mayaUsd/ufe/Utils.h>
#include <mayaUsd/undo/MayaUsdUndoBlock.h>
#include <mayaUsdUI/ui/undoChunkUtils.h>
#include <usdUfe/undo/UsdUndoManager.h>
#include <pxr/usd/sdf/layer.h>
#include <pxr/usd/usd/prim.h>
// This is added to prevent multiple definitions of the MApiVersion string.
#define MNoVersionString
#include <maya/MArgParser.h>
#include <maya/MFnPlugin.h>
#include <maya/MGlobal.h>
#include <maya/MQtUtil.h>
#include <maya/MStatus.h>
#include <maya/MString.h>
#include <maya/MSyntax.h>
#include <ufe/globalSelection.h>
#include <ufe/observableSelection.h>
#include <ufe/observer.h>
#include <ufe/path.h>
#include <ufe/pathString.h>
#include <ufe/selectionNotification.h>
#include <QtCore/QPointer>
#include <QtCore/QString>
#include <QtCore/QVariant>
#include <QtGui/QPalette>
#include <QtWidgets/QApplication>
#include <QtWidgets/QWidget>
#include <UsdDebugUI/ApplicationHost.h>
#include <UsdDebugUI/CompositionEditorWidget.h>
#include <algorithm>
#include <string>
namespace MAYAUSD_NS_DEF {
const MString CompositionEditorCmd::name("mayaUsdCompositionEditor");
namespace {
constexpr auto kPrimPathFlag = "-pp";
constexpr auto kPrimPathFlagLong = "-primPath";
constexpr auto kReloadFlag = "-rl";
constexpr auto kReloadFlagLong = "-reload";
const MString WORKSPACE_CONTROL_NAME = "mayaUsdCompositionEditor";
QPointer<Adsk::UsdDebug::CompositionEditorWidget> g_compositionEditorWidget;
Ufe::Observer::Ptr g_selectionObserver;
PXR_NS::UsdPrim resolvePrimFromArg(const MString& primPathStr)
{
if (primPathStr.length() == 0) {
return PXR_NS::UsdPrim();
}
Ufe::Path ufePath = Ufe::PathString::path(primPathStr.asChar());
return ufe::ufePathToPrim(ufePath);
}
PXR_NS::UsdPrim resolvePrimFromSelection()
{
auto selection = Ufe::GlobalSelection::get();
if (!selection || selection->empty()) {
return PXR_NS::UsdPrim();
}
return ufe::ufePathToPrim(selection->front()->path());
}
class SelectionObserver : public Ufe::Observer
{
public:
void operator()(const Ufe::Notification& notification) override
{
if (!dynamic_cast<const Ufe::SelectionChanged*>(¬ification)) {
return;
}
if (!g_compositionEditorWidget) {
return;
}
g_compositionEditorWidget->setPrim(resolvePrimFromSelection());
}
};
class MayaCompositionEditorHost : public Adsk::UsdDebug::ApplicationHost
{
public:
static void ensureInstalled()
{
static MayaCompositionEditorHost* s_instance = nullptr;
if (!s_instance) {
s_instance = new MayaCompositionEditorHost();
}
}
int pm(const PixelMetric& metric) const override
{
switch (metric) {
case PixelMetric::OuterMargin: return MQtUtil::dpiScale(6);
case PixelMetric::ContentMargin: return MQtUtil::dpiScale(4);
}
return 0;
}
QColor themeColor(const ThemeColors& color) const override
{
switch (color) {
case ThemeColors::DisabledForeground:
return QApplication::palette().color(QPalette::Disabled, QPalette::WindowText);
default:
// All other roles use the base host's themed defaults.
return ApplicationHost::themeColor(color);
}
}
bool executeInCmd(
const std::string& editLabel,
const std::string& layerId,
const std::function<bool()>& edit) override
{
if (!edit) {
return false;
}
// Ensure the layer being edited has a UsdUndoStateDelegate so the inverse
// of the edit is recorded
if (PXR_NS::SdfLayerHandle layer = PXR_NS::SdfLayer::Find(layerId)) {
UsdUfe::UsdUndoManager::instance().trackLayerStates(layer);
}
MayaUsdUI::UndoChunkGuard undoChunk(editLabel);
MayaUsdUndoBlock undoBlock;
return edit();
}
float uiScale() const override { return static_cast<float>(MQtUtil::dpiScale(1.0f)); }
QVariant loadPersistentData(const QString& group, const QString& key) const override
{
const MString varName = optionVarName(group, key);
if (!MGlobal::optionVarExists(varName)) {
return QVariant();
}
return QVariant(QString::fromUtf8(MGlobal::optionVarStringValue(varName).asChar()));
}
void
savePersistentData(const QString& group, const QString& key, const QVariant& value) override
{
// Store every value as a string optionVar
const MString varName = optionVarName(group, key);
MGlobal::setOptionVarValue(varName, MString(value.toString().toUtf8().constData()));
}
protected:
MayaCompositionEditorHost() { injectInstance(this); }
static MString optionVarName(const QString& group, const QString& key)
{
const QString name = QStringLiteral("mayaUsd_CompositionEditor_%1_%2").arg(group, key);
return MString(name.toUtf8().constData());
}
};
bool workspaceControlExists()
{
MString cmd;
cmd.format("workspaceControl -exists \"^1s\"", WORKSPACE_CONTROL_NAME);
int result = 0;
MGlobal::executeCommand(cmd, result);
return result != 0;
}
// Instantiate the widget into Maya's current parent (the workspace control's
// QWidget when invoked through workspaceControl's -uiScript callback).
void buildWidgetIntoCurrentParent(const PXR_NS::UsdPrim& prim)
{
MayaCompositionEditorHost::ensureInstalled();
QWidget* mayaParent = MQtUtil::getCurrentParent();
g_compositionEditorWidget = new Adsk::UsdDebug::CompositionEditorWidget(nullptr);
if (prim) {
g_compositionEditorWidget->setPrim(prim);
}
MQtUtil::addWidgetToMayaLayout(g_compositionEditorWidget.data(), mayaParent);
// Mirror the global UFE selection into the widget so the user does not
// have to re-invoke the command after each selection change. Observer
// is registered once and reused across widget rebuilds (close+reopen).
if (!g_selectionObserver) {
if (auto sel = Ufe::GlobalSelection::get()) {
g_selectionObserver = std::make_shared<SelectionObserver>();
sel->addObserver(g_selectionObserver);
}
}
}
} // namespace
/*static*/
MStatus CompositionEditorCmd::initialize(MFnPlugin& plugin)
{
return plugin.registerCommand(
name, CompositionEditorCmd::creator, CompositionEditorCmd::createSyntax);
}
/*static*/
MStatus CompositionEditorCmd::finalize(MFnPlugin& plugin)
{
if (g_selectionObserver) {
if (auto sel = Ufe::GlobalSelection::get()) {
sel->removeObserver(g_selectionObserver);
}
g_selectionObserver.reset();
}
if (workspaceControlExists()) {
MString closeCmd;
closeCmd.format("workspaceControl -e -close \"^1s\"", WORKSPACE_CONTROL_NAME);
MGlobal::executeCommand(closeCmd);
}
g_compositionEditorWidget.clear();
return plugin.deregisterCommand(name);
}
void* CompositionEditorCmd::creator() { return new CompositionEditorCmd(); }
MStatus CompositionEditorCmd::doIt(const MArgList& args)
{
MStatus st;
MArgParser argData(syntax(), args, &st);
if (!st) {
return MS::kInvalidParameter;
}
MString primPathStr;
if (argData.isFlagSet(kPrimPathFlag)) {
argData.getFlagArgument(kPrimPathFlag, 0, primPathStr);
}
PXR_NS::UsdPrim prim = resolvePrimFromArg(primPathStr);
if (!prim) {
prim = resolvePrimFromSelection();
}
const bool isReload = argData.isFlagSet(kReloadFlag);
if (isReload) {
// Maya is invoking us through workspaceControl's -uiScript to rebuild
// the widget inside an already-existing workspace control container.
buildWidgetIntoCurrentParent(prim);
return MS::kSuccess;
}
if (workspaceControlExists()) {
// Bring an existing (possibly closed/hidden) workspace control back.
MString restoreCmd;
restoreCmd.format("workspaceControl -e -restore \"^1s\"", WORKSPACE_CONTROL_NAME);
MGlobal::executeCommand(restoreCmd);
if (g_compositionEditorWidget && prim) {
g_compositionEditorWidget->setPrim(prim);
}
return MS::kSuccess;
}
// First invocation: create the workspace control wrapper, then create
// the widget inside it. -retain false + -deleteLater false combined
// with -uiScript matches the Layer Editor pattern so the widget is
// rebuilt on layout save/restore. The workspace control persists its own
// size across sessions, so -initialWidth/-initialHeight only seed the
// first-launch geometry.
MString createCmd;
createCmd.format(
"workspaceControl"
" -label \"USD Composition Editor\""
" -retain false"
" -deleteLater false"
" -loadImmediately true"
" -floating true"
" -initialWidth 700"
" -initialHeight 600"
" -requiredPlugin \"mayaUsdPlugin\""
" \"^1s\"",
WORKSPACE_CONTROL_NAME);
MGlobal::executeCommand(createCmd);
buildWidgetIntoCurrentParent(prim);
// Install the -uiScript only after the initial build, so it doesn't
// run twice on creation. Mirrors the Layer Editor pattern.
MString uiScriptCmd;
uiScriptCmd.format(
"workspaceControl -e -uiScript \"^1s -reload\" \"^2s\"",
CompositionEditorCmd::name,
WORKSPACE_CONTROL_NAME);
MGlobal::executeCommand(uiScriptCmd);
return MS::kSuccess;
}
MSyntax CompositionEditorCmd::createSyntax()
{
MSyntax syntax;
syntax.enableQuery(false);
syntax.enableEdit(false);
syntax.addFlag(kPrimPathFlag, kPrimPathFlagLong, MSyntax::kString);
syntax.addFlag(kReloadFlag, kReloadFlagLong);
return syntax;
}
} // namespace MAYAUSD_NS_DEF