Cleanup: spelling
[blender.git] / source / blender / blenkernel / intern / armature_update.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) 2015 Blender Foundation.
19  * All rights reserved.
20  *
21  * Original Author: Joshua Leung
22  * Contributor(s): None Yet
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  *
26  * Defines and code for core node types
27  */
28
29 #include "MEM_guardedalloc.h"
30
31 #include "BLI_utildefines.h"
32 #include "BLI_listbase.h"
33 #include "BLI_math.h"
34
35 #include "DNA_armature_types.h"
36 #include "DNA_constraint_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_scene_types.h"
39
40 #include "BKE_action.h"
41 #include "BKE_anim.h"
42 #include "BKE_armature.h"
43 #include "BKE_curve.h"
44 #include "BKE_depsgraph.h"
45 #include "BKE_displist.h"
46 #include "BKE_fcurve.h"
47 #include "BKE_scene.h"
48
49 #include "BIK_api.h"
50
51 #include "BKE_global.h"
52 #include "BKE_main.h"
53
54 #include "DEG_depsgraph.h"
55
56 #ifdef WITH_LEGACY_DEPSGRAPH
57 #  define DEBUG_PRINT if (!DEG_depsgraph_use_legacy() && G.debug & G_DEBUG_DEPSGRAPH) printf
58 #else
59 #  define DEBUG_PRINT if (G.debug & G_DEBUG_DEPSGRAPH) printf
60 #endif
61
62 /* ********************** SPLINE IK SOLVER ******************* */
63
64 /* Temporary evaluation tree data used for Spline IK */
65 typedef struct tSplineIK_Tree {
66         struct tSplineIK_Tree *next, *prev;
67
68         int type;                    /* type of IK that this serves (CONSTRAINT_TYPE_KINEMATIC or ..._SPLINEIK) */
69
70         bool free_points;            /* free the point positions array */
71         short chainlen;              /* number of bones in the chain */
72
73         float *points;               /* parametric positions for the joints along the curve */
74         bPoseChannel **chain;        /* chain of bones to affect using Spline IK (ordered from the tip) */
75
76         bPoseChannel *root;          /* bone that is the root node of the chain */
77
78         bConstraint *con;            /* constraint for this chain */
79         bSplineIKConstraint *ikData; /* constraint settings for this chain */
80 } tSplineIK_Tree;
81
82 /* ----------- */
83
84 /* Tag the bones in the chain formed by the given bone for IK */
85 static void splineik_init_tree_from_pchan(Scene *scene, Object *UNUSED(ob), bPoseChannel *pchan_tip)
86 {
87         bPoseChannel *pchan, *pchanRoot = NULL;
88         bPoseChannel *pchanChain[255];
89         bConstraint *con = NULL;
90         bSplineIKConstraint *ikData = NULL;
91         float boneLengths[255], *jointPoints;
92         float totLength = 0.0f;
93         bool free_joints = 0;
94         int segcount = 0;
95
96         /* find the SplineIK constraint */
97         for (con = pchan_tip->constraints.first; con; con = con->next) {
98                 if (con->type == CONSTRAINT_TYPE_SPLINEIK) {
99                         ikData = con->data;
100
101                         /* target can only be curve */
102                         if ((ikData->tar == NULL) || (ikData->tar->type != OB_CURVE))
103                                 continue;
104                         /* skip if disabled */
105                         if ((con->enforce == 0.0f) || (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)))
106                                 continue;
107
108                         /* otherwise, constraint is ok... */
109                         break;
110                 }
111         }
112         if (con == NULL)
113                 return;
114
115         /* make sure that the constraint targets are ok
116          *     - this is a workaround for a depsgraph bug...
117          */
118         if (ikData->tar) {
119                 /* note: when creating constraints that follow path, the curve gets the CU_PATH set now,
120                  *       currently for paths to work it needs to go through the bevlist/displist system (ton)
121                  */
122
123                 /* only happens on reload file, but violates depsgraph still... fix! */
124                 if (ELEM(NULL,  ikData->tar->curve_cache, ikData->tar->curve_cache->path, ikData->tar->curve_cache->path->data)) {
125                         BKE_displist_make_curveTypes(scene, ikData->tar, 0);
126                         
127                         /* path building may fail in EditMode after removing verts [#33268]*/
128                         if (ELEM(NULL, ikData->tar->curve_cache->path, ikData->tar->curve_cache->path->data)) {
129                                 /* BLI_assert(cu->path != NULL); */
130                                 return;
131                         }
132                 }
133         }
134
135         /* find the root bone and the chain of bones from the root to the tip
136          * NOTE: this assumes that the bones are connected, but that may not be true... */
137         for (pchan = pchan_tip; pchan && (segcount < ikData->chainlen); pchan = pchan->parent, segcount++) {
138                 /* store this segment in the chain */
139                 pchanChain[segcount] = pchan;
140
141                 /* if performing rebinding, calculate the length of the bone */
142                 boneLengths[segcount] = pchan->bone->length;
143                 totLength += boneLengths[segcount];
144         }
145
146         if (segcount == 0)
147                 return;
148         else
149                 pchanRoot = pchanChain[segcount - 1];
150
151         /* perform binding step if required */
152         if ((ikData->flag & CONSTRAINT_SPLINEIK_BOUND) == 0) {
153                 float segmentLen = (1.0f / (float)segcount);
154                 int i;
155
156                 /* setup new empty array for the points list */
157                 if (ikData->points)
158                         MEM_freeN(ikData->points);
159                 ikData->numpoints = ikData->chainlen + 1;
160                 ikData->points = MEM_mallocN(sizeof(float) * ikData->numpoints, "Spline IK Binding");
161
162                 /* bind 'tip' of chain (i.e. first joint = tip of bone with the Spline IK Constraint) */
163                 ikData->points[0] = 1.0f;
164
165                 /* perform binding of the joints to parametric positions along the curve based
166                  * proportion of the total length that each bone occupies
167                  */
168                 for (i = 0; i < segcount; i++) {
169                         /* 'head' joints, traveling towards the root of the chain
170                          *  - 2 methods; the one chosen depends on whether we've got usable lengths
171                          */
172                         if ((ikData->flag & CONSTRAINT_SPLINEIK_EVENSPLITS) || (totLength == 0.0f)) {
173                                 /* 1) equi-spaced joints */
174                                 ikData->points[i + 1] = ikData->points[i] - segmentLen;
175                         }
176                         else {
177                                 /* 2) to find this point on the curve, we take a step from the previous joint
178                                  *    a distance given by the proportion that this bone takes
179                                  */
180                                 ikData->points[i + 1] = ikData->points[i] - (boneLengths[i] / totLength);
181                         }
182                 }
183
184                 /* spline has now been bound */
185                 ikData->flag |= CONSTRAINT_SPLINEIK_BOUND;
186         }
187
188         /* disallow negative values (happens with float precision) */
189         CLAMP_MIN(ikData->points[segcount], 0.0f);
190
191         /* apply corrections for sensitivity to scaling on a copy of the bind points,
192          * since it's easier to determine the positions of all the joints beforehand this way
193          */
194         if ((ikData->flag & CONSTRAINT_SPLINEIK_SCALE_LIMITED) && (totLength != 0.0f)) {
195                 float splineLen, maxScale;
196                 int i;
197
198                 /* make a copy of the points array, that we'll store in the tree
199                  *     - although we could just multiply the points on the fly, this approach means that
200                  *       we can introduce per-segment stretchiness later if it is necessary
201                  */
202                 jointPoints = MEM_dupallocN(ikData->points);
203                 free_joints = 1;
204
205                 /* get the current length of the curve */
206                 /* NOTE: this is assumed to be correct even after the curve was resized */
207                 splineLen = ikData->tar->curve_cache->path->totdist;
208
209                 /* calculate the scale factor to multiply all the path values by so that the
210                  * bone chain retains its current length, such that
211                  *     maxScale * splineLen = totLength
212                  */
213                 maxScale = totLength / splineLen;
214
215                 /* apply scaling correction to all of the temporary points */
216                 /* TODO: this is really not adequate enough on really short chains */
217                 for (i = 0; i < segcount; i++)
218                         jointPoints[i] *= maxScale;
219         }
220         else {
221                 /* just use the existing points array */
222                 jointPoints = ikData->points;
223                 free_joints = 0;
224         }
225
226         /* make a new Spline-IK chain, and store it in the IK chains */
227         /* TODO: we should check if there is already an IK chain on this, since that would take precedence... */
228         {
229                 /* make new tree */
230                 tSplineIK_Tree *tree = MEM_callocN(sizeof(tSplineIK_Tree), "SplineIK Tree");
231                 tree->type = CONSTRAINT_TYPE_SPLINEIK;
232
233                 tree->chainlen = segcount;
234
235                 /* copy over the array of links to bones in the chain (from tip to root) */
236                 tree->chain = MEM_mallocN(sizeof(bPoseChannel *) * segcount, "SplineIK Chain");
237                 memcpy(tree->chain, pchanChain, sizeof(bPoseChannel *) * segcount);
238
239                 /* store reference to joint position array */
240                 tree->points = jointPoints;
241                 tree->free_points = free_joints;
242
243                 /* store references to different parts of the chain */
244                 tree->root = pchanRoot;
245                 tree->con = con;
246                 tree->ikData = ikData;
247
248                 /* AND! link the tree to the root */
249                 BLI_addtail(&pchanRoot->siktree, tree);
250         }
251
252         /* mark root channel having an IK tree */
253         pchanRoot->flag |= POSE_IKSPLINE;
254 }
255
256 /* Tag which bones are members of Spline IK chains */
257 static void splineik_init_tree(Scene *scene, Object *ob, float UNUSED(ctime))
258 {
259         bPoseChannel *pchan;
260
261         /* find the tips of Spline IK chains, which are simply the bones which have been tagged as such */
262         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
263                 if (pchan->constflag & PCHAN_HAS_SPLINEIK)
264                         splineik_init_tree_from_pchan(scene, ob, pchan);
265         }
266 }
267
268 /* ----------- */
269
270 /* Evaluate spline IK for a given bone */
271 static void splineik_evaluate_bone(tSplineIK_Tree *tree, Scene *scene, Object *ob, bPoseChannel *pchan,
272                                    int index, float ctime)
273 {
274         bSplineIKConstraint *ikData = tree->ikData;
275         float poseHead[3], poseTail[3], poseMat[4][4];
276         float splineVec[3], scaleFac, radius = 1.0f;
277
278         /* firstly, calculate the bone matrix the standard way, since this is needed for roll control */
279         BKE_pose_where_is_bone(scene, ob, pchan, ctime, 1);
280
281         copy_v3_v3(poseHead, pchan->pose_head);
282         copy_v3_v3(poseTail, pchan->pose_tail);
283
284         /* step 1: determine the positions for the endpoints of the bone */
285         {
286                 float vec[4], dir[3], rad;
287                 float tailBlendFac = 1.0f;
288
289                 /* determine if the bone should still be affected by SplineIK */
290                 if (tree->points[index + 1] >= 1.0f) {
291                         /* spline doesn't affect the bone anymore, so done... */
292                         pchan->flag |= POSE_DONE;
293                         return;
294                 }
295                 else if ((tree->points[index] >= 1.0f) && (tree->points[index + 1] < 1.0f)) {
296                         /* blending factor depends on the amount of the bone still left on the chain */
297                         tailBlendFac = (1.0f - tree->points[index + 1]) / (tree->points[index] - tree->points[index + 1]);
298                 }
299
300                 /* tail endpoint */
301                 if (where_on_path(ikData->tar, tree->points[index], vec, dir, NULL, &rad, NULL)) {
302                         /* apply curve's object-mode transforms to the position
303                          * unless the option to allow curve to be positioned elsewhere is activated (i.e. no root)
304                          */
305                         if ((ikData->flag & CONSTRAINT_SPLINEIK_NO_ROOT) == 0)
306                                 mul_m4_v3(ikData->tar->obmat, vec);
307
308                         /* convert the position to pose-space, then store it */
309                         mul_m4_v3(ob->imat, vec);
310                         interp_v3_v3v3(poseTail, pchan->pose_tail, vec, tailBlendFac);
311
312                         /* set the new radius */
313                         radius = rad;
314                 }
315
316                 /* head endpoint */
317                 if (where_on_path(ikData->tar, tree->points[index + 1], vec, dir, NULL, &rad, NULL)) {
318                         /* apply curve's object-mode transforms to the position
319                          * unless the option to allow curve to be positioned elsewhere is activated (i.e. no root)
320                          */
321                         if ((ikData->flag & CONSTRAINT_SPLINEIK_NO_ROOT) == 0)
322                                 mul_m4_v3(ikData->tar->obmat, vec);
323
324                         /* store the position, and convert it to pose space */
325                         mul_m4_v3(ob->imat, vec);
326                         copy_v3_v3(poseHead, vec);
327
328                         /* set the new radius (it should be the average value) */
329                         radius = (radius + rad) / 2;
330                 }
331         }
332
333         /* step 2: determine the implied transform from these endpoints
334          *     - splineVec: the vector direction that the spline applies on the bone
335          *     - scaleFac: the factor that the bone length is scaled by to get the desired amount
336          */
337         sub_v3_v3v3(splineVec, poseTail, poseHead);
338         scaleFac = len_v3(splineVec) / pchan->bone->length;
339
340         /* step 3: compute the shortest rotation needed to map from the bone rotation to the current axis
341          *      - this uses the same method as is used for the Damped Track Constraint (see the code there for details)
342          */
343         {
344                 float dmat[3][3], rmat[3][3], tmat[3][3];
345                 float raxis[3], rangle;
346
347                 /* compute the raw rotation matrix from the bone's current matrix by extracting only the
348                  * orientation-relevant axes, and normalizing them
349                  */
350                 copy_v3_v3(rmat[0], pchan->pose_mat[0]);
351                 copy_v3_v3(rmat[1], pchan->pose_mat[1]);
352                 copy_v3_v3(rmat[2], pchan->pose_mat[2]);
353                 normalize_m3(rmat);
354
355                 /* also, normalize the orientation imposed by the bone, now that we've extracted the scale factor */
356                 normalize_v3(splineVec);
357
358                 /* calculate smallest axis-angle rotation necessary for getting from the
359                  * current orientation of the bone, to the spline-imposed direction
360                  */
361                 cross_v3_v3v3(raxis, rmat[1], splineVec);
362
363                 rangle = dot_v3v3(rmat[1], splineVec);
364                 CLAMP(rangle, -1.0f, 1.0f);
365                 rangle = acosf(rangle);
366
367                 /* multiply the magnitude of the angle by the influence of the constraint to
368                  * control the influence of the SplineIK effect
369                  */
370                 rangle *= tree->con->enforce;
371
372                 /* construct rotation matrix from the axis-angle rotation found above
373                  *      - this call takes care to make sure that the axis provided is a unit vector first
374                  */
375                 axis_angle_to_mat3(dmat, raxis, rangle);
376
377                 /* combine these rotations so that the y-axis of the bone is now aligned as the spline dictates,
378                  * while still maintaining roll control from the existing bone animation
379                  */
380                 mul_m3_m3m3(tmat, dmat, rmat); /* m1, m3, m2 */
381                 normalize_m3(tmat); /* attempt to reduce shearing, though I doubt this'll really help too much now... */
382                 copy_m4_m3(poseMat, tmat);
383         }
384
385         /* step 4: set the scaling factors for the axes */
386         {
387                 /* only multiply the y-axis by the scaling factor to get nice volume-preservation */
388                 mul_v3_fl(poseMat[1], scaleFac);
389
390                 /* set the scaling factors of the x and z axes from... */
391                 switch (ikData->xzScaleMode) {
392                         case CONSTRAINT_SPLINEIK_XZS_ORIGINAL:
393                         {
394                                 /* original scales get used */
395                                 float scale;
396
397                                 /* x-axis scale */
398                                 scale = len_v3(pchan->pose_mat[0]);
399                                 mul_v3_fl(poseMat[0], scale);
400                                 /* z-axis scale */
401                                 scale = len_v3(pchan->pose_mat[2]);
402                                 mul_v3_fl(poseMat[2], scale);
403                                 break;
404                         }
405                         case CONSTRAINT_SPLINEIK_XZS_INVERSE:
406                         {
407                                 /* old 'volume preservation' method using the inverse scale */
408                                 float scale;
409
410                                 /* calculate volume preservation factor which is
411                                  * basically the inverse of the y-scaling factor
412                                  */
413                                 if (fabsf(scaleFac) != 0.0f) {
414                                         scale = 1.0f / fabsf(scaleFac);
415
416                                         /* we need to clamp this within sensible values */
417                                         /* NOTE: these should be fine for now, but should get sanitised in future */
418                                         CLAMP(scale, 0.0001f, 100000.0f);
419                                 }
420                                 else
421                                         scale = 1.0f;
422
423                                 /* apply the scaling */
424                                 mul_v3_fl(poseMat[0], scale);
425                                 mul_v3_fl(poseMat[2], scale);
426                                 break;
427                         }
428                         case CONSTRAINT_SPLINEIK_XZS_VOLUMETRIC:
429                         {
430                                 /* improved volume preservation based on the Stretch To constraint */
431                                 float final_scale;
432                                 
433                                 /* as the basis for volume preservation, we use the inverse scale factor... */
434                                 if (fabsf(scaleFac) != 0.0f) {
435                                         /* NOTE: The method here is taken wholesale from the Stretch To constraint */
436                                         float bulge = powf(1.0f / fabsf(scaleFac), ikData->bulge);
437                                         
438                                         if (bulge > 1.0f) {
439                                                 if (ikData->flag & CONSTRAINT_SPLINEIK_USE_BULGE_MAX) {
440                                                         float bulge_max = max_ff(ikData->bulge_max, 1.0f);
441                                                         float hard = min_ff(bulge, bulge_max);
442                                                         
443                                                         float range = bulge_max - 1.0f;
444                                                         float scale = (range > 0.0f) ? 1.0f / range : 0.0f;
445                                                         float soft = 1.0f + range * atanf((bulge - 1.0f) * scale) / (float)M_PI_2;
446                                                         
447                                                         bulge = interpf(soft, hard, ikData->bulge_smooth);
448                                                 }
449                                         }
450                                         if (bulge < 1.0f) {
451                                                 if (ikData->flag & CONSTRAINT_SPLINEIK_USE_BULGE_MIN) {
452                                                         float bulge_min = CLAMPIS(ikData->bulge_min, 0.0f, 1.0f);
453                                                         float hard = max_ff(bulge, bulge_min);
454                                                         
455                                                         float range = 1.0f - bulge_min;
456                                                         float scale = (range > 0.0f) ? 1.0f / range : 0.0f;
457                                                         float soft = 1.0f - range * atanf((1.0f - bulge) * scale) / (float)M_PI_2;
458                                                         
459                                                         bulge = interpf(soft, hard, ikData->bulge_smooth);
460                                                 }
461                                         }
462                                         
463                                         /* compute scale factor for xz axes from this value */
464                                         final_scale = sqrtf(bulge);
465                                 }
466                                 else {
467                                         /* no scaling, so scale factor is simple */
468                                         final_scale = 1.0f;
469                                 }
470                                 
471                                 /* apply the scaling (assuming normalised scale) */
472                                 mul_v3_fl(poseMat[0], final_scale);
473                                 mul_v3_fl(poseMat[2], final_scale);
474                                 break;
475                         }
476                 }
477
478                 /* finally, multiply the x and z scaling by the radius of the curve too,
479                  * to allow automatic scales to get tweaked still
480                  */
481                 if ((ikData->flag & CONSTRAINT_SPLINEIK_NO_CURVERAD) == 0) {
482                         mul_v3_fl(poseMat[0], radius);
483                         mul_v3_fl(poseMat[2], radius);
484                 }
485         }
486
487         /* step 5: set the location of the bone in the matrix */
488         if (ikData->flag & CONSTRAINT_SPLINEIK_NO_ROOT) {
489                 /* when the 'no-root' option is affected, the chain can retain
490                  * the shape but be moved elsewhere
491                  */
492                 copy_v3_v3(poseHead, pchan->pose_head);
493         }
494         else if (tree->con->enforce < 1.0f) {
495                 /* when the influence is too low
496                  *      - blend the positions for the 'root' bone
497                  *      - stick to the parent for any other
498                  */
499                 if (pchan->parent) {
500                         copy_v3_v3(poseHead, pchan->pose_head);
501                 }
502                 else {
503                         /* FIXME: this introduces popping artifacts when we reach 0.0 */
504                         interp_v3_v3v3(poseHead, pchan->pose_head, poseHead, tree->con->enforce);
505                 }
506         }
507         copy_v3_v3(poseMat[3], poseHead);
508
509         /* finally, store the new transform */
510         copy_m4_m4(pchan->pose_mat, poseMat);
511         copy_v3_v3(pchan->pose_head, poseHead);
512
513         /* recalculate tail, as it's now outdated after the head gets adjusted above! */
514         BKE_pose_where_is_bone_tail(pchan);
515
516         /* done! */
517         pchan->flag |= POSE_DONE;
518 }
519
520 /* Evaluate the chain starting from the nominated bone */
521 static void splineik_execute_tree(Scene *scene, Object *ob, bPoseChannel *pchan_root, float ctime)
522 {
523         tSplineIK_Tree *tree;
524
525         /* for each pose-tree, execute it if it is spline, otherwise just free it */
526         while ((tree = pchan_root->siktree.first) != NULL) {
527                 int i;
528
529                 /* walk over each bone in the chain, calculating the effects of spline IK
530                  *     - the chain is traversed in the opposite order to storage order (i.e. parent to children)
531                  *       so that dependencies are correct
532                  */
533                 for (i = tree->chainlen - 1; i >= 0; i--) {
534                         bPoseChannel *pchan = tree->chain[i];
535                         splineik_evaluate_bone(tree, scene, ob, pchan, i, ctime);
536                 }
537
538                 /* free the tree info specific to SplineIK trees now */
539                 if (tree->chain)
540                         MEM_freeN(tree->chain);
541                 if (tree->free_points)
542                         MEM_freeN(tree->points);
543
544                 /* free this tree */
545                 BLI_freelinkN(&pchan_root->siktree, tree);
546         }
547 }
548
549 void BKE_pose_splineik_init_tree(Scene *scene, Object *ob, float ctime)
550 {
551         splineik_init_tree(scene, ob, ctime);
552 }
553
554 void BKE_splineik_execute_tree(Scene *scene, Object *ob, bPoseChannel *pchan_root, float ctime)
555 {
556         splineik_execute_tree(scene, ob, pchan_root, ctime);
557 }
558
559 /* *************** Depsgraph evaluation callbacks ************ */
560
561 void BKE_pose_eval_init(EvaluationContext *UNUSED(eval_ctx),
562                         Scene *scene,
563                         Object *ob,
564                         bPose *pose)
565 {
566         float ctime = BKE_scene_frame_get(scene); /* not accurate... */
567         bPoseChannel *pchan;
568
569         DEBUG_PRINT("%s on %s\n", __func__, ob->id.name);
570
571         BLI_assert(ob->type == OB_ARMATURE);
572
573         /* We demand having proper pose. */
574         BLI_assert(ob->pose != NULL);
575         BLI_assert((ob->pose->flag & POSE_RECALC) == 0);
576
577         /* imat is needed for solvers. */
578         invert_m4_m4(ob->imat, ob->obmat);
579
580         /* 1. clear flags */
581         for (pchan = pose->chanbase.first; pchan != NULL; pchan = pchan->next) {
582                 pchan->flag &= ~(POSE_DONE | POSE_CHAIN | POSE_IKTREE | POSE_IKSPLINE);
583         }
584
585         /* 2a. construct the IK tree (standard IK) */
586         BIK_initialize_tree(scene, ob, ctime);
587
588         /* 2b. construct the Spline IK trees
589          *  - this is not integrated as an IK plugin, since it should be able
590          *        to function in conjunction with standard IK
591          */
592         BKE_pose_splineik_init_tree(scene, ob, ctime);
593 }
594
595 void BKE_pose_eval_bone(EvaluationContext *UNUSED(eval_ctx),
596                         Scene *scene,
597                         Object *ob,
598                         bPoseChannel *pchan)
599 {
600         bArmature *arm = (bArmature *)ob->data;
601         DEBUG_PRINT("%s on %s pchan %s\n", __func__, ob->id.name, pchan->name);
602         BLI_assert(ob->type == OB_ARMATURE);
603         if (arm->edbo || (arm->flag & ARM_RESTPOS)) {
604                 Bone *bone = pchan->bone;
605                 if (bone) {
606                         copy_m4_m4(pchan->pose_mat, bone->arm_mat);
607                         copy_v3_v3(pchan->pose_head, bone->arm_head);
608                         copy_v3_v3(pchan->pose_tail, bone->arm_tail);
609                 }
610         }
611         else {
612                 /* TODO(sergey): Currently if there are constraints full transform is being
613                  * evaluated in BKE_pose_constraints_evaluate.
614                  */
615                 if (pchan->constraints.first == NULL) {
616                         if (pchan->flag & POSE_IKTREE || pchan->flag & POSE_IKSPLINE) {
617                                 /* pass */
618                         }
619                         else {
620                                 /* TODO(sergey): Use time source node for time. */
621                                 float ctime = BKE_scene_frame_get(scene); /* not accurate... */
622                                 if ((pchan->flag & POSE_DONE) == 0) {
623                                         BKE_pose_where_is_bone(scene, ob, pchan, ctime, 1);
624                                 }
625                         }
626                 }
627         }
628 }
629
630 void BKE_pose_constraints_evaluate(EvaluationContext *UNUSED(eval_ctx),
631                                    Object *ob,
632                                    bPoseChannel *pchan)
633 {
634         Scene *scene = G.main->scene.first;
635         DEBUG_PRINT("%s on %s pchan %s\n", __func__, ob->id.name, pchan->name);
636         bArmature *arm = (bArmature *)ob->data;
637         if (arm->flag & ARM_RESTPOS) {
638                 return;
639         }
640         else if (pchan->flag & POSE_IKTREE || pchan->flag & POSE_IKSPLINE) {
641                 /* IK are being solved separately/ */
642         }
643         else {
644                 float ctime = BKE_scene_frame_get(scene); /* not accurate... */
645                 if ((pchan->flag & POSE_DONE) == 0) {
646                         BKE_pose_where_is_bone(scene, ob, pchan, ctime, 1);
647                 }
648         }
649 }
650
651 void BKE_pose_bone_done(EvaluationContext *UNUSED(eval_ctx),
652                         bPoseChannel *pchan)
653 {
654         float imat[4][4];
655         DEBUG_PRINT("%s on pchan %s\n", __func__, pchan->name);
656         if (pchan->bone) {
657                 invert_m4_m4(imat, pchan->bone->arm_mat);
658                 mul_m4_m4m4(pchan->chan_mat, pchan->pose_mat, imat);
659         }
660 }
661
662 void BKE_pose_iktree_evaluate(EvaluationContext *UNUSED(eval_ctx),
663                               Scene *scene,
664                               Object *ob,
665                               bPoseChannel *rootchan)
666 {
667         float ctime = BKE_scene_frame_get(scene); /* not accurate... */
668         DEBUG_PRINT("%s on %s pchan %s\n", __func__, ob->id.name, rootchan->name);
669         BIK_execute_tree(scene, ob, rootchan, ctime);
670 }
671
672 void BKE_pose_splineik_evaluate(EvaluationContext *UNUSED(eval_ctx),
673                                 Scene *scene,
674                                 Object *ob,
675                                 bPoseChannel *rootchan)
676 {
677         float ctime = BKE_scene_frame_get(scene); /* not accurate... */
678         DEBUG_PRINT("%s on %s pchan %s\n", __func__, ob->id.name, rootchan->name);
679         BKE_splineik_execute_tree(scene, ob, rootchan, ctime);
680 }
681
682 void BKE_pose_eval_flush(EvaluationContext *UNUSED(eval_ctx),
683                          Scene *scene,
684                          Object *ob,
685                          bPose *UNUSED(pose))
686 {
687         float ctime = BKE_scene_frame_get(scene); /* not accurate... */
688         DEBUG_PRINT("%s on %s\n", __func__, ob->id.name);
689         BLI_assert(ob->type == OB_ARMATURE);
690
691         /* 6. release the IK tree */
692         BIK_release_tree(scene, ob, ctime);
693
694         ob->recalc &= ~OB_RECALC_ALL;
695 }
696
697 void BKE_pose_eval_proxy_copy(EvaluationContext *UNUSED(eval_ctx), Object *ob)
698 {
699         BLI_assert(ID_IS_LINKED_DATABLOCK(ob) && ob->proxy_from != NULL);
700         DEBUG_PRINT("%s on %s\n", __func__, ob->id.name);
701         if (BKE_pose_copy_result(ob->pose, ob->proxy_from->pose) == false) {
702                 printf("Proxy copy error, lib Object: %s proxy Object: %s\n",
703                        ob->id.name + 2, ob->proxy_from->id.name + 2);
704         }
705 }