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