e307909cb1140ab829ccd7cb45ba646a7e13a552
[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 extern "C" {
41 #include "BLI_blenlib.h"
42 #include "BLI_string.h"
43 #include "BLI_utildefines.h"
44
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
50 #include "BKE_action.h"
51 #include "BKE_armature.h"
52
53 #include "DEG_depsgraph.h"
54 #include "DEG_depsgraph_build.h"
55 } /* extern "C" */
56
57 #include "intern/builder/deg_builder.h"
58 #include "intern/nodes/deg_node.h"
59 #include "intern/nodes/deg_node_component.h"
60 #include "intern/nodes/deg_node_operation.h"
61 #include "intern/depsgraph_types.h"
62 #include "intern/depsgraph_intern.h"
63 #include "util/deg_util_foreach.h"
64
65 namespace DEG {
66
67 void DepsgraphNodeBuilder::build_pose_constraints(Scene *scene, Object *ob, bPoseChannel *pchan)
68 {
69         /* create node for constraint stack */
70         add_operation_node(&ob->id, DEPSNODE_TYPE_BONE, pchan->name,
71                            DEPSOP_TYPE_EXEC,
72                            function_bind(BKE_pose_constraints_evaluate, _1, scene, ob, pchan),
73                            DEG_OPCODE_BONE_CONSTRAINTS);
74 }
75
76 /* IK Solver Eval Steps */
77 void DepsgraphNodeBuilder::build_ik_pose(Scene *scene, Object *ob, bPoseChannel *pchan, bConstraint *con)
78 {
79         bKinematicConstraint *data = (bKinematicConstraint *)con->data;
80
81         /* Find the chain's root. */
82         bPoseChannel *rootchan = BKE_armature_ik_solver_find_root(pchan, data);
83
84         if (has_operation_node(&ob->id, DEPSNODE_TYPE_EVAL_POSE, rootchan->name,
85                                DEG_OPCODE_POSE_IK_SOLVER))
86         {
87                 return;
88         }
89
90         /* Operation node for evaluating/running IK Solver. */
91         add_operation_node(&ob->id, DEPSNODE_TYPE_EVAL_POSE, rootchan->name,
92                            DEPSOP_TYPE_SIM, function_bind(BKE_pose_iktree_evaluate, _1, scene, ob, rootchan),
93                            DEG_OPCODE_POSE_IK_SOLVER);
94 }
95
96 /* Spline IK Eval Steps */
97 void DepsgraphNodeBuilder::build_splineik_pose(Scene *scene, Object *ob, bPoseChannel *pchan, bConstraint *con)
98 {
99         bSplineIKConstraint *data = (bSplineIKConstraint *)con->data;
100
101         /* Find the chain's root. */
102         bPoseChannel *rootchan = BKE_armature_splineik_solver_find_root(pchan, data);
103
104         /* Operation node for evaluating/running Spline IK Solver.
105          * Store the "root bone" of this chain in the solver, so it knows where to start.
106          */
107         add_operation_node(&ob->id, DEPSNODE_TYPE_EVAL_POSE, rootchan->name,
108                            DEPSOP_TYPE_SIM, function_bind(BKE_pose_splineik_evaluate, _1, scene, ob, rootchan),
109                            DEG_OPCODE_POSE_SPLINE_IK_SOLVER);
110 }
111
112 /* Pose/Armature Bones Graph */
113 void DepsgraphNodeBuilder::build_rig(Scene *scene, Object *ob)
114 {
115         bArmature *arm = (bArmature *)ob->data;
116
117         /* animation and/or drivers linking posebones to base-armature used to define them
118          * NOTE: AnimData here is really used to control animated deform properties,
119          *       which ideally should be able to be unique across different instances.
120          *       Eventually, we need some type of proxy/isolation mechanism in-between here
121          *       to ensure that we can use same rig multiple times in same scene...
122          */
123         if ((arm->id.tag & LIB_TAG_DOIT) == 0) {
124                 build_animdata(&arm->id);
125
126                 /* Make sure pose is up-to-date with armature updates. */
127                 add_operation_node(&arm->id,
128                                    DEPSNODE_TYPE_PARAMETERS,
129                                    DEPSOP_TYPE_EXEC,
130                                    NULL,
131                                    DEG_OPCODE_PLACEHOLDER,
132                                    "Armature Eval");
133         }
134
135         /* Rebuild pose if not up to date. */
136         if (ob->pose == NULL || (ob->pose->flag & POSE_RECALC)) {
137                 BKE_pose_rebuild_ex(ob, arm, false);
138                 /* XXX: Without this animation gets lost in certain circumstances
139                  * after loading file. Need to investigate further since it does
140                  * not happen with simple scenes..
141                  */
142                 if (ob->adt) {
143                         ob->adt->recalc |= ADT_RECALC_ANIM;
144                 }
145         }
146
147         /* speed optimization for animation lookups */
148         if (ob->pose) {
149                 BKE_pose_channels_hash_make(ob->pose);
150                 if (ob->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
151                         BKE_pose_update_constraint_flags(ob->pose);
152                 }
153         }
154
155         /**
156          * Pose Rig Graph
157          * ==============
158          *
159          * Pose Component:
160          * - Mainly used for referencing Bone components.
161          * - This is where the evaluation operations for init/exec/cleanup
162          *   (ik) solvers live, and are later hooked up (so that they can be
163          *   interleaved during runtime) with bone-operations they depend on/affect.
164          * - init_pose_eval() and cleanup_pose_eval() are absolute first and last
165          *   steps of pose eval process. ALL bone operations must be performed
166          *   between these two...
167          *
168          * Bone Component:
169          * - Used for representing each bone within the rig
170          * - Acts to encapsulate the evaluation operations (base matrix + parenting,
171          *   and constraint stack) so that they can be easily found.
172          * - Everything else which depends on bone-results hook up to the component only
173          *   so that we can redirect those to point at either the the post-IK/
174          *   post-constraint/post-matrix steps, as needed.
175          */
176
177         /* pose eval context */
178         add_operation_node(&ob->id, DEPSNODE_TYPE_EVAL_POSE,
179                            DEPSOP_TYPE_INIT, function_bind(BKE_pose_eval_init, _1, scene, ob, ob->pose), DEG_OPCODE_POSE_INIT);
180
181         add_operation_node(&ob->id, DEPSNODE_TYPE_EVAL_POSE,
182                            DEPSOP_TYPE_POST, function_bind(BKE_pose_eval_flush, _1, scene, ob, ob->pose), DEG_OPCODE_POSE_DONE);
183
184         /* bones */
185         LINKLIST_FOREACH (bPoseChannel *, pchan, &ob->pose->chanbase) {
186                 /* node for bone eval */
187                 add_operation_node(&ob->id, DEPSNODE_TYPE_BONE, pchan->name,
188                                    DEPSOP_TYPE_INIT, NULL, // XXX: BKE_pose_eval_bone_local
189                                    DEG_OPCODE_BONE_LOCAL);
190
191                 add_operation_node(&ob->id, DEPSNODE_TYPE_BONE, pchan->name,
192                                    DEPSOP_TYPE_EXEC, function_bind(BKE_pose_eval_bone, _1, scene, ob, pchan), // XXX: BKE_pose_eval_bone_pose
193                                    DEG_OPCODE_BONE_POSE_PARENT);
194
195                 add_operation_node(&ob->id, DEPSNODE_TYPE_BONE, pchan->name,
196                                    DEPSOP_TYPE_OUT, NULL, /* NOTE: dedicated noop for easier relationship construction */
197                                    DEG_OPCODE_BONE_READY);
198
199                 add_operation_node(&ob->id, DEPSNODE_TYPE_BONE, pchan->name,
200                                    DEPSOP_TYPE_POST, function_bind(BKE_pose_bone_done, _1, pchan),
201                                    DEG_OPCODE_BONE_DONE);
202
203                 /* constraints */
204                 if (pchan->constraints.first != NULL) {
205                         build_pose_constraints(scene, ob, pchan);
206                 }
207
208                 /**
209                  * IK Solvers...
210                  *
211                  * - These require separate processing steps are pose-level
212                  *   to be executed between chains of bones (i.e. once the
213                  *   base transforms of a bunch of bones is done)
214                  *
215                  * Unsolved Issues:
216                  * - Care is needed to ensure that multi-headed trees work out the same as in ik-tree building
217                  * - Animated chain-lengths are a problem...
218                  */
219                 LINKLIST_FOREACH (bConstraint *, con, &pchan->constraints) {
220                         switch (con->type) {
221                                 case CONSTRAINT_TYPE_KINEMATIC:
222                                         build_ik_pose(scene, ob, pchan, con);
223                                         break;
224
225                                 case CONSTRAINT_TYPE_SPLINEIK:
226                                         build_splineik_pose(scene, ob, pchan, con);
227                                         break;
228
229                                 default:
230                                         break;
231                         }
232                 }
233         }
234 }
235
236 void DepsgraphNodeBuilder::build_proxy_rig(Object *ob)
237 {
238         ID *obdata = (ID *)ob->data;
239         build_animdata(obdata);
240
241         BLI_assert(ob->pose != NULL);
242
243         /* speed optimization for animation lookups */
244         BKE_pose_channels_hash_make(ob->pose);
245         if (ob->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
246                 BKE_pose_update_constraint_flags(ob->pose);
247         }
248
249         add_operation_node(&ob->id,
250                            DEPSNODE_TYPE_EVAL_POSE,
251                            DEPSOP_TYPE_INIT,
252                            function_bind(BKE_pose_eval_proxy_copy, _1, ob),
253                            DEG_OPCODE_POSE_INIT);
254
255         LINKLIST_FOREACH (bPoseChannel *, pchan, &ob->pose->chanbase) {
256                 add_operation_node(&ob->id, DEPSNODE_TYPE_BONE, pchan->name,
257                                    DEPSOP_TYPE_INIT, NULL,
258                                    DEG_OPCODE_BONE_LOCAL);
259
260                 add_operation_node(&ob->id, DEPSNODE_TYPE_BONE, pchan->name,
261                                    DEPSOP_TYPE_EXEC, NULL,
262                                    DEG_OPCODE_BONE_READY);
263
264                 add_operation_node(&ob->id, DEPSNODE_TYPE_BONE, pchan->name,
265                                    DEPSOP_TYPE_POST, NULL,
266                                    DEG_OPCODE_BONE_DONE);
267         }
268
269         add_operation_node(&ob->id,
270                            DEPSNODE_TYPE_EVAL_POSE,
271                            DEPSOP_TYPE_POST,
272                            NULL,
273                            DEG_OPCODE_POSE_DONE);
274 }
275
276 }  // namespace DEG