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