-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAEMpmSimulatorTemplate.mel
340 lines (303 loc) · 10.1 KB
/
AEMpmSimulatorTemplate.mel
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
335
336
337
338
339
340
global proc string getNodeShape(string $attrName)
{
string $nodeName = `plugNode $attrName`;
$nodeNameType = `ls -showType $nodeName`;
if ($nodeNameType == "transform")
{
$nodeName = `listRelatives -shapes $nodeName`;
}
return $nodeName;
}
global proc initParticle(string $nodeName)
{
string $ptcl = `getAttr ($nodeName + ".particleName")`;
string $ptclShape = "";
if(`size($ptcl)` != 0 && `objExists $ptcl` != 0)
{
$ptclShape = getFirstShape($ptcl);
}
if(`size($ptclShape)` != 0 && `objExists $ptclShape` != 0)
{
int $frame = `currentTime -q`;
int $numPtcl = `mpmSimulate -numParticle -name $nodeName`;// `getAttr ($nodeName + ".numSampledParticle")`;
if($frame == 1)
{
currentTime 2;
}
clearParticleStartState $ptclShape;
currentTime 1;
print("num ptcl:" + $numPtcl);
int $i;
string $emitCmd = ("emit -object " + $ptclShape + "\n");
for($i = 0; $i < $numPtcl; ++$i)
{
//emit -object $ptclShape -pos 0 0 0;
//vector $pos = sphrand(10);
//$emitCmd += " -pos "+$pos+"\n";
$emitCmd += " -pos 0 0 0\n";
}
if($numPtcl > 0)
{
eval( $emitCmd );
}
// go back to current frame and set it as initial state
currentTime $frame;
saveInitialState $ptclShape;
}
}
global proc initSimulator(string $nodeName)
{
// execute
//print("nodeName:" + $nodeName)
mpmSimulate -init -name $nodeName;
initParticle($nodeName);
refresh -f;
}
// type: simulation type
// 0 dynamic simulation
// 1 static simulation
// 2 static simulation with general force
global proc execSimulator(string $nodeName, int $type)
{
int $length = `intSliderGrp -query -value simLengthSlider`;
// init progress bar
global string $gMainProgressBar;
progressBar -edit
-beginProgress
-isInterruptable true
-status "Simulating... "
-maxValue $length
$gMainProgressBar;
int $saveEveryFrame = `getAttr ($nodeName+".saveEveryFrame")`;
int $begTime = `currentTime -q`;
int $t;
$startSecond = `timerX`;
setAttr ($nodeName+".displayType") 1;
for ($t = $begTime; $t < $begTime + $length; $t++)
{
// step progress bar
if(`progressBar -query -isCancelled $gMainProgressBar`)
break;
print("\n\ncurrent Frame:"+ $t + "\n");
progressBar -edit -step 1 $gMainProgressBar;
int $res;
$frameStartSecond = `timerX`;
switch($type)
{
case 0:
$res = `mpmSimulate -step -name $nodeName`;
break;
}
$frameTotalSecond = `timerX -startTime $frameStartSecond`;
print ("Computation Time: "+$frameTotalSecond+"s\n");
if($res == 0)
break;
if($saveEveryFrame != 0)
{
$res = `mpmSimulate -save "" -name $nodeName`;
}
currentTime ($t+1);
}
setAttr ($nodeName+".displayType") 0;
// code that is being timed
$totalSecond = `timerX -startTime $startSecond`;
$totalFrame= $t - $begTime;
if ($totalFrame != 0)
{
float $timeRate = $totalSecond / float($totalFrame);
print ("Total Time: "+$totalSecond+"s\n");
print ("Average Time: "+$timeRate+"s\n");
}
// delete progress bar
progressBar -edit -endProgress $gMainProgressBar;
}
global proc saveSimulationData(string $nodeName)
{
string $filePath = `getAttr ($nodeName+".resultPath")`;
//`fileDialog -m 1`;
// int $res = `rigSimulate -name $nodeName -save $filePath`;
}
// simulation buttons
global proc initSimulatorControl(string $attrName)
{
string $nodeName = `plugNode $attrName`;
setUITemplate -pst "attributeEditorTemplate";
button -label "Initialize" -c ("initSimulator " + $nodeName) initButton;
intSliderGrp -label "Simulation Length" -field true
-minValue 1 -maxValue 500
-fieldMinValue 1 -fieldMaxValue 10000
-value 100
simLengthSlider;
button -label "Simulate" -c ("execSimulator " + $nodeName + " 0") simButton;
//button -label "Save Simulation Result" -c ("saveSimulationData " + $nodeName) saveButton;
setUITemplate -ppt;
}
global proc modifySimulatorControl(string $attrName)
{
string $nodeName = `plugNode $attrName`;
button -e -c ("initSimulator " + $nodeName) initButton;
button -e -c ("execSimulator " + $nodeName + " 0") simButton;
//button -e -c ("saveSimulationData " + $nodeName) saveButton;
}
// particle buttons
global proc string getFirstShape(string $transformName)
{
string $shapes[] = `listRelatives -shapes $transformName`;
return $shapes[0];
}
global proc buildOrUpdateParticleData(string $nodeName)
{
string $ptcl = `getAttr ($nodeName + ".particleName")`;
if(`size $ptcl` == 0)
{
confirmDialog -title "Error" -message "Please enter particle name." -button "OK";
return;
}
// if object not exist, create one
if(`objExists $ptcl` == 0)
{
nParticle -name $ptcl;
}
// add attribute
string $ptclShape = getFirstShape($ptcl);
$nextPlug = `listConnections -type nucleus -plugs true ($ptclShape+".nextState")`;
if (`size $nextPlug` != 0)
{
connectAttr -f $nextPlug ($nodeName + ".inputParticle");
connectAttr -f ($nodeName + ".outputParticle") ($ptclShape+".nextState");
}
initParticle($nodeName);
}
global proc initParticleControl(string $attrName)
{
string $nodeName = `plugNode $attrName`;
setUITemplate -pst "attributeEditorTemplate";
button -label "Build or Update Particle Data" -c ("buildOrUpdateParticleData " + $nodeName) particleButton;
setUITemplate -ppt;
}
global proc modifyParticleControl(string $attrName)
{
string $nodeName = `plugNode $attrName`;
button -e -c ("buildOrUpdateParticleData " + $nodeName) particleButton;
}
global proc loadCurFrame(string $nodeName)
{
int $append = `checkBox -query -value appendLoadCheckBox`;
string $filter = "*.dat";
string $result[] = `fileDialog2 -fileFilter $filter -fileMode 4`;
int $i;
int $frame = `currentTime -q`;
print($result);
for($i = 0; $i < size($result); ++$i, ++$frame)
{
if ($append)
{
$res = `mpmSimulate -load $result[$i] -append -name $nodeName`;
}
else
{
$res = `mpmSimulate -load $result[$i] -name $nodeName`;
}
currentTime ($frame+1);
}
}
global proc saveCurFrame(string $nodeName)
{
string $filter = "*.dat";
string $result[] = `fileDialog2 -fileFilter $filter -fileMode 0`;
if(size($result) > 0)
{
$res = `mpmSimulate -save $result[0] -name $nodeName`;
}
}
global proc initSaveControl(string $attrName)
{
string $nodeName = `plugNode $attrName`;
setUITemplate -pst "attributeEditorTemplate";
checkBox -label "Append To Current Status" appendLoadCheckBox;
button -label "Load Frames" -c ("loadCurFrame " + $nodeName) loadButton;
button -label "Save Current Frame" -c ("saveCurFrame " + $nodeName) saveButton;
setUITemplate -ppt;
}
global proc modifySaveControl(string $attrName)
{
string $nodeName = `plugNode $attrName`;
button -e -c ("loadCurFrame " + $nodeName) loadButton;
button -e -c ("saveCurFrame " + $nodeName) saveButton;
}
// main
global proc AEMpmSimulatorTemplate(string $nodeName)
{
editorTemplate -beginScrollLayout;
editorTemplate -addControl "time";
editorTemplate -beginLayout "Physical Parameters" -collapse true;
editorTemplate -addControl "youngsModulus";
editorTemplate -addControl "possionRatio";
editorTemplate -addControl "hardeningFactor";
editorTemplate -addControl "criticalCompression";
editorTemplate -addControl "criticalStretch";
editorTemplate -addControl "friction";
editorTemplate -addControl "flipPercent";
editorTemplate -addControl "particleDensity";
editorTemplate -addControl "gravity";
editorTemplate -addControl "deltaTime";
editorTemplate -endLayout;
editorTemplate -beginLayout "Initialization" -collapse false;
editorTemplate -addControl "boxMin";
editorTemplate -addControl "boxMax";
editorTemplate -addControl "cellSize";
editorTemplate -addControl "particlePerCell";
editorTemplate -addControl "boundary";
editorTemplate -addControl "initType";
editorTemplate -addControl "numberOfSubsteps";
editorTemplate -callCustom "initSimulatorControl" "modifySimulatorControl" "noAttr";
editorTemplate -beginLayout "Initial Velocity" -collapse true;
editorTemplate -addControl "initStartTransform";
editorTemplate -addControl "initEndTransform";
editorTemplate -addControl "initDeltaTime";
editorTemplate -endLayout;
editorTemplate -endLayout;
editorTemplate -beginLayout "Display" -collapse true;
editorTemplate -addControl "displayType";
editorTemplate -addControl "displaySdf";
editorTemplate -addControl "displaySdfVelocity";
editorTemplate -addControl "displayCell";
editorTemplate -endLayout;
editorTemplate -beginLayout "Load and Save" -collapse true;
editorTemplate -addControl "loadFilePath";
editorTemplate -callCustom "initSaveControl" "modifySaveControl" "noAttr";
editorTemplate -addControl "saveEveryFrame";
editorTemplate -addControl "saveFolder";
editorTemplate -addControl "saveFilePrefix";
editorTemplate -endLayout;
editorTemplate -suppress "initParticle";
editorTemplate -suppress "inputParticle";
editorTemplate -suppress "outputParticle";
editorTemplate -suppress "colliderVdb";
editorTemplate -beginLayout "Collision" -collapse true;
editorTemplate -addControl "colliderTrans";
editorTemplate -endLayout;
editorTemplate -beginLayout "Output NParticle" -collapse true;
editorTemplate -addControl "particleName";
editorTemplate -addControl "particleSampleRate";
editorTemplate -addControl "numSampledParticle";
editorTemplate -callCustom "initParticleControl" "modifyParticleControl" "noAttr";
editorTemplate -endLayout;
editorTemplate -beginLayout "Output VDB" -collapse true;
editorTemplate -addControl "immediateUpdate";
editorTemplate -addControl "vdbType";
editorTemplate -addControl "vdbVolumeFactor";
editorTemplate -addControl "vdbVelocityFactor";
editorTemplate -addControl "vdbVelocitySampleDist";
editorTemplate -addControl "vdbHalfWidth";
editorTemplate -addControl "vdbVoxelSize";
editorTemplate -addControl "minParticleRadius";
editorTemplate -addControl "maxParticleRadius";
editorTemplate -endLayout;
editorTemplate -beginLayout (uiRes("m_AElocatorTemplate.kLocatorAttributes")) -collapse true;
AElocatorCommon $nodeName;
editorTemplate -endLayout;
AEgeometryShapeTemplate $nodeName;
editorTemplate -addExtraControls;
editorTemplate -endScrollLayout;
}