Depsgraph: Ensure root pchan is always found
[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(Object *object,
68                                                   bPoseChannel *pchan,
69                                                   int pchan_index)
70 {
71         /* create node for constraint stack */
72         add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
73                            function_bind(BKE_pose_constraints_evaluate,
74                                          _1,
75                                          scene_,
76                                          object,
77                                          pchan_index),
78                            DEG_OPCODE_BONE_CONSTRAINTS);
79 }
80
81 /* IK Solver Eval Steps */
82 void DepsgraphNodeBuilder::build_ik_pose(Object *object,
83                                          bPoseChannel *pchan,
84                                          bConstraint *con)
85 {
86         bKinematicConstraint *data = (bKinematicConstraint *)con->data;
87
88         /* Find the chain's root. */
89         bPoseChannel *rootchan = BKE_armature_ik_solver_find_root(pchan, data);
90         if (rootchan == NULL) {
91                 return;
92         }
93
94         if (has_operation_node(&object->id, DEG_NODE_TYPE_EVAL_POSE, rootchan->name,
95                                DEG_OPCODE_POSE_IK_SOLVER))
96         {
97                 return;
98         }
99
100         int rootchan_index = BLI_findindex(&object->pose->chanbase, rootchan);
101         BLI_assert(rootchan_index != -1);
102         /* Operation node for evaluating/running IK Solver. */
103         add_operation_node(&object->id, DEG_NODE_TYPE_EVAL_POSE, rootchan->name,
104                            function_bind(BKE_pose_iktree_evaluate,
105                                          _1,
106                                          scene_,
107                                          object,
108                                          rootchan_index),
109                            DEG_OPCODE_POSE_IK_SOLVER);
110 }
111
112 /* Spline IK Eval Steps */
113 void DepsgraphNodeBuilder::build_splineik_pose(Object *object,
114                                                bPoseChannel *pchan,
115                                                bConstraint *con)
116 {
117         bSplineIKConstraint *data = (bSplineIKConstraint *)con->data;
118
119         /* Find the chain's root. */
120         bPoseChannel *rootchan = BKE_armature_splineik_solver_find_root(pchan, data);
121
122         /* Operation node for evaluating/running Spline IK Solver.
123          * Store the "root bone" of this chain in the solver, so it knows where to start.
124          */
125         int rootchan_index = BLI_findindex(&object->pose->chanbase, rootchan);
126         BLI_assert(rootchan_index != -1);
127         add_operation_node(&object->id, DEG_NODE_TYPE_EVAL_POSE, rootchan->name,
128                            function_bind(BKE_pose_splineik_evaluate,
129                                          _1,
130                                          scene_,
131                                          object,
132                                          rootchan_index),
133                            DEG_OPCODE_POSE_SPLINE_IK_SOLVER);
134 }
135
136 /* Pose/Armature Bones Graph */
137 void DepsgraphNodeBuilder::build_rig(Object *object)
138 {
139         bArmature *arm = (bArmature *)object->data;
140         OperationDepsNode *op_node;
141
142         /* animation and/or drivers linking posebones to base-armature used to define them
143          * NOTE: AnimData here is really used to control animated deform properties,
144          *       which ideally should be able to be unique across different instances.
145          *       Eventually, we need some type of proxy/isolation mechanism in-between here
146          *       to ensure that we can use same rig multiple times in same scene...
147          */
148         if (!built_map_.checkIsBuilt(arm)) {
149                 build_animdata(&arm->id);
150                 /* Make sure pose is up-to-date with armature updates. */
151                 add_operation_node(&arm->id,
152                                    DEG_NODE_TYPE_PARAMETERS,
153                                    NULL,
154                                    DEG_OPCODE_PLACEHOLDER,
155                                    "Armature Eval");
156         }
157
158         /* Rebuild pose if not up to date. */
159         if (object->pose == NULL || (object->pose->flag & POSE_RECALC)) {
160                 BKE_pose_rebuild_ex(object, arm, false);
161                 /* XXX: Without this animation gets lost in certain circumstances
162                  * after loading file. Need to investigate further since it does
163                  * not happen with simple scenes..
164                  */
165                 if (object->adt) {
166                         object->adt->recalc |= ADT_RECALC_ANIM;
167                 }
168         }
169
170         /* speed optimization for animation lookups */
171         if (object->pose) {
172                 BKE_pose_channels_hash_make(object->pose);
173                 if (object->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
174                         BKE_pose_update_constraint_flags(object->pose);
175                 }
176         }
177
178         /**
179          * Pose Rig Graph
180          * ==============
181          *
182          * Pose Component:
183          * - Mainly used for referencing Bone components.
184          * - This is where the evaluation operations for init/exec/cleanup
185          *   (ik) solvers live, and are later hooked up (so that they can be
186          *   interleaved during runtime) with bone-operations they depend on/affect.
187          * - init_pose_eval() and cleanup_pose_eval() are absolute first and last
188          *   steps of pose eval process. ALL bone operations must be performed
189          *   between these two...
190          *
191          * Bone Component:
192          * - Used for representing each bone within the rig
193          * - Acts to encapsulate the evaluation operations (base matrix + parenting,
194          *   and constraint stack) so that they can be easily found.
195          * - Everything else which depends on bone-results hook up to the component only
196          *   so that we can redirect those to point at either the the post-IK/
197          *   post-constraint/post-matrix steps, as needed.
198          */
199
200         /* pose eval context */
201         op_node = add_operation_node(&object->id,
202                                      DEG_NODE_TYPE_EVAL_POSE,
203                                      function_bind(BKE_pose_eval_init,
204                                                    _1,
205                                                    scene_,
206                                                    object),
207                                      DEG_OPCODE_POSE_INIT);
208         op_node->set_as_entry();
209
210         op_node = add_operation_node(&object->id,
211                                      DEG_NODE_TYPE_EVAL_POSE,
212                                      function_bind(BKE_pose_eval_init_ik,
213                                                    _1,
214                                                    scene_,
215                                                    object),
216                                      DEG_OPCODE_POSE_INIT_IK);
217
218         op_node = add_operation_node(&object->id,
219                                      DEG_NODE_TYPE_EVAL_POSE,
220                                      function_bind(BKE_pose_eval_flush,
221                                                    _1,
222                                                    scene_,
223                                                    object),
224                                      DEG_OPCODE_POSE_DONE);
225         op_node->set_as_exit();
226
227         /* bones */
228         int pchan_index = 0;
229         LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) {
230                 /* Node for bone evaluation. */
231                 op_node = add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name, NULL,
232                                              DEG_OPCODE_BONE_LOCAL);
233                 op_node->set_as_entry();
234
235                 add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
236                                    function_bind(BKE_pose_eval_bone, _1, scene_, object, pchan_index),
237                                    DEG_OPCODE_BONE_POSE_PARENT);
238
239                 add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
240                                    NULL, /* NOTE: dedicated noop for easier relationship construction */
241                                    DEG_OPCODE_BONE_READY);
242
243                 op_node = add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
244                                              function_bind(BKE_pose_bone_done,
245                                                            _1,
246                                                            object,
247                                                            pchan_index),
248                                              DEG_OPCODE_BONE_DONE);
249                 op_node->set_as_exit();
250                 /* Custom properties. */
251                 if (pchan->prop != NULL) {
252                         add_operation_node(&object->id,
253                                            DEG_NODE_TYPE_PARAMETERS,
254                                            NULL,
255                                            DEG_OPCODE_PARAMETERS_EVAL,
256                                            pchan->name);
257                 }
258                 /* Constraints. */
259                 if (pchan->constraints.first != NULL) {
260                         build_pose_constraints(object, pchan, pchan_index);
261                 }
262                 /**
263                  * IK Solvers.
264                  *
265                  * - These require separate processing steps are pose-level
266                  *   to be executed between chains of bones (i.e. once the
267                  *   base transforms of a bunch of bones is done)
268                  *
269                  * Unsolved Issues:
270                  * - Care is needed to ensure that multi-headed trees work out the same
271                  *   as in ik-tree building.
272                  * - Animated chain-lengths are a problem...
273                  */
274                 LISTBASE_FOREACH (bConstraint *, con, &pchan->constraints) {
275                         switch (con->type) {
276                                 case CONSTRAINT_TYPE_KINEMATIC:
277                                         build_ik_pose(object, pchan, con);
278                                         break;
279
280                                 case CONSTRAINT_TYPE_SPLINEIK:
281                                         build_splineik_pose(object, pchan, con);
282                                         break;
283
284                                 default:
285                                         break;
286                         }
287                 }
288
289                 /* Custom shape. */
290                 if (pchan->custom != NULL) {
291                         build_object(NULL, pchan->custom);
292                 }
293
294                 pchan_index++;
295         }
296 }
297
298 void DepsgraphNodeBuilder::build_proxy_rig(Object *object)
299 {
300         bArmature *arm = (bArmature *)object->data;
301         OperationDepsNode *op_node;
302
303         build_animdata(&arm->id);
304
305         BLI_assert(object->pose != NULL);
306
307         /* speed optimization for animation lookups */
308         BKE_pose_channels_hash_make(object->pose);
309         if (object->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
310                 BKE_pose_update_constraint_flags(object->pose);
311         }
312
313         op_node = add_operation_node(&object->id,
314                                      DEG_NODE_TYPE_EVAL_POSE,
315                                      function_bind(BKE_pose_eval_proxy_copy, _1, object),
316                                      DEG_OPCODE_POSE_INIT);
317         op_node->set_as_entry();
318
319
320         LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) {
321                 op_node = add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
322                                              NULL, DEG_OPCODE_BONE_LOCAL);
323                 op_node->set_as_entry();
324
325                 add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
326                                    NULL, DEG_OPCODE_BONE_READY);
327
328                 op_node = add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
329                                              NULL, DEG_OPCODE_BONE_DONE);
330                 op_node->set_as_exit();
331
332                 /* Custom properties. */
333                 if (pchan->prop != NULL) {
334                         add_operation_node(&object->id,
335                                            DEG_NODE_TYPE_PARAMETERS,
336                                            NULL,
337                                            DEG_OPCODE_PARAMETERS_EVAL,
338                                            pchan->name);
339                 }
340         }
341
342         op_node = add_operation_node(&object->id, DEG_NODE_TYPE_EVAL_POSE,
343                                      NULL, DEG_OPCODE_POSE_DONE);
344         op_node->set_as_exit();
345 }
346
347 }  // namespace DEG