Fix layers unittest - we cannot use nestted get_pointer calls
[blender.git] / source / blender / ikplugin / intern / iksolver_plugin.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Original author: Benoit Bolsee
24  * Contributor(s):
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/ikplugin/intern/iksolver_plugin.c
30  *  \ingroup ikplugin
31  */
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BIK_api.h"
36 #include "BLI_blenlib.h"
37 #include "BLI_math.h"
38 #include "BLI_utildefines.h"
39
40 #include "BKE_armature.h"
41 #include "BKE_constraint.h"
42
43 #include "DNA_object_types.h"
44 #include "DNA_action_types.h"
45 #include "DNA_constraint_types.h"
46 #include "DNA_armature_types.h"
47
48 #include "IK_solver.h"
49 #include "iksolver_plugin.h"
50
51 #include <string.h> /* memcpy */
52
53 #define USE_NONUNIFORM_SCALE
54
55 /* ********************** THE IK SOLVER ******************* */
56
57 /* allocates PoseTree, and links that to root bone/channel */
58 /* Note: detecting the IK chain is duplicate code... in drawarmature.c and in transform_conversions.c */
59 static void initialize_posetree(struct Object *UNUSED(ob), bPoseChannel *pchan_tip)
60 {
61         bPoseChannel *curchan, *pchan_root = NULL, *chanlist[256], **oldchan;
62         PoseTree *tree;
63         PoseTarget *target;
64         bConstraint *con;
65         bKinematicConstraint *data;
66         int a, t, segcount = 0, size, newsize, *oldparent, parent;
67
68         /* find IK constraint, and validate it */
69         for (con = pchan_tip->constraints.first; con; con = con->next) {
70                 if (con->type == CONSTRAINT_TYPE_KINEMATIC) {
71                         data = (bKinematicConstraint *)con->data;
72                         if (data->flag & CONSTRAINT_IK_AUTO) break;
73                         if (data->tar == NULL) continue;
74                         if (data->tar->type == OB_ARMATURE && data->subtarget[0] == 0) continue;
75                         if ((con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) == 0 && (con->enforce != 0.0f)) break;
76                 }
77         }
78         if (con == NULL) return;
79
80         /* exclude tip from chain? */
81         if (!(data->flag & CONSTRAINT_IK_TIP))
82                 pchan_tip = pchan_tip->parent;
83
84         /* Find the chain's root & count the segments needed */
85         for (curchan = pchan_tip; curchan; curchan = curchan->parent) {
86                 pchan_root = curchan;
87
88                 curchan->flag |= POSE_CHAIN;    // don't forget to clear this
89                 chanlist[segcount] = curchan;
90                 segcount++;
91
92                 if (segcount == data->rootbone || segcount > 255) break;  // 255 is weak
93         }
94         if (!segcount) return;
95
96         /* setup the chain data */
97
98         /* we make tree-IK, unless all existing targets are in this chain */
99         for (tree = pchan_root->iktree.first; tree; tree = tree->next) {
100                 for (target = tree->targets.first; target; target = target->next) {
101                         curchan = tree->pchan[target->tip];
102                         if (curchan->flag & POSE_CHAIN)
103                                 curchan->flag &= ~POSE_CHAIN;
104                         else
105                                 break;
106                 }
107                 if (target) break;
108         }
109
110         /* create a target */
111         target = MEM_callocN(sizeof(PoseTarget), "posetarget");
112         target->con = con;
113         pchan_tip->flag &= ~POSE_CHAIN;
114
115         if (tree == NULL) {
116                 /* make new tree */
117                 tree = MEM_callocN(sizeof(PoseTree), "posetree");
118
119                 tree->type = CONSTRAINT_TYPE_KINEMATIC;
120
121                 tree->iterations = data->iterations;
122                 tree->totchannel = segcount;
123                 tree->stretch = (data->flag & CONSTRAINT_IK_STRETCH);
124
125                 tree->pchan = MEM_callocN(segcount * sizeof(void *), "ik tree pchan");
126                 tree->parent = MEM_callocN(segcount * sizeof(int), "ik tree parent");
127                 for (a = 0; a < segcount; a++) {
128                         tree->pchan[a] = chanlist[segcount - a - 1];
129                         tree->parent[a] = a - 1;
130                 }
131                 target->tip = segcount - 1;
132
133                 /* AND! link the tree to the root */
134                 BLI_addtail(&pchan_root->iktree, tree);
135         }
136         else {
137                 tree->iterations = MAX2(data->iterations, tree->iterations);
138                 tree->stretch = tree->stretch && !(data->flag & CONSTRAINT_IK_STRETCH);
139
140                 /* skip common pose channels and add remaining*/
141                 size = MIN2(segcount, tree->totchannel);
142                 a = t = 0;
143                 while (a < size && t < tree->totchannel) {
144                         /* locate first matching channel */
145                         for (; t < tree->totchannel && tree->pchan[t] != chanlist[segcount - a - 1]; t++) ;
146                         if (t >= tree->totchannel)
147                                 break;
148                         for (; a < size && t < tree->totchannel && tree->pchan[t] == chanlist[segcount - a - 1]; a++, t++) ;
149                 }
150
151                 segcount = segcount - a;
152                 target->tip = tree->totchannel + segcount - 1;
153
154                 if (segcount > 0) {
155                         for (parent = a - 1; parent < tree->totchannel; parent++)
156                                 if (tree->pchan[parent] == chanlist[segcount - 1]->parent)
157                                         break;
158
159                         /* shouldn't happen, but could with dependency cycles */
160                         if (parent == tree->totchannel)
161                                 parent = a - 1;
162
163                         /* resize array */
164                         newsize = tree->totchannel + segcount;
165                         oldchan = tree->pchan;
166                         oldparent = tree->parent;
167
168                         tree->pchan = MEM_callocN(newsize * sizeof(void *), "ik tree pchan");
169                         tree->parent = MEM_callocN(newsize * sizeof(int), "ik tree parent");
170                         memcpy(tree->pchan, oldchan, sizeof(void *) * tree->totchannel);
171                         memcpy(tree->parent, oldparent, sizeof(int) * tree->totchannel);
172                         MEM_freeN(oldchan);
173                         MEM_freeN(oldparent);
174
175                         /* add new pose channels at the end, in reverse order */
176                         for (a = 0; a < segcount; a++) {
177                                 tree->pchan[tree->totchannel + a] = chanlist[segcount - a - 1];
178                                 tree->parent[tree->totchannel + a] = tree->totchannel + a - 1;
179                         }
180                         tree->parent[tree->totchannel] = parent;
181
182                         tree->totchannel = newsize;
183                 }
184
185                 /* move tree to end of list, for correct evaluation order */
186                 BLI_remlink(&pchan_root->iktree, tree);
187                 BLI_addtail(&pchan_root->iktree, tree);
188         }
189
190         /* add target to the tree */
191         BLI_addtail(&tree->targets, target);
192         /* mark root channel having an IK tree */
193         pchan_root->flag |= POSE_IKTREE;
194 }
195
196
197 /* transform from bone(b) to bone(b+1), store in chan_mat */
198 static void make_dmats(bPoseChannel *pchan)
199 {
200         if (pchan->parent) {
201                 float iR_parmat[4][4];
202                 invert_m4_m4(iR_parmat, pchan->parent->pose_mat);
203                 mul_m4_m4m4(pchan->chan_mat, iR_parmat,  pchan->pose_mat); // delta mat
204         }
205         else {
206                 copy_m4_m4(pchan->chan_mat, pchan->pose_mat);
207         }
208 }
209
210 /* applies IK matrix to pchan, IK is done separated */
211 /* formula: pose_mat(b) = pose_mat(b-1) * diffmat(b-1, b) * ik_mat(b) */
212 /* to make this work, the diffmats have to be precalculated! Stored in chan_mat */
213 static void where_is_ik_bone(bPoseChannel *pchan, float ik_mat[3][3])   // nr = to detect if this is first bone
214 {
215         float vec[3], ikmat[4][4];
216
217         copy_m4_m3(ikmat, ik_mat);
218
219         if (pchan->parent)
220                 mul_m4_series(pchan->pose_mat, pchan->parent->pose_mat, pchan->chan_mat, ikmat);
221         else
222                 mul_m4_m4m4(pchan->pose_mat, pchan->chan_mat, ikmat);
223
224         /* calculate head */
225         copy_v3_v3(pchan->pose_head, pchan->pose_mat[3]);
226         /* calculate tail */
227         copy_v3_v3(vec, pchan->pose_mat[1]);
228         mul_v3_fl(vec, pchan->bone->length);
229         add_v3_v3v3(pchan->pose_tail, pchan->pose_head, vec);
230
231         pchan->flag |= POSE_DONE;
232 }
233
234
235 /* called from within the core BKE_pose_where_is loop, all animsystems and constraints
236  * were executed & assigned. Now as last we do an IK pass */
237 static void execute_posetree(struct Scene *scene, Object *ob, PoseTree *tree)
238 {
239         float R_parmat[3][3], identity[3][3];
240         float iR_parmat[3][3];
241         float R_bonemat[3][3];
242         float goalrot[3][3], goalpos[3];
243         float rootmat[4][4], imat[4][4];
244         float goal[4][4], goalinv[4][4];
245         float irest_basis[3][3], full_basis[3][3];
246         float end_pose[4][4], world_pose[4][4];
247         float basis[3][3], rest_basis[3][3], start[3], *ikstretch = NULL;
248         float resultinf = 0.0f;
249         int a, flag, hasstretch = 0, resultblend = 0;
250         bPoseChannel *pchan;
251         IK_Segment *seg, *parent, **iktree, *iktarget;
252         IK_Solver *solver;
253         PoseTarget *target;
254         bKinematicConstraint *data, *poleangledata = NULL;
255         Bone *bone;
256
257         if (tree->totchannel == 0)
258                 return;
259
260         iktree = MEM_mallocN(sizeof(void *) * tree->totchannel, "ik tree");
261
262         for (a = 0; a < tree->totchannel; a++) {
263                 float length;
264                 pchan = tree->pchan[a];
265                 bone = pchan->bone;
266
267                 /* set DoF flag */
268                 flag = 0;
269                 if (!(pchan->ikflag & BONE_IK_NO_XDOF) && !(pchan->ikflag & BONE_IK_NO_XDOF_TEMP))
270                         flag |= IK_XDOF;
271                 if (!(pchan->ikflag & BONE_IK_NO_YDOF) && !(pchan->ikflag & BONE_IK_NO_YDOF_TEMP))
272                         flag |= IK_YDOF;
273                 if (!(pchan->ikflag & BONE_IK_NO_ZDOF) && !(pchan->ikflag & BONE_IK_NO_ZDOF_TEMP))
274                         flag |= IK_ZDOF;
275
276                 if (tree->stretch && (pchan->ikstretch > 0.0f)) {
277                         flag |= IK_TRANS_YDOF;
278                         hasstretch = 1;
279                 }
280
281                 seg = iktree[a] = IK_CreateSegment(flag);
282
283                 /* find parent */
284                 if (a == 0)
285                         parent = NULL;
286                 else
287                         parent = iktree[tree->parent[a]];
288
289                 IK_SetParent(seg, parent);
290
291                 /* get the matrix that transforms from prevbone into this bone */
292                 copy_m3_m4(R_bonemat, pchan->pose_mat);
293
294                 /* gather transformations for this IK segment */
295
296                 if (pchan->parent)
297                         copy_m3_m4(R_parmat, pchan->parent->pose_mat);
298                 else
299                         unit_m3(R_parmat);
300
301                 /* bone offset */
302                 if (pchan->parent && (a > 0))
303                         sub_v3_v3v3(start, pchan->pose_head, pchan->parent->pose_tail);
304                 else
305                         /* only root bone (a = 0) has no parent */
306                         start[0] = start[1] = start[2] = 0.0f;
307
308                 /* change length based on bone size */
309                 length = bone->length * len_v3(R_bonemat[1]);
310
311                 /* compute rest basis and its inverse */
312                 copy_m3_m3(rest_basis, bone->bone_mat);
313                 transpose_m3_m3(irest_basis, bone->bone_mat);
314
315                 /* compute basis with rest_basis removed */
316                 invert_m3_m3(iR_parmat, R_parmat);
317                 mul_m3_m3m3(full_basis, iR_parmat, R_bonemat);
318                 mul_m3_m3m3(basis, irest_basis, full_basis);
319
320                 /* basis must be pure rotation */
321                 normalize_m3(basis);
322
323                 /* transform offset into local bone space */
324                 normalize_m3(iR_parmat);
325                 mul_m3_v3(iR_parmat, start);
326
327                 IK_SetTransform(seg, start, rest_basis, basis, length);
328
329                 if (pchan->ikflag & BONE_IK_XLIMIT)
330                         IK_SetLimit(seg, IK_X, pchan->limitmin[0], pchan->limitmax[0]);
331                 if (pchan->ikflag & BONE_IK_YLIMIT)
332                         IK_SetLimit(seg, IK_Y, pchan->limitmin[1], pchan->limitmax[1]);
333                 if (pchan->ikflag & BONE_IK_ZLIMIT)
334                         IK_SetLimit(seg, IK_Z, pchan->limitmin[2], pchan->limitmax[2]);
335
336                 IK_SetStiffness(seg, IK_X, pchan->stiffness[0]);
337                 IK_SetStiffness(seg, IK_Y, pchan->stiffness[1]);
338                 IK_SetStiffness(seg, IK_Z, pchan->stiffness[2]);
339
340                 if (tree->stretch && (pchan->ikstretch > 0.0f)) {
341                         const float ikstretch_sq = SQUARE(pchan->ikstretch);
342                         /* this function does its own clamping */
343                         IK_SetStiffness(seg, IK_TRANS_Y, 1.0f - ikstretch_sq);
344                         IK_SetLimit(seg, IK_TRANS_Y, IK_STRETCH_STIFF_MIN, IK_STRETCH_STIFF_MAX);
345                 }
346         }
347
348         solver = IK_CreateSolver(iktree[0]);
349
350         /* set solver goals */
351
352         /* first set the goal inverse transform, assuming the root of tree was done ok! */
353         pchan = tree->pchan[0];
354         if (pchan->parent) {
355                 /* transform goal by parent mat, so this rotation is not part of the
356                  * segment's basis. otherwise rotation limits do not work on the
357                  * local transform of the segment itself. */
358                 copy_m4_m4(rootmat, pchan->parent->pose_mat);
359                 /* However, we do not want to get (i.e. reverse) parent's scale, as it generates [#31008]
360                  * kind of nasty bugs... */
361                 normalize_m4(rootmat);
362         }
363         else
364                 unit_m4(rootmat);
365         copy_v3_v3(rootmat[3], pchan->pose_head);
366
367         mul_m4_m4m4(imat, ob->obmat, rootmat);
368         invert_m4_m4(goalinv, imat);
369
370         for (target = tree->targets.first; target; target = target->next) {
371                 float polepos[3];
372                 int poleconstrain = 0;
373
374                 data = (bKinematicConstraint *)target->con->data;
375
376                 /* 1.0=ctime, we pass on object for auto-ik (owner-type here is object, even though
377                  * strictly speaking, it is a posechannel)
378                  */
379                 BKE_constraint_target_matrix_get(scene, target->con, 0, CONSTRAINT_OBTYPE_OBJECT, ob, rootmat, 1.0);
380
381                 /* and set and transform goal */
382                 mul_m4_m4m4(goal, goalinv, rootmat);
383
384                 copy_v3_v3(goalpos, goal[3]);
385                 copy_m3_m4(goalrot, goal);
386                 normalize_m3(goalrot);
387
388                 /* same for pole vector target */
389                 if (data->poletar) {
390                         BKE_constraint_target_matrix_get(scene, target->con, 1, CONSTRAINT_OBTYPE_OBJECT, ob, rootmat, 1.0);
391
392                         if (data->flag & CONSTRAINT_IK_SETANGLE) {
393                                 /* don't solve IK when we are setting the pole angle */
394                                 break;
395                         }
396                         else {
397                                 mul_m4_m4m4(goal, goalinv, rootmat);
398                                 copy_v3_v3(polepos, goal[3]);
399                                 poleconstrain = 1;
400
401                                 /* for pole targets, we blend the result of the ik solver
402                                  * instead of the target position, otherwise we can't get
403                                  * a smooth transition */
404                                 resultblend = 1;
405                                 resultinf = target->con->enforce;
406
407                                 if (data->flag & CONSTRAINT_IK_GETANGLE) {
408                                         poleangledata = data;
409                                         data->flag &= ~CONSTRAINT_IK_GETANGLE;
410                                 }
411                         }
412                 }
413
414                 /* do we need blending? */
415                 if (!resultblend && target->con->enforce != 1.0f) {
416                         float q1[4], q2[4], q[4];
417                         float fac = target->con->enforce;
418                         float mfac = 1.0f - fac;
419
420                         pchan = tree->pchan[target->tip];
421
422                         /* end effector in world space */
423                         copy_m4_m4(end_pose, pchan->pose_mat);
424                         copy_v3_v3(end_pose[3], pchan->pose_tail);
425                         mul_m4_series(world_pose, goalinv, ob->obmat, end_pose);
426
427                         /* blend position */
428                         goalpos[0] = fac * goalpos[0] + mfac * world_pose[3][0];
429                         goalpos[1] = fac * goalpos[1] + mfac * world_pose[3][1];
430                         goalpos[2] = fac * goalpos[2] + mfac * world_pose[3][2];
431
432                         /* blend rotation */
433                         mat3_to_quat(q1, goalrot);
434                         mat4_to_quat(q2, world_pose);
435                         interp_qt_qtqt(q, q1, q2, mfac);
436                         quat_to_mat3(goalrot, q);
437                 }
438
439                 iktarget = iktree[target->tip];
440
441                 if ((data->flag & CONSTRAINT_IK_POS) && data->weight != 0.0f) {
442                         if (poleconstrain)
443                                 IK_SolverSetPoleVectorConstraint(solver, iktarget, goalpos,
444                                                                  polepos, data->poleangle, (poleangledata == data));
445                         IK_SolverAddGoal(solver, iktarget, goalpos, data->weight);
446                 }
447                 if ((data->flag & CONSTRAINT_IK_ROT) && (data->orientweight != 0.0f))
448                         if ((data->flag & CONSTRAINT_IK_AUTO) == 0)
449                                 IK_SolverAddGoalOrientation(solver, iktarget, goalrot,
450                                                             data->orientweight);
451         }
452
453         /* solve */
454         IK_Solve(solver, 0.0f, tree->iterations);
455
456         if (poleangledata)
457                 poleangledata->poleangle = IK_SolverGetPoleAngle(solver);
458
459         IK_FreeSolver(solver);
460
461         /* gather basis changes */
462         tree->basis_change = MEM_mallocN(sizeof(float[3][3]) * tree->totchannel, "ik basis change");
463         if (hasstretch)
464                 ikstretch = MEM_mallocN(sizeof(float) * tree->totchannel, "ik stretch");
465
466         for (a = 0; a < tree->totchannel; a++) {
467                 IK_GetBasisChange(iktree[a], tree->basis_change[a]);
468
469                 if (hasstretch) {
470                         /* have to compensate for scaling received from parent */
471                         float parentstretch, stretch;
472
473                         pchan = tree->pchan[a];
474                         parentstretch = (tree->parent[a] >= 0) ? ikstretch[tree->parent[a]] : 1.0f;
475
476                         if (tree->stretch && (pchan->ikstretch > 0.0f)) {
477                                 float trans[3], length;
478
479                                 IK_GetTranslationChange(iktree[a], trans);
480                                 length = pchan->bone->length * len_v3(pchan->pose_mat[1]);
481
482                                 ikstretch[a] = (length == 0.0f) ? 1.0f : (trans[1] + length) / length;
483                         }
484                         else
485                                 ikstretch[a] = 1.0;
486
487                         stretch = (parentstretch == 0.0f) ? 1.0f : ikstretch[a] / parentstretch;
488
489                         mul_v3_fl(tree->basis_change[a][0], stretch);
490                         mul_v3_fl(tree->basis_change[a][1], stretch);
491                         mul_v3_fl(tree->basis_change[a][2], stretch);
492                 }
493
494                 if (resultblend && resultinf != 1.0f) {
495                         unit_m3(identity);
496                         blend_m3_m3m3(tree->basis_change[a], identity,
497                                       tree->basis_change[a], resultinf);
498                 }
499
500                 IK_FreeSegment(iktree[a]);
501         }
502
503         MEM_freeN(iktree);
504         if (ikstretch) MEM_freeN(ikstretch);
505 }
506
507 static void free_posetree(PoseTree *tree)
508 {
509         BLI_freelistN(&tree->targets);
510         if (tree->pchan) MEM_freeN(tree->pchan);
511         if (tree->parent) MEM_freeN(tree->parent);
512         if (tree->basis_change) MEM_freeN(tree->basis_change);
513         MEM_freeN(tree);
514 }
515
516 ///----------------------------------------
517 /// Plugin API for legacy iksolver
518
519 void iksolver_initialize_tree(struct Scene *UNUSED(scene), struct Object *ob, float UNUSED(ctime))
520 {
521         bPoseChannel *pchan;
522
523         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
524                 if (pchan->constflag & PCHAN_HAS_IK) // flag is set on editing constraints
525                         initialize_posetree(ob, pchan);  // will attach it to root!
526         }
527         ob->pose->flag &= ~POSE_WAS_REBUILT;
528 }
529
530 void iksolver_execute_tree(struct Scene *scene, Object *ob,  bPoseChannel *pchan_root, float ctime)
531 {
532         while (pchan_root->iktree.first) {
533                 PoseTree *tree = pchan_root->iktree.first;
534                 int a;
535
536                 /* stop on the first tree that isn't a standard IK chain */
537                 if (tree->type != CONSTRAINT_TYPE_KINEMATIC)
538                         return;
539
540                 /* 4. walk over the tree for regular solving */
541                 for (a = 0; a < tree->totchannel; a++) {
542                         if (!(tree->pchan[a]->flag & POSE_DONE))    // successive trees can set the flag
543                                 BKE_pose_where_is_bone(scene, ob, tree->pchan[a], ctime, 1);
544                         /* tell blender that this channel was controlled by IK, it's cleared on each BKE_pose_where_is() */
545                         tree->pchan[a]->flag |= POSE_CHAIN;
546                 }
547
548 #ifdef USE_NONUNIFORM_SCALE
549                 float (*pchan_scale_data)[3] = MEM_mallocN(sizeof(float[3]) * tree->totchannel, __func__);
550
551                 for (a = 0; a < tree->totchannel; a++) {
552                         mat4_to_size(pchan_scale_data[a], tree->pchan[a]->pose_mat);
553
554                         /* make uniform at y scale since this controls the length */
555                         normalize_v3_length(tree->pchan[a]->pose_mat[0], pchan_scale_data[a][1]);
556                         normalize_v3_length(tree->pchan[a]->pose_mat[2], pchan_scale_data[a][1]);
557                 }
558 #endif
559
560                 /* 5. execute the IK solver */
561                 execute_posetree(scene, ob, tree);
562
563                 /* 6. apply the differences to the channels,
564                  *    we need to calculate the original differences first */
565                 for (a = 0; a < tree->totchannel; a++) {
566                         make_dmats(tree->pchan[a]);
567                 }
568
569                 for (a = 0; a < tree->totchannel; a++) {
570                         /* sets POSE_DONE */
571                         where_is_ik_bone(tree->pchan[a], tree->basis_change[a]);
572                 }
573
574 #ifdef USE_NONUNIFORM_SCALE
575                 for (a = 0; a < tree->totchannel; a++) {
576                         normalize_v3_length(tree->pchan[a]->pose_mat[0], pchan_scale_data[a][0]);
577                         normalize_v3_length(tree->pchan[a]->pose_mat[2], pchan_scale_data[a][2]);
578                 }
579                 MEM_freeN(pchan_scale_data);
580 #endif
581
582                 /* 7. and free */
583                 BLI_remlink(&pchan_root->iktree, tree);
584                 free_posetree(tree);
585         }
586 }
587