-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecon.mel
582 lines (540 loc) · 17.1 KB
/
recon.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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
//v0.1h - new - cpcns() - change childs (copy-move cns)
//v0.1g - upd - addSCNS() - arg doKeepOffset
// - new - cnsgrp()
//v0.1f - upd - doRecon() - redo with connected blendings
// - upd - renamed doCnsGetTargets > _getCnsTargets()
//v0.1e - upd - ! not tested ! doOS - fix new node names
//v0.1d - new - addSCNS - add scale constraint to already constrained
//v0.1c - new - repcon, oscon, addOSBlendAttr, doOS
//v0.1b - new - cnsgetch, cnsgettrg
//v0.1a - xcon - removes and returns removed results
global proc oscon(){
// select ctrl its group and parent space transfrom
$lssl = `ls -sl`;
$bl_at = "BlendSpace";
if (size($lssl) < 3)
return;
$ctrl = $lssl[0];
$grp = $lssl[1];
$os = $lssl[2];
$bl_at = $ctrl + $bl_at;
$bl_plg = $ctrl + "." + $bl_at;
print {$bl_plg};
}
global proc addOSBlendAttr(string $sel, string $at_data[], float $dv){
float $min = 0.0;
float $max = 1.0;
if ($dv > $max){
$max = $dv;
}
if (size($at_data) == 1){
$at_data[1] = "";
}
addAttr -ln $at_data[0] -nn $at_data[1]
-at double
-min $min -max $max
-k 1
$sel;
$plg = $sel + "." + $at_data[0];
setKeyframe -f $dv -t 1 $plg;
}
global proc string[] doOS(string $sel, string $os_grp, string $os){
// args:
// -sel - root group of control
// -os_grp - name of new group child of constraint
// if empty, $sel will be used as os_grp if it exist
// -os - parent space object - target of constraint
string $cns_data[2];
// solve given args
string $grp = $sel;
if ($os_grp != ""){
$grp = $os_grp;
if (!`objExists $grp`){
$new_grp = doGr($sel, "_os", 1, 1);
$grp = `rename $new_grp $grp`;
}
}
// main job
string $cns_res[] = `parentConstraint -mo $os $grp`;
string $cns = $cns_res[0];
string $bn = `match "[^|]+$" $grp`;
string $pb = `createNode -n ("pb_" + $bn + "_os") pairBlend`;
connectAttr ($cns + ".crx") ".irx2";
connectAttr ($cns + ".cry") ".iry2";
connectAttr ($cns + ".crz") ".irz2";
connectAttr ($cns + ".ctx") ".itx2";
connectAttr ($cns + ".cty") ".ity2";
connectAttr ($cns + ".ctz") ".itz2";
connectAttr -f ".orx" ($grp + ".rx");
connectAttr -f ".ory" ($grp + ".ry");
connectAttr -f ".orz" ($grp + ".rz");
connectAttr -f ".otx" ($grp + ".tx");
connectAttr -f ".oty" ($grp + ".ty");
connectAttr -f ".otz" ($grp + ".tz");
$cns_data = {$cns, $pb};
return $cns_data;
}
global proc repcon(){
// reconstraint - replacing first selected target to second
//
// usage:
// a. Select new_target and parentConstraint (order is not matter)
// or if constarint has multiple targets:
// b. Select old_taarget new_target and parentConstraint
// (order is only matter between targets)
// (!) new target will be of index 1 (".w1") if constraint has one target
// (!) if constraint has multiple targets and only one target is selected
// constraint's last target will be replaced to selected
$lssl = `ls -sl`;
$ls_cns = `ls -type constraint $lssl`;
$targets = stringArrayRemove($ls_cns, $lssl);
$old_tg = $targets[0];
$new_tg = $targets[size($targets)-1];
if (size($targets) == 1){
$ls_tg = `parentConstraint -q -tl $ls_cns[0]`;
$old_tg = $ls_tg[size($ls_tg)-1];
}
parentConstraint -e -mo
$new_tg $ls_cns[0];
parentConstraint -e -rm
$old_tg $ls_cns[0];
}
global proc string[] recon(){
string $ls_cns[];
$lssl = `ls -sl`;
for ($sel in $lssl){
$ls_cns = stringArrayCatenate($ls_cns, `doRecon $sel`);
}
return $ls_cns;
}
global proc string[] findCns(string $sel){
string $ls_con[] = `listConnections -s 0 -d 1
-type constraint
($sel + ".pim")`;
return $ls_con;
}
global proc string doReconstraint(string $cns, string $chld){
string $eval_cmd;
string $ls_cns[], $ls_cns[], $tls[], $wls[];
float $wl_state[];
int $c;
if ($chld == ""){
$ls_chld = `listConnections -s 1 -d 0 ($cns + ".cpim")`;
if (!size($ls_chld))
print {" ! Broken Constraint: " + $cns};
return "";
$chld = $ls_chld[0];
}
print {" Child: " + $chld};
$cns_typ = `objectType $cns`;
$q_tl_cmd = `format -s $cns_typ -s $cns "^1s -q -tl ^2s"`;
$q_wl_cmd = `format -s $cns_typ -s $cns "^1s -q -wal ^2s"`;
$eval_cmd = `format -s $cns_typ "^1s -mo"`;
// print $q_tl_cmd;
$tls = eval($q_tl_cmd);
$wls = eval($q_wl_cmd);
if (!size($tls)){
print "x_X\n";
return "";
}
$eval_cmd += " " + stringArrayToString($tls, " ");
// print {$eval_cmd};
$eval_cmd += " " + $chld;
int $res;
int $t;
for ($at in $wls){
$plg = $cns + "." + $at;
$wl_state[$t] = `getAttr $plg`;
$res = catchQuiet(`setAttr $plg 0`);
$sn_plg = `listAttr -sn $plg`;
$wls[$t++] = $sn_plg[0];
}
if ($res){
print {" ! Can't swtich off target weights: " + $cns};
return "" ;
}
// print{"Failed: " + $res};
// print "tls: ";
// print $tls;
// recreate old constraint
// if ($remove)
// delete $cns;
print {" >>: " + $eval_cmd};
$ls_cns = eval($eval_cmd);
if (`objExists $ls_cns[0]`){
int $w;
// set back saved weight values
for ($at in $wls){
$plg = $ls_cns[0] + "." + $at;
$val = $wl_state[$w++];
setAttr $plg $val;
}
}
return $ls_cns[0];
}
global proc string[] doRecon(string $sel){
// redo constraint with blend connections
// select child
string $cns;
string $ls_cns[];
print{" cur child: " + $sel};
$ls_con = `listConnections -s 0 -d 1 -type constraint ($sel + ".pim")`;
if (!size($ls_con)){
print "O_o\n";
return {};
}
for ($cns in $ls_con){
// save blend connections:
// - get targets
$trgs = getCnsTargets($cns);
$sz_t = size($trgs);
string $cns_w_plgs[];
string $blend_plgs[];
for ($i=0; $i<$sz_t; $i++){
$cns_w = `format -s $cns -s $i "^1s.w^2s"`;
string $ls_plgs[] = `listConnections -s 1 -d 0 -p 1 $cns_w`;
$cns_w_plgs[$i] = $cns_w;
$blend_plgs[$i] = $ls_plgs[0];
}
// - disconnect if any exists
$pairs = `listConnections -c 1 -p 1 -s 1 -d 0 $cns_w_plgs`;
for ($i=0; $i<size($pairs); $i+=2)
disconnectAttr $pairs[$i+1] $pairs[$i];
// redo constraint
$cns = `doReconstraint $cns $sel`;
// restore blend connectinos
for ($i=0; $i<size($pairs); $i+=2)
connectAttr $pairs[$i+1] $pairs[$i];
if ($cns != "")
$ls_cns = stringArrayCatenate($ls_cns, {$cns});
}
return $ls_cns;
}
global proc string[] getConEval(string $cns){
string $cmds[];
string $tls[], $wls[];
$ls_chld = `listConnections -s 1 -d 0 ($cns + ".cpim")`;
if (!size($ls_chld)){
print "X_x child\n";
return {};
}
$chld = $ls_chld[0];
$con_typ = `objectType $cns`;
string $q_tl_cmd = `format -s $con_typ -s $cns "^1s -q -tl ^2s"`;
string $q_wl_cmd = `format -s $con_typ -s $cns "^1s -q -wal ^2s"`;
string $eval_cmd = `format -s $con_typ "^1s -mo"`;
// print $q_tl_cmd;
$tls = eval($q_tl_cmd);
// TODO: make with wight values
$wls = eval($q_wl_cmd);
string $wl_sn[];
int $w;
for ($wl in $wls){
$plg = $cns + "." + $wl;
$sn = `listAttr -sn $plg`;
$wl_sn[$w++] = $sn[0];
}
if (!size($tls)){
print "x_X\n";
return {};
}
$eval_cmd += " " + stringArrayToString($tls, " ");
$eval_cmd += " " + $chld;
$cmds = {$eval_cmd, $q_tl_cmd};
print $cmds;
return $cmds;
}
global proc updcon(string $lssl[]){
if (!size($lssl)){
for ($sel in `ls -sl`)
$lssl = stringArrayCatenate(
$lssl,
`listConnections
-s 0 -d 1
-type constraint ($sel+".pim")`
);
}
//$con = $lssl[0]
print{" constraints:"};
print $lssl;
for ($con in $lssl){
$con_typ = `objectType $con`;
$eval_cmd = `format -s $con_typ "^1s -e -mo"`;
$q_tl_cmd = `format -s $con_typ -s $con "^1s -q -tl ^2s"`;
$ls_chld = `listConnections -s 1 -d 0 ($con + ".cpim")`;
$tls = eval($q_tl_cmd);
$eval_cmd += " " + stringArrayToString($tls, " ");
if (!size($ls_chld)){
print `format -s $con "\n -_- no child: ^1s\n"`;
continue;
}
$eval_cmd += " " + $ls_chld[0];
eval($eval_cmd);
setAttr ($con + ".nds") 0;
}
}
global proc conGoRest(string $cont){
print {"c:" + $cont};
$chld = `listConnections -s 1 -d 0 ($cont + ".cpim")`;
$pos_plg = $cont + ".rst";
if (`objExists $pos_plg`){
float $vals[] = `getAttr ($cont + ".rst")`;
catchQuiet(`setAttr ($chld[0] + ".t") $vals[0] $vals[1] $vals[2]`);
}
$rot_plg = $cont + ".rsrr";
if (`objExists $rot_plg`){
float $vals[] = `getAttr ($cont + ".rsrr")`;
catchQuiet(`setAttr ($chld[0] + ".r") $vals[0] $vals[1] $vals[2]`);
}
$scl_plg = $cont + ".rs";
if (`objExists $scl_plg`){
float $vals[] = `getAttr ($cont + ".rs")`;
catchQuiet(`setAttr ($chld[0] + ".s") $vals[0] $vals[1] $vals[2]`);
}
}
proc updcont(string $cont){
setAttr ($cont + ".nds") 2;
$c_type = `objectType $cont`;
$cmd = $c_type;
$tl_cmd = " -q -tl " + $cont;
$tl = eval($cmd + $tl_cmd);
$mo_cmd = " -e -mo "
+ stringArrayToString($tl, " ")
+ " "
+ $cont;
print({$cmd + $mo_cmd});
eval($cmd + $mo_cmd);
setAttr ($cont + ".nds") 0;
}
global proc conSetRest(string $cns,
vector $rpos,
vector $rrot,
vector $rscl
){
print $rpos;
print $rrot;
print $rscl;
$pos_plg = $cns + ".rst";
$rot_plg = $cns + ".rsrr";
$scl_plg = $cns + ".rs";
if (`objExists $pos_plg`){
catchQuiet(`setAttr $pos_plg ($rpos.x) ($rpos.y) ($rpos.z)`);
}
if (`objExists $rot_plg`){
catchQuiet(`setAttr $rot_plg ($rrot.x) ($rrot.y) ($rrot.z)`);
}
if (`objExists $scl_plg`){
catchQuiet(`setAttr $scl_plg ($rscl.x) ($rscl.y) ($rscl.z)`);
}
}
global proc recon_(){
$cons = `ls -sl -dag -type constraint`;
for($cont in $cons){
setAttr ($cont + ".nds") 2;
conGoRest $cont;
updcont $cont;
}
}
global proc int doDeleteConstraints(string $lssl[]){
if (!size($lssl))
$lssl = `ls -sl`;
int $res = 0;
int $sz;
for ($sel in $lssl){
string $ls_pac = `parentConstraint -q $sel`;
string $ls_poc = `pointConstraint -q $sel`;
string $ls_ac = `aimConstraint -q $sel`;
string $ls_oc = `orientConstraint -q $sel`;
string $ls_sc = `scaleConstraint -q $sel`;
$ls_cons = ls({$ls_pac, $ls_poc, $ls_ac, $ls_oc, $ls_sc});
$sz = size($ls_cons);
catchQuiet(delete($ls_pac));
catchQuiet(delete($ls_poc));
catchQuiet(delete($ls_ac));
catchQuiet(delete($ls_oc));
catchQuiet(delete($ls_sc));
$sz_after = size(ls({$ls_pac, $ls_poc, $ls_ac, $ls_oc, $ls_sc}));
$res += $sz - $sz_after;
// parentConstraint -mo $src $sel;
// scaleConstraint -mo $src $sel;
}
return $res;
}
global proc string[] cnsgettrg(){
string $items[];
string $lssl[] = `ls -sl`;
//$sel = $lssl[0]
for ($sel in $lssl){
$targets = _getCnsTargets($sel);
if (size($targets))
$items = stringArrayCatenate($items, $targets);
}
if (size($items))
select $items;
return $items;
}
global proc string[] _getCnsTargets(string $sel){
// get target from selected(constrainted) child
string $targets[];
$ls_cns = `listConnections -s 0 -d 1 -type constraint ($sel + ".pim")`;
if (size($ls_cns)){
$cns_cmds = `getConEval $ls_cns[0]`;
$targets = eval($cns_cmds[1]);
}
return $targets;
}
global proc string[] getCnsTargets(string $cns){
// get target from selected(constrainted) child
string $targets[];
$cns_cmds = `getConEval $cns`;
$targets = eval($cns_cmds[1]);
return $targets;
}
global proc string[] cnsgetch(){
// returns list of childs
string $items[];
string $lssl[] = `ls -sl`;
//$sel = $lssl[0]
for ($sel in $lssl){
$ls_cns = `listConnections -s 0 -d 1 -type constraint ($sel + ".pm")`;
if (!size($ls_cns))
continue;
for ($cns in $ls_cns){
$ls_chld = `listConnections -s 1 -d 0 ($cns + ".cpim")`;
if (!size($ls_chld)){
print {" ! Broken Constraint: " + $cns};
continue;
}
$items = stringArrayCatenate($items, $ls_chld);
}
}
if (size($items))
select $items;
return $items;
}
global proc string[] doCnsGetChilds(string $sel){
// find child from given target
string $childs[];
$ls_cns = `listConnections -s 0 -d 1 -type constraint ($sel + ".pm")`;
if (size($ls_cns))
for ($cns in $ls_cns){
$ls_chld = `listConnections -s 1 -d 0 ($cns + ".cpim")`;
if (size($ls_chld))
$childs = stringArrayCatenate($childs, $ls_chld);
}
return $childs;
}
global proc int xcon(){
// remove constraints on selected items
int $res = doDeleteConstraints({});
return $res;
}
global proc string[] addSCNS(int $doKeepO){
string $lssl[] = `ls -sl -tr`;
string $items[];
int $i;
// $sel = $lssl[0]
for ($sel in $lssl){
$ls_cns = `listConnections -s 0 -d 1 -type constraint ($sel + ".pim")`;
if (size($ls_cns)){
if (size(`listConnections -s 0 -d 1 -type scaleConstraint ($sel + ".pim")`))
continue;
$trgs = _getCnsTargets($sel);
if ($doKeepO)
$scns = `scaleConstraint -mo $trgs[0] $sel`;
else
$scns = `scaleConstraint $trgs[0] $sel`;
$items[$i++] = $scns[0];
}
}
return $items;
}
global proc string doReCNS(string $A, string $B){
// swaping childs from A to B
$ls_cns = `findCns $A`;
if (!size($ls_cns))
return "";
$cns = `doReconstraint $ls_cns[0] $B`;
return $cns;
}
global proc int retrgcns(){
string $lssl[] = `ls -sl`;
string $ls_cns[] = `ls -type constraint $lssl`;
$targets = stringArrayRemove($ls_cns, $lssl);
$src = $targets[0];
$trg = $targets[1];
$res = doRetargetConstraint($src, $trg, $ls_cns[0]);
return $res;
}
global proc int doRetargetConstraint(string $src, string $trg, string $cns){
// returns 0 if all success, 0 < numbers of failed connections
int $res;
string $ls_plgs[] = `listConnections -s 0 -d 1 -type constraint -p 1 $src`;
for ($plg in $ls_plgs){
$cur_cns = plugNode($plg);
if ($cur_cns != $cns)
continue;
// print {$plg};
string $src_plg[] = `listConnections -s 1 - d 0 -p 1 $plg`;
$new_plg = `substitute "^.[^.]+" $src_plg[0] $trg`;
$cur_res = catchQuiet(`connectAttr -f $new_plg $plg`);
if ($cur_res){
print (`format -s $src_plg[0]
-s $plg " [! can't connect] ^1s >> ^2s\n"`);
$res = $cur_res;
}
}
return $res;
}
global proc string[] cnsgrp(){
// move constraints of selected childs to created groups
string $items[];
int $i;
$lssl = `ls -sl`;
// $sel = $lssl[0]
for ($sel in $lssl){
$ls_cns = stringArrayRemoveDuplicates(
`listConnections -type constraint $sel`
);
if (!size($ls_cns))
continue;
$ls_par = `listRelatives -f -p $sel`;
$cnsgrp = `group -em -n ($sel + "_grp")`;
$ls_trgs = getCnsTargets($ls_cns[0]);
matchTransform $cnsgrp $ls_trgs[0];
// $cns = $ls_cns[0]
for ($cns in $ls_cns)
doReconstraint($cns, $cnsgrp);
parent $sel $cnsgrp;
if (size($ls_par)){
$repar = `parent $cnsgrp $ls_par[0]`;
$cnsgrp = $repar[0];
}
delete `ls $ls_cns`;
$items[$i++] = $cnsgrp;
}
return $items;
}
global proc int cpcns(string $op){
// move(copy/cut) constraints from one child to another
// [select two transforms]
// Args:
// - $op, if 0 - copy, 1 - cut
string $lssl[] = `ls -sl`;
string $ls_cns[];
for ($sel in $lssl){
$ls_tmp = findCns($sel);
if (size($ls_tmp))
$ls_cns = stringArrayCatenate($ls_tmp, $ls_cns);
}
$targets = stringArrayRemove($ls_cns, $lssl);
$src = $targets[0];
$trg = $targets[1];
int $res;
//$cns = $ls_cns[0]
for ($cns in $ls_cns)
$res += doReconstraint($cns, $trg);
if ($op)
delete $ls_cns;
return $res;
}