-
Notifications
You must be signed in to change notification settings - Fork 60
/
AssetSubCommandSync.C
345 lines (297 loc) · 11.3 KB
/
AssetSubCommandSync.C
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
#if defined(_MSC_VER)
__pragma(warning(disable: 4996))
#endif
#include "AssetSubCommandSync.h"
#include <maya/MGlobal.h>
#include <maya/MSelectionList.h>
#include <maya/MDagPath.h>
#include <maya/MFnDagNode.h>
#include <maya/MFnDependencyNode.h>
#include "AssetNode.h"
#include "AssetNodeOptions.h"
#include "SyncAttribute.h"
#include "SyncOutputInstance.h"
#include "SyncOutputMaterial.h"
#include "SyncOutputObject.h"
AssetSubCommandSync::AssetSubCommandSync(const MObject &assetNodeObj)
: SubCommandAsset(assetNodeObj),
mySyncAll(true),
mySyncAttributes(false),
mySyncOutputs(false),
mySyncOutputHidden(false),
mySyncOutputTemplatedGeos(false)
{
}
AssetSubCommandSync::~AssetSubCommandSync()
{
for (AssetSyncs::const_iterator iter = myAssetSyncs.begin();
iter != myAssetSyncs.end(); iter++)
{
delete *iter;
}
myAssetSyncs.clear();
}
void
AssetSubCommandSync::setSyncAttributes()
{
mySyncAll = false;
mySyncAttributes = true;
}
void
AssetSubCommandSync::setSyncOutputs()
{
mySyncAll = false;
mySyncOutputs = true;
}
void
AssetSubCommandSync::setSyncOutputHidden()
{
mySyncOutputHidden = true;
}
void
AssetSubCommandSync::setSyncOutputTemplatedGeos()
{
mySyncOutputTemplatedGeos = true;
}
void
AssetSubCommandSync::deleteMaterials(MPlug &materialPlug)
{
MObject shaderObj = SyncOutputMaterial::findShader(materialPlug);
if (shaderObj.isNull())
{
return;
}
MObject shadingGroupObj = SyncOutputMaterial::findShadingGroup(shaderObj);
myDagModifier.deleteNode(shaderObj);
if (!shadingGroupObj.isNull())
{
myDagModifier.deleteNode(shadingGroupObj);
}
myDagModifier.doIt();
}
MStatus
AssetSubCommandSync::doIt()
{
MStatus status;
MFnDagNode assetNodeFn(myAssetNodeObj, &status);
// save selection
MSelectionList oldSelection;
MGlobal::getActiveSelectionList(oldSelection);
// check for pre and post sync callbacks and what args to pass
MPlug preSyncCallbackPlug(myAssetNodeObj, AssetNode::preSyncCallback);
MString preSyncCallback = preSyncCallbackPlug.asString();
MPlug postSyncCallbackPlug(myAssetNodeObj, AssetNode::postSyncCallback);
MString postSyncCallback = postSyncCallbackPlug.asString();
MString syncOutputFlag = mySyncOutputs ? " 1 " : " 0 ";
MString syncAttrFlag = mySyncAttributes ? " 1 " : " 0 ";
MDagPath assetPath;
assetNodeFn.getPath(assetPath);
// use MGlobal to execute the command rather than the MDagModifier
// cause we don't know whether the callbacks do anything undoable or not
// or whether they do something that is specifically NOT undoable
// the MDagModifier commandToExecute can fail and corrupt the undo
// if there was not actually anything undoable in the command.
if (preSyncCallback.length() > 0)
{
// command string to be built is preSyncCallback assetNode
// mySyncAttributes mySyncOutputs
MString preSyncCmd = preSyncCallback + MString(" ") +
assetPath.partialPathName() + syncAttrFlag +
syncOutputFlag;
MGlobal::executeCommand(preSyncCmd, false, true);
}
// attributes
if (mySyncAll || mySyncAttributes)
{
SubCommand *syncOutput = new SyncAttribute(myAssetNodeObj);
syncOutput->doIt();
myAssetSyncs.push_back(syncOutput);
}
// outputs
if (mySyncAll || mySyncOutputs)
{
// Delete all children nodes. This way the sync will completely recreate
// the connections. Otherwise, output connections may not be connected
// to the right output node. For example, if parts were inserted in the
// middle.
{
for (unsigned int i = 0; i < assetNodeFn.childCount(); i++)
{
MObject childNode = assetNodeFn.child(i);
// Can't use deleteNode() here, because it could delete the
// parent node as well.
MFnDagNode childFnDag(childNode);
myDagModifier.commandToExecute("delete " +
childFnDag.fullPathName());
}
// delete all the materials
MPlug materialsPlug = assetNodeFn.findPlug(
AssetNode::outputMaterials, true);
for (unsigned int i = 0; i < materialsPlug.numElements(); i++)
{
MPlug materialPlug = materialsPlug.elementByPhysicalIndex(i);
// Note that myDagModifier::doIt is invoked in deleteMaterials
// It's extremely likely that all the color outputs are still
// pointing at the same shader. So as soon as we find an output
// that has a shader it will be deleted, and the remaining
// deleteMaterials will have nothing to delete, but why take
// chances
MPlug shadingPlug =
materialPlug.child(AssetNode::outputMaterialDiffuseColor);
deleteMaterials(shadingPlug);
shadingPlug =
materialPlug.child(AssetNode::outputMaterialAmbientColor);
deleteMaterials(shadingPlug);
shadingPlug =
materialPlug.child(AssetNode::outputMaterialSpecularColor);
deleteMaterials(shadingPlug);
shadingPlug =
materialPlug.child(AssetNode::outputMaterialAlphaColor);
deleteMaterials(shadingPlug);
// there might also be a file texture connected for the diffuse
// color delete it, and the downstream shader too, it it's not
// already gone
MPlug texturePlug =
materialPlug.child(AssetNode::outputMaterialTexturePath);
MObject textureObj =
SyncOutputMaterial::findFileTexture(texturePlug);
if (!textureObj.isNull())
{
MFnDependencyNode textureFn(textureObj);
MString shadingPlugName("outColor");
shadingPlug = textureFn.findPlug(shadingPlugName, true);
deleteMaterials(shadingPlug);
myDagModifier.deleteNode(textureObj);
status = myDagModifier.doIt();
CHECK_MSTATUS_AND_RETURN_IT(status);
}
}
// Call doIt() here so that the delete commands are actually
// executed.
status = myDagModifier.doIt();
CHECK_MSTATUS_AND_RETURN_IT(status);
}
// Asset
{
MPlug assetTranslate = assetNodeFn.findPlug(
AssetNode::outputAssetTranslate, true);
MPlug assetRotate = assetNodeFn.findPlug(
AssetNode::outputAssetRotate, true);
MPlug assetScale = assetNodeFn.findPlug(
AssetNode::outputAssetScale, true);
MPlug transformTranslate = assetNodeFn.findPlug(
MPxTransform::translate, true);
MPlug transformRotate = assetNodeFn.findPlug(
MPxTransform::rotate, true);
MPlug transformScale = assetNodeFn.findPlug(
MPxTransform::scale, true);
AssetNodeOptions::AccessorFn options(
assetNodeOptionsDefinition, assetNodeFn);
if (options.useAssetObjectTransform())
{
if (Util::plugSource(transformTranslate).isNull())
myDagModifier.connect(assetTranslate, transformTranslate);
if (Util::plugSource(transformRotate).isNull())
myDagModifier.connect(assetRotate, transformRotate);
if (Util::plugSource(transformScale).isNull())
myDagModifier.connect(assetScale, transformScale);
}
else
{
if (Util::plugSource(transformTranslate) == assetTranslate)
myDagModifier.disconnect(
assetTranslate, transformTranslate);
if (Util::plugSource(transformRotate) == assetRotate)
myDagModifier.disconnect(assetRotate, transformRotate);
if (Util::plugSource(transformScale) == assetScale)
myDagModifier.disconnect(assetScale, transformScale);
}
}
status = myDagModifier.doIt();
CHECK_MSTATUS_AND_RETURN_IT(status);
// Objects
MPlug objectsPlug = assetNodeFn.findPlug(
AssetNode::outputObjects, true);
unsigned int objCount = objectsPlug.evaluateNumElements(&status);
CHECK_MSTATUS_AND_RETURN_IT(status);
int numGeosOutput = 0;
for (unsigned int i = 0; i < objCount; i++)
{
MPlug elemPlug = objectsPlug[i];
MPlug visibilityPlug = elemPlug.child(AssetNode::outputVisibility);
bool visible = visibilityPlug.asBool();
MPlug instancedPlug = elemPlug.child(AssetNode::outputIsInstanced);
bool instanced = instancedPlug.asBool();
if (mySyncOutputHidden || visible || instanced)
{
SubCommand *syncOutput = new SyncOutputObject(
elemPlug, myAssetNodeObj, visible,
mySyncOutputTemplatedGeos);
if (syncOutput->doIt() == MS::kSuccess)
numGeosOutput++;
myAssetSyncs.push_back(syncOutput);
}
}
// instancers
MPlug instancersPlug = assetNodeFn.findPlug(
AssetNode::outputInstancers, true);
unsigned int instCount = instancersPlug.numElements(&status);
CHECK_MSTATUS_AND_RETURN_IT(status);
for (unsigned int i = 0; i < instCount; i++)
{
MPlug elemPlug = instancersPlug[i];
SubCommand *syncOutput = new SyncOutputInstance(
elemPlug, i, myAssetNodeObj);
if (syncOutput->doIt() == MS::kSuccess)
numGeosOutput++;
myAssetSyncs.push_back(syncOutput);
}
if (numGeosOutput == 0)
{
getAssetNode()->setExtraAutoSync(true);
}
else
{
getAssetNode()->setExtraAutoSync(false);
}
}
if (postSyncCallback.length() > 0)
{
// command string to be built is postSyncCallback assetNode
// mySyncAttributes mySyncOutputs
MString postSyncCmd = postSyncCallback + MString(" ") +
assetPath.partialPathName() + syncAttrFlag +
syncOutputFlag;
MGlobal::executeCommand(postSyncCmd, false, true);
}
// restore old selection
MGlobal::setActiveSelectionList(oldSelection);
return MStatus::kSuccess;
}
MStatus
AssetSubCommandSync::redoIt()
{
myDagModifier.doIt();
for (AssetSyncs::iterator iter = myAssetSyncs.begin();
iter != myAssetSyncs.end(); iter++)
{
(*iter)->redoIt();
}
return MStatus::kSuccess;
}
MStatus
AssetSubCommandSync::undoIt()
{
for (AssetSyncs::reverse_iterator iter = myAssetSyncs.rbegin();
iter != myAssetSyncs.rend(); iter++)
{
(*iter)->undoIt();
}
myDagModifier.undoIt();
return MStatus::kSuccess;
}
bool
AssetSubCommandSync::isUndoable() const
{
return true;
}