-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhere.mel
389 lines (359 loc) · 11 KB
/
here.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
// TODO: parhere(); fix if src and target under the same parent
//v0.2a - fix - parhere() - cases if src and target have no parents
//v0.1f - upd - parhere() - complex hierarchy, new arg
//v0.1e - fix - parhere(); if src and trg under same parent
//v0.1d - upd - duphere(); selection logic - if only one object is selected
//v0.1c - upd - parhere(); reorder in outliner
// - new - getOutlinerOrder();
//v0.1b - upd - now substitute works on hierarchy
//v0.1a
// TODO:
//parhere -dup -mir -ren;
// -dup -duplicate and parent, for best result use with -ren
// -mir -find mirror side pair if not - create it and parent
// to existing mirrored parent or in same place, can be
// used with -ren;
// -ren -rename with prefix or suffix or with substitution
global proc string[] duphere(string $parent){
// returns first elements of duplicated items(roots)
// last selected item parent location
// Args:
// - parent - string, if empty, will be placed at the
// same place as source
// ask to kepp inputs
$okbtn = "Keep";
$okbtn2 = "All Graph";
$nobtn = "No";
string $chk_mode = inputButtons(
"Do keep input connectinos?", {$okbtn, $okbtn2, $nobtn});
if ($chk_mode == "Cancel")
return {};
int $mode = 0;
switch ($chk_mode){
case "Keep":
$mode = 1;
break;
case "All Graph":
$mode = 2;
break;
default:
break;
}
// ask for prefixes and suffixes to be applied on new hierarchy
string $pfx, $sfx;
string $fnd, $rep;
$name_parts = inputPfxSfx();
if (!size($name_parts))
return{};
$pfx = $name_parts[0];
if (size($name_parts) > 1)
$sfx = $name_parts[1];
// ask for name parts replacing within new hierarchy
$sub_data = inputFindReplace();
if (size($sub_data) == 2){
$fnd = $sub_data[0];
$rep = $sub_data[1];
}
string $lssl[] =`ls -l -sl`;
// find target address (parent)
// prefix source suffix find replace parent
$dups = `doDuplicateRenameParent
$pfx $lssl $sfx
$fnd $rep
$parent
$mode`;
return $dups;
}
global proc string[] dupsubhere(string $fnd, string $rep){
// ask to kepp inputs
$okbtn = "Keep";
$nobtn = "No";
$chk_mode = inputButtons(
"Do keep input connectinos?", {$okbtn, $nobtn});
if ($chk_mode == "Cancel")
return {};
int $mode = $chk_mode == $okbtn ? 1 : 0;
string $items[];
if ($fnd == "" && $rep == ""){
$sub_data = inputFindReplace();
if (size($sub_data) == 2){
$fnd = $sub_data[0];
$rep = $sub_data[1];
}
}
$lssl =`ls -l -sl`;
$sz = size($lssl);
$parent = $lssl[$sz-1];
stringArrayRemoveAtIndex($sz-1, $lssl);
int $i;
for ($sel in $lssl){
// prefix source suffix find replace parent
$dups = `doDuplicateRenameParent
"" $sel ""
$fnd $rep
$parent
$mode`;
$items[$i++] = $dups[0];
}
return $items;
}
global proc string[] doDuplicateRenameParent(
string $pfx, string $lssl[], string $sfx,
string $fnd, string $rep,
string $parent,
int $keepcon){
print {"pfx: " + $pfx};
print {"sfx: " + $sfx};
print {"fnd: " + $fnd};
print {"rep: " + $rep};
print {"par: " + $parent};
// Duplicates given transform and renames it and
// all its childs with given data
//
// Args:
// - parent, str - always full/long name
// duplicate
string $dups[];
if ($keepcon == 1)
$dups = `duplicate -ic -rr $lssl`;
else
if ($keepcon == 2)
$dups = `duplicate -un -rr $lssl`;
else
$dups = `duplicate -rr $lssl`;
if (size($dups) != size($lssl)){
print " Duplicate results not equal";
delete $dups;
return $dups;
}
for ($i=0; $i<size($dups); $i++){
$sel = $lssl[$i];
$dup = $dups[$i];
$bn = `substitute ".*|" $sel ""`;
// form new name
string $new_name = `substitute $fnd $bn $rep`;
$new_name = $pfx + $new_name + $sfx;
// check new name
string $chk_path = $parent + "|" + $new_name;
if (`objExists $chk_path`){
$msg = " ! Object already exists in target location:";
print {$msg + $chk_path};
return $dups;
}
// check current parent:
// if current is not same as given - do parent
// else just rename
string $ls_par[] = `listRelatives -f -p $sel`;
print {$parent + " <> " + $ls_par[0]};
if ($parent != $ls_par[0]){
if ($parent != "" && `objExists $parent`)
$cur_dups = `parent $dup $parent`;
$dup = $cur_dups[0];
// $bn = `substitute ".*|" $sel ""`;
}
$dup = `rename $dup $new_name`;
// string $newhi[] = {$dup};
// rename childrens too
string $lshi[] = `listRelatives -ad -type transform -pa $dup`;
int $n = 1;
//string $sl = $lssl[1];
for ($sl in $lshi){
print {"> " + $sl};
// rename
$pa = `match ".*|" $sl`;
$c_bn = `substitute $pa $sl ""`;
//toe_L_14_env
print {"pa: " + $pa};
print {"bn: " + $c_bn};
// substitute name parts
$replaced = `substitute $fnd $c_bn $rep`;
print {"rep: " + $rep};
//toetoe_L_FK_15_env14_env
$newn = $pfx + $replaced + $sfx;
print {"newn :" + $newn};
rename $sl $newn;
}
}
return $dups;
}
global proc string[] _doDuplicateRenameParent(
string $pfx, string $sel, string $sfx,
string $fnd, string $rep,
string $parent,
int $keepcon){
print {"pfx: " + $pfx};
print {"sfx: " + $sfx};
print {"fnd: " + $fnd};
print {"rep: " + $rep};
print {"par: " + $parent};
// Duplicates given transform and renames it and
// all its childs with given data
//
// Args:
// - parent, str - always full/long name
// form new name
$bn = `substitute ".*|" $sel ""`;
string $new_name = `substitute $fnd $bn $rep`;
$new_name = $pfx + $new_name + $sfx;
// check new name
string $chk_path = $parent + "|" + $new_name;
if (`objExists $chk_path`){
$msg = " ! Object already exists in target location:";
print {$msg + $chk_path};
return {};
}
// duplicate
string $dup[];
if ($keepcon == 1)
$dup = `duplicate -ic -rr $sel`;
else
if ($keepcon == 2)
$dup = `duplicate -un -rr $sel`;
else
$dup = `duplicate -rr $sel`;
// check current parent:
// if current is not same as given - do parent
// else just rename
string $ls_par[] = `listRelatives -f -p $sel`;
print {$parent + " <> " + $ls_par[0]};
if ($parent != $ls_par[0]){
if ($parent != "" && `objExists $parent`)
$dup = `parent $dup[0] $parent`;
// $bn = `substitute ".*|" $sel ""`;
}
$dup[0] = `rename $dup[0] $new_name`;
string $newhi[] = {$dup[0]};
// rename childrens too
string $lshi[] = `listRelatives -ad -type transform -pa $dup[0]`;
int $n = 1;
//string $sl = $lssl[1];
for ($sl in $lshi){
print {"> " + $sl};
// rename
$pa = `match ".*|" $sl`;
$c_bn = `substitute $pa $sl ""`;
//toe_L_14_env
print {"pa: " + $pa};
print {"bn: " + $c_bn};
// substitute name parts
$replaced = `substitute $fnd $c_bn $rep`;
print {"rep: " + $rep};
//toetoe_L_FK_15_env14_env
$newn = $pfx + $replaced + $sfx;
print {"newn :" + $newn};
$newhi[$n++] = `rename $sl $newn`;
}
return $newhi;
}
global proc string inputButtons(string $msg, string $btns[]){
string $eval_cmd = `format -s $msg "confirmDialog -m \"^1s\" "`;
for ($btn in $btns)
$eval_cmd += `format -s $btn " -b \"^1s\""`;
$eval_cmd += " -b \"Cancel\" -db \"Cancel\"";
print {$eval_cmd};
$_inp = eval($eval_cmd);
return $_inp;
}
global proc string[] inputPfxSfx(){
string $pfx, $sfx;
string $res[];
$_inp_pfx = `promptDialog
-m "Prefix Hierarchy"
-tx "proxy_"
-b "Ok" -b "Cancel"
-db "Ok" -cb "Cancel"`;
if ($_inp_pfx != "Cancel"){
$res[0] = `promptDialog -q -tx`;
$_inp_sfx = `promptDialog
-m "Suffix Hierarchy"
-tx "_old"
-b "Ok" -b "Cancel"
-db "Ok" -cb "Cancel"`;
if ($_inp_sfx != "Cancel")
$res[1] = `promptDialog -q -tx`;
}
return $res;
}
global proc string[] inputFindReplace(){
string $fnd, $rep;
string $res[];
$_inp_fnd = `promptDialog
-m "Substitute find string"
-tx "_L_"
-b "Ok" -b "Cancel"
-db "Ok" -cb "Cancel"`;
if ($_inp_fnd != "Cancel"){
$res[0] = `promptDialog -q -tx`;
$_inp_rep = `promptDialog
-m "Substitute replace string"
-tx "_R_"
-b "Ok" -b "Cancel"
-db "Ok" -cb "Cancel"`;
if ($_inp_rep != "Cancel")
$res[1] = `promptDialog -q -tx`;
}
return $res;
}
global proc string parhere(){
// parents near last selected object
$lssl = `ls -l -sl`;
$sz = size($lssl);
$trg = $lssl[$sz-1];
stringArrayRemoveAtIndex($sz-1, $lssl);
string $par[] = `listRelatives -p -f $trg`;
$sz_par = size($par);
$pos = `getOutlinerOrder $trg`;
int $p = 1;
int $i;
// $sel = $lssl[0]
for ($sel in $lssl){
string $cur_par[] = `listRelatives -p -f $sel`;
if ($par[0] != $cur_par[0]){
if ($sz_par){
$repar = `parent $sel $par[0]`;
$sel = $repar[0];
}
else
parent -w $sel;
}
reorder -b $sel;
reorder -r ($pos + $p) $sel;
$lssl[$i] = $sel;
$p++;
$i++;
}
select $lssl;
return $par[0];
}
global proc int getOutlinerOrder(string $sel){
// reorder in outliner
// better to use fool path in $sel
// returns int position from top, 1-based
// eg: if child is is 0(top) returns 1
// string $sel_p[] = `ls -l $sel`;
// $sel = $sel_p[0];
string $par;
if (`gmatch $sel "*|*"`){
$trim = `substitute "[^|]+$" $sel ""`;
if (size($trim) > 1)
$par = $trim;
}
else{
$ls_par = `listRelatives -f -p $sel`;
if (size($ls_par))
$par = $ls_par[0];
}
print {"parentfound: " + $par};
string $childs[];
if ($par != "")
$childs = `listRelatives -f -c $par`;
else{
$childs = `ls -l -as`;
}
$pos = stringArrayFind($sel, 0, $childs);
// $par_res = `parent $res $par`;
// $src = $par_res[0];
// reorder -r (1 + $pos) $src;
print {$sel + " : " + $pos};
return ($pos + 1);
}