Fix T52835: When driven IK influence change, ik animation have 1 frame delay
[blender.git] / source / blender / depsgraph / intern / builder / deg_builder_nodes_rig.cc
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) 2013 Blender Foundation.
19  * All rights reserved.
20  *
21  * Original Author: Joshua Leung
22  * Contributor(s): Based on original depsgraph.c code - Blender Foundation (2005-2013)
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/depsgraph/intern/builder/deg_builder_nodes_rig.cc
28  *  \ingroup depsgraph
29  *
30  * Methods for constructing depsgraph's nodes
31  */
32
33 #include "intern/builder/deg_builder_nodes.h"
34
35 #include <stdio.h>
36 #include <stdlib.h>
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_utildefines.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_string.h"
43
44 extern "C" {
45 #include "DNA_anim_types.h"
46 #include "DNA_armature_types.h"
47 #include "DNA_constraint_types.h"
48 #include "DNA_object_types.h"
49
50 #include "BKE_action.h"
51 #include "BKE_armature.h"
52 } /* extern "C" */
53
54 #include "DEG_depsgraph.h"
55 #include "DEG_depsgraph_build.h"
56
57 #include "intern/builder/deg_builder.h"
58 #include "intern/nodes/deg_node.h"
59 #include "intern/nodes/deg_node_component.h"
60 #include "intern/nodes/deg_node_operation.h"
61 #include "intern/depsgraph_types.h"
62 #include "intern/depsgraph_intern.h"
63 #include "util/deg_util_foreach.h"
64
65 namespace DEG {
66
67 void DepsgraphNodeBuilder::build_pose_constraints(Scene *scene, Object *ob, bPoseChannel *pchan)
68 {
69         /* create node for constraint stack */
70         add_operation_node(&ob->id, DEG_NODE_TYPE_BONE, pchan->name,
71                            function_bind(BKE_pose_constraints_evaluate, _1, scene, ob, pchan),
72                            DEG_OPCODE_BONE_CONSTRAINTS);
73 }
74
75 /* IK Solver Eval Steps */
76 void DepsgraphNodeBuilder::build_ik_pose(Scene *scene, Object *ob, bPoseChannel *pchan, bConstraint *con)
77 {
78         bKinematicConstraint *data = (bKinematicConstraint *)con->data;
79
80         /* Find the chain's root. */
81         bPoseChannel *rootchan = BKE_armature_ik_solver_find_root(pchan, data);
82
83         if (has_operation_node(&ob->id, DEG_NODE_TYPE_EVAL_POSE, rootchan->name,
84                                DEG_OPCODE_POSE_IK_SOLVER))
85         {
86                 return;
87         }
88
89         /* Operation node for evaluating/running IK Solver. */
90         add_operation_node(&ob->id, DEG_NODE_TYPE_EVAL_POSE, rootchan->name,
91                            function_bind(BKE_pose_iktree_evaluate, _1, scene, ob, rootchan),
92                            DEG_OPCODE_POSE_IK_SOLVER);
93 }
94
95 /* Spline IK Eval Steps */
96 void DepsgraphNodeBuilder::build_splineik_pose(Scene *scene, Object *ob, bPoseChannel *pchan, bConstraint *con)
97 {
98         bSplineIKConstraint *data = (bSplineIKConstraint *)con->data;
99
100         /* Find the chain's root. */
101         bPoseChannel *rootchan = BKE_armature_splineik_solver_find_root(pchan, data);
102
103         /* Operation node for evaluating/running Spline IK Solver.
104          * Store the "root bone" of this chain in the solver, so it knows where to start.
105          */
106         add_operation_node(&ob->id, DEG_NODE_TYPE_EVAL_POSE, rootchan->name,
107                            function_bind(BKE_pose_splineik_evaluate, _1, scene, ob, rootchan),
108                            DEG_OPCODE_POSE_SPLINE_IK_SOLVER);
109 }
110
111 /* Pose/Armature Bones Graph */
112 void DepsgraphNodeBuilder::build_rig(Scene *scene, Object *ob)
113 {
114         bArmature *arm = (bArmature *)ob->data;
115         OperationDepsNode *op_node;
116
117         /* animation and/or drivers linking posebones to base-armature used to define them
118          * NOTE: AnimData here is really used to control animated deform properties,
119          *       which ideally should be able to be unique across different instances.
120          *       Eventually, we need some type of proxy/isolation mechanism in-between here
121          *       to ensure that we can use same rig multiple times in same scene...
122          */
123         if ((arm->id.tag & LIB_TAG_DOIT) == 0) {
124                 build_animdata(&arm->id);
125
126                 /* Make sure pose is up-to-date with armature updates. */
127                 add_operation_node(&arm->id,
128                                    DEG_NODE_TYPE_PARAMETERS,
129                                    NULL,
130                                    DEG_OPCODE_PLACEHOLDER,
131                                    "Armature Eval");
132         }
133
134         /* Rebuild pose if not up to date. */
135         if (ob->pose == NULL || (ob->pose->flag & POSE_RECALC)) {
136                 BKE_pose_rebuild_ex(ob, arm, false);
137                 /* XXX: Without this animation gets lost in certain circumstances
138                  * after loading file. Need to investigate further since it does
139                  * not happen with simple scenes..
140                  */
141                 if (ob->adt) {
142                         ob->adt->recalc |= ADT_RECALC_ANIM;
143                 }
144         }
145
146         /* speed optimization for animation lookups */
147         if (ob->pose) {
148                 BKE_pose_channels_hash_make(ob->pose);
149                 if (ob->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
150                         BKE_pose_update_constraint_flags(ob->pose);
151                 }
152         }
153
154         /**
155          * Pose Rig Graph
156          * ==============
157          *
158          * Pose Component:
159          * - Mainly used for referencing Bone components.
160          * - This is where the evaluation operations for init/exec/cleanup
161          *   (ik) solvers live, and are later hooked up (so that they can be
162          *   interleaved during runtime) with bone-operations they depend on/affect.
163          * - init_pose_eval() and cleanup_pose_eval() are absolute first and last
164          *   steps of pose eval process. ALL bone operations must be performed
165          *   between these two...
166          *
167          * Bone Component:
168          * - Used for representing each bone within the rig
169          * - Acts to encapsulate the evaluation operations (base matrix + parenting,
170          *   and constraint stack) so that they can be easily found.
171          * - Everything else which depends on bone-results hook up to the component only
172          *   so that we can redirect those to point at either the the post-IK/
173          *   post-constraint/post-matrix steps, as needed.
174          */
175
176         /* pose eval context */
177         op_node = add_operation_node(&ob->id,
178                                      DEG_NODE_TYPE_EVAL_POSE,
179                                      function_bind(BKE_pose_eval_init, _1, scene, ob, ob->pose),
180                                      DEG_OPCODE_POSE_INIT);
181         op_node->set_as_entry();
182
183         op_node = add_operation_node(&ob->id,
184                                      DEG_NODE_TYPE_EVAL_POSE,
185                                      function_bind(BKE_pose_eval_init_ik, _1, scene, ob, ob->pose),
186                                      DEG_OPCODE_POSE_INIT_IK);
187
188         op_node = add_operation_node(&ob->id,
189                                      DEG_NODE_TYPE_EVAL_POSE,
190                                      function_bind(BKE_pose_eval_flush, _1, scene, ob, ob->pose),
191                                      DEG_OPCODE_POSE_DONE);
192         op_node->set_as_exit();
193
194         /* bones */
195         LINKLIST_FOREACH (bPoseChannel *, pchan, &ob->pose->chanbase) {
196                 /* node for bone eval */
197                 op_node = add_operation_node(&ob->id, DEG_NODE_TYPE_BONE, pchan->name, NULL,
198                                              DEG_OPCODE_BONE_LOCAL);
199                 op_node->set_as_entry();
200
201                 add_operation_node(&ob->id, DEG_NODE_TYPE_BONE, pchan->name,
202                                    function_bind(BKE_pose_eval_bone, _1, scene, ob, pchan),
203                                    DEG_OPCODE_BONE_POSE_PARENT);
204
205                 add_operation_node(&ob->id, DEG_NODE_TYPE_BONE, pchan->name,
206                                    NULL, /* NOTE: dedicated noop for easier relationship construction */
207                                    DEG_OPCODE_BONE_READY);
208
209                 op_node = add_operation_node(&ob->id, DEG_NODE_TYPE_BONE, pchan->name,
210                                              function_bind(BKE_pose_bone_done, _1, pchan),
211                                              DEG_OPCODE_BONE_DONE);
212                 op_node->set_as_exit();
213
214                 /* constraints */
215                 if (pchan->constraints.first != NULL) {
216                         build_pose_constraints(scene, ob, pchan);
217                 }
218
219                 /**
220                  * IK Solvers...
221                  *
222                  * - These require separate processing steps are pose-level
223                  *   to be executed between chains of bones (i.e. once the
224                  *   base transforms of a bunch of bones is done)
225                  *
226                  * Unsolved Issues:
227                  * - Care is needed to ensure that multi-headed trees work out the same as in ik-tree building
228                  * - Animated chain-lengths are a problem...
229                  */
230                 LINKLIST_FOREACH (bConstraint *, con, &pchan->constraints) {
231                         switch (con->type) {
232                                 case CONSTRAINT_TYPE_KINEMATIC:
233                                         build_ik_pose(scene, ob, pchan, con);
234                                         break;
235
236                                 case CONSTRAINT_TYPE_SPLINEIK:
237                                         build_splineik_pose(scene, ob, pchan, con);
238                                         break;
239
240                                 default:
241                                         break;
242                         }
243                 }
244         }
245 }
246
247 void DepsgraphNodeBuilder::build_proxy_rig(Object *ob)
248 {
249         ID *obdata = (ID *)ob->data;
250         OperationDepsNode *op_node;
251
252         build_animdata(obdata);
253
254         BLI_assert(ob->pose != NULL);
255
256         /* speed optimization for animation lookups */
257         BKE_pose_channels_hash_make(ob->pose);
258         if (ob->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
259                 BKE_pose_update_constraint_flags(ob->pose);
260         }
261
262         op_node = add_operation_node(&ob->id,
263                                      DEG_NODE_TYPE_EVAL_POSE,
264                                      function_bind(BKE_pose_eval_proxy_copy, _1, ob),
265                                      DEG_OPCODE_POSE_INIT);
266         op_node->set_as_entry();
267
268         LINKLIST_FOREACH (bPoseChannel *, pchan, &ob->pose->chanbase) {
269                 op_node = add_operation_node(&ob->id, DEG_NODE_TYPE_BONE, pchan->name,
270                                              NULL, DEG_OPCODE_BONE_LOCAL);
271                 op_node->set_as_entry();
272
273                 add_operation_node(&ob->id, DEG_NODE_TYPE_BONE, pchan->name,
274                                    NULL, DEG_OPCODE_BONE_READY);
275
276                 op_node = add_operation_node(&ob->id, DEG_NODE_TYPE_BONE, pchan->name,
277                                              NULL, DEG_OPCODE_BONE_DONE);
278                 op_node->set_as_exit();
279         }
280
281         op_node = add_operation_node(&ob->id, DEG_NODE_TYPE_EVAL_POSE,
282                                      NULL, DEG_OPCODE_POSE_DONE);
283         op_node->set_as_exit();
284 }
285
286 }  // namespace DEG