-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathInputParticle.C
317 lines (269 loc) · 10.6 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
#include "InputParticle.h"
#include <maya/MDoubleArray.h>
#include <maya/MFnAttribute.h>
#include <maya/MFnParticleSystem.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 "hapiutil.h"
#include "util.h"
InputParticle::InputParticle() : Input()
{
Util::PythonInterpreterLock pythonInterpreterLock;
HAPI_NodeId nodeId;
CHECK_HAPI(HoudiniApi::CreateInputNode(Util::theHAPISession.get(), -1, &nodeId, NULL));
if (!Util::statusCheckLoop())
{
DISPLAY_ERROR(MString("Unexpected error when creating input particle."));
}
HAPI_NodeInfo nodeInfo;
HoudiniApi::GetNodeInfo(Util::theHAPISession.get(), nodeId, &nodeInfo);
setTransformNodeId(nodeInfo.parentId);
setGeometryNodeId(nodeId);
}
InputParticle::~InputParticle()
{
if (!Util::theHAPISession.get())
return;
CHECK_HAPI(HoudiniApi::DeleteNode(Util::theHAPISession.get(), geometryNodeId()));
}
Input::AssetInputType
InputParticle::assetInputType() const
{
return Input::AssetInputType_Particle;
}
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;
HoudiniApi::AddAttribute(Util::theHAPISession.get(), geometryNodeId(), 0,
attributeName, &attributeInfo);
switch (storage)
{
case HAPI_STORAGETYPE_FLOAT:
HoudiniApi::SetAttributeFloatData(Util::theHAPISession.get(), geometryNodeId(),
0, attributeName, &attributeInfo,
static_cast<float *>(data), 0, count);
break;
case HAPI_STORAGETYPE_INT:
HoudiniApi::SetAttributeIntData(Util::theHAPISession.get(), geometryNodeId(),
0, 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;
{
MPlug srcPlug = Util::plugSource(plug);
if (!srcPlug.isNull())
{
particleObj = srcPlug.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;
HoudiniApi::PartInfo_Init(&partInfo);
partInfo.id = 0;
partInfo.faceCount = 0;
partInfo.vertexCount = 0;
partInfo.pointCount = particleFn.count();
HoudiniApi::SetPartInfo(
Util::theHAPISession.get(), geometryNodeId(), 0, &partInfo);
// set per-particle attributes
{
// 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);
CHECK_HAPI(
hapiSetPointAttribute(geometryNodeId(), 0, 1, "id", ids));
}
// vector attributes
{
MVectorArray vectorArray;
bool doPreserveAttrScale = false;
// query the original particle for names of the per-particle
// attributes
MString getAttributesCommand = "particle -q -perParticleVector ";
getAttributesCommand += originalParticleFn.fullPathName();
// get the per-particle attribute names
MStringArray attributeNames;
MGlobal::executeCommand(getAttributesCommand, attributeNames);
for (unsigned int ai = 0; ai < attributeNames.length(); ai++)
{
const MString attributeName = attributeNames[ai];
MObject attributeObj =
originalParticleFn.attribute(attributeName);
if (attributeObj.isNull())
{
continue;
}
// mimics "listAttr -v -w" from AEokayAttr
MFnAttribute attributeFn(attributeObj);
if (!(!attributeFn.isHidden() && attributeFn.isWritable()) &&
(attributeName != "age" &&
attributeName != "finalLifespanPP"))
{
continue;
}
// get the per-particle data
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);
}
// When particle node is initially loaded from a scene file, if
// the attribute is driven by expressions, then
// MFnParticleSystem doesn't initially seem to have data.
if (partInfo.pointCount != (int)vectorArray.length())
{
vectorArray.setLength(partInfo.pointCount);
}
// map the attribute name
const char *mappedAttributeName = attributeName.asChar();
if (strcmp(mappedAttributeName, "position") == 0)
{
mappedAttributeName = "P";
doPreserveAttrScale = true;
}
else if (strcmp(mappedAttributeName, "velocity") == 0)
{
mappedAttributeName = "v";
doPreserveAttrScale = true;
}
else if (strcmp(mappedAttributeName, "acceleration") == 0)
{
mappedAttributeName = "force";
doPreserveAttrScale = true;
}
else if (strcmp(mappedAttributeName, "rgbPP") == 0)
{
mappedAttributeName = "Cd";
}
if (myPreserveScale && doPreserveAttrScale)
{
for (unsigned int i = 0; i < vectorArray.length(); i++)
vectorArray[i] *= 0.01;
}
CHECK_HAPI(hapiSetPointAttribute(
geometryNodeId(), 0, 3, mappedAttributeName,
Util::reshapeArray<3, std::vector<double>>(vectorArray)));
}
}
// double attributes
{
MDoubleArray doubleArray;
bool doPreserveAttrScale = false;
// query the original particle for names of the per-particle
// attributes
MString getAttributesCommand = "particle -q -perParticleDouble ";
getAttributesCommand += originalParticleFn.fullPathName();
// get the per-particle attribute names
MStringArray attributeNames;
MGlobal::executeCommand(getAttributesCommand, attributeNames);
// explicitly include some special per-particle attributes that
// aren't returned by the MEL command
attributeNames.append("age");
for (unsigned int ai = 0; ai < attributeNames.length(); ai++)
{
const MString attributeName = attributeNames[ai];
MObject attributeObj =
originalParticleFn.attribute(attributeName);
if (attributeObj.isNull())
{
continue;
}
// mimics "listAttr -v -w" from AEokayAttr
MFnAttribute attributeFn(attributeObj);
if (!(!attributeFn.isHidden() && attributeFn.isWritable()) &&
(attributeName != "age" &&
attributeName != "finalLifespanPP"))
{
continue;
}
// get the per-particle data
// Maya will automatically use the original
// particle node in the case of deformed particles.
particleFn.getPerParticleAttribute(attributeName, doubleArray);
// When particle node is initially loaded from a scene file, if
// the attribute is driven by expressions, then
// MFnParticleSystem doesn't initially seem to have data.
if (partInfo.pointCount != (int)doubleArray.length())
{
doubleArray.setLength(partInfo.pointCount);
}
// map the parameter name
const char *mappedAttributeName = attributeName.asChar();
if (strcmp(mappedAttributeName, "opacityPP") == 0)
{
mappedAttributeName = "Alpha";
}
else if (strcmp(mappedAttributeName, "radiusPP") == 0)
{
mappedAttributeName = "pscale";
doPreserveAttrScale = true;
}
else if (strcmp(mappedAttributeName, "finalLifespanPP") == 0)
{
mappedAttributeName = "life";
}
if (myPreserveScale && doPreserveAttrScale)
{
for (unsigned int i = 0; i < doubleArray.length(); i++)
doubleArray[i] *= 0.01;
}
CHECK_HAPI(hapiSetPointAttribute(
geometryNodeId(), 0, 1, mappedAttributeName, doubleArray));
}
}
}
setInputName(HAPI_ATTROWNER_POINT, partInfo.pointCount, plug);
// Commit it
HoudiniApi::CommitGeo(Util::theHAPISession.get(), geometryNodeId());
}