forked from sideeffects/HoudiniEngineForMaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurveMeshInputNode.C
268 lines (217 loc) · 7.35 KB
/
CurveMeshInputNode.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
#include <maya/MFnTypedAttribute.h>
#include <maya/MDataHandle.h>
#include <maya/MPointArray.h>
#include <maya/MFnNurbsCurve.h>
#include <maya/MFnIntArrayData.h>
#include "CurveMeshInputNode.h"
#include "MayaTypeID.h"
#include "util.h"
#include <cassert>
MString CurveMeshInputNode::typeName = "houdiniCurveMeshInput";
MTypeId CurveMeshInputNode::typeId = MayaTypeID_HoudiniCurveMeshInput;
MObject
CurveMeshInputNode::theInputCurves,
CurveMeshInputNode::theOutputObjectMetaData;
void*
CurveMeshInputNode::creator()
{
CurveMeshInputNode* ret = new CurveMeshInputNode();
return ret;
}
MStatus
CurveMeshInputNode::initialize()
{
MFnTypedAttribute tAttr;
theInputCurves = tAttr.create( "inputCurves", "incs",
MFnData::kNurbsCurve );
tAttr.setArray(true);
addAttribute(theInputCurves);
theOutputObjectMetaData = tAttr.create(
"outputObjectMetaData", "oomd",
MFnData::kIntArray
);
tAttr.setStorable(false);
tAttr.setWritable(false);
addAttribute(theOutputObjectMetaData);
attributeAffects(theInputCurves, theOutputObjectMetaData);
return MS::kSuccess;
}
CurveMeshInputNode::CurveMeshInputNode()
: myAssetId(-1)
{
}
CurveMeshInputNode::~CurveMeshInputNode()
{
if ( myAssetId > 0 )
{
CHECK_HAPI(HAPI_DestroyAsset(myAssetId));
}
}
MStatus
CurveMeshInputNode::compute(const MPlug& plug, MDataBlock& data)
{
MStatus status;
if ( plug != theOutputObjectMetaData )
{
return MPxNode::compute(plug, data);
}
if ( myAssetId < 0 )
{
CHECK_HAPI( HAPI_CreateInputAsset(&myAssetId, NULL) );
if ( !Util::statusCheckLoop() )
{
DISPLAY_ERROR(
MString("Unexpected error when creating input asset.")
);
}
MDataHandle metaDataHandle = data.outputValue(theOutputObjectMetaData);
// Meta data
MFnIntArrayData ffIAD;
MIntArray metaDataArray;
metaDataArray.append(myAssetId);
metaDataArray.append(0); // object ID
MObject newMetaData = ffIAD.create(metaDataArray);
metaDataHandle.set(newMetaData);
}
MArrayDataHandle inputCurves(data.inputArrayValue(theInputCurves, &status));
CHECK_MSTATUS_AND_RETURN_IT(status);
const int nInputCurves = inputCurves.elementCount();
if ( nInputCurves <= 0 )
{
data.setClean(plug);
return MStatus::kSuccess;
}
HAPI_CurveInfo curveInfo = HAPI_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;
for ( int iCurve = 0; iCurve < nInputCurves; ++iCurve )
{
MDataHandle curveHandle = inputCurves.inputValue();
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 ) );
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 )
{
const MPoint& cv = cvArray[iCV];
cvP.push_back(cv.x);
cvP.push_back(cv.y);
cvP.push_back(cv.z);
if ( !curveInfo.isRational )
{
if ( cv.w != 1.0 )
{
curveInfo.isRational = true;
cvPw.resize( curveInfo.vertexCount, 1.0f );
cvPw.push_back(cv.w);
}
}
else
{
cvPw.push_back(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]) );
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;
if ( !inputCurves.next() )
{
break;
}
}
curveInfo.hasKnots = curveInfo.knotCount > 0;
HAPI_PartInfo partInfo = HAPI_PartInfo_Create();
partInfo.vertexCount = partInfo.pointCount = curveInfo.vertexCount;
partInfo.faceCount = curveInfo.curveCount;
partInfo.isCurve = true;
CHECK_HAPI( HAPI_SetPartInfo( myAssetId, 0, 0, &partInfo ) );
CHECK_HAPI( HAPI_SetCurveInfo( myAssetId, 0, 0, 0, &curveInfo ) );
CHECK_HAPI( HAPI_SetCurveCounts( myAssetId, 0, 0, 0,
&cvCounts.front(), 0, cvCounts.size() ) );
HAPI_AttributeInfo attrInfo = HAPI_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( HAPI_AddAttribute( myAssetId, 0, 0, "P", &attrInfo ) );
CHECK_HAPI(
HAPI_SetAttributeFloatData(
myAssetId, 0, 0, "P", &attrInfo,
&cvP.front(), 0, static_cast<int>(cvP.size() / 3)
)
);
if ( curveInfo.isRational )
{
attrInfo.tupleSize = 1;
CHECK_HAPI( HAPI_AddAttribute( myAssetId, 0, 0, "Pw", &attrInfo ) );
CHECK_HAPI(
HAPI_SetAttributeFloatData(
myAssetId, 0, 0, "Pw", &attrInfo,
&cvPw.front(), 0, static_cast<int>(cvPw.size())
)
);
}
if ( curveInfo.hasKnots )
{
CHECK_HAPI( HAPI_SetCurveKnots( myAssetId, 0, 0, 0, &knots.front(),
0, static_cast<int>(knots.size()) ) );
}
CHECK_HAPI( HAPI_CommitGeo( myAssetId, 0, 0 ) );
data.setClean(plug);
return MStatus::kSuccess;
}