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(Scene *scene,
70                                                   Object *ob,
71                                                   bPoseChannel *pchan)
72 {
73         /* create node for constraint stack */
74         add_operation_node(&ob->id, DEG_NODE_TYPE_BONE, pchan->name,
75                            function_bind(BKE_pose_constraints_evaluate,
76                                          _1,
77                                          get_cow_datablock(scene),
78                                          get_cow_datablock(ob),
79                                          pchan),
80                            DEG_OPCODE_BONE_CONSTRAINTS);
81 }
82
83 /* IK Solver Eval Steps */
84 void DepsgraphNodeBuilder::build_ik_pose(Scene *scene,
85                                          Object *ob,
86                                          bPoseChannel *pchan,
87                                          bConstraint *con)
88 {
89         bKinematicConstraint *data = (bKinematicConstraint *)con->data;
90
91         /* Find the chain's root. */
92         bPoseChannel *rootchan = BKE_armature_ik_solver_find_root(pchan, data);
93
94         if (has_operation_node(&ob->id, DEG_NODE_TYPE_EVAL_POSE, rootchan->name,
95                                DEG_OPCODE_POSE_IK_SOLVER))
96         {
97                 return;
98         }
99
100         /* Operation node for evaluating/running IK Solver. */
101         add_operation_node(&ob->id, DEG_NODE_TYPE_EVAL_POSE, rootchan->name,
102                            function_bind(BKE_pose_iktree_evaluate, _1,
103                                          get_cow_datablock(scene),
104                                          get_cow_datablock(ob),
105                                          rootchan),
106                            DEG_OPCODE_POSE_IK_SOLVER);
107 }
108
109 /* Spline IK Eval Steps */
110 void DepsgraphNodeBuilder::build_splineik_pose(Scene *scene,
111                                                Object *ob,
112                                                bPoseChannel *pchan,
113                                                bConstraint *con)
114 {
115         bSplineIKConstraint *data = (bSplineIKConstraint *)con->data;
116
117         /* Find the chain's root. */
118         bPoseChannel *rootchan = BKE_armature_splineik_solver_find_root(pchan, data);
119
120         /* Operation node for evaluating/running Spline IK Solver.
121          * Store the "root bone" of this chain in the solver, so it knows where to
122          * start.
123          */
124         add_operation_node(&ob->id, DEG_NODE_TYPE_EVAL_POSE, rootchan->name,
125                            function_bind(BKE_pose_splineik_evaluate,
126                                          _1,
127                                          get_cow_datablock(scene),
128                                          get_cow_datablock(ob),
129                                          rootchan),
130                            DEG_OPCODE_POSE_SPLINE_IK_SOLVER);
131 }
132
133 /* Pose/Armature Bones Graph */
134 void DepsgraphNodeBuilder::build_rig(Scene *scene, Object *object)
135 {
136         bArmature *armature = (bArmature *)object->data;
137         const short armature_tag = armature->id.tag;
138 #ifdef WITH_COPY_ON_WRITE
139         /* NOTE: We need to expand both object and armature, so this way we can
140          * safely create object level pose.
141          */
142         Scene *scene_cow = get_cow_datablock(scene);
143         Object *object_cow = expand_cow_datablock(object);
144         bArmature *armature_cow = expand_cow_datablock(armature);
145 #else
146         Scene *scene_cow = scene;
147         Object *object_cow = object;
148         bArmature *armature_cow = armature;
149 #endif
150         OperationDepsNode *op_node;
151
152         /* Animation and/or drivers linking posebones to base-armature used to
153          * define them.
154          *
155          * NOTE: AnimData here is really used to control animated deform properties,
156          *       which ideally should be able to be unique across different
157          *       instances. Eventually, we need some type of proxy/isolation
158          *       mechanism in-between here to ensure that we can use same rig
159          *       multiple times in same scene.
160          */
161         if ((armature_tag & LIB_TAG_DOIT) == 0) {
162                 build_animdata(&armature->id);
163
164                 /* Make sure pose is up-to-date with armature updates. */
165                 add_operation_node(&armature->id,
166                                    DEG_NODE_TYPE_PARAMETERS,
167                                    NULL,
168                                    DEG_OPCODE_PLACEHOLDER,
169                                    "Armature Eval");
170         }
171
172         /* Rebuild pose if not up to date. */
173         if (object_cow->pose == NULL || (object->pose->flag & POSE_RECALC)) {
174                 BKE_pose_rebuild(object_cow, armature_cow);
175                 /* XXX: Without this animation gets lost in certain circumstances
176                  * after loading file. Need to investigate further since it does
177                  * not happen with simple scenes..
178                  */
179                 if (object_cow->adt) {
180                         object_cow->adt->recalc |= ADT_RECALC_ANIM;
181                 }
182         }
183
184         /* speed optimization for animation lookups */
185         if (object_cow->pose != NULL) {
186                 BKE_pose_channels_hash_make(object_cow->pose);
187                 if (object_cow->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
188                         BKE_pose_update_constraint_flags(object_cow->pose);
189                 }
190         }
191
192         /**
193          * Pose Rig Graph
194          * ==============
195          *
196          * Pose Component:
197          * - Mainly used for referencing Bone components.
198          * - This is where the evaluation operations for init/exec/cleanup
199          *   (ik) solvers live, and are later hooked up (so that they can be
200          *   interleaved during runtime) with bone-operations they depend on/affect.
201          * - init_pose_eval() and cleanup_pose_eval() are absolute first and last
202          *   steps of pose eval process. ALL bone operations must be performed
203          *   between these two...
204          *
205          * Bone Component:
206          * - Used for representing each bone within the rig
207          * - Acts to encapsulate the evaluation operations (base matrix + parenting,
208          *   and constraint stack) so that they can be easily found.
209          * - Everything else which depends on bone-results hook up to the component
210          *   only so that we can redirect those to point at either the the post-IK/
211          *   post-constraint/post-matrix steps, as needed.
212          */
213
214         /* pose eval context */
215         op_node = add_operation_node(&object->id,
216                                      DEG_NODE_TYPE_EVAL_POSE,
217                                      function_bind(BKE_pose_eval_init,
218                                                    _1,
219                                                    scene_cow,
220                                                    object_cow,
221                                                    object_cow->pose),
222                                      DEG_OPCODE_POSE_INIT);
223         op_node->set_as_entry();
224
225         op_node = add_operation_node(&object->id,
226                                      DEG_NODE_TYPE_EVAL_POSE,
227                                      function_bind(BKE_pose_eval_init_ik,
228                                                    _1,
229                                                    scene_cow,
230                                                    object_cow,
231                                                    object_cow->pose),
232                                      DEG_OPCODE_POSE_INIT_IK);
233
234         op_node = add_operation_node(&object->id,
235                                      DEG_NODE_TYPE_EVAL_POSE,
236                                      function_bind(BKE_pose_eval_flush,
237                                                    _1,
238                                                    scene_cow,
239                                                    object_cow,
240                                                    object_cow->pose),
241                                      DEG_OPCODE_POSE_DONE);
242         op_node->set_as_exit();
243
244         /* bones */
245         LINKLIST_FOREACH (bPoseChannel *, pchan, &object_cow->pose->chanbase) {
246                 /* node for bone eval */
247                 op_node = add_operation_node(&object->id, DEG_NODE_TYPE_BONE,
248                                              pchan->name, NULL, DEG_OPCODE_BONE_LOCAL);
249                 op_node->set_as_entry();
250
251                 add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
252                                    function_bind(BKE_pose_eval_bone, _1,
253                                                  scene_cow,
254                                                  object_cow,
255                                                  pchan),
256                                    DEG_OPCODE_BONE_POSE_PARENT);
257
258                 /* NOTE: Dedicated noop for easier relationship construction. */
259                 add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
260                                    NULL,
261                                    DEG_OPCODE_BONE_READY);
262
263                 op_node = add_operation_node(&object->id, DEG_NODE_TYPE_BONE, pchan->name,
264                                              function_bind(BKE_pose_bone_done, _1, pchan),
265                                              DEG_OPCODE_BONE_DONE);
266                 op_node->set_as_exit();
267                 /* Build constraints. */
268                 if (pchan->constraints.first != NULL) {
269                         build_pose_constraints(scene, object, pchan);
270                 }
271                 /**
272                  * IK Solvers.
273                  *
274                  * - These require separate processing steps are pose-level
275                  *   to be executed between chains of bones (i.e. once the
276                  *   base transforms of a bunch of bones is done)
277                  *
278                  * Unsolved Issues:
279                  * - Care is needed to ensure that multi-headed trees work out the same
280                  *   as in ik-tree building
281                  * - Animated chain-lengths are a problem.
282                  */
283                 LINKLIST_FOREACH (bConstraint *, con, &pchan->constraints) {
284                         switch (con->type) {
285                                 case CONSTRAINT_TYPE_KINEMATIC:
286                                         build_ik_pose(scene, object, pchan, con);
287                                         break;
288
289                                 case CONSTRAINT_TYPE_SPLINEIK:
290                                         build_splineik_pose(scene, object, pchan, con);
291                                         break;
292
293                                 default:
294                                         break;
295                         }
296                 }
297                 /* Custom shape. */
298                 /* NOTE: Custom shape datablock is already remapped to CoW version. */
299                 if (pchan->custom != NULL) {
300                         build_object(scene, get_orig_datablock(pchan->custom));
301                 }
302         }
303 }
304
305 void DepsgraphNodeBuilder::build_proxy_rig(Object *ob)
306 {
307         ID *obdata = (ID *)ob->data;
308         OperationDepsNode *op_node;
309
310         build_animdata(obdata);
311
312         BLI_assert(ob->pose != NULL);
313
314         /* speed optimization for animation lookups */
315         BKE_pose_channels_hash_make(ob->pose);
316         if (ob->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
317                 BKE_pose_update_constraint_flags(ob->pose);
318         }
319
320         op_node = add_operation_node(&ob->id,
321                                      DEG_NODE_TYPE_EVAL_POSE,
322                                      function_bind(BKE_pose_eval_proxy_copy, _1, ob),
323                                      DEG_OPCODE_POSE_INIT);
324         op_node->set_as_entry();
325
326         LINKLIST_FOREACH (bPoseChannel *, pchan, &ob->pose->chanbase) {
327                 op_node = add_operation_node(&ob->id, DEG_NODE_TYPE_BONE, pchan->name,
328                                              NULL, DEG_OPCODE_BONE_LOCAL);
329                 op_node->set_as_entry();
330
331                 add_operation_node(&ob->id, DEG_NODE_TYPE_BONE, pchan->name,
332                                    NULL, DEG_OPCODE_BONE_READY);
333
334                 op_node = add_operation_node(&ob->id, DEG_NODE_TYPE_BONE, pchan->name,
335                                              NULL, DEG_OPCODE_BONE_DONE);
336                 op_node->set_as_exit();
337         }
338
339         op_node = add_operation_node(&ob->id, DEG_NODE_TYPE_EVAL_POSE,
340                                      NULL, DEG_OPCODE_POSE_DONE);
341         op_node->set_as_exit();
342 }
343
344 }  // namespace DEG