-
Notifications
You must be signed in to change notification settings - Fork 60
/
SyncOutputInstance.C
287 lines (238 loc) · 8.8 KB
/
SyncOutputInstance.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
#include "SyncOutputInstance.h"
#include <maya/MDagModifier.h>
#include <maya/MDagPath.h>
#include <maya/MGlobal.h>
#include <maya/MFnDagNode.h>
#include <maya/MItDag.h>
#include "AssetNode.h"
#include "AssetNodeOptions.h"
#include "util.h"
SyncOutputInstance::SyncOutputInstance(const MPlug &outputPlug,
const int parentMultiIndex,
const MObject &assetNodeObj)
: myOutputPlug(outputPlug),
myAssetNodeObj(assetNodeObj),
myParentMultiIndex(parentMultiIndex)
{
}
SyncOutputInstance::~SyncOutputInstance() {}
MStatus
SyncOutputInstance::doIt()
{
MStatus status;
status = createOutput();
CHECK_MSTATUS_AND_RETURN_IT(status);
return redoIt();
}
MStatus
SyncOutputInstance::undoIt()
{
MStatus status;
status = myDagModifier.undoIt();
CHECK_MSTATUS_AND_RETURN_IT(status);
return MStatus::kSuccess;
}
MStatus
SyncOutputInstance::redoIt()
{
MStatus status;
status = myDagModifier.doIt();
CHECK_MSTATUS_AND_RETURN_IT(status);
return MStatus::kSuccess;
}
bool
SyncOutputInstance::isUndoable() const
{
return true;
}
void
SyncOutputInstance::instanceObject(MDagPath &objToInstance,
MObject instancerTransform,
int pointIndex)
{
MFnDagNode fnAsset(myAssetNodeObj);
MDagPath assetPath;
fnAsset.getPath(assetPath);
MFnDagNode fnInstancerTransform(instancerTransform);
MDagPath instancerTransformPath;
fnInstancerTransform.getPath(instancerTransformPath);
MString cmd = "houdiniEngine_createInstance(\"";
cmd += assetPath.partialPathName();
cmd += "\",\"";
cmd += objToInstance.partialPathName();
cmd += "\",\"";
cmd += instancerTransformPath.partialPathName();
cmd += "\",";
cmd += myParentMultiIndex;
cmd += ",";
cmd += pointIndex;
cmd += ");";
MGlobal::executeCommand(cmd);
}
bool
SyncOutputInstance::stringStartsWith(const MString &string,
const MString &startsWith)
{
if (string.length() >= startsWith.length())
{
if (string.substring(0, startsWith.length() - 1) == startsWith)
{
return true;
}
}
return false;
}
bool
SyncOutputInstance::instanceObjects(MObject searchRoot,
MObject instancerTransform,
int pointIndex,
const MString &objectToInstanceName,
const MString &houdiniInstanceAttr,
const MString &houdiniNameAttr)
{
MItDag dagIt(MItDag::kDepthFirst, MFn::kTransform);
dagIt.reset(searchRoot);
for (; !dagIt.isDone(); dagIt.next())
{
MDagPath currPath;
if (dagIt.getPath(currPath) != MS::kSuccess)
{
continue;
}
MObject node = currPath.node();
MFnDagNode fnNode(node);
MString nodeName = fnNode.name();
if (objectToInstanceName.length() > 0 &&
stringStartsWith(nodeName, objectToInstanceName))
{
instanceObject(currPath, instancerTransform, pointIndex);
return true;
}
if (houdiniInstanceAttr.length() > 0)
{
MStringArray splitObjName;
houdiniInstanceAttr.split('/', splitObjName);
MString instanceAttrObjectName =
splitObjName[splitObjName.length() - 1];
if (stringStartsWith(nodeName, instanceAttrObjectName))
{
instanceObject(currPath, instancerTransform, pointIndex);
return true;
}
}
if (houdiniNameAttr.length() > 0)
{
if (stringStartsWith(nodeName, houdiniNameAttr))
{
instanceObject(currPath, instancerTransform, pointIndex);
return true;
}
}
}
return false;
}
MStatus
SyncOutputInstance::createOutput()
{
MStatus status;
MFnDagNode assetNodeFn(myAssetNodeObj);
AssetNodeOptions::AccessorFn options(
assetNodeOptionsDefinition, assetNodeFn);
if (options.useInstancerNode())
{
// create the instancer node
MObject instancer = myDagModifier.createNode(
"instancer", myAssetNodeObj, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);
MFnDependencyNode instancerFn(instancer, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);
// set the rotation units to radians
status = myDagModifier.newPlugValueInt(
instancerFn.findPlug("rotationAngleUnits", true), 1);
CHECK_MSTATUS_AND_RETURN_IT(status);
{
MPlug srcPlug;
MPlug dstPlug;
// inputPoints
srcPlug = myOutputPlug.child(AssetNode::outputInstancerData);
dstPlug = instancerFn.findPlug("inputPoints", true);
status = myDagModifier.connect(srcPlug, dstPlug);
CHECK_MSTATUS_AND_RETURN_IT(status);
// go through every instanced objects
MPlug instancedNamesPlug =
myOutputPlug.child(AssetNode::outputInstancedObjectNames);
MPlug inputHierarchyPlug = instancerFn.findPlug(
"inputHierarchy", true);
for (unsigned int i = 0; i < instancedNamesPlug.numElements(); i++)
{
MObject objectTransform = Util::findDagChild(
assetNodeFn, instancedNamesPlug[i].asString());
MFnDependencyNode objectTransformFn(objectTransform);
// connect inputHierarchy
srcPlug = objectTransformFn.findPlug("matrix", true);
dstPlug = inputHierarchyPlug.elementByLogicalIndex(i);
status = myDagModifier.connect(srcPlug, dstPlug);
CHECK_MSTATUS_AND_RETURN_IT(status);
// set objectTransform hidden
status = myDagModifier.newPlugValueInt(
objectTransformFn.findPlug("visibility", true), 0);
CHECK_MSTATUS_AND_RETURN_IT(status);
}
}
}
else
{
MObject instancerTransform = myDagModifier.createNode(
"transform", myAssetNodeObj, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);
myDagModifier.renameNode(instancerTransform, "instancer");
myDagModifier.doIt();
MPlug instanceTransformPlug = assetNodeFn.findPlug(
AssetNode::outputInstanceTransform, true);
instanceTransformPlug.selectAncestorLogicalIndex(
myParentMultiIndex, AssetNode::outputInstancers);
unsigned int numPoints = instanceTransformPlug.numElements();
MString objectToInstanceName("");
MPlug instanceObjectNamesPlug = assetNodeFn.findPlug(
AssetNode::outputInstancedObjectNames, true);
instanceObjectNamesPlug.selectAncestorLogicalIndex(
myParentMultiIndex, AssetNode::outputInstancers);
unsigned int numInstanceObjects = instanceObjectNamesPlug.numElements();
if (numInstanceObjects == 1)
{
MPlug namePlug = instanceObjectNamesPlug.elementByLogicalIndex(0);
objectToInstanceName = namePlug.asString();
}
MPlug houdiniInstanceAttributesPlug = assetNodeFn.findPlug(
AssetNode::outputHoudiniInstanceAttribute, true);
houdiniInstanceAttributesPlug.selectAncestorLogicalIndex(
myParentMultiIndex, AssetNode::outputInstancers);
unsigned int numHoudiniInstanceAttributes =
houdiniInstanceAttributesPlug.numElements();
MPlug houdiniNameAttributesPlug = assetNodeFn.findPlug(
AssetNode::outputHoudiniNameAttribute, true);
houdiniNameAttributesPlug.selectAncestorLogicalIndex(
myParentMultiIndex, AssetNode::outputInstancers);
unsigned int numHoudiniNameAttributes =
houdiniNameAttributesPlug.numElements();
for (unsigned int ii = 0; ii < numPoints; ii++)
{
MPlug houdiniInstanceAttrPlug =
houdiniInstanceAttributesPlug.elementByLogicalIndex(ii);
MString instanceAttrStr("");
if (numHoudiniInstanceAttributes == numPoints)
instanceAttrStr = houdiniInstanceAttrPlug.asString();
MPlug houdiniNameAttrPlug =
houdiniNameAttributesPlug.elementByLogicalIndex(ii);
MString nameAttrStr("");
if (numHoudiniNameAttributes == numPoints)
nameAttrStr = houdiniNameAttrPlug.asString();
instanceObjects(myAssetNodeObj, instancerTransform, ii,
objectToInstanceName, instanceAttrStr, nameAttrStr);
}
}
// doIt
status = myDagModifier.doIt();
CHECK_MSTATUS_AND_RETURN_IT(status);
return MStatus::kSuccess;
}