-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCenterPointNode.cpp
141 lines (109 loc) · 3.01 KB
/
CenterPointNode.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
#include "CenterPointNode.h"
static const MTypeId TYPE_ID = MTypeId(0x0007F7F3);
static const MString TYPE_NAME = "centerPoint";
MObject CenterPointNode::inputObjectsAttr;
MObject CenterPointNode::outputPosAttr;
CenterPointNode::CenterPointNode()
{
}
MStatus CenterPointNode::compute(const MPlug& plug, MDataBlock& data)
{
if (!isDirty(plug))
{
return MS::kSuccess;
}
std::vector<MVector> positions;
MStatus result;
MArrayDataHandle arrayDataHandle = data.inputArrayValue(inputObjectsAttr, &result);
if (!result)
{
MGlobal::displayError("Error getting the input value!");
return MS::kFailure;
}
result = arrayDataHandle.jumpToArrayElement(0);
if (!result)
{
MGlobal::displayError("Error getting the first element of the inputs!");
return MS::kFailure;
}
do
{
MDataHandle inputDataHandle = arrayDataHandle.inputValue(&result);
if (!result)
{
MGlobal::displayError("Error getting value of the input!");
return MS::kFailure;
}
MMatrix worldMatrix = inputDataHandle.asMatrix();
MTransformationMatrix trasnformationMatrix(worldMatrix);
MVector translation = trasnformationMatrix.getTranslation(MSpace::kWorld, &result);
if (!result)
{
MGlobal::displayError("Error getting the translation value of the input!");
return MS::kFailure;
}
positions.push_back(translation);
} while (arrayDataHandle.next() == MS::kSuccess);
MVector centerPoint = findCenterPoint(positions);
MDataHandle outputDataHandle = data.outputValue(outputPosAttr, &result);
if (!result)
{
MGlobal::displayError("Error getting the output data handle!");
return MS::kFailure;
}
outputDataHandle.set3Double(centerPoint.x, centerPoint.y, centerPoint.z);
data.setClean(plug);
return MS::kSuccess;
}
void* CenterPointNode::Creator()
{
return new CenterPointNode();
}
MStatus CenterPointNode::Initialize()
{
defineAttributes();
return MS::kSuccess;
}
MTypeId CenterPointNode::GetTypeId()
{
return TYPE_ID;
}
MString CenterPointNode::GetTypeName()
{
return TYPE_NAME;
}
bool CenterPointNode::isDirty(const MPlug& plug)
{
return plug == outputPosAttr;
}
MVector CenterPointNode::findCenterPoint(std::vector<MVector> positions)
{
MVector sum_vector(0.0f, 0.0f, 0.0f);
for (MVector pos : positions)
{
sum_vector += pos;
}
MVector center_point = sum_vector / positions.size();
return center_point;
}
void CenterPointNode::defineAttributes()
{
MFnMatrixAttribute matrixAttrFn;
inputObjectsAttr = matrixAttrFn.create("input", "in", MFnMatrixAttribute::kDouble);
matrixAttrFn.setArray(true);
matrixAttrFn.setStorable(true);
matrixAttrFn.setKeyable(true);
matrixAttrFn.setReadable(false);
matrixAttrFn.setWritable(true);
addAttribute(inputObjectsAttr);
MFnNumericAttribute numericAttrFn;
outputPosAttr = numericAttrFn.create("outputPosition", "op", MFnNumericData::k3Double);
numericAttrFn.setStorable(false);
numericAttrFn.setReadable(true);
numericAttrFn.setWritable(false);
addAttribute(outputPosAttr);
attributeAffects(inputObjectsAttr, outputPosAttr);
}
CenterPointNode::~CenterPointNode()
{
}