-
Notifications
You must be signed in to change notification settings - Fork 60
/
InputCurveNode.C
307 lines (246 loc) · 9.62 KB
/
InputCurveNode.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
#include <maya/MDataHandle.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFnNurbsCurve.h>
#include <maya/MFnTypedAttribute.h>
#include <maya/MPointArray.h>
#include "InputCurveNode.h"
#include "MayaTypeID.h"
#include "hapiutil.h"
#include "util.h"
#include <cassert>
MString InputCurveNode::typeName = "houdiniInputCurve";
MTypeId InputCurveNode::typeId = MayaTypeID_HoudiniInputCurveNode;
MObject InputCurveNode::inputCurve;
MObject InputCurveNode::outputNodeId;
MObject InputCurveNode::preserveScale;
void *
InputCurveNode::creator()
{
InputCurveNode *ret = new InputCurveNode();
return ret;
}
MStatus
InputCurveNode::initialize()
{
MFnNumericAttribute nAttr;
MFnTypedAttribute tAttr;
InputCurveNode::inputCurve = tAttr.create(
"inputCurve", "inputCurve", MFnData::kNurbsCurve);
tAttr.setDisconnectBehavior(MFnAttribute::kDelete);
tAttr.setArray(true);
addAttribute(InputCurveNode::inputCurve);
InputCurveNode::outputNodeId = nAttr.create(
"outputNodeId", "outputNodeId", MFnNumericData::kInt);
nAttr.setStorable(false);
nAttr.setWritable(false);
InputCurveNode::preserveScale = nAttr.create(
"preserveScale", "preserveScale", MFnNumericData::kBoolean, false);
nAttr.setCached(false);
nAttr.setStorable(true);
addAttribute(InputCurveNode::preserveScale);
addAttribute(InputCurveNode::outputNodeId);
attributeAffects(InputCurveNode::inputCurve, InputCurveNode::outputNodeId);
attributeAffects(
InputCurveNode::preserveScale, InputCurveNode::outputNodeId);
return MS::kSuccess;
}
InputCurveNode::InputCurveNode() : myNodeId(-1) {}
InputCurveNode::~InputCurveNode()
{
if (!Util::theHAPISession.get())
return;
if (myNodeId > 0)
{
CHECK_HAPI(HoudiniApi::DeleteNode(Util::theHAPISession.get(), myNodeId));
}
}
MStatus
InputCurveNode::compute(const MPlug &plug, MDataBlock &data)
{
MStatus status;
if (plug != InputCurveNode::outputNodeId)
{
return MPxNode::compute(plug, data);
}
if (myNodeId < 0)
{
Util::PythonInterpreterLock pythonInterpreterLock;
CHECK_HAPI(
HoudiniApi::CreateInputNode(Util::theHAPISession.get(), -1, &myNodeId, NULL));
if (!Util::statusCheckLoop())
{
DISPLAY_ERROR(
MString("Unexpected error when creating input curve node."));
}
// Meta data
MDataHandle metaDataHandle =
data.outputValue(InputCurveNode::outputNodeId);
metaDataHandle.setInt(myNodeId);
}
MPlug preserveScalePlug(thisMObject(), InputCurveNode::preserveScale);
bool preserveScale = preserveScalePlug.asBool();
MPlug inputCurveArrayPlug(thisMObject(), InputCurveNode::inputCurve);
const int nInputCurves = inputCurveArrayPlug.numElements();
if (nInputCurves <= 0)
{
data.setClean(plug);
return MStatus::kSuccess;
}
HAPI_CurveInfo curveInfo = HoudiniApi::CurveInfo_Create();
curveInfo.curveType = HAPI_CURVETYPE_NURBS;
curveInfo.isRational = false;
std::vector<float> cvP, cvPw;
std::vector<int> cvCounts;
std::vector<float> knots;
std::vector<int> orders;
std::vector<std::string> names;
for (int iCurve = 0; iCurve < nInputCurves; ++iCurve)
{
MPlug inputCurvePlug =
inputCurveArrayPlug.elementByPhysicalIndex(iCurve);
MDataHandle curveHandle = data.inputValue(inputCurvePlug);
MObject curveObject = curveHandle.asNurbsCurve();
MFnNurbsCurve fnCurve(curveObject);
const bool isPeriodic = fnCurve.form() == MFnNurbsCurve::kPeriodic;
if (iCurve == 0)
{
curveInfo.isPeriodic = isPeriodic;
}
else if (isPeriodic != curveInfo.isPeriodic)
{
DISPLAY_ERROR(
MString("Curve has a non-matching periodicity, skipping"));
continue;
}
const int order = fnCurve.degree() + 1;
if (iCurve == 0)
{
curveInfo.order = order;
}
else if (curveInfo.order == HAPI_CURVE_ORDER_VARYING)
{
orders.push_back(order);
}
else if (order != curveInfo.order)
{
orders.resize(curveInfo.curveCount, curveInfo.order);
curveInfo.order = HAPI_CURVE_ORDER_VARYING;
orders.push_back(order);
}
MPointArray cvArray;
CHECK_MSTATUS_AND_RETURN_IT(fnCurve.getCVs(cvArray, MSpace::kWorld));
unsigned int nCVs = cvArray.length();
// Maya provides fnCurve.degree() more cvs in its data definition
// than Houdini for periodic curves -- but they are conincident
// with the first ones. Houdini ignores them, so we don't
// output them.
if (curveInfo.isPeriodic && static_cast<int>(nCVs) > fnCurve.degree())
{
nCVs -= fnCurve.degree();
}
cvCounts.push_back(nCVs);
for (unsigned int iCV = 0; iCV < nCVs; ++iCV, ++curveInfo.vertexCount)
{
MPoint &cv = cvArray[iCV];
if (preserveScale)
{
cv.x *= 0.01f;
cv.y *= 0.01f;
cv.z *= 0.01f;
}
cvP.push_back((float)cv.x);
cvP.push_back((float)cv.y);
cvP.push_back((float)cv.z);
if (!curveInfo.isRational)
{
if (cv.w != 1.0)
{
curveInfo.isRational = true;
cvPw.resize(curveInfo.vertexCount, 1.0f);
cvPw.push_back((float)cv.w);
}
}
else
{
cvPw.push_back((float)cv.w);
}
}
MDoubleArray knotsArray;
CHECK_MSTATUS_AND_RETURN_IT(fnCurve.getKnots(knotsArray));
if (knotsArray.length() > 0)
{
// Maya doesn't provide the first and last knots
curveInfo.knotCount += knotsArray.length() + 2;
knots.push_back(static_cast<float>(knotsArray[0]));
// Maya seems ok with having end knots of multiplicity > order
// (counting the 1st and last knots added above)
// so if we detect this, warn the user to rebuild their curves
if (fabs(knotsArray[0] - knotsArray[order - 1]) < .0001)
{
MPlug curveSrcPlug = Util::plugSource(inputCurvePlug);
MFnDependencyNode curveSrcObj(curveSrcPlug.node());
DISPLAY_WARNING("Curve ^1s has knots with higher multiplicity "
"than the order of the curve."
"You may need to rebuild the curve in order to "
"see it in Houdini",
curveSrcObj.name());
}
for (unsigned int iKnot = 0; iKnot < knotsArray.length(); ++iKnot)
{
knots.push_back(static_cast<float>(knotsArray[iKnot]));
}
knots.push_back(
static_cast<float>(knotsArray[knotsArray.length() - 1]));
}
++curveInfo.curveCount;
names.push_back(
Util::getNodeName(Util::plugSource(inputCurvePlug).node())
.asChar());
}
curveInfo.hasKnots = curveInfo.knotCount > 0;
HAPI_PartInfo partInfo = HoudiniApi::PartInfo_Create();
partInfo.vertexCount = partInfo.pointCount = curveInfo.vertexCount;
partInfo.faceCount = curveInfo.curveCount;
partInfo.type = HAPI_PARTTYPE_CURVE;
CHECK_HAPI(
HoudiniApi::SetPartInfo(Util::theHAPISession.get(), myNodeId, 0, &partInfo));
CHECK_HAPI(
HoudiniApi::SetCurveInfo(Util::theHAPISession.get(), myNodeId, 0, &curveInfo));
CHECK_HAPI(HoudiniApi::SetCurveCounts(Util::theHAPISession.get(), myNodeId, 0,
&cvCounts.front(), 0, cvCounts.size()));
if (curveInfo.order == HAPI_CURVE_ORDER_VARYING)
{
CHECK_HAPI(HoudiniApi::SetCurveOrders(Util::theHAPISession.get(), myNodeId, 0,
&orders.front(), 0, orders.size()));
}
HAPI_AttributeInfo attrInfo = HoudiniApi::AttributeInfo_Create();
attrInfo.count = partInfo.pointCount;
attrInfo.tupleSize = 3; // 3 floats per CV (x, y, z)
attrInfo.exists = true;
attrInfo.owner = HAPI_ATTROWNER_POINT;
attrInfo.storage = HAPI_STORAGETYPE_FLOAT;
CHECK_HAPI(HoudiniApi::AddAttribute(
Util::theHAPISession.get(), myNodeId, 0, "P", &attrInfo));
CHECK_HAPI(HoudiniApi::SetAttributeFloatData(Util::theHAPISession.get(), myNodeId,
0, "P", &attrInfo, &cvP.front(), 0,
static_cast<int>(cvP.size() / 3)));
if (curveInfo.isRational)
{
attrInfo.tupleSize = 1;
CHECK_HAPI(HoudiniApi::AddAttribute(
Util::theHAPISession.get(), myNodeId, 0, "Pw", &attrInfo));
CHECK_HAPI(HoudiniApi::SetAttributeFloatData(
Util::theHAPISession.get(), myNodeId, 0, "Pw", &attrInfo,
&cvPw.front(), 0, static_cast<int>(cvPw.size())));
}
if (curveInfo.hasKnots)
{
CHECK_HAPI(HoudiniApi::SetCurveKnots(Util::theHAPISession.get(), myNodeId, 0,
&knots.front(), 0,
static_cast<int>(knots.size())));
}
CHECK_HAPI(hapiSetPrimAttribute(myNodeId, 0, 1, "name", names));
CHECK_HAPI(HoudiniApi::CommitGeo(Util::theHAPISession.get(), myNodeId));
data.setClean(plug);
return MStatus::kSuccess;
}