forked from vishal980-glitch/Eternal_Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cometJointOrient.mel
498 lines (382 loc) · 14.3 KB
/
cometJointOrient.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// --------------------------------------------------------------------------
// cometJointOrient.mel - MEL Script
// --------------------------------------------------------------------------
//
// DESCRIPTION:
// A nicer utility for setting rotation axis and aiming of joints
// automatically.
//
// REQUIRES:
// Nothing.
//
//
// USAGE:
// source "cometJointOrient.mel"; cometJointOrient();
//
// AUTHORS:
// Michael B. Comet - comet@comet-cartoons.com
// Copyright ©2004 Michael B. Comet - All Rights Reserved.
//
// VERSIONS:
// 1.00 - Feb 21, 2004 - Initial Release.
// 1.01 - Feb 21, 2004 - Added dot check for up vector so that it will
// stop the up vector from flipping in cases where the bones go
// up and then down.
// 1.02 - Sep 17, 2004 - Added + and - tweak buttons, and right click on tweak.
// 1.03 - April 10, 2005 - Fixed so only unparents transform/joint not shapes.
//
// --------------------------------------------------------------------------
global string $cJO_version = "1.03" ;
global string $cJO_date = "Apr 10, 2004" ;
// --------------------------------------------------------------------------
/*
* cometJointOrient() - Main UI entry.
*/
global proc cometJointOrient()
{
global string $cJO_version ;
global string $cJO_date ;
if (`window -ex cometJointOrientWin` != true)
{
window -w 310 -h 256 -t ("cometJointOrient - "+$cJO_version) -in "cometJointOrient" -s true -tb true cometJointOrientWin;
formLayout mainForm ;
separator -style "in" -h 3 sep0 ;
button -l "Show Axis" -h 18 -al "center" -c ("toggle -state on -localAxis; ") -ann ("Show Local Axis") btnShow ;
button -l "Hide Axis" -h 18 -al "center" -c ("toggle -state off -localAxis; ") -ann ("Hide Local Axis") btnHide ;
radioButtonGrp -l "Aim Axis:" -nrb 3 -la3 "X" "Y" "Z" -sl 2 -cw4 80 40 40 40 rbgAim ;
radioButtonGrp -l "Up Axis:" -nrb 3 -la3 "X" "Y" "Z" -sl 1 -cw4 80 40 40 40 rbgUp ;
checkBox -l "Reverse" -v 0 cbRevAim ;
checkBox -l "Reverse" -v 0 cbRevUp ;
separator -style "in" -h 3 sep1 ;
floatFieldGrp -nf 3 -label "World Up Dir:" -v1 1.0 -v2 0.0 -v3 0.0 -cw4 80 50 50 50 ffgUpDir ;
button -l "X" -w 20 -c ("floatFieldGrp -e -v1 1.0 -v2 0.0 -v3 0.0 ffgUpDir; ") -ann ("Auto Set UpDir to X-Axis") btnX ;
button -l "Y" -w 20 -c ("floatFieldGrp -e -v1 0.0 -v2 1.0 -v3 0.0 ffgUpDir; ") -ann ("Auto Set UpDir to Y-Axis") btnY ;
button -l "Z" -w 20 -c ("floatFieldGrp -e -v1 0.0 -v2 0.0 -v3 1.0 ffgUpDir; ") -ann ("Auto Set UpDir to Z-Axis") btnZ ;
checkBox -l "Auto-Guess Up Direction" -v 0 cbAutoDir ;
button -l "Orient Joints" -al "center" -c ("cJO_orientUI();") -ann ("Orient selected joints based on settings above.") btnOJ;
separator -style "double" -h 7 sepBig ;
floatFieldGrp -nf 3 -label "Tweak:" -v1 0.0 -v2 0.0 -v3 0.0 -cw4 80 50 50 50 ffgTweak ;
button -l "ZERO" -w 40 -c ("floatFieldGrp -e -v1 0.0 -v2 0.0 -v3 0.0 ffgTweak; ") -ann ("Zero's tweak values.") btnZero ;
popupMenu -p ffgTweak ;
menuItem -l "X=1" -c ("floatFieldGrp -e -v1 1.0 ffgTweak ; ") ;
menuItem -l "Y=1" -c ("floatFieldGrp -e -v2 1.0 ffgTweak ; ") ;
menuItem -l "Z=1" -c ("floatFieldGrp -e -v3 1.0 ffgTweak ; ") ;
menuItem -divider true ;
menuItem -l "X=5" -c ("floatFieldGrp -e -v1 5.0 ffgTweak ; ") ;
menuItem -l "Y=5" -c ("floatFieldGrp -e -v2 5.0 ffgTweak ; ") ;
menuItem -l "Z=5" -c ("floatFieldGrp -e -v3 5.0 ffgTweak ; ") ;
menuItem -divider true ;
menuItem -l "X=10" -c ("floatFieldGrp -e -v1 10.0 ffgTweak ; ") ;
menuItem -l "Y=10" -c ("floatFieldGrp -e -v2 10.0 ffgTweak ; ") ;
menuItem -l "Z=10" -c ("floatFieldGrp -e -v3 10.0 ffgTweak ; ") ;
button -l "Manual + Rot Tweak" -al "center" -c ("cJO_tweakUI(1.0);") -ann ("Manually rotates selected joints axis positive.") btnTweakP ;
button -l "Manual - Rot Tweak" -al "center" -c ("cJO_tweakUI(-1.0);") -ann ("Manually rotates selected joints axis negative.") btnTweakN ;
separator -style "in" -h 3 sep2 ;
iconTextButton -style "textOnly" -l ("comet@comet-cartoons.com") -ann ("cometJointOrient - "+$cJO_version+" - "+$cJO_date+" Copyright ©2004 Michael B. Comet All Rights Reserved") -c ("showHelp -a \"http://www.comet-cartoons.com/toons/\"") -h 24 email ;
formLayout -e
-af sep0 "left" 0
-af sep0 "right" 0
-af sep0 "top" 0
-an sep0 "bottom"
-af btnShow "left" 0
-ap btnShow "right" 0 50
-ac btnShow "top" 0 sep0
-an btnShow "bottom"
-ap btnHide "left" 0 50
-af btnHide "right" 0
-ac btnHide "top" 0 sep0
-an btnHide "bottom"
-af rbgAim "left" 0
-an rbgAim "right"
-ac rbgAim "top" 0 btnShow
-an rbgAim "bottom"
-ac cbRevAim "left" 0 rbgAim
-af cbRevAim "right" 0
-ac cbRevAim "top" 0 btnShow
-an cbRevAim "bottom"
-af rbgUp "left" 0
-an rbgUp "right"
-ac rbgUp "top" 0 rbgAim
-an rbgUp "bottom"
-ac cbRevUp "left" 0 rbgUp
-af cbRevUp "right" 0
-ac cbRevUp "top" 0 rbgAim
-an cbRevUp "bottom"
-af sep1 "left" 0
-af sep1 "right" 0
-ac sep1 "top" 0 rbgUp
-an sep1 "bottom"
-af ffgUpDir "left" 0
-an ffgUpDir "right"
-ac ffgUpDir "top" 0 sep1
-an ffgUpDir "bottom"
-ac btnX "left" 2 ffgUpDir
-an btnX "right"
-ac btnX "top" 0 sep1
-an btnX "bottom"
-ac btnY "left" 2 btnX
-an btnY "right"
-ac btnY "top" 0 sep1
-an btnY "bottom"
-ac btnZ "left" 2 btnY
-an btnZ "right"
-ac btnZ "top" 0 sep1
-an btnZ "bottom"
-af cbAutoDir "left" 20
-af cbAutoDir "right" 0
-ac cbAutoDir "top" 0 ffgUpDir
-an cbAutoDir "bottom"
-af btnOJ "left" 5
-af btnOJ "right" 5
-ac btnOJ "top" 5 cbAutoDir
-an btnOJ "bottom"
-af sepBig "left" 0
-af sepBig "right" 0
-ac sepBig "top" 5 btnOJ
-an sepBig "bottom"
-af ffgTweak "left" 0
-an ffgTweak "right"
-ac ffgTweak "top" 5 sepBig
-an ffgTweak "bottom"
-ac btnZero "left" 2 ffgTweak
-an btnZero "right"
-ac btnZero "top" 5 sepBig
-an btnZero "bottom"
-af btnTweakP "left" 5
-ap btnTweakP "right" 0 49
-ac btnTweakP "top" 5 ffgTweak
-an btnTweakP "bottom"
-ap btnTweakN "left" 0 51
-af btnTweakN "right" 5
-ac btnTweakN "top" 5 ffgTweak
-an btnTweakN "bottom"
-af sep2 "left" 5
-af sep2 "right" 5
-ac sep2 "top" 5 btnTweakP
-an sep2 "bottom"
-af email "left" 0
-af email "right" 0
-ac email "top" 0 sep2
-an email "bottom"
mainForm ;
showWindow cometJointOrientWin;
}
else // else just pop it up from being minimized again
{
showWindow cometJointOrientWin;
}
}
// --------------------------------------------------------------------------
/*
* cJO_orientUI() - UI wrapper for starting an orient
*/
global proc cJO_orientUI()
{
print ("// cometJointOrient\n");
int $nAimAxis = `radioButtonGrp -q -sl rbgAim` ;
int $nUpAxis = `radioButtonGrp -q -sl rbgUp` ;
float $aimAxis[] = {0,0,0} ;
float $upAxis[] = {0,0,0} ;
float $revAim = 1.0 ;
if (`checkBox -q -v cbRevAim`)
$revAim = -1.0 ;
float $revUp = 1.0 ;
if (`checkBox -q -v cbRevUp`)
$revUp = -1.0 ;
if ($nAimAxis == $nUpAxis)
warning -sl 0 ("The AIM and UP axis are the same! Orientaiton probably won't work!") ;
$aimAxis[ ($nAimAxis-1) ] = $revAim ;
$upAxis[ ($nUpAxis-1) ] = $revUp ;
float $upDir[3] ;
$upDir[0] = `floatFieldGrp -q -v1 ffgUpDir` ;
$upDir[1] = `floatFieldGrp -q -v2 ffgUpDir` ;
$upDir[2] = `floatFieldGrp -q -v3 ffgUpDir` ;
int $doAuto = `checkBox -q -v cbAutoDir` ;
string $joints[] = `ls -type "joint" -sl` ;
// Now do it!
cJO_orient($joints, $aimAxis, $upAxis, $upDir, $doAuto) ;
// End with same stuff selected!
select -r $joints ;
}
// --------------------------------------------------------------------------
/*
* cJO_orient() - The real worker orient proc.
*
* $joints is array of joints to orient
* $aimAxis = is xyz array of what axis of joint does aim
* $upAxis = is xyz array of what axis of joint does up
* $upDir = what vector to use for up direction?
* $doAuto = If possible will try to guess the up axis otherwise
* it will use prev joint up axis or else world upDir.
*
*/
global proc cJO_orient(string $joints[], float $aimAxis[], float $upAxis[],
float $upDir[], int $doAuto)
{
int $nJnt = size($joints) ;
int $i;
vector $prevUp = <<0,0,0>>;
// Now orient each joint
for ($i=0; $i < $nJnt; ++$i)
{
// First we need to unparent everything and then store that,
string $childs[] = `listRelatives -children -type "transform" -type "joint" $joints[$i]` ;
if (size($childs) > 0)
$childs = `parent -w $childs` ; // unparent and get NEW names in case they changed...
// Find parent for later in case we need it.
string $parents[] = `listRelatives -parent $joints[$i]` ;
string $parent = $parents[0] ;
// Now if we have a child joint...aim to that.
string $aimTgt="" ;
string $child ;
for ($child in $childs)
{
if (nodeType($child) == "joint")
{
$aimTgt = $child ;
break ;
}
}
// print ("// DEBUG: JNT="+$joints[$i]+" Parent="+$parent+" AimTgt="+$aimTgt+" //\n") ;
if ($aimTgt != "")
{
float $upVec[3] = {0,0,0} ;
// First off...if $doAuto is on, we need to guess the cross axis dir.
//
if ($doAuto)
{
// Now since the first joint we want to match the second orientation
// we kind of hack the things passed in if it is the first joint
// ie: If the joint doesn't have a parent...OR if the parent it has
// has the "same" position as itself...then we use the "next" joints
// as the up cross calculations
//
float $posJ[3] = `xform -q -ws -rp $joints[$i]` ;
float $posP[3] = $posJ ;
if ($parent != "")
$posP = `xform -q -ws -rp $parent` ;
float $tol = 0.0001 ; // How close to we consider "same"?
if ($parent == "" || (abs($posJ[0] - $posP[0]) <= $tol && abs($posJ[1] - $posP[1]) <= $tol && abs($posJ[2] - $posP[2]) <= $tol ))
{
string $aimChilds[] = `listRelatives -children $aimTgt` ;
string $aimChild = "" ;
string $child ;
for ($child in $aimChilds)
{
if (nodeType($child) == "joint")
{
$aimChild = $child ;
break ;
}
}
$upVec = cJO_getCrossDir($joints[$i], $aimTgt, $aimChild) ;
}
else
$upVec = cJO_getCrossDir($parent, $joints[$i], $aimTgt) ;
}
if (!$doAuto || ($upVec[0] == 0.0 && $upVec[1] == 0.0 && $upVec[2] == 0.0))
$upVec = $upDir ; // or else use user set up Dir. if needed
string $aCons[] = `aimConstraint
-aim $aimAxis[0] $aimAxis[1] $aimAxis[2]
-upVector $upAxis[0] $upAxis[1] $upAxis[2]
-worldUpVector $upVec[0] $upVec[1] $upVec[2]
-worldUpType "vector"
-weight 1.0
$aimTgt
$joints[$i]` ;
delete $aCons ;
// Now compare the up we used to the prev one.
vector $curUp = << $upVec[0], $upVec[1], $upVec[2] >> ;
$curUp = unit($curUp) ;
float $dot = $curUp * $prevUp ; // dot product for angle betwen...
$prevUp = << $upVec[0], $upVec[1], $upVec[2] >> ; // store for later
if ($i > 0 && $dot <= 0.0)
{
// Adjust the rotation axis 180 if it looks like we've flopped the wrong way!
xform -r -os -ra ($aimAxis[0]*180.0) ($aimAxis[1]*180.0) ($aimAxis[2]*180.0) $joints[$i] ;
$prevUp *= -1.0 ;
}
// And now finish clearing out joint axis...
joint -e -zso $joints[$i] ;
makeIdentity -apply true $joints[$i] ;
}
else if ($parent != "")
{
// Otherwise if there is no target, just dup orienation of parent...
string $oCons[] = `orientConstraint
-weight 1.0
$parent
$joints[$i]` ;
delete $oCons ;
// And now finish clearing out joint axis...
joint -e -zso $joints[$i] ;
makeIdentity -apply true $joints[$i] ;
}
// Now that we're done... reparent
if (size($childs) > 0)
parent $childs $joints[$i] ;
}
}
// --------------------------------------------------------------------------
/*
* cJO_getCrossDir() - Given three nodes, this gets the cross product of
* the directions from B->A and B->C.
*/
global proc float[] cJO_getCrossDir(string $objA, string $objB, string $objC)
{
float $cross[3] = {0,0,0} ;
if ($objA == "" || $objB == "" || $objC == "" ||
objExists($objA) != true || objExists($objB) != true || objExists($objC) != true)
return $cross ;
float $posA[3] = `xform -q -ws -rp $objA` ;
float $posB[3] = `xform -q -ws -rp $objB` ;
float $posC[3] = `xform -q -ws -rp $objC` ;
vector $v1 = << $posA[0]-$posB[0], $posA[1]-$posB[1], $posA[2]-$posB[2] >> ;
vector $v2 = << $posC[0]-$posB[0], $posC[1]-$posB[1], $posC[2]-$posB[2] >> ;
vector $vC = $v1 ^ $v2 ; // Do cross product!
$vC = unit($vC) ; // normalize
$cross[0] = $vC.x ;
$cross[1] = $vC.y ;
$cross[2] = $vC.z ;
return $cross ;
}
// --------------------------------------------------------------------------
/*
* cJO_tweakUI() - UI wrapper for starting a joint tweak
*/
global proc cJO_tweakUI(float $mult)
{
float $rot[3] ;
$rot[0] = `floatFieldGrp -q -v1 ffgTweak` * $mult ;
$rot[1] = `floatFieldGrp -q -v2 ffgTweak` * $mult ;
$rot[2] = `floatFieldGrp -q -v3 ffgTweak` * $mult ;
string $joints[] = `ls -type "joint" -sl` ;
// Now do it!
cJO_tweak($joints, $rot) ;
// End with same stuff selected!
select -r $joints ;
}
// --------------------------------------------------------------------------
/*
* cJO_tweak() - Tweaks the rotation of a joint the amount specified
*
* $joints is array of joints to orient
* $rot = is xyz array of how much to rotate
*
*/
global proc cJO_tweak(string $joints[], float $rot[])
{
int $nJnt = size($joints) ;
int $i;
// Now tweak each joint
for ($i=0; $i < $nJnt; ++$i)
{
// Adjust the rotation axis
xform -r -os -ra $rot[0] $rot[1] $rot[2] $joints[$i] ;
// And now finish clearing out joint axis...
joint -e -zso $joints[$i] ;
makeIdentity -apply true $joints[$i] ;
}
}
// --------------------------------------------------------------------------
cometJointOrient();