-
Notifications
You must be signed in to change notification settings - Fork 0
/
js_splitSelJoint.mel
220 lines (192 loc) · 6.93 KB
/
js_splitSelJoint.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
/*
Script: js_splitSelJoint.mel
Version: 1.0
Author: Jason Schleifer
Website: http://jonhandhisdog.com
Descr: Splits the selected joint into the specified number of segments.
*/
global proc js_splitSelJoint (int $numSegments)
{
// This proc will split the selected joint into the specified number of segments //
//
if ($numSegments < 2)
{
error ("The number of segments has to be greater than 1.. ");
}
// VARIABLES
//
string $joints[0];
string $joint;
string $newChildJoints[0];
int $count = 0;
$joints = `ls -sl -type joint`;
for ($joint in $joints)
{
// first check the rotation order vs. the axis the joint should be rotating around. In order for this to work
// correctly, the child joint of the joint specified must have translation values ONLY IN ONE AXIS. Let's
// and, that axis MUST be the first one in the rotation order. For example, if the joint structure is:
// upArm->loArm
// and the loArm has a translate value of 6 0 0, then upArm should have a rotation order of XYZ or XZY.
//
string $child;
$child = `js_getChildJoint $joint`;
if (size($child) == "")
{
error ("Joint: " + $joint + " has no children joints.\n");
}
else
{
string $axis;
string $rotationOrder;
string $firstChar;
float $radius = `getAttr ($joint + ".radius")`;
$axis = `js_getJointAxis $child`;
// now that we have the axis, we want to check and make sure the rotation order on $joint is correct.
int $rotOrderIndex = `getAttr ($joint + ".rotateOrder")`;
$rotationOrder = `js_getRotOrderString $joint`;
// ** REMOVING CHECK FOR ROTATION ORDER MATCHING
// check that the rotaiton order will work.
//
//$firstChar = `substring $rotationOrder 1 1`;
//if ($axis == $firstChar)
//{
float $childT = 0.0;
float $tVal = 0.0;
string $attr;
// the rotation order is correct! we're set!
// get the axis attr
$attr = ("t"+$axis);
// get the value of the child
$childT = `getAttr ($child + "." + $attr)`;
float $space = $childT/$numSegments;
// create a series of locators along the joint based on the number of segments.
string $locators[0];
for ($x = 0; $x < ($numSegments-1); $x++)
{
$tmp = `spaceLocator`;
$locators[$x] = $tmp[0];
parent $locators[$x] $joint;
setAttr ($locators[$x] + ".t") 0 0 0;
setAttr ($locators[$x] + "." + $attr) ($space * ($x+1));
}
// We just want to segment the joint, nothing more.
// for each segment, we're going to insert a joint, and then move it to the position of the locator
string $prevJoint = $joint;
for ($x = 0; $x < size($locators); $x++)
{
// insert a joint
$newJoint = `insertJoint $prevJoint`;
// get the position of the locator
$pos = `xform -q -ws -rp $locators[$x]`;
// move the joint there
move -a -ws $pos[0] $pos[1] $pos[2] ($newJoint + ".scalePivot") ($newJoint + ".rotatePivot");
// rename the new joint
$newJoint = `rename ($newJoint) ($joint + "_seg_"+($x+1)+"_joint")`;
setAttr ($newJoint + ".radius") $radius;
// set the rotation order
setAttr ($newJoint + ".rotateOrder") $rotOrderIndex;
// set the prevJoint
$prevJoint = $newJoint;
}
delete $locators;
// ** END ROTATION ORDER CHECK
//}
//else
//{
// The rotation order is incorrect. The user needs to re-orient the joint
//js_orientJointWarning $joint $rotationOrder $axis $firstChar;
//}
}
}
}
global proc js_orientJointWarning (string $joint, string $rotationOrder, string $axis, string $firstChar )
{
string $message;
$message = ("Warning!!!!\n\n");
$message += ("The rotation order and joint orient for " + $joint + " do not match up.\n");
$message += ("The rotation order is: " + $rotationOrder + "\n");
$message += ("The axis aiming down the joint is: " + $axis + "\n\n");
$message += ("In order for this script to work, you need to either change the axis that\n");
$message += ("aims down the joint to " + $firstChar + ", or switch the rotation order to ");
switch ($axis)
{
case "x":
$message += ("xyz or xzy.\n");
break;
case "y":
$message += ("yxz or yzx.\n");
break;
case "z":
$message += ("zxy or zyx.\n");
break;
}
$message += ("\nSkipping Joint: " + $joint + "...\n");
confirmDialog -m $message -ma "left";
warning $message;
}
global proc string js_getRotOrderString (string $joint)
{
string $return;
int $ro;
$ro = `getAttr ($joint + ".ro")`;
switch ($ro)
{
case 0:
$return = "xyz";
break;
case 1:
$return = "yzx";
break;
case 2:
$return = "zxy";
break;
case 3:
$return = "xzy";
break;
case 4:
$return = "yxz";
break;
case 5:
$return = "zyx";
break;
}
return $return;
}
global proc string js_getJointAxis (string $child)
{
string $axis;
float $t[0];
$t = `getAttr ($child + ".t")`;
// get the translation values of the $child joint
// now check and see which one is greater than 0. We should have a tolerance value just in case
float $tol = 0.0001;
for ($x = 0; $x <= 2; $x++)
{
if (($t[$x] > $tol) || ($t[$x] < (-1 *$tol)))
{
switch ($x)
{
case 0:
$axis = "x";
break;
case 1:
$axis = "y";
break;
case 2:
$axis = "z";
break;
}
}
}
if ($axis == "")
{
error ("The child joint is too close to the parent joint. Cannot determine the proper axis to segment.");
}
return $axis;
}
global proc string js_getChildJoint (string $joint)
{
string $tmp[0];
$tmp = `listRelatives -f -c -type joint $joint`;
return ($tmp[0]);
}