forked from sideeffects/HoudiniEngineForMaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputParticle.C
327 lines (285 loc) · 9.56 KB
/
InputParticle.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
#include "InputParticle.h"
#include <maya/MFnAttribute.h>
#include <maya/MFnParticleSystem.h>
#include <maya/MDoubleArray.h>
#include <maya/MGlobal.h>
#include <maya/MMatrix.h>
#include <maya/MPlug.h>
#include <maya/MPlugArray.h>
#include <maya/MStringArray.h>
#include <maya/MVectorArray.h>
#include "util.h"
InputParticle::InputParticle(int assetId, int inputIdx) :
Input(assetId, inputIdx),
myInputAssetId(0)
{
Util::PythonInterpreterLock pythonInterpreterLock;
CHECK_HAPI(HAPI_CreateInputAsset(&myInputAssetId, NULL));
if(!Util::statusCheckLoop())
{
DISPLAY_ERROR(MString("Unexpected error when creating input asset."));
}
myInputObjectId = 0;
myInputGeoId = 0;
CHECK_HAPI(HAPI_ConnectAssetGeometry(
myInputAssetId, myInputObjectId,
myAssetId, myInputIdx
));
}
InputParticle::~InputParticle()
{
CHECK_HAPI(HAPI_DestroyAsset(myInputAssetId));
}
Input::AssetInputType
InputParticle::assetInputType() const
{
return Input::AssetInputType_Particle;
}
void
InputParticle::setInputTransform(MDataHandle &dataHandle)
{
MMatrix transformMatrix = dataHandle.asMatrix();
float matrix[16];
transformMatrix.get(reinterpret_cast<float(*)[4]>(matrix));
HAPI_TransformEuler transformEuler;
HAPI_ConvertMatrixToEuler(matrix, HAPI_SRT, HAPI_XYZ, &transformEuler);
HAPI_SetObjectTransform(myInputAssetId, myInputObjectId, &transformEuler);
}
void
InputParticle::setAttributePointData(
const char* attributeName,
HAPI_StorageType storage,
int count,
int tupleSize,
void* data
)
{
HAPI_AttributeInfo attributeInfo;
attributeInfo.exists = true;
attributeInfo.owner = HAPI_ATTROWNER_POINT;
attributeInfo.storage = storage;
attributeInfo.count = count;
attributeInfo.tupleSize = tupleSize;
HAPI_AddAttribute(
myInputAssetId, myInputObjectId, myInputGeoId,
attributeName,
&attributeInfo
);
switch(storage)
{
case HAPI_STORAGETYPE_FLOAT:
HAPI_SetAttributeFloatData(
myInputAssetId, myInputObjectId, myInputGeoId,
attributeName,
&attributeInfo,
static_cast<float*>(data),
0, count
);
break;
case HAPI_STORAGETYPE_INT:
HAPI_SetAttributeIntData(
myInputAssetId, myInputObjectId, myInputGeoId,
attributeName,
&attributeInfo,
static_cast<int*>(data),
0, count
);
break;
default:
break;
}
}
void
InputParticle::setInputGeo(
MDataBlock &dataBlock,
const MPlug &plug
)
{
MStatus status;
// get particle node
MObject particleObj;
{
MPlugArray plugArray;
plug.connectedTo(plugArray, true, false, &status);
CHECK_MSTATUS(status);
if(plugArray.length() == 1)
{
particleObj = plugArray[0].node();
}
}
if(particleObj.isNull())
{
return;
}
MFnParticleSystem particleFn(particleObj);
// get original particle node
MObject originalParticleObj;
// status parameter is needed due to a bug in Maya API
if(particleFn.isDeformedParticleShape(&status))
{
originalParticleObj = particleFn.originalParticleShape(&status);
}
else
{
originalParticleObj = particleObj;
}
MFnParticleSystem originalParticleFn(originalParticleObj);
// set up part info
HAPI_PartInfo partInfo;
HAPI_PartInfo_Init(&partInfo);
partInfo.id = 0;
partInfo.faceCount = 0;
partInfo.vertexCount = 0;
partInfo.pointCount = particleFn.count();
HAPI_SetPartInfo(
myInputAssetId, myInputObjectId, myInputGeoId,
&partInfo
);
// set per-particle attributes
{
// shared buffers for passing data
MVectorArray vectorArray;
MDoubleArray doubleArray;
char* data = new char[sizeof(float) * 3 * particleFn.count()];
// id
{
MIntArray ids;
// Must get the IDs from the original particle node. Maya will
// crash if we try to get the IDs from the deformed particle node.
originalParticleFn.particleIds(ids);
ids.get(reinterpret_cast<int*>(data));
setAttributePointData(
"id",
HAPI_STORAGETYPE_INT,
particleFn.count(),
1,
data
);
}
// 0: vector
// 1: double
for(int i = 0; i < 2; i++)
{
int tupleSize = 0;
MString getAttributesCommand = "particle -q ";
switch(i)
{
case 0:
tupleSize = 3;
getAttributesCommand += "-perParticleVector ";
break;
case 1:
tupleSize = 1;
getAttributesCommand += "-perParticleDouble ";
break;
}
// query the original particle for names of the per-particle attributes
getAttributesCommand += originalParticleFn.fullPathName();
// get the per-particle attribute names
MStringArray attributeNames;
MGlobal::executeCommand(getAttributesCommand, attributeNames);
for(unsigned int j = 0; j < attributeNames.length(); j++)
{
const MString attributeName = attributeNames[j];
MObject attributeObj = originalParticleFn.attribute(attributeName);
if(attributeObj.isNull())
{
continue;
}
// mimics "listAttr -v -w" from AEokayAttr
MFnAttribute attributeFn(attributeObj);
if(!(!attributeFn.isHidden() && attributeFn.isWritable()))
{
continue;
}
// get the per-particle data
switch(i)
{
case 0:
if(attributeName == "position")
{
// Need to use position() so that we get the right
// positions in the case of deformed particles.
particleFn.position(vectorArray);
}
else
{
// Maya will automatically use the original
// particle node in the case of deformed particles.
particleFn.getPerParticleAttribute(
attributeName,
vectorArray
);
}
vectorArray.get(reinterpret_cast<float(*)[3]>(data));
break;
case 1:
// Maya will automatically use the original
// particle node in the case of deformed particles.
particleFn.getPerParticleAttribute(
attributeName,
doubleArray
);
doubleArray.get(reinterpret_cast<float(*)>(data));
break;
}
// map the parameter name
const char* parameterName = attributeName.asChar();
switch(i)
{
case 0:
if(strcmp(parameterName, "position") == 0)
{
parameterName = "P";
}
else if(strcmp(parameterName, "velocity") == 0)
{
parameterName = "v";
}
else if(strcmp(parameterName, "acceleration") == 0)
{
parameterName = "force";
}
else if(strcmp(parameterName, "rgbPP") == 0)
{
parameterName = "Cd";
}
break;
case 1:
if(strcmp(parameterName, "opacityPP") == 0)
{
parameterName = "Alpha";
}
else if(strcmp(parameterName, "radiusPP") == 0)
{
parameterName = "pscale";
}
break;
}
setAttributePointData(
parameterName,
HAPI_STORAGETYPE_FLOAT,
particleFn.count(),
tupleSize,
data
);
}
switch(i)
{
case 0:
vectorArray.clear();
break;
case 1:
doubleArray.clear();
break;
}
}
delete [] data;
}
Input::setInputPlugMetaData(
plug,
myInputAssetId, myInputObjectId, myInputGeoId
);
// Commit it
HAPI_CommitGeo(myInputAssetId, myInputObjectId, myInputGeoId);
}