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