forked from sideeffects/HoudiniEngineForMaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssetSubCommandSync.C
246 lines (204 loc) · 7.13 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
#include "AssetSubCommandSync.h"
#include <maya/MGlobal.h>
#include <maya/MPlug.h>
#include <maya/MSelectionList.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MFnDagNode.h>
#include "AssetNode.h"
#include "AssetNodeOptions.h"
#include "SyncAttribute.h"
#include "SyncOutputObject.h"
#include "SyncOutputMaterial.h"
#include "SyncOutputInstance.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;
}
MStatus
AssetSubCommandSync::doIt()
{
MStatus status;
MFnDagNode assetNodeFn(myAssetNodeObj, &status);
// save selection
MSelectionList oldSelection;
MGlobal::getActiveSelectionList(oldSelection);
// 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);
for(unsigned int i = 0; i < materialsPlug.numElements(); i++)
{
MPlug materialPlug = materialsPlug.elementByPhysicalIndex(i);
MObject shaderObj = SyncOutputMaterial::findShader(materialPlug);
if(shaderObj.isNull())
{
continue;
}
MObject shadingGroupObj = SyncOutputMaterial::findShadingGroup(shaderObj);
if(!shaderObj.isNull())
{
myDagModifier.deleteNode(shaderObj);
}
if(!shaderObj.isNull())
{
myDagModifier.deleteNode(shadingGroupObj);
}
}
// 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);
MPlug assetRotate = assetNodeFn.findPlug(AssetNode::outputAssetRotate);
MPlug assetScale = assetNodeFn.findPlug(AssetNode::outputAssetScale);
MPlug transformTranslate = assetNodeFn.findPlug(MPxTransform::translate);
MPlug transformRotate = assetNodeFn.findPlug(MPxTransform::rotate);
MPlug transformScale = assetNodeFn.findPlug(MPxTransform::scale);
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);
unsigned int objCount = objectsPlug.evaluateNumElements(&status);
CHECK_MSTATUS_AND_RETURN_IT(status);
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);
syncOutput->doIt();
myAssetSyncs.push_back(syncOutput);
}
}
// instancers
MPlug instancersPlug = assetNodeFn.findPlug(AssetNode::outputInstancers);
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);
syncOutput->doIt();
myAssetSyncs.push_back(syncOutput);
}
}
// 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;
}