Depsgraph: Object data separation, armature
[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         /* Animation and/or drivers linking posebones to base-armature used to
152          * define them.
153          *
154          * NOTE: AnimData here is really used to control animated deform properties,
155          *       which ideally should be able to be unique across different
156          *       instances. Eventually, we need some type of proxy/isolation
157          *       mechanism in-between here to ensure that we can use same rig
158          *       multiple times in same scene.
159          */
160         /* Armature. */
161         build_armature(armature);
162         /* Rebuild pose if not up to date. */
163         if (object->pose == NULL || (object->pose->flag & POSE_RECALC)) {
164                 BKE_pose_rebuild(object, armature);
165                 /* XXX: Without this animation gets lost in certain circumstances
166                  * after loading file. Need to investigate further since it does
167                  * not happen with simple scenes..
168                  */
169                 if (object->adt) {
170                         object->adt->recalc |= ADT_RECALC_ANIM;
171                 }
172         }
173         /* Speed optimization for animation lookups. */
174         if (object->pose != NULL) {
175                 BKE_pose_channels_hash_make(object->pose);
176                 if (object->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
177                         BKE_pose_update_constraint_flags(object->pose);
178                 }
179         }
180         /**
181          * Pose Rig Graph
182          * ==============
183          *
184          * Pose Component:
185          * - Mainly used for referencing Bone components.
186          * - This is where the evaluation operations for init/exec/cleanup
187          *   (ik) solvers live, and are later hooked up (so that they can be
188          *   interleaved during runtime) with bone-operations they depend on/affect.
189          * - init_pose_eval() and cleanup_pose_eval() are absolute first and last
190          *   steps of pose eval process. ALL bone operations must be performed
191          *   between these two...
192          *
193          * Bone Component:
194          * - Used for representing each bone within the rig
195          * - Acts to encapsulate the evaluation operations (base matrix + parenting,
196          *   and constraint stack) so that they can be easily found.
197          * - Everything else which depends on bone-results hook up to the component
198          *   only so that we can redirect those to point at either the the post-IK/
199          *   post-constraint/post-matrix steps, as needed.
200          */
201         /* Pose eval context. */
202         op_node = add_operation_node(&object->id,
203                                      DEG_NODE_TYPE_EVAL_POSE,
204                                      function_bind(BKE_pose_eval_init,
205                                                    _1,
206                                                    scene_cow,
207                                                    object_cow),
208                                      DEG_OPCODE_POSE_INIT);
209         op_node->set_as_entry();
210
211         op_node = add_operation_node(&object->id,
212                                      DEG_NODE_TYPE_EVAL_POSE,
213                                      function_bind(BKE_pose_eval_init_ik,
214                                                    _1,
215                                                    scene_cow,
216                                                    object_cow),
217                                      DEG_OPCODE_POSE_INIT_IK);
218
219         op_node = add_operation_node(&object->id,
220                                      DEG_NODE_TYPE_EVAL_POSE,
221                                      function_bind(BKE_pose_eval_flush,
222                                                    _1,
223                                                    scene_cow,
224                                                    object_cow),
225                                      DEG_OPCODE_POSE_DONE);
226         op_node->set_as_exit();
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,
237                                                  scene_cow,
238                                                  object_cow,
239                                                  pchan_index),
240                                    DEG_OPCODE_BONE_POSE_PARENT);
241
242                 /* NOTE: Dedicated noop for easier relationship construction. */
243                 add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
244                                    NULL,
245                                    DEG_OPCODE_BONE_READY);
246
247                 op_node = add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
248                                              function_bind(BKE_pose_bone_done,
249                                                            _1,
250                                                            object_cow,
251                                                            pchan_index),
252                                              DEG_OPCODE_BONE_DONE);
253                 op_node->set_as_exit();
254                 /* Custom properties. */
255                 if (pchan->prop != NULL) {
256                         add_operation_node(&object->id,
257                                            DEG_NODE_TYPE_PARAMETERS,
258                                            NULL,
259                                            DEG_OPCODE_PARAMETERS_EVAL,
260                                            pchan->name);
261                 }
262                 /* Build constraints. */
263                 if (pchan->constraints.first != NULL) {
264                         build_pose_constraints(object, pchan, pchan_index);
265                 }
266                 /**
267                  * IK Solvers.
268                  *
269                  * - These require separate processing steps are pose-level
270                  *   to be executed between chains of bones (i.e. once the
271                  *   base transforms of a bunch of bones is done)
272                  *
273                  * Unsolved Issues:
274                  * - Care is needed to ensure that multi-headed trees work out the same
275                  *   as in ik-tree building
276                  * - Animated chain-lengths are a problem.
277                  */
278                 LISTBASE_FOREACH (bConstraint *, con, &pchan->constraints) {
279                         switch (con->type) {
280                                 case CONSTRAINT_TYPE_KINEMATIC:
281                                         build_ik_pose(object, pchan, con);
282                                         break;
283
284                                 case CONSTRAINT_TYPE_SPLINEIK:
285                                         build_splineik_pose(object, pchan, con);
286                                         break;
287
288                                 default:
289                                         break;
290                         }
291                 }
292                 /* Custom shape. */
293                 if (pchan->custom != NULL) {
294                         build_object(-1, pchan->custom, DEG_ID_LINKED_INDIRECTLY);
295                 }
296                 pchan_index++;
297         }
298 }
299
300 void DepsgraphNodeBuilder::build_proxy_rig(Object *object)
301 {
302         bArmature *armature = (bArmature *)object->data;
303         OperationDepsNode *op_node;
304         Object *object_cow = get_cow_datablock(object);
305         /* Sanity check. */
306         BLI_assert(object->pose != NULL);
307         /* Armature. */
308         build_armature(armature);
309         /* speed optimization for animation lookups */
310         BKE_pose_channels_hash_make(object->pose);
311         if (object->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
312                 BKE_pose_update_constraint_flags(object->pose);
313         }
314         op_node = add_operation_node(&object->id,
315                                      DEG_NODE_TYPE_EVAL_POSE,
316                                      function_bind(BKE_pose_eval_proxy_copy,
317                                                    _1,
318                                                    object_cow),
319                                      DEG_OPCODE_POSE_INIT);
320         op_node->set_as_entry();
321
322         LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) {
323                 op_node = add_operation_node(&object->id,
324                                              DEG_NODE_TYPE_BONE,
325                                              pchan->name,
326                                              NULL,
327                                              DEG_OPCODE_BONE_LOCAL);
328                 op_node->set_as_entry();
329                 /* Bone is ready for solvers. */
330                 add_operation_node(&object->id,
331                                    DEG_NODE_TYPE_BONE,
332                                    pchan->name,
333                                    NULL,
334                                    DEG_OPCODE_BONE_READY);
335                 /* Bone is fully evaluated. */
336                 op_node = add_operation_node(&object->id,
337                                              DEG_NODE_TYPE_BONE,
338                                              pchan->name,
339                                              NULL,
340                                              DEG_OPCODE_BONE_DONE);
341                 op_node->set_as_exit();
342
343                 /* Custom properties. */
344                 if (pchan->prop != NULL) {
345                         add_operation_node(&object->id,
346                                            DEG_NODE_TYPE_PARAMETERS,
347                                            NULL,
348                                            DEG_OPCODE_PARAMETERS_EVAL,
349                                            pchan->name);
350                 }
351         }
352         op_node = add_operation_node(&object->id,
353                                      DEG_NODE_TYPE_EVAL_POSE,
354                                      NULL,
355                                      DEG_OPCODE_POSE_DONE);
356         op_node->set_as_exit();
357 }
358
359 }  // namespace DEG