forked from sideeffects/HoudiniEngineForMaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OutputInstancerObject.C
374 lines (319 loc) · 12.4 KB
/
OutputInstancerObject.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include <maya/MFnMesh.h>
#include <maya/MFnMeshData.h>
#include <maya/MEulerRotation.h>
#include <maya/MQuaternion.h>
#include <maya/MFnArrayAttrsData.h>
#include <maya/MArrayDataBuilder.h>
#include "Asset.h"
#include "AssetNode.h"
#include "OutputInstancerObject.h"
#include "hapiutil.h"
#include "util.h"
OutputInstancerObject::OutputInstancerObject(
HAPI_NodeId nodeId
) :
OutputObject(
nodeId
),
myGeoInfo(HAPI_GeoInfo_Create()),
myLastSopCookCount(0)
{
//update();
}
OutputInstancerObject::~OutputInstancerObject() {}
OutputObject::ObjectType
OutputInstancerObject::type()
{
return OutputObject::OBJECT_TYPE_INSTANCER;
}
void
OutputInstancerObject::update()
{
HAPI_Result hapiResult;
hapiResult = HAPI_GetNodeInfo(
Util::theHAPISession.get(),
myNodeId, &myNodeInfo
);
CHECK_HAPI(hapiResult);
hapiResult = HAPI_GetObjectInfo(
Util::theHAPISession.get(),
myNodeId, &myObjectInfo
);
CHECK_HAPI(hapiResult);
// Get the SOP nodes
int geoCount;
hapiResult = HAPI_ComposeChildNodeList(
Util::theHAPISession.get(),
myNodeId,
HAPI_NODETYPE_SOP,
HAPI_NODEFLAGS_DISPLAY,
false,
&geoCount
);
CHECK_HAPI(hapiResult);
std::vector<HAPI_NodeId> geoNodeIds(geoCount);
if(geoCount > 0)
{
hapiResult = HAPI_GetComposedChildNodeList(
Util::theHAPISession.get(),
myNodeId,
&geoNodeIds.front(),
geoCount
);
CHECK_HAPI(hapiResult);
hapiResult = HAPI_GetNodeInfo(
Util::theHAPISession.get(),
geoNodeIds[0],
&mySopNodeInfo
);
CHECK_HAPI(hapiResult);
hapiResult = HAPI_GetGeoInfo(
Util::theHAPISession.get(),
geoNodeIds[0],
&myGeoInfo
);
CHECK_HAPI(hapiResult);
}
if(mySopNodeInfo.totalCookCount > myLastSopCookCount)
{
// clear the arrays
myInstancedObjectNames.clear();
myInstancedObjectIndices.clear();
myUniqueInstObjNames.clear();
myHoudiniInstanceAttribute.clear();
myHoudiniNameAttribute.clear();
hapiResult = HAPI_GetPartInfo(
Util::theHAPISession.get(),
mySopNodeInfo.id, 0,
&myPartInfo
);
CHECK_HAPI(hapiResult);
if(myObjectInfo.objectToInstanceId >= 0)
{
myInstancedObjectIndices = MIntArray(myPartInfo.pointCount, 0);
return;
}
// fill array of size pointCount of instanced names
HAPI_AttributeInfo attrInfo;
MStringArray instanceAttrs;
hapiGetPointAttribute(myNodeId, 0, "instance", attrInfo, instanceAttrs);
for(unsigned int i=0; i<instanceAttrs.length(); i++)
{
MStringArray splitObjName;
instanceAttrs[i].split('/', splitObjName);
myInstancedObjectNames.append(splitObjName[splitObjName.length()-1]);
myHoudiniInstanceAttribute.append(instanceAttrs[i]);
}
MStringArray nameAttrs;
hapiGetPointAttribute(myNodeId, 0, "name", attrInfo, nameAttrs);
for(unsigned int ii = 0; ii < nameAttrs.length(); ii++)
{
myHoudiniNameAttribute.append(nameAttrs[ii]);
}
// get a list of unique instanced names, and compute the object indices that would
// be passed to Maya instancer
for(unsigned int i = 0; i< myInstancedObjectNames.length(); ++i)
{
bool duplicate = false;
unsigned int j = 0;
for(; j< myUniqueInstObjNames.length(); ++j)
{
if(myUniqueInstObjNames[j] == myInstancedObjectNames[i])
{
duplicate = true;
break;
}
}
if(!duplicate)
myUniqueInstObjNames.append(myInstancedObjectNames[i]);
myInstancedObjectIndices.append((int) j);
}
// Workaround a crash where we can't determine the object to instance.
if(!myInstancedObjectNames.length())
{
myInstancedObjectIndices = MIntArray(myPartInfo.pointCount, -1);
}
}
}
MIntArray
OutputInstancerObject::getInstancedObjIds()
{
MIntArray ret;
if(myObjectInfo.objectToInstanceId >= 0)
ret.append(myObjectInfo.objectToInstanceId);
return ret;
}
MStringArray
OutputInstancerObject::getUniqueInstObjNames()
{
return myUniqueInstObjNames;
}
MStatus
//OutputInstancerObject::compute(const MPlug& plug, MDataBlock& data)
OutputInstancerObject::compute(
const MTime &time,
MDataBlock& data,
MDataHandle& handle,
bool &needToSyncOutputs
)
{
update();
if(myGeoInfo.partCount <= 0)
return MS::kFailure;
if(mySopNodeInfo.totalCookCount > myLastSopCookCount)
{
MDataHandle useInstancerHandle = data.inputValue(AssetNode::useInstancerNode);
bool useInstancer = useInstancerHandle.asBool();
MDataHandle instancerDataHandle = handle.child(AssetNode::outputInstancerData);
MArrayDataHandle instancedObjectNamesHandle = handle.child(AssetNode::outputInstancedObjectNames);
MArrayDataHandle houdiniInstanceAttributeHandle = handle.child(AssetNode::outputHoudiniInstanceAttribute);
MArrayDataHandle houdiniNameAttributeHandle = handle.child(AssetNode::outputHoudiniNameAttribute);
MArrayDataHandle instanceTransformHandle = handle.child(AssetNode::outputInstanceTransform);
//MDataHandle instHandle = data.outputValue(instancerDataPlug);
MObject arrayDataObj = instancerDataHandle.data();
MFnArrayAttrsData arrayDataFn(arrayDataObj);
if(arrayDataObj.isNull())
{
arrayDataObj = arrayDataFn.create();
instancerDataHandle.set(arrayDataObj);
arrayDataObj = instancerDataHandle.data();
arrayDataFn.setObject(arrayDataObj);
}
MVectorArray positions = arrayDataFn.vectorArray("position");
MVectorArray rotations = arrayDataFn.vectorArray("rotation");
MVectorArray scales = arrayDataFn.vectorArray("scale");
MIntArray objectIndices = arrayDataFn.intArray("objectIndex");
unsigned int size = myPartInfo.pointCount;
HAPI_Transform * instTransforms = new HAPI_Transform[size];
CHECK_HAPI(HAPI_GetInstanceTransforms(
Util::theHAPISession.get(),
mySopNodeInfo.id,
HAPI_SRT,
instTransforms,
0, size
));
MArrayDataBuilder houdiniInstanceAttributeBuilder = houdiniInstanceAttributeHandle.builder();
MArrayDataBuilder houdiniNameAttributeBuilder = houdiniNameAttributeHandle.builder();
MArrayDataBuilder instanceTransformBuilder = instanceTransformHandle.builder();
if(positions.length() != size && !useInstancer)
{
needToSyncOutputs = true;
}
positions.setLength(size);
rotations.setLength(size);
scales.setLength(size);
objectIndices.setLength(size);
for(unsigned int j=0; j<size; j++)
{
HAPI_Transform it = instTransforms[j];
MVector p(it.position[0], it.position[1], it.position[2]);
MVector r = MQuaternion(it.rotationQuaternion[0],
it.rotationQuaternion[1], it.rotationQuaternion[2],
it.rotationQuaternion[3]).asEulerRotation().asVector();
MVector s(it.scale[0], it.scale[1], it.scale[2]);
int objIndex = myInstancedObjectIndices[j];
positions[j] = p;
rotations[j] = r;
scales[j] = s;
objectIndices[j] = objIndex;
if(myHoudiniInstanceAttribute.length() == size)
{
MDataHandle intanceAttributeHandle = houdiniInstanceAttributeBuilder.addElement(j);
intanceAttributeHandle.set(myHoudiniInstanceAttribute[j]);
}
if(myHoudiniNameAttribute.length() == size)
{
MDataHandle nameAttributeHandle = houdiniNameAttributeBuilder.addElement(j);
nameAttributeHandle.set(myHoudiniNameAttribute[j]);
}
MDataHandle transformHandle = instanceTransformBuilder.addElement(j);
MDataHandle translateHandle = transformHandle.child(AssetNode::outputInstanceTranslate);
MDataHandle rotateHandle = transformHandle.child(AssetNode::outputInstanceRotate);
MDataHandle scaleHandle = transformHandle.child(AssetNode::outputInstanceScale);
MDataHandle txHandle = translateHandle.child(AssetNode::outputInstanceTranslateX);
txHandle.set(p.x);
MDataHandle tyHandle = translateHandle.child(AssetNode::outputInstanceTranslateY);
tyHandle.set(p.y);
MDataHandle tzHandle = translateHandle.child(AssetNode::outputInstanceTranslateZ);
tzHandle.set(p.z);
MDataHandle rxHandle = rotateHandle.child(AssetNode::outputInstanceRotateX);
rxHandle.set(r.x);
MDataHandle ryHandle = rotateHandle.child(AssetNode::outputInstanceRotateY);
ryHandle.set(r.y);
MDataHandle rzHandle = rotateHandle.child(AssetNode::outputInstanceRotateZ);
rzHandle.set(r.z);
MDataHandle sxHandle = scaleHandle.child(AssetNode::outputInstanceScaleX);
sxHandle.set(s.x);
MDataHandle syHandle = scaleHandle.child(AssetNode::outputInstanceScaleY);
syHandle.set(s.y);
MDataHandle szHandle = scaleHandle.child(AssetNode::outputInstanceScaleZ);
szHandle.set(s.z);
}
// clean up extra elements
for(unsigned int i = houdiniInstanceAttributeBuilder.elementCount();
i-- > size;)
{
houdiniInstanceAttributeBuilder.removeElement(i);
}
// clean up extra elements
for(unsigned int i = houdiniNameAttributeBuilder.elementCount();
i-- > size;)
{
houdiniNameAttributeBuilder.removeElement(i);
}
// clean up extra elements
for(unsigned int i = instanceTransformBuilder.elementCount();
i-- > size;)
{
instanceTransformBuilder.removeElement(i);
}
houdiniInstanceAttributeHandle.set(houdiniInstanceAttributeBuilder);
houdiniInstanceAttributeHandle.setAllClean();
houdiniNameAttributeHandle.set(houdiniNameAttributeBuilder);
houdiniNameAttributeHandle.setAllClean();
instanceTransformHandle.set(instanceTransformBuilder);
instanceTransformHandle.setAllClean();
delete[] instTransforms;
MArrayDataBuilder builder = instancedObjectNamesHandle.builder();
if(myObjectInfo.objectToInstanceId >= 0)
{
// instancing a single object
HAPI_ObjectInfo instanceObjectInfo;
CHECK_HAPI(HAPI_GetObjectInfo(
Util::theHAPISession.get(),
myObjectInfo.objectToInstanceId,
&instanceObjectInfo
));
MString name = Util::HAPIString(instanceObjectInfo.nameSH);
MDataHandle h = builder.addElement(0);
h.set(name);
// clean up extra elements
for(unsigned int i= builder.elementCount(); i-- > 1;)
{
builder.removeElement(i);
}
} else
{
// instancing multiple objects
for(unsigned int i=0; i< myUniqueInstObjNames.length(); i++)
{
MDataHandle h = builder.addElement(i);
h.set(myUniqueInstObjNames[i]);
}
// clean up extra elements
for(unsigned int i= builder.elementCount();
i-- > myUniqueInstObjNames.length();)
{
builder.removeElement(i);
}
}
instancedObjectNamesHandle.set(builder);
instancedObjectNamesHandle.setAllClean();
handle.setClean();
instancerDataHandle.setClean();
//data.setClean(plug);
//data.setClean(instancerDataPlug);
myLastSopCookCount = mySopNodeInfo.totalCookCount;
}
return MS::kSuccess;
}