Merge branch '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 } /* extern "C" */
54
55 #include "DEG_depsgraph.h"
56 #include "DEG_depsgraph_build.h"
57
58 #include "intern/builder/deg_builder.h"
59 #include "intern/eval/deg_eval_copy_on_write.h"
60 #include "intern/nodes/deg_node.h"
61 #include "intern/nodes/deg_node_component.h"
62 #include "intern/nodes/deg_node_operation.h"
63 #include "intern/depsgraph_types.h"
64 #include "intern/depsgraph_intern.h"
65 #include "util/deg_util_foreach.h"
66
67 namespace DEG {
68
69 void DepsgraphNodeBuilder::build_pose_constraints(Object *object,
70                                                   bPoseChannel *pchan)
71 {
72         /* create node for constraint stack */
73         add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
74                            function_bind(BKE_pose_constraints_evaluate,
75                                          _1,
76                                          get_cow_datablock(scene_),
77                                          get_cow_datablock(object),
78                                          pchan),
79                            DEG_OPCODE_BONE_CONSTRAINTS);
80 }
81
82 /* IK Solver Eval Steps */
83 void DepsgraphNodeBuilder::build_ik_pose(Object *object,
84                                          bPoseChannel *pchan,
85                                          bConstraint *con)
86 {
87         bKinematicConstraint *data = (bKinematicConstraint *)con->data;
88
89         /* Find the chain's root. */
90         bPoseChannel *rootchan = BKE_armature_ik_solver_find_root(pchan, data);
91         if (rootchan == NULL) {
92                 return;
93         }
94
95         if (has_operation_node(&object->id, DEG_NODE_TYPE_EVAL_POSE, rootchan->name,
96                                DEG_OPCODE_POSE_IK_SOLVER))
97         {
98                 return;
99         }
100
101         /* Operation node for evaluating/running IK Solver. */
102         add_operation_node(&object->id, DEG_NODE_TYPE_EVAL_POSE, rootchan->name,
103                            function_bind(BKE_pose_iktree_evaluate,
104                                          _1,
105                                          get_cow_datablock(scene_),
106                                          get_cow_datablock(object),
107                                          rootchan),
108                            DEG_OPCODE_POSE_IK_SOLVER);
109 }
110
111 /* Spline IK Eval Steps */
112 void DepsgraphNodeBuilder::build_splineik_pose(Object *object,
113                                                bPoseChannel *pchan,
114                                                bConstraint *con)
115 {
116         bSplineIKConstraint *data = (bSplineIKConstraint *)con->data;
117
118         /* Find the chain's root. */
119         bPoseChannel *rootchan = BKE_armature_splineik_solver_find_root(pchan, data);
120
121         /* Operation node for evaluating/running Spline IK Solver.
122          * Store the "root bone" of this chain in the solver, so it knows where to
123          * start.
124          */
125         add_operation_node(&object->id, DEG_NODE_TYPE_EVAL_POSE, rootchan->name,
126                            function_bind(BKE_pose_splineik_evaluate,
127                                          _1,
128                                          get_cow_datablock(scene_),
129                                          get_cow_datablock(object),
130                                          rootchan),
131                            DEG_OPCODE_POSE_SPLINE_IK_SOLVER);
132 }
133
134 /* Pose/Armature Bones Graph */
135 void DepsgraphNodeBuilder::build_rig(Object *object)
136 {
137         bArmature *armature = (bArmature *)object->data;
138         Scene *scene_cow;
139         Object *object_cow;
140         bArmature *armature_cow;
141         if (DEG_depsgraph_use_copy_on_write()) {
142                 /* NOTE: We need to expand both object and armature, so this way we can
143                  * safely create object level pose.
144                  */
145                 scene_cow = get_cow_datablock(scene_);
146                 object_cow = expand_cow_datablock(object);
147                 armature_cow = expand_cow_datablock(armature);
148         }
149         else {
150                 scene_cow = scene_;
151                 object_cow = object;
152                 armature_cow = armature;
153         }
154         OperationDepsNode *op_node;
155
156         /* Animation and/or drivers linking posebones to base-armature used to
157          * define them.
158          *
159          * NOTE: AnimData here is really used to control animated deform properties,
160          *       which ideally should be able to be unique across different
161          *       instances. Eventually, we need some type of proxy/isolation
162          *       mechanism in-between here to ensure that we can use same rig
163          *       multiple times in same scene.
164          */
165         if (!built_map_.checkIsBuilt(armature)) {
166                 build_animdata(&armature->id);
167                 /* Make sure pose is up-to-date with armature updates. */
168                 add_operation_node(&armature->id,
169                                    DEG_NODE_TYPE_PARAMETERS,
170                                    NULL,
171                                    DEG_OPCODE_PLACEHOLDER,
172                                    "Armature Eval");
173         }
174
175         /* Rebuild pose if not up to date. */
176         if (object_cow->pose == NULL || (object->pose->flag & POSE_RECALC)) {
177                 BKE_pose_rebuild(object_cow, armature_cow);
178                 /* XXX: Without this animation gets lost in certain circumstances
179                  * after loading file. Need to investigate further since it does
180                  * not happen with simple scenes..
181                  */
182                 if (object_cow->adt) {
183                         object_cow->adt->recalc |= ADT_RECALC_ANIM;
184                 }
185         }
186
187         /* speed optimization for animation lookups */
188         if (object_cow->pose != NULL) {
189                 BKE_pose_channels_hash_make(object_cow->pose);
190                 if (object_cow->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
191                         BKE_pose_update_constraint_flags(object_cow->pose);
192                 }
193         }
194
195         /**
196          * Pose Rig Graph
197          * ==============
198          *
199          * Pose Component:
200          * - Mainly used for referencing Bone components.
201          * - This is where the evaluation operations for init/exec/cleanup
202          *   (ik) solvers live, and are later hooked up (so that they can be
203          *   interleaved during runtime) with bone-operations they depend on/affect.
204          * - init_pose_eval() and cleanup_pose_eval() are absolute first and last
205          *   steps of pose eval process. ALL bone operations must be performed
206          *   between these two...
207          *
208          * Bone Component:
209          * - Used for representing each bone within the rig
210          * - Acts to encapsulate the evaluation operations (base matrix + parenting,
211          *   and constraint stack) so that they can be easily found.
212          * - Everything else which depends on bone-results hook up to the component
213          *   only so that we can redirect those to point at either the the post-IK/
214          *   post-constraint/post-matrix steps, as needed.
215          */
216
217         /* pose eval context */
218         op_node = add_operation_node(&object->id,
219                                      DEG_NODE_TYPE_EVAL_POSE,
220                                      function_bind(BKE_pose_eval_init,
221                                                    _1,
222                                                    scene_cow,
223                                                    object_cow,
224                                                    object_cow->pose),
225                                      DEG_OPCODE_POSE_INIT);
226         op_node->set_as_entry();
227
228         op_node = add_operation_node(&object->id,
229                                      DEG_NODE_TYPE_EVAL_POSE,
230                                      function_bind(BKE_pose_eval_init_ik,
231                                                    _1,
232                                                    scene_cow,
233                                                    object_cow,
234                                                    object_cow->pose),
235                                      DEG_OPCODE_POSE_INIT_IK);
236
237         op_node = add_operation_node(&object->id,
238                                      DEG_NODE_TYPE_EVAL_POSE,
239                                      function_bind(BKE_pose_eval_flush,
240                                                    _1,
241                                                    scene_cow,
242                                                    object_cow,
243                                                    object_cow->pose),
244                                      DEG_OPCODE_POSE_DONE);
245         op_node->set_as_exit();
246
247         /* bones */
248         LISTBASE_FOREACH (bPoseChannel *, pchan, &object_cow->pose->chanbase) {
249                 /* Node for bone evaluation. */
250                 op_node = add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name, NULL,
251                                              DEG_OPCODE_BONE_LOCAL);
252                 op_node->set_as_entry();
253
254                 add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
255                                    function_bind(BKE_pose_eval_bone, _1,
256                                                  scene_cow,
257                                                  object_cow,
258                                                  pchan),
259                                    DEG_OPCODE_BONE_POSE_PARENT);
260
261                 /* NOTE: Dedicated noop for easier relationship construction. */
262                 add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
263                                    NULL,
264                                    DEG_OPCODE_BONE_READY);
265
266                 op_node = add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
267                                              function_bind(BKE_pose_bone_done, _1, pchan),
268                                              DEG_OPCODE_BONE_DONE);
269                 op_node->set_as_exit();
270                 /* Custom properties. */
271                 if (pchan->prop != NULL) {
272                         add_operation_node(&object->id,
273                                            DEG_NODE_TYPE_PARAMETERS,
274                                            NULL,
275                                            DEG_OPCODE_PARAMETERS_EVAL,
276                                            pchan->name);
277                 }
278                 /* Build constraints. */
279                 if (pchan->constraints.first != NULL) {
280                         build_pose_constraints(object, pchan);
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                 /* NOTE: Custom shape datablock is already remapped to CoW version. */
310                 if (pchan->custom != NULL) {
311                         build_object(NULL,
312                                      get_orig_datablock(pchan->custom),
313                                      DEG_ID_LINKED_INDIRECTLY);
314                 }
315         }
316 }
317
318 void DepsgraphNodeBuilder::build_proxy_rig(Object *object)
319 {
320         bArmature *arm = (bArmature *)object->data;
321         OperationDepsNode *op_node;
322         Object *object_cow;
323         if (DEG_depsgraph_use_copy_on_write()) {
324                 /* NOTE: We need to expand both object and armature, so this way we can
325                  * safely create object level pose.
326                  */
327                 object_cow = expand_cow_datablock(object);
328         }
329         else {
330                 object_cow = object;
331         }
332         /* Sanity check. */
333         BLI_assert(object->pose != NULL);
334         /* Animation. */
335         build_animdata(&arm->id);
336         /* speed optimization for animation lookups */
337         BKE_pose_channels_hash_make(object->pose);
338         if (object_cow->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
339                 BKE_pose_update_constraint_flags(object_cow->pose);
340         }
341         op_node = add_operation_node(&object->id,
342                                      DEG_NODE_TYPE_EVAL_POSE,
343                                      function_bind(BKE_pose_eval_proxy_copy,
344                                                    _1,
345                                                    object_cow),
346                                      DEG_OPCODE_POSE_INIT);
347         op_node->set_as_entry();
348
349         LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) {
350                 op_node = add_operation_node(&object->id,
351                                              DEG_NODE_TYPE_BONE,
352                                              pchan->name,
353                                              NULL,
354                                              DEG_OPCODE_BONE_LOCAL);
355                 op_node->set_as_entry();
356                 /* Bone is ready for solvers. */
357                 add_operation_node(&object->id,
358                                    DEG_NODE_TYPE_BONE,
359                                    pchan->name,
360                                    NULL,
361                                    DEG_OPCODE_BONE_READY);
362                 /* Bone is fully evaluated. */
363                 op_node = add_operation_node(&object->id,
364                                              DEG_NODE_TYPE_BONE,
365                                              pchan->name,
366                                              NULL,
367                                              DEG_OPCODE_BONE_DONE);
368                 op_node->set_as_exit();
369
370                 /* Custom properties. */
371                 if (pchan->prop != NULL) {
372                         add_operation_node(&object->id,
373                                            DEG_NODE_TYPE_PARAMETERS,
374                                            NULL,
375                                            DEG_OPCODE_PARAMETERS_EVAL,
376                                            pchan->name);
377                 }
378         }
379         op_node = add_operation_node(&object->id,
380                                      DEG_NODE_TYPE_EVAL_POSE,
381                                      NULL,
382                                      DEG_OPCODE_POSE_DONE);
383         op_node->set_as_exit();
384 }
385
386 }  // namespace DEG