Pass EvaluationContext argument everywhere
[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_m4m4(pchan->pose_mat, pchan->parent->pose_mat, pchan->chan_mat);
221         else
222                 copy_m4_m4(pchan->pose_mat, pchan->chan_mat);
223
224 #ifdef USE_NONUNIFORM_SCALE
225         /* apply IK mat, but as if the bones have uniform scale since the IK solver
226          * is not aware of non-uniform scale */
227         float scale[3];
228         mat4_to_size(scale, pchan->pose_mat);
229         normalize_v3_length(pchan->pose_mat[0], scale[1]);
230         normalize_v3_length(pchan->pose_mat[2], scale[1]);
231 #endif
232
233         mul_m4_m4m4(pchan->pose_mat, pchan->pose_mat, ikmat);
234
235 #ifdef USE_NONUNIFORM_SCALE
236         float ik_scale[3];
237         mat3_to_size(ik_scale, ik_mat);
238         normalize_v3_length(pchan->pose_mat[0], scale[0] * ik_scale[0]);
239         normalize_v3_length(pchan->pose_mat[2], scale[2] * ik_scale[2]);
240 #endif
241
242         /* calculate head */
243         copy_v3_v3(pchan->pose_head, pchan->pose_mat[3]);
244         /* calculate tail */
245         copy_v3_v3(vec, pchan->pose_mat[1]);
246         mul_v3_fl(vec, pchan->bone->length);
247         add_v3_v3v3(pchan->pose_tail, pchan->pose_head, vec);
248
249         pchan->flag |= POSE_DONE;
250 }
251
252
253 /* called from within the core BKE_pose_where_is loop, all animsystems and constraints
254  * were executed & assigned. Now as last we do an IK pass */
255 static void execute_posetree(struct EvaluationContext *eval_ctx, struct Scene *scene, Object *ob, PoseTree *tree)
256 {
257         float R_parmat[3][3], identity[3][3];
258         float iR_parmat[3][3];
259         float R_bonemat[3][3];
260         float goalrot[3][3], goalpos[3];
261         float rootmat[4][4], imat[4][4];
262         float goal[4][4], goalinv[4][4];
263         float irest_basis[3][3], full_basis[3][3];
264         float end_pose[4][4], world_pose[4][4];
265         float basis[3][3], rest_basis[3][3], start[3], *ikstretch = NULL;
266         float resultinf = 0.0f;
267         int a, flag, hasstretch = 0, resultblend = 0;
268         bPoseChannel *pchan;
269         IK_Segment *seg, *parent, **iktree, *iktarget;
270         IK_Solver *solver;
271         PoseTarget *target;
272         bKinematicConstraint *data, *poleangledata = NULL;
273         Bone *bone;
274
275         if (tree->totchannel == 0)
276                 return;
277
278         iktree = MEM_mallocN(sizeof(void *) * tree->totchannel, "ik tree");
279
280         for (a = 0; a < tree->totchannel; a++) {
281                 float length;
282                 pchan = tree->pchan[a];
283                 bone = pchan->bone;
284
285                 /* set DoF flag */
286                 flag = 0;
287                 if (!(pchan->ikflag & BONE_IK_NO_XDOF) && !(pchan->ikflag & BONE_IK_NO_XDOF_TEMP))
288                         flag |= IK_XDOF;
289                 if (!(pchan->ikflag & BONE_IK_NO_YDOF) && !(pchan->ikflag & BONE_IK_NO_YDOF_TEMP))
290                         flag |= IK_YDOF;
291                 if (!(pchan->ikflag & BONE_IK_NO_ZDOF) && !(pchan->ikflag & BONE_IK_NO_ZDOF_TEMP))
292                         flag |= IK_ZDOF;
293
294                 if (tree->stretch && (pchan->ikstretch > 0.0f)) {
295                         flag |= IK_TRANS_YDOF;
296                         hasstretch = 1;
297                 }
298
299                 seg = iktree[a] = IK_CreateSegment(flag);
300
301                 /* find parent */
302                 if (a == 0)
303                         parent = NULL;
304                 else
305                         parent = iktree[tree->parent[a]];
306
307                 IK_SetParent(seg, parent);
308
309                 /* get the matrix that transforms from prevbone into this bone */
310                 copy_m3_m4(R_bonemat, pchan->pose_mat);
311
312                 /* gather transformations for this IK segment */
313
314                 if (pchan->parent)
315                         copy_m3_m4(R_parmat, pchan->parent->pose_mat);
316                 else
317                         unit_m3(R_parmat);
318
319                 /* bone offset */
320                 if (pchan->parent && (a > 0))
321                         sub_v3_v3v3(start, pchan->pose_head, pchan->parent->pose_tail);
322                 else
323                         /* only root bone (a = 0) has no parent */
324                         start[0] = start[1] = start[2] = 0.0f;
325
326                 /* change length based on bone size */
327                 length = bone->length * len_v3(R_bonemat[1]);
328
329                 /* basis must be pure rotation */
330                 normalize_m3(R_bonemat);
331                 normalize_m3(R_parmat);
332
333                 /* compute rest basis and its inverse */
334                 copy_m3_m3(rest_basis, bone->bone_mat);
335                 transpose_m3_m3(irest_basis, bone->bone_mat);
336
337                 /* compute basis with rest_basis removed */
338                 invert_m3_m3(iR_parmat, R_parmat);
339                 mul_m3_m3m3(full_basis, iR_parmat, R_bonemat);
340                 mul_m3_m3m3(basis, irest_basis, full_basis);
341
342                 /* transform offset into local bone space */
343                 mul_m3_v3(iR_parmat, start);
344
345                 IK_SetTransform(seg, start, rest_basis, basis, length);
346
347                 if (pchan->ikflag & BONE_IK_XLIMIT)
348                         IK_SetLimit(seg, IK_X, pchan->limitmin[0], pchan->limitmax[0]);
349                 if (pchan->ikflag & BONE_IK_YLIMIT)
350                         IK_SetLimit(seg, IK_Y, pchan->limitmin[1], pchan->limitmax[1]);
351                 if (pchan->ikflag & BONE_IK_ZLIMIT)
352                         IK_SetLimit(seg, IK_Z, pchan->limitmin[2], pchan->limitmax[2]);
353
354                 IK_SetStiffness(seg, IK_X, pchan->stiffness[0]);
355                 IK_SetStiffness(seg, IK_Y, pchan->stiffness[1]);
356                 IK_SetStiffness(seg, IK_Z, pchan->stiffness[2]);
357
358                 if (tree->stretch && (pchan->ikstretch > 0.0f)) {
359                         const float ikstretch_sq = SQUARE(pchan->ikstretch);
360                         /* this function does its own clamping */
361                         IK_SetStiffness(seg, IK_TRANS_Y, 1.0f - ikstretch_sq);
362                         IK_SetLimit(seg, IK_TRANS_Y, IK_STRETCH_STIFF_MIN, IK_STRETCH_STIFF_MAX);
363                 }
364         }
365
366         solver = IK_CreateSolver(iktree[0]);
367
368         /* set solver goals */
369
370         /* first set the goal inverse transform, assuming the root of tree was done ok! */
371         pchan = tree->pchan[0];
372         if (pchan->parent) {
373                 /* transform goal by parent mat, so this rotation is not part of the
374                  * segment's basis. otherwise rotation limits do not work on the
375                  * local transform of the segment itself. */
376                 copy_m4_m4(rootmat, pchan->parent->pose_mat);
377                 /* However, we do not want to get (i.e. reverse) parent's scale, as it generates [#31008]
378                  * kind of nasty bugs... */
379                 normalize_m4(rootmat);
380         }
381         else
382                 unit_m4(rootmat);
383         copy_v3_v3(rootmat[3], pchan->pose_head);
384
385         mul_m4_m4m4(imat, ob->obmat, rootmat);
386         invert_m4_m4(goalinv, imat);
387
388         for (target = tree->targets.first; target; target = target->next) {
389                 float polepos[3];
390                 int poleconstrain = 0;
391
392                 data = (bKinematicConstraint *)target->con->data;
393
394                 /* 1.0=ctime, we pass on object for auto-ik (owner-type here is object, even though
395                  * strictly speaking, it is a posechannel)
396                  */
397                 BKE_constraint_target_matrix_get(eval_ctx, scene, target->con, 0, CONSTRAINT_OBTYPE_OBJECT, ob, rootmat, 1.0);
398
399                 /* and set and transform goal */
400                 mul_m4_m4m4(goal, goalinv, rootmat);
401
402                 copy_v3_v3(goalpos, goal[3]);
403                 copy_m3_m4(goalrot, goal);
404                 normalize_m3(goalrot);
405
406                 /* same for pole vector target */
407                 if (data->poletar) {
408                         BKE_constraint_target_matrix_get(eval_ctx, scene, target->con, 1, CONSTRAINT_OBTYPE_OBJECT, ob, rootmat, 1.0);
409
410                         if (data->flag & CONSTRAINT_IK_SETANGLE) {
411                                 /* don't solve IK when we are setting the pole angle */
412                                 break;
413                         }
414                         else {
415                                 mul_m4_m4m4(goal, goalinv, rootmat);
416                                 copy_v3_v3(polepos, goal[3]);
417                                 poleconstrain = 1;
418
419                                 /* for pole targets, we blend the result of the ik solver
420                                  * instead of the target position, otherwise we can't get
421                                  * a smooth transition */
422                                 resultblend = 1;
423                                 resultinf = target->con->enforce;
424
425                                 if (data->flag & CONSTRAINT_IK_GETANGLE) {
426                                         poleangledata = data;
427                                         data->flag &= ~CONSTRAINT_IK_GETANGLE;
428                                 }
429                         }
430                 }
431
432                 /* do we need blending? */
433                 if (!resultblend && target->con->enforce != 1.0f) {
434                         float q1[4], q2[4], q[4];
435                         float fac = target->con->enforce;
436                         float mfac = 1.0f - fac;
437
438                         pchan = tree->pchan[target->tip];
439
440                         /* end effector in world space */
441                         copy_m4_m4(end_pose, pchan->pose_mat);
442                         copy_v3_v3(end_pose[3], pchan->pose_tail);
443                         mul_m4_series(world_pose, goalinv, ob->obmat, end_pose);
444
445                         /* blend position */
446                         goalpos[0] = fac * goalpos[0] + mfac * world_pose[3][0];
447                         goalpos[1] = fac * goalpos[1] + mfac * world_pose[3][1];
448                         goalpos[2] = fac * goalpos[2] + mfac * world_pose[3][2];
449
450                         /* blend rotation */
451                         mat3_to_quat(q1, goalrot);
452                         mat4_to_quat(q2, world_pose);
453                         interp_qt_qtqt(q, q1, q2, mfac);
454                         quat_to_mat3(goalrot, q);
455                 }
456
457                 iktarget = iktree[target->tip];
458
459                 if ((data->flag & CONSTRAINT_IK_POS) && data->weight != 0.0f) {
460                         if (poleconstrain)
461                                 IK_SolverSetPoleVectorConstraint(solver, iktarget, goalpos,
462                                                                  polepos, data->poleangle, (poleangledata == data));
463                         IK_SolverAddGoal(solver, iktarget, goalpos, data->weight);
464                 }
465                 if ((data->flag & CONSTRAINT_IK_ROT) && (data->orientweight != 0.0f))
466                         if ((data->flag & CONSTRAINT_IK_AUTO) == 0)
467                                 IK_SolverAddGoalOrientation(solver, iktarget, goalrot,
468                                                             data->orientweight);
469         }
470
471         /* solve */
472         IK_Solve(solver, 0.0f, tree->iterations);
473
474         if (poleangledata)
475                 poleangledata->poleangle = IK_SolverGetPoleAngle(solver);
476
477         IK_FreeSolver(solver);
478
479         /* gather basis changes */
480         tree->basis_change = MEM_mallocN(sizeof(float[3][3]) * tree->totchannel, "ik basis change");
481         if (hasstretch)
482                 ikstretch = MEM_mallocN(sizeof(float) * tree->totchannel, "ik stretch");
483
484         for (a = 0; a < tree->totchannel; a++) {
485                 IK_GetBasisChange(iktree[a], tree->basis_change[a]);
486
487                 if (hasstretch) {
488                         /* have to compensate for scaling received from parent */
489                         float parentstretch, stretch;
490
491                         pchan = tree->pchan[a];
492                         parentstretch = (tree->parent[a] >= 0) ? ikstretch[tree->parent[a]] : 1.0f;
493
494                         if (tree->stretch && (pchan->ikstretch > 0.0f)) {
495                                 float trans[3], length;
496
497                                 IK_GetTranslationChange(iktree[a], trans);
498                                 length = pchan->bone->length * len_v3(pchan->pose_mat[1]);
499
500                                 ikstretch[a] = (length == 0.0f) ? 1.0f : (trans[1] + length) / length;
501                         }
502                         else
503                                 ikstretch[a] = 1.0;
504
505                         stretch = (parentstretch == 0.0f) ? 1.0f : ikstretch[a] / parentstretch;
506
507                         mul_v3_fl(tree->basis_change[a][0], stretch);
508                         mul_v3_fl(tree->basis_change[a][1], stretch);
509                         mul_v3_fl(tree->basis_change[a][2], stretch);
510                 }
511
512                 if (resultblend && resultinf != 1.0f) {
513                         unit_m3(identity);
514                         blend_m3_m3m3(tree->basis_change[a], identity,
515                                       tree->basis_change[a], resultinf);
516                 }
517
518                 IK_FreeSegment(iktree[a]);
519         }
520
521         MEM_freeN(iktree);
522         if (ikstretch) MEM_freeN(ikstretch);
523 }
524
525 static void free_posetree(PoseTree *tree)
526 {
527         BLI_freelistN(&tree->targets);
528         if (tree->pchan) MEM_freeN(tree->pchan);
529         if (tree->parent) MEM_freeN(tree->parent);
530         if (tree->basis_change) MEM_freeN(tree->basis_change);
531         MEM_freeN(tree);
532 }
533
534 ///----------------------------------------
535 /// Plugin API for legacy iksolver
536
537 void iksolver_initialize_tree(struct EvaluationContext *UNUSED(eval_ctx), struct Scene *UNUSED(scene), struct Object *ob, float UNUSED(ctime))
538 {
539         bPoseChannel *pchan;
540
541         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
542                 if (pchan->constflag & PCHAN_HAS_IK) // flag is set on editing constraints
543                         initialize_posetree(ob, pchan);  // will attach it to root!
544         }
545         ob->pose->flag &= ~POSE_WAS_REBUILT;
546 }
547
548 void iksolver_execute_tree(struct EvaluationContext *eval_ctx, struct Scene *scene, Object *ob,  bPoseChannel *pchan_root, float ctime)
549 {
550         while (pchan_root->iktree.first) {
551                 PoseTree *tree = pchan_root->iktree.first;
552                 int a;
553
554                 /* stop on the first tree that isn't a standard IK chain */
555                 if (tree->type != CONSTRAINT_TYPE_KINEMATIC)
556                         return;
557
558                 /* 4. walk over the tree for regular solving */
559                 for (a = 0; a < tree->totchannel; a++) {
560                         if (!(tree->pchan[a]->flag & POSE_DONE))    // successive trees can set the flag
561                                 BKE_pose_where_is_bone(eval_ctx, scene, ob, tree->pchan[a], ctime, 1);
562                         /* tell blender that this channel was controlled by IK, it's cleared on each BKE_pose_where_is() */
563                         tree->pchan[a]->flag |= POSE_CHAIN;
564                 }
565
566                 /* 5. execute the IK solver */
567                 execute_posetree(eval_ctx, scene, ob, tree);
568
569                 /* 6. apply the differences to the channels,
570                  *    we need to calculate the original differences first */
571                 for (a = 0; a < tree->totchannel; a++) {
572                         make_dmats(tree->pchan[a]);
573                 }
574
575                 for (a = 0; a < tree->totchannel; a++) {
576                         /* sets POSE_DONE */
577                         where_is_ik_bone(tree->pchan[a], tree->basis_change[a]);
578                 }
579
580                 /* 7. and free */
581                 BLI_remlink(&pchan_root->iktree, tree);
582                 free_posetree(tree);
583         }
584 }
585