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