-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLSystemNode.cpp
196 lines (150 loc) · 6.09 KB
/
LSystemNode.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
#include "LSystemNode.h"
#include "LSystem.h"
#include "LSystemCmd.h"
#include <maya\MPoint.h>
#include <maya\MPointArray.h>
#include <maya\MFnMesh.h>
#include <maya\MTypeId.h>
#include <maya\MFnStringData.h>
typedef std::pair<vec3, vec3> Branch;
//Static variables
MTypeId LSystemNode::nodeID (0x80000);
MObject LSystemNode::time;
MObject LSystemNode::outputMesh;
MObject LSystemNode::angle;
MObject LSystemNode::stepSize;
MObject LSystemNode::iterations;
MObject LSystemNode::grammar;
LSystemNode::LSystemNode()
{}
LSystemNode::~LSystemNode()
{}
MStatus LSystemNode::doIt(const MArgList& args)
{
MGlobal::displayInfo("NODEEEEE!");
return MStatus::kSuccess;
}
MStatus LSystemNode::initialize()
{
MStatus returnStatus;
MFnUnitAttribute UAtime;
MFnTypedAttribute mesh;
MFnNumericAttribute m_angle;
MFnNumericAttribute m_stepSize;
MFnNumericAttribute m_iterations;
MFnTypedAttribute m_grammar;
// Create and add an input MFnTypedAttribute for the grammar file. You can specify files relative to the plug-in .mll location using MFnPlugin::loadPath()
LSystemNode::time = UAtime.create("time", "tm", MFnUnitAttribute::kTime, 0.0, &returnStatus);
McheckErr(returnStatus, "ERROR creating LSystemNode time attribute\n");
LSystemNode::outputMesh = mesh.create("outputgeometry", "out", MFnData::kMesh, &returnStatus);
McheckErr(returnStatus, "ERROR creating LSystemNode outGeom attribute\n");
//stepsize
LSystemNode::stepSize = m_stepSize.create("stepSize", "ss", MFnNumericData::kFloat , 1.0, &returnStatus);
McheckErr(returnStatus, "ERROR creating LSystemNode stepsize attribute\n");
//angle
LSystemNode::angle = m_angle.create("angle", "a", MFnNumericData::kFloat , 45.0, &returnStatus);
McheckErr(returnStatus, "ERROR creating LSystemNode angle attribute\n");
//iterations
iterations = m_iterations.create("iterations", "i", MFnNumericData::kInt, 1.0, &returnStatus);
McheckErr(returnStatus, "ERROR creating LSystemNode iterations attribute\n");
//grammar
MString defaultGrammar = "F \n" "F->F[+F]F[-F]F";
LSystemNode::grammar = m_grammar.create("grammar", "g", MFnData::kString, MFnStringData().create(defaultGrammar) ,&returnStatus);
McheckErr(returnStatus, "ERROR creating LSystemNode grammar attribute\n");
mesh.setStorable(false);
//Attribute AfterAffects() functions assigns dependencies between attributes - establishes which are input and which are output
returnStatus = addAttribute(LSystemNode::time);
McheckErr(returnStatus, "ERROR adding time attribute\n");
returnStatus = addAttribute(LSystemNode::outputMesh);
McheckErr(returnStatus, "ERROR adding outputMesh attribute\n");
returnStatus = addAttribute(LSystemNode::grammar);
McheckErr(returnStatus, "ERROR adding grammar attribute\n");
returnStatus = addAttribute(LSystemNode::stepSize);
McheckErr(returnStatus, "ERROR adding stepSize attribute\n");
returnStatus = addAttribute(LSystemNode::angle);
McheckErr(returnStatus, "ERROR adding angle attribute\n");
returnStatus = attributeAffects(LSystemNode::time, LSystemNode::outputMesh);
McheckErr(returnStatus, "ERROR in attributeAffects\n");
returnStatus = attributeAffects(LSystemNode::grammar, LSystemNode::outputMesh);
McheckErr(returnStatus, "ERROR in attributeAffects\n");
returnStatus = attributeAffects(LSystemNode::stepSize, LSystemNode::outputMesh);
McheckErr(returnStatus, "ERROR in attributeAffects\n");
returnStatus = attributeAffects(LSystemNode::angle, LSystemNode::outputMesh);
McheckErr(returnStatus, "ERROR in attributeAffects\n");
return MS::kSuccess;
}
MStatus LSystemNode::compute(const MPlug& plug, MDataBlock& data)
{
MStatus returnStatus;
MFnMesh meshFS;
if (plug == outputMesh) {
/* Get time */
MDataHandle timeData = data.inputValue(time, &returnStatus);
McheckErr(returnStatus, "Error getting time data handle\n");
MTime ip_time = timeData.asTime();
MDataHandle outputHandle = data.outputValue(outputMesh, &returnStatus);
McheckErr(returnStatus, "ERROR getting Geom data handle\n");
MDataHandle grammarData = data.inputValue(grammar, &returnStatus);
McheckErr(returnStatus, "ERROR getting grammar data handle\n");
MString ip_grammar = grammarData.asString();
MDataHandle iterData = data.inputValue(iterations, &returnStatus);
McheckErr(returnStatus, "ERROR getting iterations data handle\n");
int ip_iter = iterData.asInt();
MDataHandle angleData = data.inputValue(angle, &returnStatus);
McheckErr(returnStatus, "ERROR getting angle data handle\n");
double ip_angle = iterData.asDouble();
MDataHandle stepData = data.inputValue(angle, &returnStatus);
McheckErr(returnStatus, "ERROR getting stepsize data handle\n");
double ip_stepSize = stepData.asDouble();
MFnMeshData dataCreator;
MObject newOutputData = dataCreator.create(&returnStatus);
McheckErr(returnStatus, "ERROR creating outputData");
createMesh(ip_time, ip_stepSize, ip_angle, ip_iter, ip_grammar.asChar(), newOutputData, returnStatus);
McheckErr(returnStatus, "ERROR");
outputHandle.set(newOutputData);
data.setClean(plug);
}
else
{
return MS::kUnknownParameter;
}
return MS::kSuccess;
}
MObject LSystemNode::createMesh(const MTime& timeInput,
const double& stepInput,
const double& angleInput,
const int& iterInput,
const std::string& grammarInput,
MObject& outData,
MStatus& stat)
{
MPointArray points;
MIntArray faceCounts;
MIntArray faceConnects;
MPoint p1;
MPoint p2;
MFnMesh meshFS;
LSystem l;
int frame = (int)timeInput.as(MTime::kFilm);
if (frame == 0)
{
frame = 1;
}
l.loadProgramFromString(grammarInput);
l.setDefaultAngle(angleInput);
l.setDefaultStep(stepInput);
std::vector<Branch> b = std::vector<Branch>();
l.process(frame, b);
for (int i = 0; i < b.size(); i++)
{
p1 = MPoint(b[i].first[0], b[i].first[1], b[i].first[2], 1.0);
p2 = MPoint(b[i].second[0], b[i].second[1], b[i].second[2], 1.0);
CylinderMesh c(p1, p2, 0.1);
c.appendToMesh(points, faceCounts, faceConnects);
}
int numFaces = faceCounts.length();
int numVertices = points.length();
int numFaceConnects = faceConnects.length();
MObject newMesh = meshFS.create(points.length(), faceCounts.length(), points, faceCounts, faceConnects, outData);
return newMesh;
}