Merge branch 'master' into blender2.8
[blender.git] / source / blender / blenkernel / intern / armature_update.c
index 241a842d73fceef9e01f473a32ad2819978e6c49..1460a3cb205f7b2a93ec1417f39111f180c62a24 100644 (file)
  * Defines and code for core node types
  */
 
+/** \file blender/blenkernel/intern/armature_update.c
+ *  \ingroup bke
+ */
+
 #include "MEM_guardedalloc.h"
 
 #include "BLI_utildefines.h"
@@ -41,9 +45,9 @@
 #include "BKE_anim.h"
 #include "BKE_armature.h"
 #include "BKE_curve.h"
-#include "BKE_depsgraph.h"
 #include "BKE_displist.h"
 #include "BKE_fcurve.h"
+#include "BKE_object.h"
 #include "BKE_scene.h"
 
 #include "BIK_api.h"
@@ -113,9 +117,11 @@ static void splineik_init_tree_from_pchan(Scene *scene, Object *UNUSED(ob), bPos
                 *       currently for paths to work it needs to go through the bevlist/displist system (ton)
                 */
 
+               /* TODO: Make sure this doesn't crash. */
+#if 0
                /* only happens on reload file, but violates depsgraph still... fix! */
                if (ELEM(NULL,  ikData->tar->curve_cache, ikData->tar->curve_cache->path, ikData->tar->curve_cache->path->data)) {
-                       BKE_displist_make_curveTypes(scene, ikData->tar, 0);
+                       BKE_displist_make_curveTypes(depsgraph, scene, ikData->tar, 0);
 
                        /* path building may fail in EditMode after removing verts [#33268]*/
                        if (ELEM(NULL, ikData->tar->curve_cache->path, ikData->tar->curve_cache->path->data)) {
@@ -123,6 +129,9 @@ static void splineik_init_tree_from_pchan(Scene *scene, Object *UNUSED(ob), bPos
                                return;
                        }
                }
+#else
+               (void) scene;
+#endif
        }
 
        /* find the root bone and the chain of bones from the root to the tip
@@ -197,7 +206,7 @@ static void splineik_init_tree_from_pchan(Scene *scene, Object *UNUSED(ob), bPos
 
                /* get the current length of the curve */
                /* NOTE: this is assumed to be correct even after the curve was resized */
-               splineLen = ikData->tar->curve_cache->path->totdist;
+               splineLen = ikData->tar->runtime.curve_cache->path->totdist;
 
                /* calculate the scale factor to multiply all the path values by so that the
                 * bone chain retains its current length, such that
@@ -261,15 +270,16 @@ static void splineik_init_tree(Scene *scene, Object *ob, float UNUSED(ctime))
 /* ----------- */
 
 /* Evaluate spline IK for a given bone */
-static void splineik_evaluate_bone(tSplineIK_Tree *tree, Scene *scene, Object *ob, bPoseChannel *pchan,
-                                   int index, float ctime)
+static void splineik_evaluate_bone(
+        struct Depsgraph *depsgraph, tSplineIK_Tree *tree, Scene *scene, Object *ob, bPoseChannel *pchan,
+        int index, float ctime)
 {
        bSplineIKConstraint *ikData = tree->ikData;
        float poseHead[3], poseTail[3], poseMat[4][4];
        float splineVec[3], scaleFac, radius = 1.0f;
 
        /* firstly, calculate the bone matrix the standard way, since this is needed for roll control */
-       BKE_pose_where_is_bone(scene, ob, pchan, ctime, 1);
+       BKE_pose_where_is_bone(depsgraph, scene, ob, pchan, ctime, 1);
 
        copy_v3_v3(poseHead, pchan->pose_head);
        copy_v3_v3(poseTail, pchan->pose_tail);
@@ -511,7 +521,7 @@ static void splineik_evaluate_bone(tSplineIK_Tree *tree, Scene *scene, Object *o
 }
 
 /* Evaluate the chain starting from the nominated bone */
-static void splineik_execute_tree(Scene *scene, Object *ob, bPoseChannel *pchan_root, float ctime)
+static void splineik_execute_tree(struct Depsgraph *depsgraph, Scene *scene, Object *ob, bPoseChannel *pchan_root, float ctime)
 {
        tSplineIK_Tree *tree;
 
@@ -525,7 +535,7 @@ static void splineik_execute_tree(Scene *scene, Object *ob, bPoseChannel *pchan_
                 */
                for (i = tree->chainlen - 1; i >= 0; i--) {
                        bPoseChannel *pchan = tree->chain[i];
-                       splineik_evaluate_bone(tree, scene, ob, pchan, i, ctime);
+                       splineik_evaluate_bone(depsgraph, tree, scene, ob, pchan, i, ctime);
                }
 
                /* free the tree info specific to SplineIK trees now */
@@ -544,57 +554,73 @@ void BKE_pose_splineik_init_tree(Scene *scene, Object *ob, float ctime)
        splineik_init_tree(scene, ob, ctime);
 }
 
-void BKE_splineik_execute_tree(Scene *scene, Object *ob, bPoseChannel *pchan_root, float ctime)
+void BKE_splineik_execute_tree(
+        struct Depsgraph *depsgraph, Scene *scene,
+        Object *ob, bPoseChannel *pchan_root, float ctime)
 {
-       splineik_execute_tree(scene, ob, pchan_root, ctime);
+       splineik_execute_tree(depsgraph, scene, ob, pchan_root, ctime);
 }
 
 /* *************** Depsgraph evaluation callbacks ************ */
 
+static void pose_pchan_index_create(bPose *pose)
+{
+       const int num_channels = BLI_listbase_count(&pose->chanbase);
+       pose->chan_array = MEM_malloc_arrayN(
+               num_channels, sizeof(bPoseChannel *), "pose->chan_array");
+       int pchan_index = 0;
+       for (bPoseChannel *pchan = pose->chanbase.first; pchan != NULL; pchan = pchan->next) {
+               pose->chan_array[pchan_index++] = pchan;
+       }
+}
+
 BLI_INLINE bPoseChannel *pose_pchan_get_indexed(Object *ob, int pchan_index)
 {
        bPose *pose = ob->pose;
        BLI_assert(pose != NULL);
+       BLI_assert(pose->chan_array != NULL);
        BLI_assert(pchan_index >= 0);
        BLI_assert(pchan_index < MEM_allocN_len(pose->chan_array) / sizeof(bPoseChannel *));
        return pose->chan_array[pchan_index];
 }
 
-void BKE_pose_eval_init(EvaluationContext *UNUSED(eval_ctx),
+void BKE_pose_eval_init(struct Depsgraph *depsgraph,
                         Scene *UNUSED(scene),
-                        Object *ob)
+                        Object *object)
 {
-       bPose *pose = ob->pose;
+       bPose *pose = object->pose;
        BLI_assert(pose != NULL);
 
-       DEG_debug_print_eval(__func__, ob->id.name, ob);
+       DEG_debug_print_eval(depsgraph, __func__, object->id.name, object);
 
-       BLI_assert(ob->type == OB_ARMATURE);
+       BLI_assert(object->type == OB_ARMATURE);
 
        /* We demand having proper pose. */
-       BLI_assert(ob->pose != NULL);
-       BLI_assert((ob->pose->flag & POSE_RECALC) == 0);
+       BLI_assert(object->pose != NULL);
+       BLI_assert((object->pose->flag & POSE_RECALC) == 0);
 
        /* imat is needed for solvers. */
-       invert_m4_m4(ob->imat, ob->obmat);
-
-       const int num_channels = BLI_listbase_count(&pose->chanbase);
-       pose->chan_array = MEM_malloc_arrayN(
-               num_channels, sizeof(bPoseChannel *), "pose->chan_array");
+       invert_m4_m4(object->imat, object->obmat);
 
        /* clear flags */
-       int pchan_index = 0;
        for (bPoseChannel *pchan = pose->chanbase.first; pchan != NULL; pchan = pchan->next) {
                pchan->flag &= ~(POSE_DONE | POSE_CHAIN | POSE_IKTREE | POSE_IKSPLINE);
-               pose->chan_array[pchan_index++] = pchan;
+
+               /* Free B-Bone shape data cache if it's not a B-Bone. */
+               if (pchan->bone == NULL || pchan->bone->segments <= 1) {
+                       BKE_pose_channel_free_bbone_cache(pchan);
+               }
        }
+
+       pose_pchan_index_create(pose);
+       BKE_armature_cached_bbone_deformation_free_data(object);
 }
 
-void BKE_pose_eval_init_ik(EvaluationContext *UNUSED(eval_ctx),
+void BKE_pose_eval_init_ik(struct Depsgraph *depsgraph,
                            Scene *scene,
                            Object *ob)
 {
-       DEG_debug_print_eval(__func__, ob->id.name, ob);
+       DEG_debug_print_eval(depsgraph, __func__, ob->id.name, ob);
        BLI_assert(ob->type == OB_ARMATURE);
        const float ctime = BKE_scene_frame_get(scene); /* not accurate... */
        bArmature *arm = (bArmature *)ob->data;
@@ -602,7 +628,7 @@ void BKE_pose_eval_init_ik(EvaluationContext *UNUSED(eval_ctx),
                return;
        }
        /* construct the IK tree (standard IK) */
-       BIK_initialize_tree(scene, ob, ctime);
+       BIK_initialize_tree(depsgraph, scene, ob, ctime);
        /* construct the Spline IK trees
         * - this is not integrated as an IK plugin, since it should be able
         *   to function in conjunction with standard IK
@@ -610,14 +636,14 @@ void BKE_pose_eval_init_ik(EvaluationContext *UNUSED(eval_ctx),
        BKE_pose_splineik_init_tree(scene, ob, ctime);
 }
 
-void BKE_pose_eval_bone(EvaluationContext *UNUSED(eval_ctx),
+void BKE_pose_eval_bone(struct Depsgraph *depsgraph,
                         Scene *scene,
                         Object *ob,
                         int pchan_index)
 {
        bPoseChannel *pchan = pose_pchan_get_indexed(ob, pchan_index);
        DEG_debug_print_eval_subdata(
-               __func__, ob->id.name, ob, "pchan", pchan->name, pchan);
+               depsgraph, __func__, ob->id.name, ob, "pchan", pchan->name, pchan);
        BLI_assert(ob->type == OB_ARMATURE);
        bArmature *arm = (bArmature *)ob->data;
        if (arm->edbo || (arm->flag & ARM_RESTPOS)) {
@@ -640,21 +666,21 @@ void BKE_pose_eval_bone(EvaluationContext *UNUSED(eval_ctx),
                                if ((pchan->flag & POSE_DONE) == 0) {
                                        /* TODO(sergey): Use time source node for time. */
                                        float ctime = BKE_scene_frame_get(scene); /* not accurate... */
-                                       BKE_pose_where_is_bone(scene, ob, pchan, ctime, 1);
+                                       BKE_pose_where_is_bone(depsgraph, scene, ob, pchan, ctime, 1);
                                }
                        }
                }
        }
 }
 
