-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbind_HRBF.mel
177 lines (147 loc) · 5.63 KB
/
bind_HRBF.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
// How to skin:
// 1) select some bone on skeleton
// 2) select mesh
// 3) skin by running bind_HRBFskin(). MAKE SURE YOUR SKELETON POSE MATCHES MESH POSE
// 4) reimport weights
proc connectJointCluster( string $jointName, int $i, string $skinClusterName, int $parent_idx )
{
/* not sure what this does, but it breaks some kinds of binds and the attribute
seems to be off for binding to FBX import skeletons anyway.
if ( objExists( $jointName+".lockInfluenceWeights1" ) == false )
{
select -r $jointName;
addAttr -sn "liw" -ln "lockInfluenceWeights1" -at "bool";
}
connectAttr ($jointName+".liw") ($skinClusterName + ".lockWeights["+$i+"]"); */
print("linking " + $jointName + "\n");
connectAttr ($jointName+".worldMatrix[0]") ($skinClusterName + ".matrix["+$i+"]");
connectAttr ($jointName+".objectColorRGB") ($skinClusterName + ".influenceColor["+$i+"]");
float $m[] = `getAttr ($jointName+".wim")`;
setAttr ($skinClusterName + ".bindPreMatrix["+$i+"]") -type "matrix" $m[0] $m[1] $m[2] $m[3] $m[4] $m[5] $m[6] $m[7] $m[8] $m[9] $m[10] $m[11] $m[12] $m[13] $m[14] $m[15];
setAttr ($skinClusterName + ".parentJointIDCS["+$i+"]") $parent_idx;
setAttr ($skinClusterName + ".jointNames[" + $i + "]") -type "string" $jointName;
}
proc get_skeleton_hierarchy(string $jointNames[], int $parentIndices[]) {
/*
Input jointNames is a list of skeleton bones as they will be passed into
the node.
parentIndices will be written to contain the parent index of each bone.
*/
int $numJoints = size($jointNames);
for ($i = 0; $i < $numJoints; $i++) {
//print($jointNames[$i]);
string $parentName[] = `listRelatives -p $jointNames[$i]`;
$parentIndices[$i] = -1; // default for root
for ($j = 0; $j < $numJoints; $j++) {
// walk over the list and find index of parent
if ($jointNames[$j] == $parentName[0]) {
$parentIndices[$i] = $j;
continue;
}
}
}
}
proc get_joints_DF(string $rootName, string $jointNames[]) {
/* test with:
string $rootName = "Hips";
string $dft[];
get_joints_DF($rootName, $dft);
for ($i = 0; $i < size($dft); $i++) {
print($dft[$i] + "\n");
}
*/
// check to make sure we have the root
string $rels[] = `listRelatives -p $rootName`;
if (size($rels) > 0) {
print("ERROR! PLEASE SELECT THE ROOT OF THE SKELETON!");
return;
}
// perform DF traversal using a fake stack
int $numJoints = size(`listRelatives -ad $rootName`) + 1;
string $dfStack[];
int $stackTop = 1;
$dfStack[0] = $rootName;
int $jointNamesIDX = 0;
while ($jointNamesIDX < $numJoints) {
// pop off the stack
string $popped = $dfStack[$stackTop - 1];
$stackTop--;
// stick on the explored queue
$jointNames[$jointNamesIDX] = $popped;
$jointNamesIDX++;
// get children
string $children[] = `listRelatives -c $popped`;
int $numChildren = size($children);
// btw we need to reverse the children, or this doesn't match Maya's DFT
string $swap;
for ($i = 0; $i < $numChildren / 2; $i++) {
$swap = $children[$i];
$children[$i] = $children[$numChildren - 1 - $i];
$children[$numChildren - 1 - $i] = $swap;
}
// push em on the stack
for ($i = 0; $i < $numChildren; $i++) {
$dfStack[$stackTop] = $children[$i];
$stackTop++;
}
}
}
proc bind_HRBFskin() {
string $selectedNodes[] = `ls -sl`; // get selected nodes
print($selectedNodes); // debug
// get a joint
string $rootJointName;
// walk over everything selected
// select a root joint
for ($i = 0; $i < size($selectedNodes); $i++) {
if (`objectType $selectedNodes[$i]` == "joint") {
$rootJointName = $selectedNodes[$i];
break;
}
}
// make sure what was selected was the root
string $rels[] = `listRelatives -p $rootJointName`;
if (size($rels) > 0) {
print("ERROR! PLEASE SELECT THE ROOT OF THE SKELETON!");
return;
}
// get joints in a hierarchy
string $jointsHierarchy[];
int $parentIndices[];
/*
$jointsHierarchy[0] = $rootJointName;
string $otherJoints[] = `listRelatives -ad $rootJointName`;
int $numJoints = size($otherJoints) + 1;
for ($i = 0; $i < $numJoints - 1; $i++) {
$jointsHierarchy[$i + 1] = $otherJoints[$i];
} */
get_joints_DF($rootJointName, $jointsHierarchy);
int $numJoints = size($jointsHierarchy);
print("detected " + $numJoints + "joints");
get_skeleton_hierarchy($jointsHierarchy, $parentIndices);
// debug1
//print($jointsHierarchy[0] + "\n");
for ($i = 0; $i < $numJoints; $i++) {
print($i + ": " + $jointsHierarchy[$i] + " " + $parentIndices[$i] + "\n");
}
// get the mesh
string $meshName;
for ($i = 0; $i < size($selectedNodes); $i++) {
if (`objectType $selectedNodes[$i]` == "transform") {
$meshName = $selectedNodes[$i] + "Shape";
break;
}
}
// select the mesh
print("selecting " + $meshName + "\n");
select -r $meshName;
// make the skin node
string $clusterStr[] = `deformer -type "HRBFSkinCluster"`;
print($clusterStr);
string $skinClusterName = $clusterStr[0];
// link up
for ($i = 0; $i < $numJoints; $i++) {
connectJointCluster( $jointsHierarchy[$i], $i, $skinClusterName, $parentIndices[$i]);
}
//setAttr ($skinClusterName + ".ParentJointIDCS") -type "Int32Array" $parentIndices;
}