-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathusdSettingsNode.cpp
More file actions
303 lines (262 loc) · 10.9 KB
/
Copy pathusdSettingsNode.cpp
File metadata and controls
303 lines (262 loc) · 10.9 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
//
// 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 "usdSettingsNode.h"
#include "usdSceneSettingsManager.h"
#include <pxr/base/tf/diagnostic.h>
#include <pxr/usd/sdf/layer.h>
#include <pxr/usd/usd/stage.h>
#include <maya/MFnData.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MFnStringData.h>
#include <maya/MFnTypedAttribute.h>
#include <maya/MPlug.h>
namespace MAYAUSD_NS_DEF {
const MTypeId UsdSettingsNode::typeId(0x580000A6);
const MString UsdSettingsNode::typeName("UsdDefaultSettings");
MObject UsdSettingsNode::serializedRootLayerAttr;
MObject UsdSettingsNode::serializedSessionLayerAttr;
MObject UsdSettingsNode::activeSettingsPathAttr;
MObject UsdSettingsNode::currentRendererAttr;
/* static */
void* UsdSettingsNode::creator() { return new UsdSettingsNode(); }
/* static */
MStatus UsdSettingsNode::initialize()
{
MStatus status;
MFnTypedAttribute typedAttrFn;
MFnStringData stringDataFn;
const MObject defaultStringDataObj = stringDataFn.create("");
// Serialized root layer text. Stored on the node so the in-memory USD stage
// round-trips through the Maya scene file with no external .usd asset; hidden
// and internal because it is an implementation detail (users edit the stage,
// not this string).
serializedRootLayerAttr = typedAttrFn.create(
"serializedRootLayer", "srl", MFnData::kString, defaultStringDataObj, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);
typedAttrFn.setStorable(true);
typedAttrFn.setHidden(true);
typedAttrFn.setInternal(true);
status = addAttribute(serializedRootLayerAttr);
CHECK_MSTATUS_AND_RETURN_IT(status);
// Serialized session layer text; mirrors serializedRootLayer above so transient
// session-layer edits also persist with the Maya scene file.
serializedSessionLayerAttr = typedAttrFn.create(
"serializedSessionLayer", "ssl", MFnData::kString, defaultStringDataObj, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);
typedAttrFn.setStorable(true);
typedAttrFn.setHidden(true);
typedAttrFn.setInternal(true);
status = addAttribute(serializedSessionLayerAttr);
CHECK_MSTATUS_AND_RETURN_IT(status);
// UFE path of the currently active settings prim, persisted with the
// scene. Hidden/internal: reads/writes go through the typed accessors.
activeSettingsPathAttr = typedAttrFn.create(
"activeSettingsPath", "asp", MFnData::kString, defaultStringDataObj, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);
typedAttrFn.setStorable(true);
typedAttrFn.setHidden(true);
typedAttrFn.setInternal(true);
status = addAttribute(activeSettingsPathAttr);
CHECK_MSTATUS_AND_RETURN_IT(status);
// Hydra renderer plugin name persisted on the node to select which Hydra
// backend is used for USD rendering. Hidden/internal: reads/writes go
// through the typed accessors.
currentRendererAttr = typedAttrFn.create(
"currentRenderer", "crn", MFnData::kString, defaultStringDataObj, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);
typedAttrFn.setStorable(true);
typedAttrFn.setHidden(true);
typedAttrFn.setInternal(true);
status = addAttribute(currentRendererAttr);
CHECK_MSTATUS_AND_RETURN_IT(status);
return status;
}
UsdSettingsNode::UsdSettingsNode()
: MPxNode()
{
}
PXR_NS::UsdTimeCode UsdSettingsNode::getTime() const { return PXR_NS::UsdTimeCode::Default(); }
PXR_NS::UsdStageRefPtr UsdSettingsNode::getUsdStage() const
{
ensureStage();
return _stage;
}
std::string UsdSettingsNode::nodeName() const
{
MFnDependencyNode depFn(thisMObject());
return depFn.name().asChar();
}
std::string UsdSettingsNode::activeSettingsPath() const
{
MPlug plug(thisMObject(), activeSettingsPathAttr);
return plug.asString().asChar();
}
bool UsdSettingsNode::setActiveSettingsPath(const std::string& ufePath)
{
MPlug plug(thisMObject(), activeSettingsPathAttr);
MStatus status = plug.setString(MString(ufePath.c_str()));
return status == MS::kSuccess;
}
std::string UsdSettingsNode::currentRenderer() const
{
MPlug plug(thisMObject(), currentRendererAttr);
return plug.asString().asChar();
}
bool UsdSettingsNode::setCurrentRenderer(const std::string& rendererName)
{
MPlug plug(thisMObject(), currentRendererAttr);
MStatus status = plug.setString(MString(rendererName.c_str()));
return status == MS::kSuccess;
}
void UsdSettingsNode::ensureStage() const
{
if (_stage) {
return;
}
const std::string name = nodeName();
PXR_NS::SdfLayerRefPtr rootLayer = PXR_NS::SdfLayer::CreateAnonymous(name + "Root");
PXR_NS::SdfLayerRefPtr sessionLayer = PXR_NS::SdfLayer::CreateAnonymous(name + "Session");
if (!rootLayer || !sessionLayer) {
PXR_NAMESPACE_USING_DIRECTIVE
TF_RUNTIME_ERROR(
"UsdSettingsNode::ensureStage: SdfLayer::CreateAnonymous returned null for '%s'.",
name.c_str());
return;
}
_stage = PXR_NS::UsdStage::Open(rootLayer, sessionLayer);
if (!_stage) {
PXR_NAMESPACE_USING_DIRECTIVE
TF_RUNTIME_ERROR(
"UsdSettingsNode::ensureStage: UsdStage::Open returned null for '%s'.", name.c_str());
return;
}
UsdSceneSettingsManager::callPopulator(name, _stage, const_cast<UsdSettingsNode&>(*this));
// Hook USD-notice observers (UFE notification flow) onto the freshly
// created stage. Done after the populator so the initial population is
// observed; idempotent if the hook is missing or the stage is already
// observed.
UsdSceneSettingsManager::notifyStageObserver(_stage);
}
void UsdSettingsNode::serializeToAttributes()
{
// Force the populator to run before persisting. Without this, a New ->
// Save sequence in which no caller ever reached getUsdStage() would
// serialize empty default-string plugs, and the next File > Open would
// restore an empty stage missing all populator-authored content.
if (!_stage) {
getUsdStage();
}
if (!_stage) {
return;
}
// Export each layer and only overwrite the matching plug on success.
// ExportToString returns false on USD's side when serialization fails;
// overwriting the plug with the partially-populated string would silently
// corrupt the next File > Open. The previous valid serialization is
// preserved in that case so the file remains loadable.
const std::string nameForLog = nodeName();
std::string rootStr;
if (_stage->GetRootLayer()->ExportToString(&rootStr)) {
MPlug rootPlug(thisMObject(), serializedRootLayerAttr);
MStatus rootStatus = rootPlug.setString(MString(rootStr.c_str()));
if (rootStatus != MS::kSuccess) {
PXR_NAMESPACE_USING_DIRECTIVE
TF_RUNTIME_ERROR(
"UsdSettingsNode::serializeToAttributes: setString on the root layer plug failed "
"for '%s'.",
nameForLog.c_str());
}
} else {
PXR_NAMESPACE_USING_DIRECTIVE
TF_RUNTIME_ERROR(
"UsdSettingsNode::serializeToAttributes: ExportToString on the root layer failed for "
"'%s'; keeping previous serialized value.",
nameForLog.c_str());
}
std::string sessionStr;
if (_stage->GetSessionLayer()->ExportToString(&sessionStr)) {
MPlug sessionPlug(thisMObject(), serializedSessionLayerAttr);
MStatus sessionStatus = sessionPlug.setString(MString(sessionStr.c_str()));
if (sessionStatus != MS::kSuccess) {
PXR_NAMESPACE_USING_DIRECTIVE
TF_RUNTIME_ERROR(
"UsdSettingsNode::serializeToAttributes: setString on the session layer plug "
"failed for '%s'.",
nameForLog.c_str());
}
} else {
PXR_NAMESPACE_USING_DIRECTIVE
TF_RUNTIME_ERROR(
"UsdSettingsNode::serializeToAttributes: ExportToString on the session layer failed "
"for '%s'; keeping previous serialized value.",
nameForLog.c_str());
}
}
void UsdSettingsNode::deserializeFromAttributes()
{
const std::string name = nodeName();
MPlug rootPlug(thisMObject(), serializedRootLayerAttr);
MPlug sessionPlug(thisMObject(), serializedSessionLayerAttr);
MString rootStr = rootPlug.asString();
MString sessionStr = sessionPlug.asString();
PXR_NS::SdfLayerRefPtr rootLayer = PXR_NS::SdfLayer::CreateAnonymous(name + "Root");
PXR_NS::SdfLayerRefPtr sessionLayer = PXR_NS::SdfLayer::CreateAnonymous(name + "Session");
if (!rootLayer || !sessionLayer) {
PXR_NAMESPACE_USING_DIRECTIVE
TF_RUNTIME_ERROR(
"UsdSettingsNode::deserializeFromAttributes: failed to create anonymous layers for "
"'%s'.",
name.c_str());
return;
}
if (rootStr.length() > 0 && !rootLayer->ImportFromString(std::string(rootStr.asChar()))) {
PXR_NAMESPACE_USING_DIRECTIVE
TF_WARN(
"UsdSettingsNode::deserializeFromAttributes: ImportFromString failed on the root layer "
"of '%s'; falling back to defaults.",
name.c_str());
rootStr = MString();
}
if (sessionStr.length() > 0
&& !sessionLayer->ImportFromString(std::string(sessionStr.asChar()))) {
PXR_NAMESPACE_USING_DIRECTIVE
TF_WARN(
"UsdSettingsNode::deserializeFromAttributes: ImportFromString failed on the session "
"layer of '%s'.",
name.c_str());
sessionStr = MString();
}
_stage = PXR_NS::UsdStage::Open(rootLayer, sessionLayer);
if (!_stage) {
PXR_NAMESPACE_USING_DIRECTIVE
TF_RUNTIME_ERROR(
"UsdSettingsNode::deserializeFromAttributes: UsdStage::Open returned null for '%s'.",
name.c_str());
return;
}
// If neither the root nor the session layer carried persisted content,
// we are loading a scene file that was saved before this node ever
// materialized its stage (so onBeforeSave's serializeToAttributes had
// empty layers to write). Run the populator so the resulting stage
// matches what a freshly-created node would produce.
if (rootStr.length() == 0 && sessionStr.length() == 0) {
UsdSceneSettingsManager::callPopulator(name, _stage, *this);
}
// Newly deserialized stage: re-hook USD-notice observers via the manager
// (see ensureStage() for rationale).
UsdSceneSettingsManager::notifyStageObserver(_stage);
}
} // namespace MAYAUSD_NS_DEF