Depsgraph: Comb code to a better state all over
[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/node/deg_node.h"
62 #include "intern/node/deg_node_component.h"
63 #include "intern/node/deg_node_operation.h"
64 #include "intern/depsgraph_type.h"
65
66 namespace DEG {
67
68 void DepsgraphNodeBuilder::build_pose_constraints(
69         Object *object,
70         bPoseChannel *pchan,
71         int pchan_index,
72         bool is_object_visible)
73 {
74         /* Pull indirect dependencies via constraints. */
75         BuilderWalkUserData data;
76         data.builder = this;
77         data.is_parent_visible = is_object_visible;
78         BKE_constraints_id_loop(&pchan->constraints, constraint_walk, &data);
79         /* Create node for constraint stack. */
80         add_operation_node(&object->id, NodeType::BONE, pchan->name,
81                            function_bind(BKE_pose_constraints_evaluate,
82                                          _1,
83                                          get_cow_datablock(scene_),
84                                          get_cow_datablock(object),
85                                          pchan_index),
86                            OperationCode::BONE_CONSTRAINTS);
87 }
88
89 /* IK Solver Eval Steps */
90 void DepsgraphNodeBuilder::build_ik_pose(Object *object,
91                                          bPoseChannel *pchan,
92                                          bConstraint *con)
93 {
94         bKinematicConstraint *data = (bKinematicConstraint *)con->data;
95
96         /* Find the chain's root. */
97         bPoseChannel *rootchan = BKE_armature_ik_solver_find_root(pchan, data);
98         if (rootchan == NULL) {
99                 return;
100         }
101
102         if (has_operation_node(&object->id, NodeType::EVAL_POSE, rootchan->name,
103                                OperationCode::POSE_IK_SOLVER))
104         {
105                 return;
106         }
107
108         int rootchan_index = BLI_findindex(&object->pose->chanbase, rootchan);
109         BLI_assert(rootchan_index != -1);
110         /* Operation node for evaluating/running IK Solver. */
111         add_operation_node(&object->id, NodeType::EVAL_POSE, rootchan->name,
112                            function_bind(BKE_pose_iktree_evaluate,
113                                          _1,
114                                          get_cow_datablock(scene_),
115                                          get_cow_datablock(object),
116                                          rootchan_index),
117                            OperationCode::POSE_IK_SOLVER);
118 }
119
120 /* Spline IK Eval Steps */
121 void DepsgraphNodeBuilder::build_splineik_pose(Object *object,
122                                                bPoseChannel *pchan,
123                                                bConstraint *con)
124 {
125         bSplineIKConstraint *data = (bSplineIKConstraint *)con->data;
126
127         /* Find the chain's root. */
128         bPoseChannel *rootchan = BKE_armature_splineik_solver_find_root(pchan, data);
129
130         /* Operation node for evaluating/running Spline IK Solver.
131          * Store the "root bone" of this chain in the solver, so it knows where to
132          * start. */
133         int rootchan_index = BLI_findindex(&object->pose->chanbase, rootchan);
134         BLI_assert(rootchan_index != -1);
135         add_operation_node(&object->id, NodeType::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                            OperationCode::POSE_SPLINE_IK_SOLVER);
142 }
143
144 /* Pose/Armature Bones Graph */
145 void DepsgraphNodeBuilder::build_rig(Object *object, bool is_object_visible)
146 {
147         bArmature *armature = (bArmature *)object->data;
148         Scene *scene_cow = get_cow_datablock(scene_);
149         Object *object_cow = get_cow_datablock(object);
150         OperationNode *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         /* Armature. */
160         build_armature(armature);
161         /* Rebuild pose if not up to date. */
162         if (object->pose == NULL || (object->pose->flag & POSE_RECALC)) {
163                 /* By definition, no need to tag depsgraph as dirty from here, so we can pass NULL bmain. */
164                 BKE_pose_rebuild(NULL, object, armature, true);
165         }
166         /* Speed optimization for animation lookups. */
167         if (object->pose != NULL) {
168                 BKE_pose_channels_hash_make(object->pose);
169                 if (object->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
170                         BKE_pose_update_constraint_flags(object->pose);
171                 }
172         }
173         /**
174          * Pose Rig Graph
175          * ==============
176          *
177          * Pose Component:
178          * - Mainly used for referencing Bone components.
179          * - This is where the evaluation operations for init/exec/cleanup
180          *   (ik) solvers live, and are later hooked up (so that they can be
181          *   interleaved during runtime) with bone-operations they depend on/affect.
182          * - init_pose_eval() and cleanup_pose_eval() are absolute first and last
183          *   steps of pose eval process. ALL bone operations must be performed
184          *   between these two...
185          *
186          * Bone Component:
187          * - Used for representing each bone within the rig
188          * - Acts to encapsulate the evaluation operations (base matrix + parenting,
189          *   and constraint stack) so that they can be easily found.
190          * - Everything else which depends on bone-results hook up to the component
191          *   only so that we can redirect those to point at either the the post-IK/
192          *   post-constraint/post-matrix steps, as needed. */
193         /* Pose eval context. */
194         op_node = add_operation_node(&object->id,
195                                      NodeType::EVAL_POSE,
196                                      function_bind(BKE_pose_eval_init,
197                                                    _1,
198                                                    scene_cow,
199                                                    object_cow),
200                                      OperationCode::POSE_INIT);
201         op_node->set_as_entry();
202
203         op_node = add_operation_node(&object->id,
204                                      NodeType::EVAL_POSE,
205                                      function_bind(BKE_pose_eval_init_ik,
206                                                    _1,
207                                                    scene_cow,
208                                                    object_cow),
209                                      OperationCode::POSE_INIT_IK);
210
211         add_operation_node(&object->id,
212                            NodeType::EVAL_POSE,
213                            function_bind(BKE_pose_eval_cleanup,
214                                          _1,
215                                          scene_cow,
216                                          object_cow),
217                            OperationCode::POSE_CLEANUP);
218
219         op_node = add_operation_node(&object->id,
220                                      NodeType::EVAL_POSE,
221                                      function_bind(BKE_pose_eval_done,
222                                                    _1,
223                                                    object_cow),
224                                      OperationCode::POSE_DONE);
225         op_node->set_as_exit();
226         /* Bones. */
227         int pchan_index = 0;
228         LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) {
229                 /* Node for bone evaluation. */
230                 op_node = add_operation_node(&object->id, NodeType::BONE, pchan->name, NULL,
231                                              OperationCode::BONE_LOCAL);
232                 op_node->set_as_entry();
233
234                 add_operation_node(&object->id, NodeType::BONE, pchan->name,
235                                    function_bind(BKE_pose_eval_bone, _1,
236                                                  scene_cow,
237                                                  object_cow,
238                                                  pchan_index),
239                                    OperationCode::BONE_POSE_PARENT);
240
241                 /* NOTE: Dedicated noop for easier relationship construction. */
242                 add_operation_node(&object->id, NodeType::BONE, pchan->name,
243                                    NULL,
244                                    OperationCode::BONE_READY);
245
246                 op_node = add_operation_node(&object->id, NodeType::BONE, pchan->name,
247                                              function_bind(BKE_pose_bone_done,
248                                                            _1,
249                                                            object_cow,
250                                                            pchan_index),
251                                              OperationCode::BONE_DONE);
252
253                 /* B-Bone shape computation - the real last step if present. */
254                 if (pchan->bone != NULL && pchan->bone->segments > 1) {
255                         op_node = add_operation_node(&object->id, NodeType::BONE, pchan->name,
256                                                      function_bind(BKE_pose_eval_bbone_segments, _1,
257                                                                    object_cow,
258                                                                    pchan_index),
259                                                      OperationCode::BONE_SEGMENTS);
260                 }
261
262                 op_node->set_as_exit();
263
264                 /* Custom properties. */
265                 if (pchan->prop != NULL) {
266                         add_operation_node(&object->id,
267                                            NodeType::PARAMETERS,
268                                            NULL,
269                                            OperationCode::PARAMETERS_EVAL,
270                                            pchan->name);
271                 }
272                 /* Build constraints. */
273                 if (pchan->constraints.first != NULL) {
274                         build_pose_constraints(
275                                 object, pchan, pchan_index, is_object_visible);
276                 }
277                 /**
278                  * IK Solvers.
279                  *
280                  * - These require separate processing steps are pose-level
281                  *   to be executed between chains of bones (i.e. once the
282                  *   base transforms of a bunch of bones is done)
283                  *
284                  * Unsolved Issues:
285                  * - Care is needed to ensure that multi-headed trees work out the same
286                  *   as in ik-tree building
287                  * - Animated chain-lengths are a problem. */
288                 LISTBASE_FOREACH (bConstraint *, con, &pchan->constraints) {
289                         switch (con->type) {
290                                 case CONSTRAINT_TYPE_KINEMATIC:
291                                         build_ik_pose(object, pchan, con);
292                                         break;
293
294                                 case CONSTRAINT_TYPE_SPLINEIK:
295                                         build_splineik_pose(object, pchan, con);
296                                         break;
297
298                                 default:
299                                         break;
300                         }
301                 }
302                 /* Custom shape. */
303                 if (pchan->custom != NULL) {
304                         /* TODO(sergey): Use own visibility. */
305                         build_object(
306                                 -1,
307                                 pchan->custom,
308                                 DEG_ID_LINKED_INDIRECTLY,
309                                 is_object_visible);
310                 }
311                 pchan_index++;
312         }
313 }
314
315 void DepsgraphNodeBuilder::build_proxy_rig(Object *object)
316 {
317         bArmature *armature = (bArmature *)object->data;
318         OperationNode *op_node;
319         Object *object_cow = get_cow_datablock(object);
320         /* Sanity check. */
321         BLI_assert(object->pose != NULL);
322         /* Armature. */
323         build_armature(armature);
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                                      NodeType::EVAL_POSE,
331                                      function_bind(BKE_pose_eval_proxy_init,
332                                                    _1,
333                                                    object_cow),
334                                      OperationCode::POSE_INIT);
335         op_node->set_as_entry();
336
337         int pchan_index = 0;
338         LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) {
339                 op_node = add_operation_node(&object->id,
340                                              NodeType::BONE,
341                                              pchan->name,
342                                              NULL,
343                                              OperationCode::BONE_LOCAL);
344                 op_node->set_as_entry();
345                 /* Bone is ready for solvers. */
346                 add_operation_node(&object->id,
347                                    NodeType::BONE,
348                                    pchan->name,
349                                    NULL,
350                                    OperationCode::BONE_READY);
351                 /* Bone is fully evaluated. */
352                 op_node = add_operation_node(
353                         &object->id,
354                         NodeType::BONE,
355                         pchan->name,
356                         function_bind(BKE_pose_eval_proxy_copy_bone,
357                                       _1,
358                                       object_cow,
359                                       pchan_index),
360                         OperationCode::BONE_DONE);
361                 op_node->set_as_exit();
362
363                 /* Custom properties. */
364                 if (pchan->prop != NULL) {
365                         add_operation_node(&object->id,
366                                            NodeType::PARAMETERS,
367                                            NULL,
368                                            OperationCode::PARAMETERS_EVAL,
369                                            pchan->name);
370                 }
371
372                 pchan_index++;
373         }
374         op_node = add_operation_node(&object->id,
375                                      NodeType::EVAL_POSE,
376                                      function_bind(BKE_pose_eval_proxy_cleanup,
377                                                    _1,
378                                                    object_cow),
379                                      OperationCode::POSE_CLEANUP);
380         op_node = add_operation_node(&object->id,
381                                      NodeType::EVAL_POSE,
382                                      function_bind(BKE_pose_eval_proxy_done,
383                                                    _1,
384                                                    object_cow),
385                                      OperationCode::POSE_DONE);
386         op_node->set_as_exit();
387 }
388
389 }  // namespace DEG