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