Cleanup: remove redundant, invalid info from headers
[blender.git] / source / blender / depsgraph / intern / builder / deg_builder_nodes_rig.cc
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2013 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file blender/depsgraph/intern/builder/deg_builder_nodes_rig.cc
21  *  \ingroup depsgraph
22  *
23  * Methods for constructing depsgraph's nodes
24  */
25
26 #include "intern/builder/deg_builder_nodes.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "BLI_utildefines.h"
34 #include "BLI_blenlib.h"
35 #include "BLI_string.h"
36
37 extern "C" {
38 #include "DNA_anim_types.h"
39 #include "DNA_armature_types.h"
40 #include "DNA_constraint_types.h"
41 #include "DNA_object_types.h"
42
43 #include "BKE_action.h"
44 #include "BKE_armature.h"
45 #include "BKE_constraint.h"
46 } /* extern "C" */
47
48 #include "DEG_depsgraph.h"
49 #include "DEG_depsgraph_build.h"
50
51 #include "intern/builder/deg_builder.h"
52 #include "intern/nodes/deg_node.h"
53 #include "intern/nodes/deg_node_component.h"
54 #include "intern/nodes/deg_node_operation.h"
55 #include "intern/depsgraph_types.h"
56 #include "intern/depsgraph_intern.h"
57 #include "util/deg_util_foreach.h"
58
59 namespace DEG {
60
61 void DepsgraphNodeBuilder::build_pose_constraints(Object *object,
62                                                   bPoseChannel *pchan,
63                                                   int pchan_index)
64 {
65         /* Pull indirect dependencies via constraints. */
66         BuilderWalkUserData data;
67         data.builder = this;
68         BKE_constraints_id_loop(&pchan->constraints, constraint_walk, &data);
69         /* Create node for constraint stack. */
70         add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
71                            function_bind(BKE_pose_constraints_evaluate,
72                                          _1,
73                                          scene_,
74                                          object,
75                                          pchan_index),
76                            DEG_OPCODE_BONE_CONSTRAINTS);
77 }
78
79 /* IK Solver Eval Steps */
80 void DepsgraphNodeBuilder::build_ik_pose(Object *object,
81                                          bPoseChannel *pchan,
82                                          bConstraint *con)
83 {
84         bKinematicConstraint *data = (bKinematicConstraint *)con->data;
85
86         /* Find the chain's root. */
87         bPoseChannel *rootchan = BKE_armature_ik_solver_find_root(pchan, data);
88         if (rootchan == NULL) {
89                 return;
90         }
91
92         if (has_operation_node(&object->id, DEG_NODE_TYPE_EVAL_POSE, rootchan->name,
93                                DEG_OPCODE_POSE_IK_SOLVER))
94         {
95                 return;
96         }
97
98         int rootchan_index = BLI_findindex(&object->pose->chanbase, rootchan);
99         BLI_assert(rootchan_index != -1);
100         /* Operation node for evaluating/running IK Solver. */
101         add_operation_node(&object->id, DEG_NODE_TYPE_EVAL_POSE, rootchan->name,
102                            function_bind(BKE_pose_iktree_evaluate,
103                                          _1,
104                                          scene_,
105                                          object,
106                                          rootchan_index),
107                            DEG_OPCODE_POSE_IK_SOLVER);
108 }
109
110 /* Spline IK Eval Steps */
111 void DepsgraphNodeBuilder::build_splineik_pose(Object *object,
112                                                bPoseChannel *pchan,
113                                                bConstraint *con)
114 {
115         bSplineIKConstraint *data = (bSplineIKConstraint *)con->data;
116
117         /* Find the chain's root. */
118         bPoseChannel *rootchan = BKE_armature_splineik_solver_find_root(pchan, data);
119
120         /* Operation node for evaluating/running Spline IK Solver.
121          * Store the "root bone" of this chain in the solver, so it knows where to start.
122          */
123         int rootchan_index = BLI_findindex(&object->pose->chanbase, rootchan);
124         BLI_assert(rootchan_index != -1);
125         add_operation_node(&object->id, DEG_NODE_TYPE_EVAL_POSE, rootchan->name,
126                            function_bind(BKE_pose_splineik_evaluate,
127                                          _1,
128                                          scene_,
129                                          object,
130                                          rootchan_index),
131                            DEG_OPCODE_POSE_SPLINE_IK_SOLVER);
132 }
133
134 /* Pose/Armature Bones Graph */
135 void DepsgraphNodeBuilder::build_rig(Object *object)
136 {
137         bArmature *arm = (bArmature *)object->data;
138         OperationDepsNode *op_node;
139
140         /* animation and/or drivers linking posebones to base-armature used to define them
141          * NOTE: AnimData here is really used to control animated deform properties,
142          *       which ideally should be able to be unique across different instances.
143          *       Eventually, we need some type of proxy/isolation mechanism in-between here
144          *       to ensure that we can use same rig multiple times in same scene...
145          */
146         if (!built_map_.checkIsBuiltAndTag(arm)) {
147                 build_animdata(&arm->id);
148                 /* Make sure pose is up-to-date with armature updates. */
149                 add_operation_node(&arm->id,
150                                    DEG_NODE_TYPE_PARAMETERS,
151                                    NULL,
152                                    DEG_OPCODE_PLACEHOLDER,
153                                    "Armature Eval");
154         }
155
156         /* Rebuild pose if not up to date. */
157         if (object->pose == NULL || (object->pose->flag & POSE_RECALC)) {
158                 BKE_pose_rebuild_ex(object, arm, false);
159                 /* XXX: Without this animation gets lost in certain circumstances
160                  * after loading file. Need to investigate further since it does
161                  * not happen with simple scenes..
162                  */
163                 if (object->adt) {
164                         object->adt->recalc |= ADT_RECALC_ANIM;
165                 }
166         }
167
168         /* speed optimization for animation lookups */
169         if (object->pose) {
170                 BKE_pose_channels_hash_make(object->pose);
171                 if (object->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
172                         BKE_pose_update_constraint_flags(object->pose);
173                 }
174         }
175
176         /**
177          * Pose Rig Graph
178          * ==============
179          *
180          * Pose Component:
181          * - Mainly used for referencing Bone components.
182          * - This is where the evaluation operations for init/exec/cleanup
183          *   (ik) solvers live, and are later hooked up (so that they can be
184          *   interleaved during runtime) with bone-operations they depend on/affect.
185          * - init_pose_eval() and cleanup_pose_eval() are absolute first and last
186          *   steps of pose eval process. ALL bone operations must be performed
187          *   between these two...
188          *
189          * Bone Component:
190          * - Used for representing each bone within the rig
191          * - Acts to encapsulate the evaluation operations (base matrix + parenting,
192          *   and constraint stack) so that they can be easily found.
193          * - Everything else which depends on bone-results hook up to the component only
194          *   so that we can redirect those to point at either the the post-IK/
195          *   post-constraint/post-matrix steps, as needed.
196          */
197
198         /* pose eval context */
199         op_node = add_operation_node(&object->id,
200                                      DEG_NODE_TYPE_EVAL_POSE,
201                                      function_bind(BKE_pose_eval_init,
202                                                    _1,
203                                                    scene_,
204                                                    object),
205                                      DEG_OPCODE_POSE_INIT);
206         op_node->set_as_entry();
207
208         op_node = add_operation_node(&object->id,
209                                      DEG_NODE_TYPE_EVAL_POSE,
210                                      function_bind(BKE_pose_eval_init_ik,
211                                                    _1,
212                                                    scene_,
213                                                    object),
214                                      DEG_OPCODE_POSE_INIT_IK);
215
216         op_node = add_operation_node(&object->id,
217                                      DEG_NODE_TYPE_EVAL_POSE,
218                                      function_bind(BKE_pose_eval_flush,
219                                                    _1,
220                                                    scene_,
221                                                    object),
222                                      DEG_OPCODE_POSE_DONE);
223         op_node->set_as_exit();
224
225         /* bones */
226         int pchan_index = 0;
227         LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) {
228                 /* Node for bone evaluation. */
229                 op_node = add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name, NULL,
230                                              DEG_OPCODE_BONE_LOCAL);
231                 op_node->set_as_entry();
232
233                 add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
234                                    function_bind(BKE_pose_eval_bone, _1, scene_, object, pchan_index),
235                                    DEG_OPCODE_BONE_POSE_PARENT);
236
237                 add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
238                                    NULL, /* NOTE: dedicated noop for easier relationship construction */
239                                    DEG_OPCODE_BONE_READY);
240
241                 op_node = add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
242                                              function_bind(BKE_pose_bone_done,
243                                                            _1,
244                                                            object,
245                                                            pchan_index),
246                                              DEG_OPCODE_BONE_DONE);
247                 op_node->set_as_exit();
248                 /* Custom properties. */
249                 if (pchan->prop != NULL) {
250                         add_operation_node(&object->id,
251                                            DEG_NODE_TYPE_PARAMETERS,
252                                            NULL,
253                                            DEG_OPCODE_PARAMETERS_EVAL,
254                                            pchan->name);
255                 }
256                 /* Constraints. */
257                 if (pchan->constraints.first != NULL) {
258                         build_pose_constraints(object, pchan, pchan_index);
259                 }
260                 /**
261                  * IK Solvers.
262                  *
263                  * - These require separate processing steps are pose-level
264                  *   to be executed between chains of bones (i.e. once the
265                  *   base transforms of a bunch of bones is done)
266                  *
267                  * Unsolved Issues:
268                  * - Care is needed to ensure that multi-headed trees work out the same
269                  *   as in ik-tree building.
270                  * - Animated chain-lengths are a problem...
271                  */
272                 LISTBASE_FOREACH (bConstraint *, con, &pchan->constraints) {
273                         switch (con->type) {
274                                 case CONSTRAINT_TYPE_KINEMATIC:
275                                         build_ik_pose(object, pchan, con);
276                                         break;
277
278                                 case CONSTRAINT_TYPE_SPLINEIK:
279                                         build_splineik_pose(object, pchan, con);
280                                         break;
281
282                                 default:
283                                         break;
284                         }
285                 }
286
287                 /* Custom shape. */
288                 if (pchan->custom != NULL) {
289                         build_object(NULL, pchan->custom);
290                 }
291
292                 pchan_index++;
293         }
294 }
295
296 void DepsgraphNodeBuilder::build_proxy_rig(Object *object)
297 {
298         bArmature *arm = (bArmature *)object->data;
299         OperationDepsNode *op_node;
300
301         build_animdata(&arm->id);
302
303         BLI_assert(object->pose != NULL);
304
305         /* speed optimization for animation lookups */
306         BKE_pose_channels_hash_make(object->pose);
307         if (object->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
308                 BKE_pose_update_constraint_flags(object->pose);
309         }
310
311         op_node = add_operation_node(&object->id,
312                                      DEG_NODE_TYPE_EVAL_POSE,
313                                      function_bind(BKE_pose_eval_proxy_copy, _1, object),
314                                      DEG_OPCODE_POSE_INIT);
315         op_node->set_as_entry();
316
317
318         LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) {
319                 op_node = add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
320                                              NULL, DEG_OPCODE_BONE_LOCAL);
321                 op_node->set_as_entry();
322
323                 add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
324                                    NULL, DEG_OPCODE_BONE_READY);
325
326                 op_node = add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
327                                              NULL, DEG_OPCODE_BONE_DONE);
328                 op_node->set_as_exit();
329
330                 /* Custom properties. */
331                 if (pchan->prop != NULL) {
332                         add_operation_node(&object->id,
333                                            DEG_NODE_TYPE_PARAMETERS,
334                                            NULL,
335                                            DEG_OPCODE_PARAMETERS_EVAL,
336                                            pchan->name);
337                 }
338         }
339
340         op_node = add_operation_node(&object->id, DEG_NODE_TYPE_EVAL_POSE,
341                                      NULL, DEG_OPCODE_POSE_DONE);
342         op_node->set_as_exit();
343 }
344
345 }  // namespace DEG