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