Cleanup: pep8
[blender.git] / source / blender / depsgraph / intern / builder / deg_builder_relations_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
21  * \ingroup depsgraph
22  *
23  * Methods for constructing depsgraph
24  */
25
26 #include "intern/builder/deg_builder_relations.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <cstring> /* required for STREQ later on. */
31
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_utildefines.h"
35 #include "BLI_blenlib.h"
36
37 extern "C" {
38 #include "DNA_action_types.h"
39 #include "DNA_anim_types.h"
40 #include "DNA_armature_types.h"
41 #include "DNA_constraint_types.h"
42 #include "DNA_customdata_types.h"
43 #include "DNA_object_types.h"
44
45 #include "BKE_action.h"
46 #include "BKE_armature.h"
47 #include "BKE_constraint.h"
48 } /* extern "C" */
49
50 #include "DEG_depsgraph.h"
51 #include "DEG_depsgraph_build.h"
52
53 #include "intern/builder/deg_builder.h"
54 #include "intern/builder/deg_builder_pchanmap.h"
55 #include "intern/debug/deg_debug.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
60 #include "intern/depsgraph_type.h"
61
62 namespace DEG {
63
64 /* IK Solver Eval Steps */
65 void DepsgraphRelationBuilder::build_ik_pose(Object *object,
66                                              bPoseChannel *pchan,
67                                              bConstraint *con,
68                                              RootPChanMap *root_map)
69 {
70   bKinematicConstraint *data = (bKinematicConstraint *)con->data;
71   /* Attach owner to IK Solver to. */
72   bPoseChannel *rootchan = BKE_armature_ik_solver_find_root(pchan, data);
73   if (rootchan == NULL) {
74     return;
75   }
76   OperationKey pchan_local_key(
77       &object->id, NodeType::BONE, pchan->name, OperationCode::BONE_LOCAL);
78   OperationKey init_ik_key(&object->id, NodeType::EVAL_POSE, OperationCode::POSE_INIT_IK);
79   OperationKey solver_key(
80       &object->id, NodeType::EVAL_POSE, rootchan->name, OperationCode::POSE_IK_SOLVER);
81   OperationKey pose_cleanup_key(&object->id, NodeType::EVAL_POSE, OperationCode::POSE_CLEANUP);
82   add_relation(pchan_local_key, init_ik_key, "IK Constraint -> Init IK Tree");
83   add_relation(init_ik_key, solver_key, "Init IK -> IK Solver");
84   /* Never cleanup before solver is run. */
85   add_relation(solver_key, pose_cleanup_key, "IK Solver -> Cleanup", RELATION_FLAG_GODMODE);
86   /* The ITASC solver currently accesses the target transforms in init tree :(
87    * TODO: Fix ITASC and remove this.
88    */
89   bool is_itasc = (object->pose->iksolver == IKSOLVER_ITASC);
90   OperationKey target_dependent_key = is_itasc ? init_ik_key : solver_key;
91   /* IK target */
92   /* TODO(sergey): This should get handled as part of the constraint code. */
93   if (data->tar != NULL) {
94     /* Different object - requires its transform. */
95     if (data->tar != object) {
96       ComponentKey target_key(&data->tar->id, NodeType::TRANSFORM);
97       add_relation(target_key, target_dependent_key, con->name);
98     }
99     /* Subtarget references: */
100     if ((data->tar->type == OB_ARMATURE) && (data->subtarget[0])) {
101       /* Bone - use the final transformation. */
102       OperationKey target_key(
103           &data->tar->id, NodeType::BONE, data->subtarget, OperationCode::BONE_DONE);
104       add_relation(target_key, target_dependent_key, con->name);
105     }
106     else if (data->subtarget[0] && ELEM(data->tar->type, OB_MESH, OB_LATTICE)) {
107       /* Vertex group target. */
108       /* NOTE: for now, we don't need to represent vertex groups
109        * separately. */
110       ComponentKey target_key(&data->tar->id, NodeType::GEOMETRY);
111       add_relation(target_key, target_dependent_key, con->name);
112       add_customdata_mask(data->tar, DEGCustomDataMeshMasks::MaskVert(CD_MASK_MDEFORMVERT));
113     }
114     if (data->tar == object && data->subtarget[0]) {
115       /* Prevent target's constraints from linking to anything from same
116        * chain that it controls. */
117       root_map->add_bone(data->subtarget, rootchan->name);
118     }
119   }
120   /* Pole Target. */
121   /* TODO(sergey): This should get handled as part of the constraint code. */
122   if (data->poletar != NULL) {
123     /* Different object - requires its transform. */
124     if (data->poletar != object) {
125       ComponentKey target_key(&data->poletar->id, NodeType::TRANSFORM);
126       add_relation(target_key, target_dependent_key, con->name);
127     }
128     /* Subtarget references: */
129     if ((data->poletar->type == OB_ARMATURE) && (data->polesubtarget[0])) {
130       /* Bone - use the final transformation. */
131       OperationKey target_key(
132           &data->poletar->id, NodeType::BONE, data->polesubtarget, OperationCode::BONE_DONE);
133       add_relation(target_key, target_dependent_key, con->name);
134     }
135     else if (data->polesubtarget[0] && ELEM(data->poletar->type, OB_MESH, OB_LATTICE)) {
136       /* Vertex group target. */
137       /* NOTE: for now, we don't need to represent vertex groups
138        * separately. */
139       ComponentKey target_key(&data->poletar->id, NodeType::GEOMETRY);
140       add_relation(target_key, target_dependent_key, con->name);
141       add_customdata_mask(data->poletar, DEGCustomDataMeshMasks::MaskVert(CD_MASK_MDEFORMVERT));
142     }
143   }
144   DEG_DEBUG_PRINTF((::Depsgraph *)graph_,
145                    BUILD,
146                    "\nStarting IK Build: pchan = %s, target = (%s, %s), "
147                    "segcount = %d\n",
148                    pchan->name,
149                    data->tar ? data->tar->id.name : "NULL",
150                    data->subtarget,
151                    data->rootbone);
152   bPoseChannel *parchan = pchan;
153   /* Exclude tip from chain if needed. */
154   if (!(data->flag & CONSTRAINT_IK_TIP)) {
155     parchan = pchan->parent;
156   }
157   root_map->add_bone(parchan->name, rootchan->name);
158   OperationKey parchan_transforms_key(
159       &object->id, NodeType::BONE, parchan->name, OperationCode::BONE_READY);
160   add_relation(parchan_transforms_key, solver_key, "IK Solver Owner");
161   /* Walk to the chain's root. */
162   int segcount = 0;
163   while (parchan != NULL) {
164     /* Make IK-solver dependent on this bone's result, since it can only run
165      * after the standard results of the bone are know. Validate links step
166      * on the bone will ensure that users of this bone only grab the result
167      * with IK solver results. */
168     if (parchan != pchan) {
169       OperationKey parent_key(
170           &object->id, NodeType::BONE, parchan->name, OperationCode::BONE_READY);
171       add_relation(parent_key, solver_key, "IK Chain Parent");
172       OperationKey bone_done_key(
173           &object->id, NodeType::BONE, parchan->name, OperationCode::BONE_DONE);
174       add_relation(solver_key, bone_done_key, "IK Chain Result");
175     }
176     else {
177       OperationKey final_transforms_key(
178           &object->id, NodeType::BONE, parchan->name, OperationCode::BONE_DONE);
179       add_relation(solver_key, final_transforms_key, "IK Solver Result");
180     }
181     parchan->flag |= POSE_DONE;
182     root_map->add_bone(parchan->name, rootchan->name);
183     /* continue up chain, until we reach target number of items. */
184     DEG_DEBUG_PRINTF((::Depsgraph *)graph_, BUILD, "  %d = %s\n", segcount, parchan->name);
185     /* TODO(sergey): This is an arbitrary value, which was just following
186      * old code convention. */
187     segcount++;
188     if ((segcount == data->rootbone) || (segcount > 255)) {
189       break;
190     }
191     parchan = parchan->parent;
192   }
193   OperationKey pose_done_key(&object->id, NodeType::EVAL_POSE, OperationCode::POSE_DONE);
194   add_relation(solver_key, pose_done_key, "PoseEval Result-Bone Link");
195 }
196
197 /* Spline IK Eval Steps */
198 void DepsgraphRelationBuilder::build_splineik_pose(Object *object,
199                                                    bPoseChannel *pchan,
200                                                    bConstraint *con,
201                                                    RootPChanMap *root_map)
202 {
203   bSplineIKConstraint *data = (bSplineIKConstraint *)con->data;
204   bPoseChannel *rootchan = BKE_armature_splineik_solver_find_root(pchan, data);
205   OperationKey transforms_key(&object->id, NodeType::BONE, pchan->name, OperationCode::BONE_READY);
206   OperationKey init_ik_key(&object->id, NodeType::EVAL_POSE, OperationCode::POSE_INIT_IK);
207   OperationKey solver_key(
208       &object->id, NodeType::EVAL_POSE, rootchan->name, OperationCode::POSE_SPLINE_IK_SOLVER);
209   OperationKey pose_cleanup_key(&object->id, NodeType::EVAL_POSE, OperationCode::POSE_CLEANUP);
210   /* Solver depends on initialization. */
211   add_relation(init_ik_key, solver_key, "Init IK -> IK Solver");
212   /* Never cleanup before solver is run. */
213   add_relation(solver_key, pose_cleanup_key, "IK Solver -> Cleanup");
214   /* Attach owner to IK Solver. */
215   add_relation(transforms_key, solver_key, "Spline IK Solver Owner", RELATION_FLAG_GODMODE);
216   /* Attach path dependency to solver. */
217   if (data->tar != NULL) {
218     ComponentKey target_geometry_key(&data->tar->id, NodeType::GEOMETRY);
219     add_relation(target_geometry_key, solver_key, "Curve.Path -> Spline IK");
220     ComponentKey target_transform_key(&data->tar->id, NodeType::TRANSFORM);
221     add_relation(target_transform_key, solver_key, "Curve.Transform -> Spline IK");
222     add_special_eval_flag(&data->tar->id, DAG_EVAL_NEED_CURVE_PATH);
223   }
224   pchan->flag |= POSE_DONE;
225   OperationKey final_transforms_key(
226       &object->id, NodeType::BONE, pchan->name, OperationCode::BONE_DONE);
227   add_relation(solver_key, final_transforms_key, "Spline IK Result");
228   root_map->add_bone(pchan->name, rootchan->name);
229   /* Walk to the chain's root/ */
230   int segcount = 1;
231   for (bPoseChannel *parchan = pchan->parent; parchan != NULL && segcount < data->chainlen;
232        parchan = parchan->parent, segcount++) {
233     /* Make Spline IK solver dependent on this bone's result, since it can
234      * only run after the standard results of the bone are know. Validate
235      * links step on the bone will ensure that users of this bone only grab
236      * the result with IK solver results. */
237     OperationKey parent_key(&object->id, NodeType::BONE, parchan->name, OperationCode::BONE_READY);
238     add_relation(parent_key, solver_key, "Spline IK Solver Update");
239     OperationKey bone_done_key(
240         &object->id, NodeType::BONE, parchan->name, OperationCode::BONE_DONE);
241     add_relation(solver_key, bone_done_key, "Spline IK Solver Result");
242     parchan->flag |= POSE_DONE;
243     root_map->add_bone(parchan->name, rootchan->name);
244   }
245   OperationKey pose_done_key(&object->id, NodeType::EVAL_POSE, OperationCode::POSE_DONE);
246   add_relation(solver_key, pose_done_key, "PoseEval Result-Bone Link");
247 }
248
249 /* Pose/Armature Bones Graph */
250 void DepsgraphRelationBuilder::build_rig(Object *object)
251 {
252   /* Armature-Data */
253   bArmature *armature = (bArmature *)object->data;
254   // TODO: selection status?
255   /* Attach links between pose operations. */
256   ComponentKey local_transform(&object->id, NodeType::TRANSFORM);
257   OperationKey pose_init_key(&object->id, NodeType::EVAL_POSE, OperationCode::POSE_INIT);
258   OperationKey pose_init_ik_key(&object->id, NodeType::EVAL_POSE, OperationCode::POSE_INIT_IK);
259   OperationKey pose_cleanup_key(&object->id, NodeType::EVAL_POSE, OperationCode::POSE_CLEANUP);
260   OperationKey pose_done_key(&object->id, NodeType::EVAL_POSE, OperationCode::POSE_DONE);
261   add_relation(local_transform, pose_init_key, "Local Transform -> Pose Init");
262   add_relation(pose_init_key, pose_init_ik_key, "Pose Init -> Pose Init IK");
263   add_relation(pose_init_ik_key, pose_done_key, "Pose Init IK -> Pose Cleanup");
264   /* Make sure pose is up-to-date with armature updates. */
265   build_armature(armature);
266   OperationKey armature_key(&armature->id, NodeType::ARMATURE, OperationCode::ARMATURE_EVAL);
267   add_relation(armature_key, pose_init_key, "Data dependency");
268   /* Run cleanup even when there are no bones. */
269   add_relation(pose_init_key, pose_cleanup_key, "Init -> Cleanup");
270   /* IK Solvers.
271    *
272    * - These require separate processing steps are pose-level to be executed
273    *   between chains of bones (i.e. once the base transforms of a bunch of
274    *   bones is done).
275    *
276    * - We build relations for these before the dependencies between operations
277    *   in the same component as it is necessary to check whether such bones
278    *   are in the same IK chain (or else we get weird issues with either
279    *   in-chain references, or with bones being parented to IK'd bones).
280    *
281    * Unsolved Issues:
282    * - Care is needed to ensure that multi-headed trees work out the same as
283    *   in ik-tree building
284    * - Animated chain-lengths are a problem. */
285   RootPChanMap root_map;
286   bool pose_depends_on_local_transform = false;
287   LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) {
288     LISTBASE_FOREACH (bConstraint *, con, &pchan->constraints) {
289       switch (con->type) {
290         case CONSTRAINT_TYPE_KINEMATIC:
291           build_ik_pose(object, pchan, con, &root_map);
292           pose_depends_on_local_transform = true;
293           break;
294         case CONSTRAINT_TYPE_SPLINEIK:
295           build_splineik_pose(object, pchan, con, &root_map);
296           pose_depends_on_local_transform = true;
297           break;
298         /* Constraints which needs world's matrix for transform.
299          * TODO(sergey): More constraints here? */
300         case CONSTRAINT_TYPE_ROTLIKE:
301         case CONSTRAINT_TYPE_SIZELIKE:
302         case CONSTRAINT_TYPE_LOCLIKE:
303         case CONSTRAINT_TYPE_TRANSLIKE:
304           /* TODO(sergey): Add used space check. */
305           pose_depends_on_local_transform = true;
306           break;
307         default:
308           break;
309       }
310     }
311   }
312   // root_map.print_debug();
313   if (pose_depends_on_local_transform) {
314     /* TODO(sergey): Once partial updates are possible use relation between
315      * object transform and solver itself in it's build function. */
316     ComponentKey pose_key(&object->id, NodeType::EVAL_POSE);
317     ComponentKey local_transform_key(&object->id, NodeType::TRANSFORM);
318     add_relation(local_transform_key, pose_key, "Local Transforms");
319   }
320   /* Links between operations for each bone. */
321   LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) {
322     OperationKey bone_local_key(
323         &object->id, NodeType::BONE, pchan->name, OperationCode::BONE_LOCAL);
324     OperationKey bone_pose_key(
325         &object->id, NodeType::BONE, pchan->name, OperationCode::BONE_POSE_PARENT);
326     OperationKey bone_ready_key(
327         &object->id, NodeType::BONE, pchan->name, OperationCode::BONE_READY);
328     OperationKey bone_done_key(&object->id, NodeType::BONE, pchan->name, OperationCode::BONE_DONE);
329     pchan->flag &= ~POSE_DONE;
330     /* Pose init to bone local. */
331     add_relation(pose_init_key, bone_local_key, "Pose Init - Bone Local", RELATION_FLAG_GODMODE);
332     /* Local to pose parenting operation. */
333     add_relation(bone_local_key, bone_pose_key, "Bone Local - Bone Pose");
334     /* Parent relation. */
335     if (pchan->parent != NULL) {
336       OperationCode parent_key_opcode;
337       /* NOTE: this difference in handling allows us to prevent lockups
338        * while ensuring correct poses for separate chains. */
339       if (root_map.has_common_root(pchan->name, pchan->parent->name)) {
340         parent_key_opcode = OperationCode::BONE_READY;
341       }
342       else {
343         parent_key_opcode = OperationCode::BONE_DONE;
344       }
345
346       OperationKey parent_key(&object->id, NodeType::BONE, pchan->parent->name, parent_key_opcode);
347       add_relation(parent_key, bone_pose_key, "Parent Bone -> Child Bone");
348     }
349     /* Build constraints. */
350     if (pchan->constraints.first != NULL) {
351       /* Build relations for indirectly linked objects. */
352       BuilderWalkUserData data;
353       data.builder = this;
354       BKE_constraints_id_loop(&pchan->constraints, constraint_walk, &data);
355       /* Constraints stack and constraint dependencies. */
356       build_constraints(&object->id, NodeType::BONE, pchan->name, &pchan->constraints, &root_map);
357       /* Pose -> constraints. */
358       OperationKey constraints_key(
359           &object->id, NodeType::BONE, pchan->name, OperationCode::BONE_CONSTRAINTS);
360       add_relation(bone_pose_key, constraints_key, "Pose -> Constraints Stack");
361       add_relation(bone_local_key, constraints_key, "Local -> Constraints Stack");
362       /* Constraints -> ready/ */
363       /* TODO(sergey): When constraint stack is exploded, this step should
364        * occur before the first IK solver.  */
365       add_relation(constraints_key, bone_ready_key, "Constraints -> Ready");
366     }
367     else {
368       /* Pose -> Ready */
369       add_relation(bone_pose_key, bone_ready_key, "Pose -> Ready");
370     }
371     /* Bone ready -> Bone done.
372      * NOTE: For bones without IK, this is all that's needed.
373      *       For IK chains however, an additional rel is created from IK
374      *       to done, with transitive reduction removing this one. */
375     add_relation(bone_ready_key, bone_done_key, "Ready -> Done");
376     /* B-Bone shape is the real final step after Done if present. */
377     if (check_pchan_has_bbone(object, pchan)) {
378       OperationKey bone_segments_key(
379           &object->id, NodeType::BONE, pchan->name, OperationCode::BONE_SEGMENTS);
380       /* B-Bone shape depends on the final position of the bone. */
381       add_relation(bone_done_key, bone_segments_key, "Done -> B-Bone Segments");
382       /* B-Bone shape depends on final position of handle bones. */
383       bPoseChannel *prev, *next;
384       BKE_pchan_bbone_handles_get(pchan, &prev, &next);
385       if (prev) {
386         OperationCode opcode = OperationCode::BONE_DONE;
387         /* Inheriting parent roll requires access to prev handle's B-Bone properties. */
388         if ((pchan->bone->flag & BONE_ADD_PARENT_END_ROLL) != 0 &&
389             check_pchan_has_bbone_segments(object, prev)) {
390           opcode = OperationCode::BONE_SEGMENTS;
391         }
392         OperationKey prev_key(&object->id, NodeType::BONE, prev->name, opcode);
393         add_relation(prev_key, bone_segments_key, "Prev Handle -> B-Bone Segments");
394       }
395       if (next) {
396         OperationKey next_key(&object->id, NodeType::BONE, next->name, OperationCode::BONE_DONE);
397         add_relation(next_key, bone_segments_key, "Next Handle -> B-Bone Segments");
398       }
399       /* Pose requires the B-Bone shape. */
400       add_relation(
401           bone_segments_key, pose_done_key, "PoseEval Result-Bone Link", RELATION_FLAG_GODMODE);
402       add_relation(bone_segments_key, pose_cleanup_key, "Cleanup dependency");
403     }
404     else {
405       /* Assume that all bones must be done for the pose to be ready
406        * (for deformers). */
407       add_relation(bone_done_key, pose_done_key, "PoseEval Result-Bone Link");
408
409       /* Bones must be traversed before cleanup. */
410       add_relation(bone_done_key, pose_cleanup_key, "Done -> Cleanup");
411
412       add_relation(bone_ready_key, pose_cleanup_key, "Ready -> Cleanup");
413     }
414     /* Custom shape. */
415     if (pchan->custom != NULL) {
416       build_object(NULL, pchan->custom);
417     }
418   }
419 }
420
421 void DepsgraphRelationBuilder::build_proxy_rig(Object *object)
422 {
423   bArmature *armature = (bArmature *)object->data;
424   Object *proxy_from = object->proxy_from;
425   build_armature(armature);
426   OperationKey pose_init_key(&object->id, NodeType::EVAL_POSE, OperationCode::POSE_INIT);
427   OperationKey pose_done_key(&object->id, NodeType::EVAL_POSE, OperationCode::POSE_DONE);
428   OperationKey pose_cleanup_key(&object->id, NodeType::EVAL_POSE, OperationCode::POSE_CLEANUP);
429   LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) {
430     OperationKey bone_local_key(
431         &object->id, NodeType::BONE, pchan->name, OperationCode::BONE_LOCAL);
432     OperationKey bone_ready_key(
433         &object->id, NodeType::BONE, pchan->name, OperationCode::BONE_READY);
434     OperationKey bone_done_key(&object->id, NodeType::BONE, pchan->name, OperationCode::BONE_DONE);
435     OperationKey from_bone_done_key(
436         &proxy_from->id, NodeType::BONE, pchan->name, OperationCode::BONE_DONE);
437     add_relation(pose_init_key, bone_local_key, "Pose Init -> Bone Local");
438     add_relation(bone_local_key, bone_ready_key, "Local -> Ready");
439     add_relation(bone_ready_key, bone_done_key, "Ready -> Done");
440     add_relation(bone_done_key, pose_cleanup_key, "Bone Done -> Pose Cleanup");
441     add_relation(bone_done_key, pose_done_key, "Bone Done -> Pose Done", RELATION_FLAG_GODMODE);
442     /* Make sure bone in the proxy is not done before it's FROM is done. */
443     if (check_pchan_has_bbone(object, pchan)) {
444       OperationKey from_bone_segments_key(
445           &proxy_from->id, NodeType::BONE, pchan->name, OperationCode::BONE_SEGMENTS);
446       add_relation(from_bone_segments_key,
447                    bone_done_key,
448                    "Bone Segments -> Bone Done",
449                    RELATION_FLAG_GODMODE);
450     }
451     else {
452       add_relation(from_bone_done_key, bone_done_key, "Bone Done -> Bone Done");
453     }
454
455     /* Parent relation: even though the proxy bone itself doesn't need
456      * the parent bone, some users expect the parent to be ready if the
457      * bone itself is (e.g. for computing the local space matrix).
458      */
459     if (pchan->parent != NULL) {
460       OperationKey parent_key(
461           &object->id, NodeType::BONE, pchan->parent->name, OperationCode::BONE_DONE);
462       add_relation(parent_key, bone_done_key, "Parent Bone -> Child Bone");
463     }
464
465     if (pchan->prop != NULL) {
466       OperationKey bone_parameters(
467           &object->id, NodeType::PARAMETERS, OperationCode::PARAMETERS_EVAL, pchan->name);
468       OperationKey from_bone_parameters(
469           &proxy_from->id, NodeType::PARAMETERS, OperationCode::PARAMETERS_EVAL, pchan->name);
470       add_relation(from_bone_parameters, bone_parameters, "Proxy Bone Parameters");
471     }
472   }
473 }
474
475 }  // namespace DEG