-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathrenderSetupWindowCmd.cpp
More file actions
320 lines (271 loc) · 10.1 KB
/
Copy pathrenderSetupWindowCmd.cpp
File metadata and controls
320 lines (271 loc) · 10.1 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
//
// 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 "renderSetupWindowCmd.h"
#include "mayaEditCommitter.h"
#include <mayaUsd/nodes/proxyShapeBase.h>
#include <mayaUsd/nodes/usdSceneSettingsManager.h>
#include <mayaUsd/ufe/Utils.h>
#include <mayaUsd/utils/mayaNodeTypeObserver.h>
// This is added to prevent multiple definitions of the MApiVersion string.
#define MNoVersionString
#include <maya/MArgParser.h>
#include <maya/MFn.h>
#include <maya/MFnPlugin.h>
#include <maya/MGlobal.h>
#include <maya/MMessage.h>
#include <maya/MNodeMessage.h>
#include <maya/MQtUtil.h>
#include <maya/MSceneMessage.h>
#include <maya/MSyntax.h>
#include <QtCore/QPointer>
#include <QtCore/QTimer>
#include <QtGui/QAction>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QVBoxLayout>
#include <RenderSetup/RenderSetupWidget.h>
#include <algorithm>
#include <vector>
namespace MAYAUSD_NS_DEF {
class RenderSetupWindow;
const MString RenderSetupWindowCmd::commandName("mayaUsdRenderSetupWindow");
const std::string kUSDRenderSettingsNodeName("UsdDefaultRenderSettings");
namespace {
constexpr auto kReloadFlag = "-rl";
constexpr auto kReloadFlagLong = "-reload";
const MString WINDOW_TITLE_NAME = "USD Render Setup";
const MString WORKSPACE_CONTROL_NAME = "mayaUsdRenderSetup";
// Global pointer to the (singleton) render setup window.
QPointer<RenderSetupWindow> g_renderSetupWindow;
bool workspaceControlExists()
{
MString cmd;
cmd.format("workspaceControl -exists \"^1s\"", WORKSPACE_CONTROL_NAME);
int result = 0;
MGlobal::executeCommand(cmd, result);
return result != 0;
}
} // namespace
class RenderSetupWindow
: public QMainWindow
, private MayaUsd::MayaNodeTypeObserver::Listener
{
public:
typedef QMainWindow PARENT_CLASS;
explicit RenderSetupWindow(QWidget* parent);
~RenderSetupWindow() override;
void refreshStages();
void processNodeAdded(MObject& node) override;
void processNodeRemoved(MObject& node) override;
static void nodeRenamedCB(MObject& node, const MString& oldName, void* clientData);
static void onSceneChangedCB(void* clientData);
private:
void applyStages()
{
_editCommitter->setStages(_hostStages);
_tree->setStages(_hostStages);
}
private:
Adsk::RenderSetupWidget* _tree;
MayaUsdRenderSetup::MayaEditCommitter* _editCommitter { nullptr };
std::vector<Adsk::HostStage> _hostStages;
std::vector<MCallbackId> _sceneCallbackIds;
};
RenderSetupWindow::RenderSetupWindow(QWidget* parent)
: PARENT_CLASS(parent)
{
// Create the render setup widget and set it as the central widget of the window.
_tree = new Adsk::RenderSetupWidget(this);
_editCommitter = new MayaUsdRenderSetup::MayaEditCommitter(nullptr);
_tree->setEditCommitter(std::unique_ptr<Adsk::IEditCommitter>(_editCommitter));
setCentralWidget(_tree);
_tree->show();
auto* viewMenu = menuBar()->addMenu(tr("View"));
auto* hierarchyAction = viewMenu->addAction(tr("Display USD Hierarchy"));
hierarchyAction->setCheckable(true);
hierarchyAction->setChecked(false);
connect(hierarchyAction, &QAction::toggled, [this](bool checked) {
_tree->setLayoutMode(
checked ? Adsk::RenderTreeModel::LayoutMode::Hierarchy
: Adsk::RenderTreeModel::LayoutMode::Flat);
});
layout()->setContentsMargins(0, 0, 0, 0);
layout()->setSpacing(0);
// Observe proxy shape node additions and removals directly.
PXR_NS::MayaUsdProxyShapeBase::getProxyShapesObserver().addTypeListener(*this);
// Refresh after scene open or new scene.
_sceneCallbackIds.push_back(
MSceneMessage::addCallback(MSceneMessage::kAfterOpen, onSceneChangedCB, this));
_sceneCallbackIds.push_back(
MSceneMessage::addCallback(MSceneMessage::kAfterNew, onSceneChangedCB, this));
_sceneCallbackIds.push_back(
MNodeMessage::addNameChangedCallback(MObject::kNullObj, nodeRenamedCB, this));
}
RenderSetupWindow::~RenderSetupWindow()
{
PXR_NS::MayaUsdProxyShapeBase::getProxyShapesObserver().removeTypeListener(*this);
for (auto id : _sceneCallbackIds) {
MMessage::removeCallback(id);
}
}
void RenderSetupWindow::processNodeAdded(MObject& /*node*/)
{
QTimer::singleShot(0, this, &RenderSetupWindow::refreshStages);
}
void RenderSetupWindow::processNodeRemoved(MObject& /*node*/)
{
QTimer::singleShot(0, this, &RenderSetupWindow::refreshStages);
}
/* static */
void RenderSetupWindow::nodeRenamedCB(MObject& obj, const MString& oldName, void* clientData)
{
if (oldName.length() != 0 && obj.hasFn(MFn::kShape)) {
auto* self = static_cast<RenderSetupWindow*>(clientData);
// We only need to refresh if the oldName was one we have in our list.
for (const auto& stage : self->_hostStages) {
if (stage.displayName == oldName.asChar()) {
QTimer::singleShot(0, self, &RenderSetupWindow::refreshStages);
break;
}
}
}
}
void RenderSetupWindow::onSceneChangedCB(void* clientData)
{
auto self = reinterpret_cast<RenderSetupWindow*>(clientData);
QTimer::singleShot(0, self, &RenderSetupWindow::refreshStages);
}
void RenderSetupWindow::refreshStages()
{
_hostStages.clear();
// Add all the USD stages and sort them alphabetically by display name.
for (const auto& stage : MayaUsd::ufe::getAllStages()) {
Adsk::HostStage hostStage;
hostStage.stage = stage;
hostStage.displayName = MayaUsd::ufe::stagePath(stage).back().string();
_hostStages.push_back(hostStage);
}
std::sort(_hostStages.begin(), _hostStages.end(), [](const auto& a, const auto& b) {
return a.displayName < b.displayName;
});
#ifdef MAYA_HAS_USD_SETTINGS_NODES
// Add default setting stage (from DG node) but put it first in the vector.
auto defaultStage = MayaUsd::UsdSceneSettingsManager::getStage(kUSDRenderSettingsNodeName);
if (defaultStage) {
Adsk::HostStage hostStage;
hostStage.stage = defaultStage;
hostStage.displayName = kUSDRenderSettingsNodeName;
_hostStages.insert(_hostStages.begin(), hostStage);
}
#endif
applyStages();
}
/*static*/
MStatus RenderSetupWindowCmd::initialize(MFnPlugin& plugin)
{
return plugin.registerCommand(
commandName, RenderSetupWindowCmd::creator, RenderSetupWindowCmd::createSyntax);
}
/*static*/
MStatus RenderSetupWindowCmd::finalize(MFnPlugin& plugin)
{
if (workspaceControlExists()) {
MString closeCmd;
closeCmd.format("workspaceControl -e -close \"^1s\"", WORKSPACE_CONTROL_NAME);
MGlobal::executeCommand(closeCmd);
}
g_renderSetupWindow.clear();
return plugin.deregisterCommand(commandName);
}
void* RenderSetupWindowCmd::creator() { return new RenderSetupWindowCmd(); }
void RenderSetupWindowCmd::createWindowIntoCurrentParent()
{
QWidget* mayaParent = MQtUtil::getCurrentParent();
if (!g_renderSetupWindow) {
g_renderSetupWindow = new RenderSetupWindow(nullptr);
}
g_renderSetupWindow->refreshStages();
MQtUtil::addWidgetToMayaLayout(g_renderSetupWindow.data(), mayaParent);
}
MStatus RenderSetupWindowCmd::doIt(const MArgList& argList)
{
MStatus st;
MArgParser argParser(syntax(), argList, &st);
if (!st)
return st;
const bool isReload = argParser.isFlagSet(kReloadFlag);
if (isReload) {
// Maya is invoking us through workspaceControl's -uiScript to rebuild
// the widget inside an already-existing workspace control container.
createWindowIntoCurrentParent();
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_renderSetupWindow) {
g_renderSetupWindow->refreshStages();
} else {
createWindowIntoCurrentParent();
}
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.
MString createCmd;
MStringArray cmdArgs;
cmdArgs.append(WINDOW_TITLE_NAME);
cmdArgs.append(std::to_string(MQtUtil::dpiScale(720)).c_str());
cmdArgs.append(std::to_string(MQtUtil::dpiScale(480)).c_str());
cmdArgs.append("mayaUsdPlugin");
cmdArgs.append(WORKSPACE_CONTROL_NAME);
createCmd.format(
"workspaceControl"
" -label \"^1s\""
" -retain false"
" -deleteLater false"
" -loadImmediately true"
" -floating true"
" -initialWidth ^2s"
" -initialHeight ^3s"
" -requiredPlugin \"^4s\""
" \"^5s\"",
cmdArgs);
MGlobal::executeCommand(createCmd);
createWindowIntoCurrentParent();
// 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\"",
RenderSetupWindowCmd::commandName,
WORKSPACE_CONTROL_NAME);
MGlobal::executeCommand(uiScriptCmd);
return MS::kSuccess;
}
MSyntax RenderSetupWindowCmd::createSyntax()
{
MSyntax syntax;
syntax.enableQuery(false);
syntax.enableEdit(false);
syntax.addFlag(kReloadFlag, kReloadFlagLong);
return syntax;
}
} // namespace MAYAUSD_NS_DEF