-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathFabricSpliceMayaDeformer.cpp
237 lines (188 loc) · 6.99 KB
/
FabricSpliceMayaDeformer.cpp
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
#include "FabricSpliceEditorWidget.h"
#include "FabricSpliceMayaDeformer.h"
#include "plugin.h"
#include <maya/MGlobal.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MFnTypedAttribute.h>
#include <maya/MPointArray.h>
#include <maya/MItMeshVertex.h>
#include <maya/MFnMesh.h>
#include <maya/MItMeshEdge.h>
#include <maya/MItMeshPolygon.h>
MTypeId FabricSpliceMayaDeformer::id(0x0011AE42);
MObject FabricSpliceMayaDeformer::saveData;
MObject FabricSpliceMayaDeformer::evalID;
FabricSpliceMayaDeformer::FabricSpliceMayaDeformer()
: FabricSpliceBaseInterface()
{
mGeometryInitialized = 0;
}
FabricSpliceMayaDeformer::~FabricSpliceMayaDeformer()
{
}
void FabricSpliceMayaDeformer::postConstructor(){
FabricSpliceBaseInterface::constructBaseInterface();
FabricSpliceEditorWidget::postUpdateAll();
}
void* FabricSpliceMayaDeformer::creator(){
return new FabricSpliceMayaDeformer();
}
MStatus FabricSpliceMayaDeformer::initialize(){
MFnTypedAttribute typedAttr;
MFnNumericAttribute numericAttr;
saveData = typedAttr.create("saveData", "svd", MFnData::kString);
typedAttr.setHidden(true);
addAttribute(saveData);
evalID = numericAttr.create("evalID", "evalID", MFnNumericData::kInt, 0);
numericAttr.setKeyable(true);
numericAttr.setHidden(true);
numericAttr.setReadable(true);
numericAttr.setWritable(true);
numericAttr.setStorable(false);
numericAttr.setCached(false);
addAttribute(evalID);
return MS::kSuccess;
}
MStatus FabricSpliceMayaDeformer::deform(MDataBlock& block, MItGeometry& iter, const MMatrix&, unsigned int multiIndex){
MStatus stat;
MAYASPLICE_CATCH_BEGIN(&stat);
FabricSplice::Logging::AutoTimer timer("Maya::deform()");
if(!_spliceGraph.checkErrors()){
return MStatus::kFailure; // avoid evaluating on errors
}
MString meshIdStr;
meshIdStr.set(multiIndex);
if(mGeometryInitialized < 1){
MArrayDataHandle inputArray = block.inputArrayValue(input);
for(int i = 0; i < inputArray.elementCount(); ++i){
MPlug inputPlug(thisMObject(), input);
MPlug inputElementPlug = inputPlug.elementByPhysicalIndex(multiIndex);
MPlug meshPlug = inputElementPlug.child(inputGeom);
mGeometryInitialized = initializePolygonMeshPorts(meshIdStr, meshPlug, block);
if(mGeometryInitialized < 0){
return MStatus::kFailure;
}
}
}
transferInputValuesToSplice(block);
FabricSplice::DGPort port = _spliceGraph.getDGPort(("mesh" + meshIdStr).asChar());
if(port.getMode() != FabricSplice::Port_Mode_IO)
return MStatus::kSuccess;
FabricCore::RTVal rtMesh = port.getRTVal();
if(!rtMesh.isValid() || rtMesh.isNullObject())
return MStatus::kSuccess;
MPointArray mayaPoints;
iter.allPositions(mayaPoints);
// MPlug inputPlug(thisMObject(), input);
// MPlug inputElementPlug = inputPlug.elementByPhysicalIndex(multiIndex);
// MPlug meshPlug = inputElementPlug.child(inputGeom);
// MDataHandle handle = block.outputValue(meshPlug);
// MObject meshObj = handle.asMesh();
// MFnMesh mesh(meshObj);
// MFloatVectorArray mayaFnormals;
// mesh.getNormals(mayaFnormals);
// MIntArray mayaCounts, mayaIndices;
// mesh.getNormalIds(mayaCounts, mayaIndices);
// MVectorArray mayaNormals;
// mayaNormals.setLength(mayaIndices.length());
// for(unsigned int i=0;i<mayaIndices.length();i++)
// {
// mayaNormals[i].x = mayaFnormals[mayaIndices[i]].x;
// mayaNormals[i].y = mayaFnormals[mayaIndices[i]].y;
// mayaNormals[i].z = mayaFnormals[mayaIndices[i]].z;
// }
try
{
std::vector<FabricCore::RTVal> args(2);
args[0] = FabricSplice::constructExternalArrayRTVal("Float64", mayaPoints.length() * 4, &mayaPoints[0]);
args[1] = FabricSplice::constructUInt32RTVal(4); // components
rtMesh.callMethod("", "setPointsFromExternalArray_d", 2, &args[0]);
// FabricCore::RTVal normalsVar =
// FabricSplice::constructExternalArrayRTVal("Float64", mayaNormals.length() * 3, &mayaNormals[0]);
// rtMesh.callMethod("", "setNormalsFromFloat64Array", 1, &normalsVar);
}
catch(FabricCore::Exception e)
{
mayaLogErrorFunc(e.getDesc_cstr());
return MStatus::kSuccess;
}
port.setRTVal(rtMesh);
evaluate();
try
{
std::vector<FabricCore::RTVal> args(2);
args[0] = FabricSplice::constructExternalArrayRTVal("Float64", mayaPoints.length() * 4, &mayaPoints[0]);
args[1] = FabricSplice::constructUInt32RTVal(4); // components
rtMesh.callMethod("", "getPointsAsExternalArray_d", 2, &args[0]);
// FabricCore::RTVal normalsVar =
// FabricSplice::constructExternalArrayRTVal("Float64", mayaNormals.length() * 3, &mayaNormals[0]);
// rtMesh.callMethod("", "getNormalsAsFloat64Array", 1, &normalsVar);
// for(unsigned int i=0;i<mayaIndices.length();i++)
// {
// mayaFnormals[mayaIndices[i]].x = mayaNormals[i].x;
// mayaFnormals[mayaIndices[i]].y = mayaNormals[i].y;
// mayaFnormals[mayaIndices[i]].z = mayaNormals[i].z;
// }
}
catch(FabricCore::Exception e)
{
mayaLogErrorFunc(e.getDesc_cstr());
return MStatus::kSuccess;
}
iter.setAllPositions(mayaPoints);
// mesh.setNormals(mayaFnormals);
transferOutputValuesToMaya(block, true);
MAYASPLICE_CATCH_END(&stat);
return stat;
}
MStatus FabricSpliceMayaDeformer::setDependentsDirty(MPlug const &inPlug, MPlugArray &affectedPlugs){
MStatus stat;
MAYASPLICE_CATCH_BEGIN(&stat);
collectDirtyPlug(inPlug);
FabricSpliceBaseInterface::setDependentsDirty(thisMObject(), inPlug, affectedPlugs);
MFnDependencyNode thisNode(thisMObject());
MPlug output = thisNode.findPlug("outputGeometry");
affectedPlugs.append(output);
for(int i = 0; i < output.numElements(); ++i){
affectedPlugs.append(output.elementByPhysicalIndex(i));
}
MAYASPLICE_CATCH_END(&stat);
return stat;
}
void FabricSpliceMayaDeformer::invalidateNode()
{
FabricSpliceBaseInterface::invalidateNode();
MFnDependencyNode thisNode(thisMObject());
MPlug output = thisNode.findPlug("outputGeometry");
invalidatePlug(output);
for(int i = 0; i < output.numElements(); ++i){
MPlug plug = output.elementByPhysicalIndex(i);
invalidatePlug(plug);
}
mGeometryInitialized = false;
}
MStatus FabricSpliceMayaDeformer::shouldSave(const MPlug &plug, bool &isSaving){
// guarantee dynamically added attributes are saved
isSaving = true;
return MS::kSuccess;
}
void FabricSpliceMayaDeformer::copyInternalData(MPxNode *node){
FabricSpliceBaseInterface::copyInternalData(node);
}
int FabricSpliceMayaDeformer::initializePolygonMeshPorts(MString &meshId, MPlug &meshPlug, MDataBlock &data){
MFnDependencyNode thisNode(getThisMObject());
MString portName = "mesh" + meshId;
FabricSplice::DGPort port = _spliceGraph.getDGPort(portName.asChar());
if(!port.isValid()){
return 0;
}
std::string dataType = port.getDataType();
if(dataType != "PolygonMesh"){
MGlobal::displayWarning("FabricSpliceMayaDeformer: Wrong port type");
return -1;
}
getSplicePlugToPortFunc("PolygonMesh")(meshPlug, data, port);
invalidatePlug(meshPlug);
_spliceGraph.requireEvaluate();
return 1;
}