-void BKE_pose_constraints_evaluate(EvaluationContext *UNUSED(eval_ctx),
+void BKE_pose_constraints_evaluate(struct Depsgraph *depsgraph,
                                    Scene *scene,
                                    Object *ob,
                                    int pchan_index)
 {
        bPoseChannel *pchan = pose_pchan_get_indexed(ob, pchan_index);
        DEG_debug_print_eval_subdata(
-               __func__, ob->id.name, ob, "pchan", pchan->name, pchan);
+               depsgraph, __func__, ob->id.name, ob, "pchan", pchan->name, pchan);
        bArmature *arm = (bArmature *)ob->data;
        if (arm->flag & ARM_RESTPOS) {
                return;
@@ -665,42 +691,69 @@ void BKE_pose_constraints_evaluate(EvaluationContext *UNUSED(eval_ctx),
        else {
                if ((pchan->flag & POSE_DONE) == 0) {
                        float ctime = BKE_scene_frame_get(scene); /* not accurate... */
-                       BKE_pose_where_is_bone(scene, ob, pchan, ctime, 1);
+                       BKE_pose_where_is_bone(depsgraph, scene, ob, pchan, ctime, 1);
                }
        }
 }
 
-void BKE_pose_bone_done(EvaluationContext *UNUSED(eval_ctx),
+void BKE_pose_bone_done(struct Depsgraph *depsgraph,
                         struct Object *ob,
                         int pchan_index)
 {
        bPoseChannel *pchan = pose_pchan_get_indexed(ob, pchan_index);
        float imat[4][4];
-       DEG_debug_print_eval(__func__, pchan->name, pchan);
+       DEG_debug_print_eval(depsgraph, __func__, pchan->name, pchan);
        if (pchan->bone) {
                invert_m4_m4(imat, pchan->bone->arm_mat);
                mul_m4_m4m4(pchan->chan_mat, pchan->pose_mat, imat);
        }
+       bArmature *arm = (bArmature *)ob->data;
+       if (DEG_is_active(depsgraph) && arm->edbo == NULL) {
+               bPoseChannel *pchan_orig = pchan->orig_pchan;
+               copy_m4_m4(pchan_orig->pose_mat, pchan->pose_mat);
+               copy_m4_m4(pchan_orig->chan_mat, pchan->chan_mat);
+               copy_v3_v3(pchan_orig->pose_head, pchan->pose_mat[3]);
+               copy_m4_m4(pchan_orig->constinv, pchan->constinv);
+               BKE_pose_where_is_bone_tail(pchan_orig);
+               if (pchan->bone == NULL || pchan->bone->segments <= 1) {
+                       BKE_pose_channel_free_bbone_cache(pchan_orig);
+               }
+       }
+}
+
+void BKE_pose_eval_bbone_segments(struct Depsgraph *depsgraph,
+                                  struct Object *ob,
+                                  int pchan_index)
+{
+       bPoseChannel *pchan = pose_pchan_get_indexed(ob, pchan_index);
+       DEG_debug_print_eval(depsgraph, __func__, pchan->name, pchan);
+       if (pchan->bone != NULL && pchan->bone->segments > 1) {
+               BKE_pchan_bbone_segments_cache_compute(pchan);
+               bArmature *arm = (bArmature *)ob->data;
+               if (DEG_is_active(depsgraph) && arm->edbo == NULL) {
+                       BKE_pchan_bbone_segments_cache_copy(pchan->orig_pchan, pchan);
+               }
+       }
 }
 
-void BKE_pose_iktree_evaluate(EvaluationContext *UNUSED(eval_ctx),
+void BKE_pose_iktree_evaluate(struct Depsgraph *depsgraph,
                               Scene *scene,
                               Object *ob,
                               int rootchan_index)
 {
        bPoseChannel *rootchan = pose_pchan_get_indexed(ob, rootchan_index);
        DEG_debug_print_eval_subdata(
-               __func__, ob->id.name, ob, "rootchan", rootchan->name, rootchan);
+               depsgraph, __func__, ob->id.name, ob, "rootchan", rootchan->name, rootchan);
        BLI_assert(ob->type == OB_ARMATURE);
        const float ctime = BKE_scene_frame_get(scene); /* not accurate... */
        bArmature *arm = (bArmature *)ob->data;
        if (arm->flag & ARM_RESTPOS) {
                return;
        }
-       BIK_execute_tree(scene, ob, rootchan, ctime);
+       BIK_execute_tree(depsgraph, scene, ob, rootchan, ctime);
 }
 
-void BKE_pose_splineik_evaluate(EvaluationContext *UNUSED(eval_ctx),
+void BKE_pose_splineik_evaluate(struct Depsgraph *depsgraph,
                                 Scene *scene,
                                 Object *ob,
                                 int rootchan_index)
@@ -708,47 +761,101 @@ void BKE_pose_splineik_evaluate(EvaluationContext *UNUSED(eval_ctx),
 {
        bPoseChannel *rootchan = pose_pchan_get_indexed(ob, rootchan_index);
        DEG_debug_print_eval_subdata(
-               __func__, ob->id.name, ob, "rootchan", rootchan->name, rootchan);
+               depsgraph, __func__, ob->id.name, ob, "rootchan", rootchan->name, rootchan);
        BLI_assert(ob->type == OB_ARMATURE);
        const float ctime = BKE_scene_frame_get(scene); /* not accurate... */
        bArmature *arm = (bArmature *)ob->data;
        if (arm->flag & ARM_RESTPOS) {
                return;
        }
-       BKE_splineik_execute_tree(scene, ob, rootchan, ctime);
+       BKE_splineik_execute_tree(depsgraph, scene, ob, rootchan, ctime);
 }
 
-void BKE_pose_eval_flush(EvaluationContext *UNUSED(eval_ctx),
-                         Scene *scene,
-                         Object *ob)
+/* Common part for both original and proxy armatrues. */
+static void pose_eval_done_common(struct Depsgraph *depsgraph, Object *object)
 {
-       bPose *pose = ob->pose;
+       bPose *pose = object->pose;
+       UNUSED_VARS_NDEBUG(pose);
+       BLI_assert(pose != NULL);
+       BKE_armature_cached_bbone_deformation_update(object);
+       BKE_object_eval_boundbox(depsgraph, object);
+}
+static void pose_eval_cleanup_common(Object *object)
+{
+       bPose *pose = object->pose;
        BLI_assert(pose != NULL);
+       BLI_assert(pose->chan_array != NULL || BLI_listbase_is_empty(&pose->chanbase));
+       MEM_SAFE_FREE(pose->chan_array);
+}
 
-       float ctime = BKE_scene_frame_get(scene); /* not accurate... */
-       DEG_debug_print_eval(__func__, ob->id.name, ob);
-       BLI_assert(ob->type == OB_ARMATURE);
+void BKE_pose_eval_done(struct Depsgraph *depsgraph, Object *object)
+{
+       bPose *pose = object->pose;
+       BLI_assert(pose != NULL);
+       UNUSED_VARS_NDEBUG(pose);
+       DEG_debug_print_eval(depsgraph, __func__, object->id.name, object);
+       BLI_assert(object->type == OB_ARMATURE);
+       pose_eval_done_common(depsgraph, object);
+}
 
-       /* release the IK tree */
-       BIK_release_tree(scene, ob, ctime);
+void BKE_pose_eval_cleanup(struct Depsgraph *depsgraph,
+                           Scene *scene,
+                           Object *object)
+{
+       bPose *pose = object->pose;
+       BLI_assert(pose != NULL);
+       UNUSED_VARS_NDEBUG(pose);
+       const float ctime = BKE_scene_frame_get(scene); /* not accurate... */
+       DEG_debug_print_eval(depsgraph, __func__, object->id.name, object);
+       BLI_assert(object->type == OB_ARMATURE);
+       /* Release the IK tree. */
+       BIK_release_tree(scene, object, ctime);
+       pose_eval_cleanup_common(object);
+}
 
-       ob->recalc &= ~OB_RECALC_ALL;
+void BKE_pose_eval_proxy_init(struct Depsgraph *depsgraph, Object *object)
+{
+       BLI_assert(ID_IS_LINKED(object) && object->proxy_from != NULL);
+       DEG_debug_print_eval(depsgraph, __func__, object->id.name, object);
 
-       BLI_assert(pose->chan_array != NULL);
-       MEM_freeN(pose->chan_array);
-       pose->chan_array = NULL;
+       pose_pchan_index_create(object->pose);
+       BKE_armature_cached_bbone_deformation_free_data(object);
 }
 
-void BKE_pose_eval_proxy_copy(EvaluationContext *UNUSED(eval_ctx), Object *ob)
+void BKE_pose_eval_proxy_done(struct Depsgraph *depsgraph, Object *object)
 {
-       BLI_assert(ID_IS_LINKED(ob) && ob->proxy_from != NULL);
-       DEG_debug_print_eval(__func__, ob->id.name, ob);
-       if (BKE_pose_copy_result(ob->pose, ob->proxy_from->pose) == false) {
-               printf("Proxy copy error, lib Object: %s proxy Object: %s\n",
-                      ob->id.name + 2, ob->proxy_from->id.name + 2);
-       }
-       /* Rest of operations are NO-OP in depsgraph, so can clear
-        * flag now.
+       BLI_assert(ID_IS_LINKED(object) && object->proxy_from != NULL);
+       DEG_debug_print_eval(depsgraph, __func__, object->id.name, object);
+       pose_eval_done_common(depsgraph, object);
+}
+
+void BKE_pose_eval_proxy_cleanup(struct Depsgraph *depsgraph, Object *object)
+{
+       BLI_assert(ID_IS_LINKED(object) && object->proxy_from != NULL);
+       DEG_debug_print_eval(depsgraph, __func__, object->id.name, object);
+       pose_eval_cleanup_common(object);
+}
+
+void BKE_pose_eval_proxy_copy_bone(
+        struct Depsgraph *depsgraph,
+        Object *object,
+        int pchan_index)
+{
+       BLI_assert(ID_IS_LINKED(object) && object->proxy_from != NULL);
+       DEG_debug_print_eval(depsgraph, __func__, object->id.name, object);
+       bPoseChannel *pchan = pose_pchan_get_indexed(object, pchan_index);
+       /* TODO(sergey): Use indexec lookup, once it's guaranteed to be kept
+        * around for the time while proxies are evaluating.
         */
-       ob->recalc &= ~OB_RECALC_ALL;
+#if 0
+       bPoseChannel *pchan_from = pose_pchan_get_indexed(
+               object->proxy_from, pchan_index);
+#else
+       bPoseChannel *pchan_from = BKE_pose_channel_find_name(
+               object->proxy_from->pose, pchan->name);
+#endif
+       BLI_assert(pchan != NULL);
+       BLI_assert(pchan_from != NULL);
+       BKE_pose_copyesult_pchan_result(pchan, pchan_from);
+       BKE_pchan_bbone_segments_cache_copy(pchan, pchan_from);
 }