Merge remote-tracking branch 'origin/master' into blender2.8
[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                 /* XXX: Without this animation gets lost in certain circumstances
170                  * after loading file. Need to investigate further since it does
171                  * not happen with simple scenes..
172                  */
173                 if (object->adt) {
174                         object->adt->recalc |= ADT_RECALC_ANIM;
175                 }
176         }
177         /* Speed optimization for animation lookups. */
178         if (object->pose != NULL) {
179                 BKE_pose_channels_hash_make(object->pose);
180                 if (object->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
181                         BKE_pose_update_constraint_flags(object->pose);
182                 }
183         }
184         /**
185          * Pose Rig Graph
186          * ==============
187          *
188          * Pose Component:
189          * - Mainly used for referencing Bone components.
190          * - This is where the evaluation operations for init/exec/cleanup
191          *   (ik) solvers live, and are later hooked up (so that they can be
192          *   interleaved during runtime) with bone-operations they depend on/affect.
193          * - init_pose_eval() and cleanup_pose_eval() are absolute first and last
194          *   steps of pose eval process. ALL bone operations must be performed
195          *   between these two...
196          *
197          * Bone Component:
198          * - Used for representing each bone within the rig
199          * - Acts to encapsulate the evaluation operations (base matrix + parenting,
200          *   and constraint stack) so that they can be easily found.
201          * - Everything else which depends on bone-results hook up to the component
202          *   only so that we can redirect those to point at either the the post-IK/
203          *   post-constraint/post-matrix steps, as needed.
204          */
205         /* Pose eval context. */
206         op_node = add_operation_node(&object->id,
207                                      DEG_NODE_TYPE_EVAL_POSE,
208                                      function_bind(BKE_pose_eval_init,
209                                                    _1,
210                                                    scene_cow,
211                                                    object_cow),
212                                      DEG_OPCODE_POSE_INIT);
213         op_node->set_as_entry();
214
215         op_node = add_operation_node(&object->id,
216                                      DEG_NODE_TYPE_EVAL_POSE,
217                                      function_bind(BKE_pose_eval_init_ik,
218                                                    _1,
219                                                    scene_cow,
220                                                    object_cow),
221                                      DEG_OPCODE_POSE_INIT_IK);
222
223         add_operation_node(&object->id,
224                            DEG_NODE_TYPE_EVAL_POSE,
225                            function_bind(BKE_pose_eval_cleanup,
226                                          _1,
227                                          scene_cow,
228                                          object_cow),
229                            DEG_OPCODE_POSE_CLEANUP);
230
231         op_node = add_operation_node(&object->id,
232                                      DEG_NODE_TYPE_EVAL_POSE,
233                                      function_bind(BKE_pose_eval_done,
234                                                    _1,
235                                                    object_cow),
236                                      DEG_OPCODE_POSE_DONE);
237         op_node->set_as_exit();
238         /* Bones. */
239         int pchan_index = 0;
240         LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) {
241                 /* Node for bone evaluation. */
242                 op_node = add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name, NULL,
243                                              DEG_OPCODE_BONE_LOCAL);
244                 op_node->set_as_entry();
245
246                 add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
247                                    function_bind(BKE_pose_eval_bone, _1,
248                                                  scene_cow,
249                                                  object_cow,
250                                                  pchan_index),
251                                    DEG_OPCODE_BONE_POSE_PARENT);
252
253                 /* NOTE: Dedicated noop for easier relationship construction. */
254                 add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
255                                    NULL,
256                                    DEG_OPCODE_BONE_READY);
257
258                 op_node = add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
259                                              function_bind(BKE_pose_bone_done,
260                                                            _1,
261                                                            object_cow,
262                                                            pchan_index),
263                                              DEG_OPCODE_BONE_DONE);
264
265                 /* B-Bone shape computation - the real last step if present. */
266                 if (pchan->bone != NULL && pchan->bone->segments > 1) {
267                         op_node = add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
268                                                      function_bind(BKE_pose_eval_bbone_segments, _1,
269                                                                    object_cow,
270                                                                    pchan_index),
271                                                      DEG_OPCODE_BONE_SEGMENTS);
272                 }
273
274                 op_node->set_as_exit();
275
276                 /* Custom properties. */
277                 if (pchan->prop != NULL) {
278                         add_operation_node(&object->id,
279                                            DEG_NODE_TYPE_PARAMETERS,
280                                            NULL,
281                                            DEG_OPCODE_PARAMETERS_EVAL,
282                                            pchan->name);
283                 }
284                 /* Build constraints. */
285                 if (pchan->constraints.first != NULL) {
286                         build_pose_constraints(
287                                 object, pchan, pchan_index, is_object_visible);
288                 }
289                 /**
290                  * IK Solvers.
291                  *
292                  * - These require separate processing steps are pose-level
293                  *   to be executed between chains of bones (i.e. once the
294                  *   base transforms of a bunch of bones is done)
295                  *
296                  * Unsolved Issues:
297                  * - Care is needed to ensure that multi-headed trees work out the same
298                  *   as in ik-tree building
299                  * - Animated chain-lengths are a problem.
300                  */
301                 LISTBASE_FOREACH (bConstraint *, con, &pchan->constraints) {
302                         switch (con->type) {
303                                 case CONSTRAINT_TYPE_KINEMATIC:
304                                         build_ik_pose(object, pchan, con);
305                                         break;
306
307                                 case CONSTRAINT_TYPE_SPLINEIK:
308                                         build_splineik_pose(object, pchan, con);
309                                         break;
310
311                                 default:
312                                         break;
313                         }
314                 }
315                 /* Custom shape. */
316                 if (pchan->custom != NULL) {
317                         /* TODO(sergey): Use own visibility. */
318                         build_object(
319                                 -1,
320                                 pchan->custom,
321                                 DEG_ID_LINKED_INDIRECTLY,
322                                 is_object_visible);
323                 }
324                 pchan_index++;
325         }
326 }
327
328 void DepsgraphNodeBuilder::build_proxy_rig(Object *object)
329 {
330         bArmature *armature = (bArmature *)object->data;
331         OperationDepsNode *op_node;
332         Object *object_cow = get_cow_datablock(object);
333         /* Sanity check. */
334         BLI_assert(object->pose != NULL);
335         /* Armature. */
336         build_armature(armature);
337         /* speed optimization for animation lookups */
338         BKE_pose_channels_hash_make(object->pose);
339         if (object->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
340                 BKE_pose_update_constraint_flags(object->pose);
341         }
342         op_node = add_operation_node(&object->id,
343                                      DEG_NODE_TYPE_EVAL_POSE,
344                                      function_bind(BKE_pose_eval_proxy_init,
345                                                    _1,
346                                                    object_cow),
347                                      DEG_OPCODE_POSE_INIT);
348         op_node->set_as_entry();
349
350         int pchan_index = 0;
351         LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) {
352                 op_node = add_operation_node(&object->id,
353                                              DEG_NODE_TYPE_BONE,
354                                              pchan->name,
355                                              NULL,
356                                              DEG_OPCODE_BONE_LOCAL);
357                 op_node->set_as_entry();
358                 /* Bone is ready for solvers. */
359                 add_operation_node(&object->id,
360                                    DEG_NODE_TYPE_BONE,
361                                    pchan->name,
362                                    NULL,
363                                    DEG_OPCODE_BONE_READY);
364                 /* Bone is fully evaluated. */
365                 op_node = add_operation_node(
366                         &object->id,
367                         DEG_NODE_TYPE_BONE,
368                         pchan->name,
369                         function_bind(BKE_pose_eval_proxy_copy_bone,
370                                       _1,
371                                       object_cow,
372                                       pchan_index),
373                         DEG_OPCODE_BONE_DONE);
374                 op_node->set_as_exit();
375
376                 /* Custom properties. */
377                 if (pchan->prop != NULL) {
378                         add_operation_node(&object->id,
379                                            DEG_NODE_TYPE_PARAMETERS,
380                                            NULL,
381                                            DEG_OPCODE_PARAMETERS_EVAL,
382                                            pchan->name);
383                 }
384
385                 pchan_index++;
386         }
387         op_node = add_operation_node(&object->id,
388                                      DEG_NODE_TYPE_EVAL_POSE,
389                                      function_bind(BKE_pose_eval_proxy_cleanup,
390                                                    _1,
391                                                    object_cow),
392                                      DEG_OPCODE_POSE_CLEANUP);
393         op_node = add_operation_node(&object->id,
394                                      DEG_NODE_TYPE_EVAL_POSE,
395                                      function_bind(BKE_pose_eval_proxy_done,
396                                                    _1,
397                                                    object_cow),
398                                      DEG_OPCODE_POSE_DONE);
399         op_node->set_as_exit();
400 }
401
402 }  // namespace DEG