Cleanup: remove original author
[blender.git] / source / blender / depsgraph / intern / builder / deg_builder_nodes_rig.cc
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2013 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file blender/depsgraph/intern/builder/deg_builder_nodes_rig.cc
21  *  \ingroup depsgraph
22  *
23  * Methods for constructing depsgraph's nodes
24  */
25
26 #include "intern/builder/deg_builder_nodes.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "BLI_utildefines.h"
34 #include "BLI_blenlib.h"
35 #include "BLI_string.h"
36
37 extern "C" {
38 #include "DNA_anim_types.h"
39 #include "DNA_armature_types.h"
40 #include "DNA_constraint_types.h"
41 #include "DNA_object_types.h"
42 #include "DNA_scene_types.h"
43
44 #include "BKE_action.h"
45 #include "BKE_armature.h"
46 #include "BKE_constraint.h"
47 } /* extern "C" */
48
49 #include "DEG_depsgraph.h"
50 #include "DEG_depsgraph_build.h"
51
52 #include "intern/builder/deg_builder.h"
53 #include "intern/eval/deg_eval_copy_on_write.h"
54 #include "intern/node/deg_node.h"
55 #include "intern/node/deg_node_component.h"
56 #include "intern/node/deg_node_operation.h"
57 #include "intern/depsgraph_type.h"
58
59 namespace DEG {
60
61 void DepsgraphNodeBuilder::build_pose_constraints(
62         Object *object,
63         bPoseChannel *pchan,
64         int pchan_index,
65         bool is_object_visible)
66 {
67         /* Pull indirect dependencies via constraints. */
68         BuilderWalkUserData data;
69         data.builder = this;
70         data.is_parent_visible = is_object_visible;
71         BKE_constraints_id_loop(&pchan->constraints, constraint_walk, &data);
72         /* Create node for constraint stack. */
73         add_operation_node(&object->id,
74                            NodeType::BONE,
75                            pchan->name,
76                            OperationCode::BONE_CONSTRAINTS,
77                            function_bind(BKE_pose_constraints_evaluate,
78                                          _1,
79                                          get_cow_datablock(scene_),
80                                          get_cow_datablock(object),
81                                          pchan_index));
82 }
83
84 /* IK Solver Eval Steps */
85 void DepsgraphNodeBuilder::build_ik_pose(Object *object,
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         if (rootchan == NULL) {
94                 return;
95         }
96
97         if (has_operation_node(&object->id, NodeType::EVAL_POSE, rootchan->name,
98                                OperationCode::POSE_IK_SOLVER))
99         {
100                 return;
101         }
102
103         int rootchan_index = BLI_findindex(&object->pose->chanbase, rootchan);
104         BLI_assert(rootchan_index != -1);
105         /* Operation node for evaluating/running IK Solver. */
106         add_operation_node(&object->id,
107                            NodeType::EVAL_POSE,
108                            rootchan->name,
109                            OperationCode::POSE_IK_SOLVER,
110                            function_bind(BKE_pose_iktree_evaluate,
111                                          _1,
112                                          get_cow_datablock(scene_),
113                                          get_cow_datablock(object),
114                                          rootchan_index));
115 }
116
117 /* Spline IK Eval Steps */
118 void DepsgraphNodeBuilder::build_splineik_pose(Object *object,
119                                                bPoseChannel *pchan,
120                                                bConstraint *con)
121 {
122         bSplineIKConstraint *data = (bSplineIKConstraint *)con->data;
123
124         /* Find the chain's root. */
125         bPoseChannel *rootchan = BKE_armature_splineik_solver_find_root(pchan, data);
126
127         /* Operation node for evaluating/running Spline IK Solver.
128          * Store the "root bone" of this chain in the solver, so it knows where to
129          * start. */
130         int rootchan_index = BLI_findindex(&object->pose->chanbase, rootchan);
131         BLI_assert(rootchan_index != -1);
132         add_operation_node(&object->id,
133                            NodeType::EVAL_POSE,
134                            rootchan->name,
135                            OperationCode::POSE_SPLINE_IK_SOLVER,
136                            function_bind(BKE_pose_splineik_evaluate,
137                                          _1,
138                                          get_cow_datablock(scene_),
139                                          get_cow_datablock(object),
140                                          rootchan_index));
141 }
142
143 /* Pose/Armature Bones Graph */
144 void DepsgraphNodeBuilder::build_rig(Object *object, bool is_object_visible)
145 {
146         bArmature *armature = (bArmature *)object->data;
147         Scene *scene_cow = get_cow_datablock(scene_);
148         Object *object_cow = get_cow_datablock(object);
149         OperationNode *op_node;
150         /* Animation and/or drivers linking posebones to base-armature used to
151          * define them.
152          *
153          * NOTE: AnimData here is really used to control animated deform properties,
154          *       which ideally should be able to be unique across different
155          *       instances. Eventually, we need some type of proxy/isolation
156          *       mechanism in-between here to ensure that we can use same rig
157          *       multiple times in same scene. */
158         /* Armature. */
159         build_armature(armature);
160         /* Rebuild pose if not up to date. */
161         if (object->pose == NULL || (object->pose->flag & POSE_RECALC)) {
162                 /* By definition, no need to tag depsgraph as dirty from here, so we can pass NULL bmain. */
163                 BKE_pose_rebuild(NULL, object, armature, true);
164         }
165         /* Speed optimization for animation lookups. */
166         if (object->pose != NULL) {
167                 BKE_pose_channels_hash_make(object->pose);
168                 if (object->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
169                         BKE_pose_update_constraint_flags(object->pose);
170                 }
171         }
172         /**
173          * Pose Rig Graph
174          * ==============
175          *
176          * Pose Component:
177          * - Mainly used for referencing Bone components.
178          * - This is where the evaluation operations for init/exec/cleanup
179          *   (ik) solvers live, and are later hooked up (so that they can be
180          *   interleaved during runtime) with bone-operations they depend on/affect.
181          * - init_pose_eval() and cleanup_pose_eval() are absolute first and last
182          *   steps of pose eval process. ALL bone operations must be performed
183          *   between these two...
184          *
185          * Bone Component:
186          * - Used for representing each bone within the rig
187          * - Acts to encapsulate the evaluation operations (base matrix + parenting,
188          *   and constraint stack) so that they can be easily found.
189          * - Everything else which depends on bone-results hook up to the component
190          *   only so that we can redirect those to point at either the the post-IK/
191          *   post-constraint/post-matrix steps, as needed. */
192         /* Pose eval context. */
193         op_node = add_operation_node(&object->id,
194                                      NodeType::EVAL_POSE,
195                                      OperationCode::POSE_INIT,
196                                      function_bind(BKE_pose_eval_init,
197                                                    _1,
198                                                    scene_cow,
199                                                    object_cow));
200         op_node->set_as_entry();
201
202         op_node = add_operation_node(&object->id,
203                                      NodeType::EVAL_POSE,
204                                      OperationCode::POSE_INIT_IK,
205                                      function_bind(BKE_pose_eval_init_ik,
206                                                    _1,
207                                                    scene_cow,
208                                                    object_cow));
209
210         add_operation_node(&object->id,
211                            NodeType::EVAL_POSE,
212                            OperationCode::POSE_CLEANUP,
213                            function_bind(BKE_pose_eval_cleanup,
214                                          _1,
215                                          scene_cow,
216                                          object_cow));
217
218         op_node = add_operation_node(&object->id,
219                                      NodeType::EVAL_POSE,
220                                      OperationCode::POSE_DONE,
221                                      function_bind(BKE_pose_eval_done,
222                                                    _1,
223                                                    object_cow));
224         op_node->set_as_exit();
225         /* Bones. */
226         int pchan_index = 0;
227         LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) {
228                 /* Node for bone evaluation. */
229                 op_node = add_operation_node(&object->id,
230                                              NodeType::BONE,
231                                              pchan->name,
232                                              OperationCode::BONE_LOCAL);
233                 op_node->set_as_entry();
234
235                 add_operation_node(&object->id,
236                                    NodeType::BONE,
237                                    pchan->name,
238                                    OperationCode::BONE_POSE_PARENT,
239                                    function_bind(BKE_pose_eval_bone, _1,
240                                                  scene_cow,
241                                                  object_cow,
242                                                  pchan_index));
243
244                 /* NOTE: Dedicated noop for easier relationship construction. */
245                 add_operation_node(&object->id,
246                                    NodeType::BONE,
247                                    pchan->name,
248                                    OperationCode::BONE_READY);
249
250                 op_node = add_operation_node(&object->id,
251                                              NodeType::BONE,
252                                              pchan->name,
253                                              OperationCode::BONE_DONE,
254                                              function_bind(BKE_pose_bone_done,
255                                                            _1,
256                                                            object_cow,
257                                                            pchan_index));
258
259                 /* B-Bone shape computation - the real last step if present. */
260                 if (pchan->bone != NULL && pchan->bone->segments > 1) {
261                         op_node = add_operation_node(&object->id,
262                                                      NodeType::BONE,
263                                                      pchan->name,
264                                                      OperationCode::BONE_SEGMENTS,
265                                                      function_bind(BKE_pose_eval_bbone_segments, _1,
266                                                                    object_cow,
267                                                                    pchan_index));
268                 }
269
270                 op_node->set_as_exit();
271
272                 /* Custom properties. */
273                 if (pchan->prop != NULL) {
274                         add_operation_node(&object->id,
275                                            NodeType::PARAMETERS,
276                                            OperationCode::PARAMETERS_EVAL,
277                                            NULL,
278                                            pchan->name);
279                 }
280                 /* Build constraints. */
281                 if (pchan->constraints.first != NULL) {
282                         build_pose_constraints(
283                                 object, pchan, pchan_index, is_object_visible);
284                 }
285                 /**
286                  * IK Solvers.
287                  *
288                  * - These require separate processing steps are pose-level
289                  *   to be executed between chains of bones (i.e. once the
290                  *   base transforms of a bunch of bones is done)
291                  *
292                  * Unsolved Issues:
293                  * - Care is needed to ensure that multi-headed trees work out the same
294                  *   as in ik-tree building
295                  * - Animated chain-lengths are a problem. */
296                 LISTBASE_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                 if (pchan->custom != NULL) {
312                         /* TODO(sergey): Use own visibility. */
313                         build_object(
314                                 -1,
315                                 pchan->custom,
316                                 DEG_ID_LINKED_INDIRECTLY,
317                                 is_object_visible);
318                 }
319                 pchan_index++;
320         }
321 }
322
323 void DepsgraphNodeBuilder::build_proxy_rig(Object *object)
324 {
325         bArmature *armature = (bArmature *)object->data;
326         OperationNode *op_node;
327         Object *object_cow = get_cow_datablock(object);
328         /* Sanity check. */
329         BLI_assert(object->pose != NULL);
330         /* Armature. */
331         build_armature(armature);
332         /* speed optimization for animation lookups */
333         BKE_pose_channels_hash_make(object->pose);
334         if (object->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
335                 BKE_pose_update_constraint_flags(object->pose);
336         }
337         op_node = add_operation_node(&object->id,
338                                      NodeType::EVAL_POSE,
339                                      OperationCode::POSE_INIT,
340                                      function_bind(BKE_pose_eval_proxy_init,
341                                                    _1,
342                                                    object_cow));
343         op_node->set_as_entry();
344
345         int pchan_index = 0;
346         LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) {
347                 op_node = add_operation_node(&object->id,
348                                              NodeType::BONE,
349                                              pchan->name,
350                                              OperationCode::BONE_LOCAL);
351                 op_node->set_as_entry();
352                 /* Bone is ready for solvers. */
353                 add_operation_node(&object->id,
354                                    NodeType::BONE,
355                                    pchan->name,
356                                    OperationCode::BONE_READY);
357                 /* Bone is fully evaluated. */
358                 op_node = add_operation_node(
359                         &object->id,
360                         NodeType::BONE,
361                         pchan->name,
362                         OperationCode::BONE_DONE,
363                         function_bind(BKE_pose_eval_proxy_copy_bone,
364                                       _1,
365                                       object_cow,
366                                       pchan_index));
367                 op_node->set_as_exit();
368
369                 /* Custom properties. */
370                 if (pchan->prop != NULL) {
371                         add_operation_node(&object->id,
372                                            NodeType::PARAMETERS,
373                                            OperationCode::PARAMETERS_EVAL,
374                                            NULL,
375                                            pchan->name);
376                 }
377
378                 pchan_index++;
379         }
380         op_node = add_operation_node(&object->id,
381                                      NodeType::EVAL_POSE,
382                                      OperationCode::POSE_CLEANUP,
383                                      function_bind(BKE_pose_eval_proxy_cleanup,
384                                                    _1,
385                                                    object_cow));
386         op_node = add_operation_node(&object->id,
387                                      NodeType::EVAL_POSE,
388                                      OperationCode::POSE_DONE,
389                                      function_bind(BKE_pose_eval_proxy_done,
390                                                    _1,
391                                                    object_cow));
392         op_node->set_as_exit();
393 }
394
395 }  // namespace DEG