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