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         const short armature_tag = armature->id.tag;
139         Scene *scene_cow;
140         Object *object_cow;
141         bArmature *armature_cow;
142         if (DEG_depsgraph_use_copy_on_write()) {
143                 /* NOTE: We need to expand both object and armature, so this way we can
144                  * safely create object level pose.
145                  */
146                 scene_cow = get_cow_datablock(scene_);
147                 object_cow = expand_cow_datablock(object);
148                 armature_cow = expand_cow_datablock(armature);
149         }
150         else {
151                 scene_cow = scene_;
152                 object_cow = object;
153                 armature_cow = armature;
154         }
155         OperationDepsNode *op_node;
156
157         /* Animation and/or drivers linking posebones to base-armature used to
158          * define them.
159          *
160          * NOTE: AnimData here is really used to control animated deform properties,
161          *       which ideally should be able to be unique across different
162          *       instances. Eventually, we need some type of proxy/isolation
163          *       mechanism in-between here to ensure that we can use same rig
164          *       multiple times in same scene.
165          */
166         if ((armature_tag & LIB_TAG_DOIT) == 0) {
167                 build_animdata(&armature->id);
168
169                 /* Make sure pose is up-to-date with armature updates. */
170                 add_operation_node(&armature->id,
171                                    DEG_NODE_TYPE_PARAMETERS,
172                                    NULL,
173                                    DEG_OPCODE_PLACEHOLDER,
174                                    "Armature Eval");
175         }
176
177         /* Rebuild pose if not up to date. */
178         if (object_cow->pose == NULL || (object->pose->flag & POSE_RECALC)) {
179                 BKE_pose_rebuild(object_cow, armature_cow);
180                 /* XXX: Without this animation gets lost in certain circumstances
181                  * after loading file. Need to investigate further since it does
182                  * not happen with simple scenes..
183                  */
184                 if (object_cow->adt) {
185                         object_cow->adt->recalc |= ADT_RECALC_ANIM;
186                 }
187         }
188
189         /* speed optimization for animation lookups */
190         if (object_cow->pose != NULL) {
191                 BKE_pose_channels_hash_make(object_cow->pose);
192                 if (object_cow->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
193                         BKE_pose_update_constraint_flags(object_cow->pose);
194                 }
195         }
196
197         /**
198          * Pose Rig Graph
199          * ==============
200          *
201          * Pose Component:
202          * - Mainly used for referencing Bone components.
203          * - This is where the evaluation operations for init/exec/cleanup
204          *   (ik) solvers live, and are later hooked up (so that they can be
205          *   interleaved during runtime) with bone-operations they depend on/affect.
206          * - init_pose_eval() and cleanup_pose_eval() are absolute first and last
207          *   steps of pose eval process. ALL bone operations must be performed
208          *   between these two...
209          *
210          * Bone Component:
211          * - Used for representing each bone within the rig
212          * - Acts to encapsulate the evaluation operations (base matrix + parenting,
213          *   and constraint stack) so that they can be easily found.
214          * - Everything else which depends on bone-results hook up to the component
215          *   only so that we can redirect those to point at either the the post-IK/
216          *   post-constraint/post-matrix steps, as needed.
217          */
218
219         /* pose eval context */
220         op_node = add_operation_node(&object->id,
221                                      DEG_NODE_TYPE_EVAL_POSE,
222                                      function_bind(BKE_pose_eval_init,
223                                                    _1,
224                                                    scene_cow,
225                                                    object_cow,
226                                                    object_cow->pose),
227                                      DEG_OPCODE_POSE_INIT);
228         op_node->set_as_entry();
229
230         op_node = add_operation_node(&object->id,
231                                      DEG_NODE_TYPE_EVAL_POSE,
232                                      function_bind(BKE_pose_eval_init_ik,
233                                                    _1,
234                                                    scene_cow,
235                                                    object_cow,
236                                                    object_cow->pose),
237                                      DEG_OPCODE_POSE_INIT_IK);
238
239         op_node = add_operation_node(&object->id,
240                                      DEG_NODE_TYPE_EVAL_POSE,
241                                      function_bind(BKE_pose_eval_flush,
242                                                    _1,
243                                                    scene_cow,
244                                                    object_cow,
245                                                    object_cow->pose),
246                                      DEG_OPCODE_POSE_DONE);
247         op_node->set_as_exit();
248
249         /* bones */
250         LINKLIST_FOREACH (bPoseChannel *, pchan, &object_cow->pose->chanbase) {
251                 /* Node for bone evaluation. */
252                 op_node = add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name, NULL,
253                                              DEG_OPCODE_BONE_LOCAL);
254                 op_node->set_as_entry();
255
256                 add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
257                                    function_bind(BKE_pose_eval_bone, _1,
258                                                  scene_cow,
259                                                  object_cow,
260                                                  pchan),
261                                    DEG_OPCODE_BONE_POSE_PARENT);
262
263                 /* NOTE: Dedicated noop for easier relationship construction. */
264                 add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
265                                    NULL,
266                                    DEG_OPCODE_BONE_READY);
267
268                 op_node = add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
269                                              function_bind(BKE_pose_bone_done, _1, pchan),
270                                              DEG_OPCODE_BONE_DONE);
271                 op_node->set_as_exit();
272                 /* Custom properties. */
273                 if (pchan->prop != NULL) {
274                         add_operation_node(&object->id,
275                                            DEG_NODE_TYPE_PARAMETERS,
276                                            NULL,
277                                            DEG_OPCODE_PARAMETERS_EVAL,
278                                            pchan->name);
279                 }
280                 /* Build constraints. */
281                 if (pchan->constraints.first != NULL) {
282                         build_pose_constraints(object, pchan);
283                 }
284                 /**
285                  * IK Solvers.
286                  *
287                  * - These require separate processing steps are pose-level
288                  *   to be executed between chains of bones (i.e. once the
289                  *   base transforms of a bunch of bones is done)
290                  *
291                  * Unsolved Issues:
292                  * - Care is needed to ensure that multi-headed trees work out the same
293                  *   as in ik-tree building
294                  * - Animated chain-lengths are a problem.
295                  */
296                 LINKLIST_FOREACH (bConstraint *, con, &pchan->constraints) {
297                         switch (con->type) {
298                                 case CONSTRAINT_TYPE_KINEMATIC:
299                                         build_ik_pose(object, pchan, con);
300                                         break;
301
302                                 case CONSTRAINT_TYPE_SPLINEIK:
303                                         build_splineik_pose(object, pchan, con);
304                                         break;
305
306                                 default:
307                                         break;
308                         }
309                 }
310                 /* Custom shape. */
311                 /* NOTE: Custom shape datablock is already remapped to CoW version. */
312                 if (pchan->custom != NULL) {
313                         build_object(NULL,
314                                      get_orig_datablock(pchan->custom),
315                                      DEG_ID_LINKED_INDIRECTLY);
316                 }
317         }
318 }
319
320 void DepsgraphNodeBuilder::build_proxy_rig(Object *object)
321 {
322         ID *obdata = (ID *)object->data;
323         OperationDepsNode *op_node;
324         Object *object_cow = get_cow_datablock(object);
325         /* Sanity check. */
326         BLI_assert(object->pose != NULL);
327         /* Animation. */
328         build_animdata(obdata);
329         /* speed optimization for animation lookups */
330         BKE_pose_channels_hash_make(object->pose);
331         if (object->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
332                 BKE_pose_update_constraint_flags(object->pose);
333         }
334         op_node = add_operation_node(&object->id,
335                                      DEG_NODE_TYPE_EVAL_POSE,
336                                      function_bind(BKE_pose_eval_proxy_copy,
337                                                    _1,
338                                                    object_cow),
339                                      DEG_OPCODE_POSE_INIT);
340         op_node->set_as_entry();
341         LINKLIST_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) {
342                 /* Local bone transform. */
343                 op_node = add_operation_node(&object->id,
344                                              DEG_NODE_TYPE_BONE,
345                                              pchan->name,
346                                              NULL,
347                                              DEG_OPCODE_BONE_LOCAL);
348                 op_node->set_as_entry();
349                 /* Bone is ready for solvers. */
350                 add_operation_node(&object->id,
351                                    DEG_NODE_TYPE_BONE,
352                                    pchan->name,
353                                    NULL,
354                                    DEG_OPCODE_BONE_READY);
355                 /* Bone is fully evaluated. */
356                 op_node = add_operation_node(&object->id,
357                                              DEG_NODE_TYPE_BONE,
358                                              pchan->name,
359                                              NULL,
360                                              DEG_OPCODE_BONE_DONE);
361                 op_node->set_as_exit();
362
363                 /* Custom properties. */
364                 if (pchan->prop != NULL) {
365                         add_operation_node(&object->id,
366                                            DEG_NODE_TYPE_PARAMETERS,
367                                            NULL,
368                                            DEG_OPCODE_PARAMETERS_EVAL,
369                                            pchan->name);
370                 }
371         }
372         op_node = add_operation_node(&object->id,
373                                      DEG_NODE_TYPE_EVAL_POSE,
374                                      NULL,
375                                      DEG_OPCODE_POSE_DONE);
376         op_node->set_as_exit();
377 }
378
379 }  // namespace DEG