-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginMain.cpp
More file actions
334 lines (291 loc) · 14.6 KB
/
Copy pathPluginMain.cpp
File metadata and controls
334 lines (291 loc) · 14.6 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include <maya/MPxCommand.h>
#include <maya/MFnPlugin.h>
#include <maya/MIOStream.h>
#include <maya/MString.h>
#include <maya/MArgList.h>
#include <maya/MGlobal.h>
#include <maya/MSimple.h>
#include <maya/MDoubleArray.h>
#include <maya/MPoint.h>
#include <maya/MPointArray.h>
#include <maya/MFnNurbsCurve.h>
#include <maya/MDGModifier.h>
#include <maya/MPlugArray.h>
#include <maya/MVector.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MStringArray.h>
#include <list>
#include "FractalCmd.h"
void createSelectionUI(MStatus* status) {
const char* melCreateSelectionUI = R"(
////////////////////////////////////////////////////////////////////////
// Global declarations and initialization
////////////////////////////////////////////////////////////////////////
global int $nodeCounter;
global string $nodesContainer;
$nodeCounter = 1;
////////////////////////////////////////////////////////////////////////
// Procedure to create a single fractal node UI group.
// The unique nodeID is appended to each control's name.
////////////////////////////////////////////////////////////////////////
global proc createFractalNodeFields(string $nodeID) {
// Create a frameLayout for this fractal node.
frameLayout -label ("Fractal Node " + $nodeID) -collapsable true -marginWidth 10 -marginHeight 10 -height 480 ("nodeFrame_" + $nodeID);
columnLayout -adjustableColumn true -columnAlign "left";
// Selected Object Field for this node.
textFieldButtonGrp -columnAlign3 "left" "left" "left"
-label "Selected Object: "
-buttonLabel "Select"
-bc "updateSelectionField"
("mySelectionField_" + $nodeID);
string $btnName = "generateButton_" + $nodeID;
button
-label "Generate Portal"
-command ("createPortalCube \"" + $nodeID + "\"")
$btnName;
// Generated portal mesh name
textFieldGrp
-label "Portal Mesh:"
-columnAlign2 "left" "left"
-enable false
-text ""
("generatedMeshField_" + $nodeID);
// Noise Scale Slider
floatSliderGrp -label "Noise Scale" -field true -minValue 0 -maxValue 0.5 -value 0.05 -step 0.0001 -precision 4 -columnAlign3 "left" "left" "left" ("myAlphaSlider_" + $nodeID);
text
-align "left"
-label " Multiplicative factor influencing magnitude of Perlin noise."
-enable true
-width 200
-wordWrap true;
// Noise Offset Slider
floatSliderGrp -label "Noise Offset" -field true -minValue 0 -maxValue 10 -value 0.0 -step 0.01 -precision 2 -columnAlign3 "left" "left" "left" ("myBetaSlider_" + $nodeID);
text
-align "left"
-label " Constant vector added to the noise input coordinates to translate the Perlin noise pattern across the mesh surface."
-enable true
-width 200
-wordWrap true;
// Versor Scale Slider
floatSliderGrp -label "Versor Scale" -field true -minValue 0 -maxValue 10 -value 9.0 -step 0.01 -precision 2 -columnAlign3 "left" "left" "left" ("myVersorScaleSlider_" + $nodeID);
text
-align "left"
-label " Multiplicative factor controlling the overall strength of the quaternion-based noise warp applied to the mesh."
-enable true
-width 200
-wordWrap true;
// Versor Octave Slider
intSliderGrp -label "Versor Octave" -field true -minValue 0 -maxValue 8 -value 1 -columnAlign3 "left" "left" "left" ("myVersorOctaveSlider_" + $nodeID);
text
-align "left"
-label " Number of sequential noise layers summed to introduce progressively finer fractal detail into the warp."
-enable true
-width 200
-wordWrap true;
// Iterations Slider
intSliderGrp -label "Iterations" -field true -minValue 1 -maxValue 8 -value 2 -columnAlign3 "left" "left" "left" ("myNumIterationSlider_" + $nodeID);
text
-align "left"
-label " Number of fractalization iterations to perform."
-enable true
-width 200
-wordWrap true;
// RowLayout for Delete Button (centered)
rowLayout -numberOfColumns 1 -columnAlign1 "center";
button -label "Delete Fractal Node" -command ("deleteFractalNode \"" + $nodeID + "\"");
setParent ..;
setParent ..; // End columnLayout
setParent ..; // End frameLayout
}
////////////////////////////////////////////////////////////////
// Procedure to create a cube named "portalCube_<nodeID>"
////////////////////////////////////////////////////////////////
global proc createPortalCube(string $nodeID) {
string $node[] = `polyCube -w 1 -h 1 -d 1 -name ("portalCube_" + $nodeID)`;
string $name = $node[0];
// turn on shape-override template (wireframe-only) display
setAttr ($name + ".overrideEnabled") 1;
setAttr ($name + ".overrideDisplayType") 1;
string $fieldName = ("generatedMeshField_" + $nodeID);
if (`control -exists $fieldName`) {
textFieldGrp -e -text $name $fieldName;
}
string $btn = "generateButton_" + $nodeID;
if (`control -exists $btn`) {
button -e -enable false $btn;
} else {
warning("Couldn��t find button: " + $btn);
}
print("Created portal: " + $name + "\n");
}
////////////////////////////////////////////////////////////////////////
// Procedure to add a new fractal node group.
////////////////////////////////////////////////////////////////////////
global proc addFractalNode() {
global int $nodeCounter;
global string $nodesContainer;
string $nodeID = $nodeCounter;
// Set parent to the nodes container so the new node's controls are added there.
setParent $nodesContainer;
createFractalNodeFields($nodeID);
$nodeCounter++;
setParent ..; // Return to the previous layout.
}
////////////////////////////////////////////////////////////////////////
// Procedure to update the selection field.
// This updates the selection field for the most recently added node.
////////////////////////////////////////////////////////////////////////
global proc updateSelectionField() {
global int $nodeCounter;
string $selection[] = `ls -sl`;
if (size($selection) > 0) {
string $currentField = ("mySelectionField_" + ($nodeCounter - 1));
textFieldButtonGrp -e -text $selection[0] $currentField;
} else {
warning "No object selected. Please select an object.";
}
}
////////////////////////////////////////////////////////////////////////
// New procedure to delete a fractal node UI group.
////////////////////////////////////////////////////////////////////////
global proc deleteFractalNode(string $nodeID) {
// Delete the frameLayout for the specified fractal node.
string $frameName = ("nodeFrame_" + $nodeID);
if (`control -exists $frameName`) {
deleteUI $frameName;
}
}
////////////////////////////////////////////////////////////////////////
// Generate callback that processes each fractal node group.
////////////////////////////////////////////////////////////////////////
global proc onGeneratePressed() {
int $lowResMode = `checkBox -q -value "myToggleCheckbox"`;
global int $nodeCounter;
int $numNodes = $nodeCounter - 1;
for ($i = 1; $i <= $numNodes; $i++) {
string $selField = "mySelectionField_" + $i;
// If the control does not exist (node deleted), skip it.
if (!`control -exists $selField`) {
continue;
}
string $selObj = `textFieldButtonGrp -q -text ("mySelectionField_" + $i)`;
string $alphaField = "myAlphaSlider_" + $i;
string $betaField = "myBetaSlider_" + $i;
string $versorScaleField = "myVersorScaleSlider_" + $i;
string $versorOctaveField = "myVersorOctaveSlider_" + $i;
string $numIterationField = "myNumIterationSlider_" + $i;
string $portalName = ("portalCube_" + $i);
string $selectedObject = `textFieldButtonGrp -q -text $selField`;
float $t[] = `xform -q -ws -translation $portalName`;
float $r[] = `xform -q -ws -rotation $portalName`;
float $s[] = `xform -q -os -scale $portalName`;
float $posX = $t[0];
float $posY = $t[1];
float $posZ = $t[2];
float $rotX = $r[0];
float $rotY = $r[1];
float $rotZ = $r[2];
float $scaleX = $s[0] / 2.0;
float $scaleY = $s[1] / 2.0;
float $scaleZ = $s[2] / 2.0;
float $alpha = `floatSliderGrp -q -value $alphaField`;
float $beta = `floatSliderGrp -q -value $betaField`;
float $versorScale = `floatSliderGrp -q -value $versorScaleField`;
int $versorOctave = `intSliderGrp -q -value $versorOctaveField`;
int $numIterations = `intSliderGrp -q -value $numIterationField`;
string $cmd = ("FractalCmd \"" + $selectedObject + "\" "
+ $posX + " " + $posY + " " + $posZ + " "
+ $rotX + " " + $rotY + " " + $rotZ + " "
+ $scaleX + " " + $scaleY + " " + $scaleZ + " "
+ $alpha + " " + $beta + " " + $versorScale + " "
+ $versorOctave + " " + $numIterations + " "
+ $lowResMode);
print ("Executing for node " + $i + ": " + $cmd + "\n");
eval($cmd);
}
}
////////////////////////////////////////////////////////////////////////
// Main UI window procedure.
////////////////////////////////////////////////////////////////////////
global proc openSelectionWindow() {
// 1) Fixed window size, auto-resize off:
if (`window -exists mySelectionWindow`) deleteUI mySelectionWindow;
window
-title "Fractal Generator"
-resizeToFitChildren true // prevent Maya from growing to fit all children
-width 320
-height 500
mySelectionWindow;
columnLayout -adjustableColumn true;
button -label "Add fractal node" -command "addFractalNode";
// 2) ScrollLayout around *only* the nodes container:
scrollLayout
-width 300
-height 500
-horizontalScrollBarThickness 16
-verticalScrollBarThickness 16
-verticalScrollBarAlwaysVisible true
-childResizable true // allow width to shrink, only scroll vertically
"fractalNodesScroll";
// This columnLayout is the one that will scroll
global string $nodesContainer;
$nodesContainer = `columnLayout -adjustableColumn true`;
setParent ..; // closes the nodesContainer columnLayout
setParent ..; // closes the scrollLayout itself
// Now these live *below* the scroll region, always visible:
checkBox
-label "Enable Low Res"
-value false
-annotation "When on, passes a 1 to FractalCmd; otherwise 0"
"myToggleCheckbox";
button -label "Generate" -command "onGeneratePressed";
showWindow mySelectionWindow;
}
)";
*status = MGlobal::executeCommand(melCreateSelectionUI);
if (*status != MS::kSuccess) {
cerr << "Error executing melCreateSelectionUI!" << endl;
}
// Create a menu item in Maya��s main window to open the UI.
MGlobal::executeCommand(R"(
global string $gMainWindow;
if (`menu -exists fractalPluginMenu`) {
deleteUI -menu fractalPluginMenu;
}
setParent $gMainWindow;
menu -label "Fractal Plugin" -tearOff true -parent $gMainWindow fractalPluginMenu;
menuItem -label "Open Selection UI" -command "openSelectionWindow" -parent fractalPluginMenu;
)");
}
MStatus initializePlugin(MObject obj)
{
MStatus status = MStatus::kSuccess;
MFnPlugin plugin(obj, "MyPlugin", "1.0", "Any");
// Register your command. Ensure FractalCmd::creator is correctly implemented.
status = plugin.registerCommand("FractalCmd", FractalCmd::creator);
if (!status) {
status.perror("registerCommand");
return status;
}
// Create the UI.
createSelectionUI(&status);
if (!status) {
status.perror("createSelectionUI");
return status;
}
return status;
}
MStatus uninitializePlugin(MObject obj)
{
MStatus status = MStatus::kSuccess;
MFnPlugin plugin(obj);
status = plugin.deregisterCommand("FractalCmd");
if (!status) {
status.perror("deregisterCommand");
return status;
}
MGlobal::executeCommand(R"(
deleteUI fractalPluginMenu;
)");
return status;
}