-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcpattrs.mel
More file actions
196 lines (176 loc) · 5.47 KB
/
cpattrs.mel
File metadata and controls
196 lines (176 loc) · 5.47 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
//v0.1a -new- cpselattrsto()
//v0.1 codefix
// last select is source
// targets shapes will be matched by src's types
// dependencies
// - /a-vfx/studio/users/barginbayev/home/maya/scripts/copyshapes.mel
source lscb;
global proc cpattrs_(){
$lssl = `ls -sl`;
// get src shape
string $src = $lssl[size($lssl)-1];
string $ls_shps[] = `listRelatives -s -ni $src`;
if (size(`ls -s -ni $src`))
$ls_shps = {$src};
print "$ls_shps\n";
print $ls_shps;
print "\n";
// targets
$stype = `objectType $src`;
print "$stype\n";
print $stype;
print "\n";
$trgs = listRelatives(
"-s", "-ni",
"-type", $stype,
stringArrayRemove({$src}, $lssl)
);
if (!size($trgs))
$trgs = ls(stringArrayRemove({$src}, $lssl));
print "$trgs\n";
print $trgs;
for ($shp in $ls_shps)
for ($trg in $trgs)
doCopyAttrs($shp, $trg, 1);
}
global proc string[] cpattrs(string $trg, string $plgs[], int $ispxy){
if (!size($plgs))
$plgs = `lscb`;
string $result[];
int $r;
for ($plg in $plgs)
$result[$r++] = doCopyAttrs($plg, $trg, $ispxy);
return $result;
}
// NB: i don't remember why pxy is bad, animation curves are
// connectiong to orginal plugs,so if you query for animated channels they
// are not treated so.
global proc string doCopyAttrs(string $srcPlg, string $trg, int $isPxy){
float $val = `getAttr $srcPlg`;
$attr = plugAttr($srcPlg);
$nd = plugNode($srcPlg);
$atype = `attributeQuery -at -n $nd $attr`;
// $iskey = `attributeQuery -k -n $nd $attr`;
$iskey = 1;
$ishid = 0;
$trgPlg = $trg + "." + $attr;
$src_attr_data = `ls -st $srcPlg`;
$lattr = plugAttr($src_attr_data[0]);
string $addattr_cmd = `format
-s $atype
-s $lattr
-s $attr
-s $iskey
-s $ishid
"addAttr -at \"^1s\" -ln \"^2s\" -sn \"^3s\" -k ^4s -h ^5s"`;
if ($atype == "enum"){
$enums = attributeQuery("-le", "-n",
plugNode($srcPlg), plugAttr($srcPlg));
$addattr_cmd += `format -s $enums " -en \":^1s\""`;
}
if (!`attributeExists $attr $trg`){
if ($isPxy)
$addattr_cmd += " -pxy " + $srcPlg;
$addattr_cmd += " " + $trg;
print {" Creating attr:\n"};
print {$addattr_cmd};
eval($addattr_cmd);
}
if (!`objExists $trgPlg`){
string $msg = `format -s $attr -s $trg " Can't create attribute: ^1s on: ^2s"`;
print {$msg};
return "";
}
setAttr $trgPlg $val;
if (!$isPxy)
connectAttr -f $trgPlg $srcPlg;
return $trgPlg;
}
// global proc cpselattrs(string $kwargs[]){
global proc cpselattrs(string $kwargs){
// usage
// >> cpselattrs "-ksc -ic";
string $args[] = stringToStringArray($kwargs, " ");
string $keys[], $vals[];
string $py_dict_upd;
int $i;
print $args;
python("cp_dict = {}");
for ($arg in $args){
$k = `match "[^-][a-zA-Z]+" $arg`;
$v = "True";
$keys[$i] = $k;
$vals[$i++] = $v;
$py_dict_upd = `format -s $k -s $v "cp_dict.update({\"^1s\":^2s})"`;
python($py_dict_upd);
python("print \">\", cp_dict");
}
python( "cp_dict.update({\"at\":[]})" );
for ($plg in `lscb`){
$at = plugAttr($plg);
$k = "at";
$v = $at;
$py_dict_upd = `format -s $at "cp_dict[\"at\"].append(\"^1s\")"`;
print {$py_dict_upd};
python ($py_dict_upd);
}
print{"import maya.cmds as cm;cm.copyAttr(**cp_dict)"};
python( "import maya.cmds as cm;cm.copyAttr(**cp_dict)" );
}
global proc cpattrsto(){
// move connectinos from source to target
// usage: select node(s): child, source and target
$lssl = `ls -sl`;
doCopyAttrsTo $lssl[0] $lssl[1] $lssl[2];
}
global proc int doCopyAttrsTo(string $sel, string $src, string $trg){
// reconnects existing source plugs to $trg
// (attributes on targets should exist)
// usage: select node(s): child, source and target
// >>: doCopyAttrsTo <child> <source> <target>;
// return: number of erros
string $inplgs[], $outplgs[];
$plugs = `getPlugsFromSource $sel $src`;
for ($i=0, $j=0; $i<size($plugs); $i+=2){
$inplgs[$j] = $plugs[$i];
$outplgs[$j++] = $plugs[$i+1];
}
$newplgs = stringArraySubstitute($src, $outplgs, $trg);
int $err = 0;
for ($i=0; $i<size($newplgs); $i++){
$oplg = $newplgs[$i];
$iplg = $inplgs[$i];
// print {(string)`format -s $oplg -s $iplg "^1s >> ^2s"`};
$res = catchQuiet(`connectAttr -f $oplg $iplg`);
if ($res)
$err++;
}
return $err;
}
global proc string[] getPlugsFromSource(string $sel, string $src){
$ls_con = `listConnections -s 1 -d 0 -c 1 -p 1 $sel`;
string $inps[];
for ($i=0, $j=0; $i<size($ls_con); $i+=2){
$plgA = $ls_con[$i+1];
$plgB = $ls_con[$i];
if (plugNode($plgA) == $src){
$inps[$j++] = $plgB;
$inps[$j++] = $plgA;
}
}
print $inps;
return $inps;
}
global proc _tmp_cpSelAttrs(string $sel, string $src){
string $inplgs[] = `lscb`;
string $outplgs[];
if (!size($inplgs)){
$plugs = `getPlugsFromSource $sel $src`;
for ($i=0, $j=0; $i<size($plugs); $i+=2){
$inplgs[$j] = $plugs[$i];
$outplgs[$j++] = $plugs[$i+1];
}
}
else
$outplgs = `listConnections -s 1 -d 0 -p 1 $inplgs`;
}