df1d19e59dc2df71b431e6606a2c5d9b95aa9bf7
[blender.git] / source / blender / depsgraph / intern / builder / deg_builder_relations_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_relations_rig.cc
28  *  \ingroup depsgraph
29  *
30  * Methods for constructing depsgraph
31  */
32
33 #include "intern/builder/deg_builder_relations.h"
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <cstring>  /* required for STREQ later on. */
38
39 #include "MEM_guardedalloc.h"
40
41 extern "C" {
42 #include "BLI_blenlib.h"
43 #include "BLI_utildefines.h"
44
45 #include "DNA_action_types.h"
46 #include "DNA_anim_types.h"
47 #include "DNA_armature_types.h"
48 #include "DNA_constraint_types.h"
49 #include "DNA_customdata_types.h"
50 #include "DNA_object_types.h"
51
52 #include "BKE_action.h"
53 #include "BKE_armature.h"
54
55 #include "DEG_depsgraph.h"
56 #include "DEG_depsgraph_build.h"
57 } /* extern "C" */
58
59 #include "intern/builder/deg_builder.h"
60 #include "intern/builder/deg_builder_pchanmap.h"
61
62 #include "intern/nodes/deg_node.h"
63 #include "intern/nodes/deg_node_component.h"
64 #include "intern/nodes/deg_node_operation.h"
65
66 #include "intern/depsgraph_intern.h"
67 #include "intern/depsgraph_types.h"
68
69 #include "util/deg_util_foreach.h"
70
71 namespace DEG {
72
73 /* IK Solver Eval Steps */
74 void DepsgraphRelationBuilder::build_ik_pose(Object *ob,
75                                              bPoseChannel *pchan,
76                                              bConstraint *con,
77                                              RootPChanMap *root_map)
78 {
79         bKinematicConstraint *data = (bKinematicConstraint *)con->data;
80
81         /* attach owner to IK Solver too
82          * - assume that owner is always part of chain
83          * - see notes on direction of rel below...
84          */
85         bPoseChannel *rootchan = BKE_armature_ik_solver_find_root(pchan, data);
86         OperationKey solver_key(&ob->id, DEPSNODE_TYPE_EVAL_POSE, rootchan->name, DEG_OPCODE_POSE_IK_SOLVER);
87
88         /* IK target */
89         // XXX: this should get handled as part of the constraint code
90         if (data->tar != NULL) {
91                 /* TODO(sergey): For until we'll store partial matricies in the depsgraph,
92                  * we create dependency between target object and pose eval component.
93                  *
94                  * This way we ensuring the whole subtree is updated from scratch without
95                  * need of intermediate matricies. This is an overkill, but good enough for
96                  * testing IK solver.
97                  */
98                 // FIXME: geometry targets...
99                 ComponentKey pose_key(&ob->id, DEPSNODE_TYPE_EVAL_POSE);
100                 if ((data->tar->type == OB_ARMATURE) && (data->subtarget[0])) {
101                         /* TODO(sergey): This is only for until granular update stores intermediate result. */
102                         if (data->tar != ob) {
103                                 /* different armature - can just read the results */
104                                 ComponentKey target_key(&data->tar->id, DEPSNODE_TYPE_BONE, data->subtarget);
105                                 add_relation(target_key, pose_key, con->name);
106                         }
107                         else {
108                                 /* same armature - we'll use the ready state only, just in case this bone is in the chain we're solving */
109                                 OperationKey target_key(&data->tar->id, DEPSNODE_TYPE_BONE, data->subtarget, DEG_OPCODE_BONE_DONE);
110                                 add_relation(target_key, solver_key, con->name);
111                         }
112                 }
113                 else if (ELEM(data->tar->type, OB_MESH, OB_LATTICE) && (data->subtarget[0])) {
114                         /* vertex group target */
115                         /* NOTE: for now, we don't need to represent vertex groups separately... */
116                         ComponentKey target_key(&data->tar->id, DEPSNODE_TYPE_GEOMETRY);
117                         add_relation(target_key, solver_key, con->name);
118
119                         if (data->tar->type == OB_MESH) {
120                                 OperationDepsNode *node2 = find_operation_node(target_key);
121                                 if (node2 != NULL) {
122                                         node2->customdata_mask |= CD_MASK_MDEFORMVERT;
123                                 }
124                         }
125                 }
126                 else {
127                         /* Standard Object Target */
128                         ComponentKey target_key(&data->tar->id, DEPSNODE_TYPE_TRANSFORM);
129                         add_relation(target_key, pose_key, con->name);
130                 }
131
132                 if ((data->tar == ob) && (data->subtarget[0])) {
133                         /* Prevent target's constraints from linking to anything from same
134                          * chain that it controls.
135                          */
136                         root_map->add_bone(data->subtarget, rootchan->name);
137                 }
138         }
139
140         /* Pole Target */
141         // XXX: this should get handled as part of the constraint code
142         if (data->poletar != NULL) {
143                 if ((data->poletar->type == OB_ARMATURE) && (data->polesubtarget[0])) {
144                         // XXX: same armature issues - ready vs done?
145                         ComponentKey target_key(&data->poletar->id, DEPSNODE_TYPE_BONE, data->polesubtarget);
146                         add_relation(target_key, solver_key, con->name);
147                 }
148                 else if (ELEM(data->poletar->type, OB_MESH, OB_LATTICE) && (data->polesubtarget[0])) {
149                         /* vertex group target */
150                         /* NOTE: for now, we don't need to represent vertex groups separately... */
151                         ComponentKey target_key(&data->poletar->id, DEPSNODE_TYPE_GEOMETRY);
152                         add_relation(target_key, solver_key, con->name);
153
154                         if (data->poletar->type == OB_MESH) {
155                                 OperationDepsNode *node2 = find_operation_node(target_key);
156                                 if (node2 != NULL) {
157                                         node2->customdata_mask |= CD_MASK_MDEFORMVERT;
158                                 }
159                         }
160                 }
161                 else {
162                         ComponentKey target_key(&data->poletar->id, DEPSNODE_TYPE_TRANSFORM);
163                         add_relation(target_key, solver_key, con->name);
164                 }
165         }
166
167         DEG_DEBUG_PRINTF("\nStarting IK Build: pchan = %s, target = (%s, %s), segcount = %d\n",
168                          pchan->name, data->tar->id.name, data->subtarget, data->rootbone);
169
170         bPoseChannel *parchan = pchan;
171         /* exclude tip from chain? */
172         if (!(data->flag & CONSTRAINT_IK_TIP)) {
173                 OperationKey tip_transforms_key(&ob->id, DEPSNODE_TYPE_BONE,
174                                                 parchan->name, DEG_OPCODE_BONE_LOCAL);
175                 add_relation(solver_key, tip_transforms_key,
176                              "IK Solver Result");
177                 parchan = pchan->parent;
178         }
179
180         root_map->add_bone(parchan->name, rootchan->name);
181
182         OperationKey parchan_transforms_key(&ob->id, DEPSNODE_TYPE_BONE,
183                                             parchan->name, DEG_OPCODE_BONE_READY);
184         add_relation(parchan_transforms_key, solver_key,
185                      "IK Solver Owner");
186
187         /* Walk to the chain's root */
188         //size_t segcount = 0;
189         int segcount = 0;
190
191         while (parchan) {
192                 /* Make IK-solver dependent on this bone's result,
193                  * since it can only run after the standard results
194                  * of the bone are know. Validate links step on the
195                  * bone will ensure that users of this bone only
196                  * grab the result with IK solver results...
197                  */
198                 if (parchan != pchan) {
199                         OperationKey parent_key(&ob->id, DEPSNODE_TYPE_BONE, parchan->name, DEG_OPCODE_BONE_READY);
200                         add_relation(parent_key, solver_key, "IK Chain Parent");
201
202                         OperationKey done_key(&ob->id, DEPSNODE_TYPE_BONE, parchan->name, DEG_OPCODE_BONE_DONE);
203                         add_relation(solver_key, done_key, "IK Chain Result");
204                 }
205                 else {
206                         OperationKey final_transforms_key(&ob->id, DEPSNODE_TYPE_BONE, parchan->name, DEG_OPCODE_BONE_DONE);
207                         add_relation(solver_key, final_transforms_key, "IK Solver Result");
208                 }
209                 parchan->flag |= POSE_DONE;
210
211
212                 root_map->add_bone(parchan->name, rootchan->name);
213
214                 /* continue up chain, until we reach target number of items... */
215                 DEG_DEBUG_PRINTF("  %d = %s\n", segcount, parchan->name);
216                 segcount++;
217                 if ((segcount == data->rootbone) || (segcount > 255)) break;  /* 255 is weak */
218
219                 parchan  = parchan->parent;
220         }
221
222         OperationKey flush_key(&ob->id, DEPSNODE_TYPE_EVAL_POSE, DEG_OPCODE_POSE_DONE);
223         add_relation(solver_key, flush_key, "PoseEval Result-Bone Link");
224 }
225
226 /* Spline IK Eval Steps */
227 void DepsgraphRelationBuilder::build_splineik_pose(Object *ob,
228                                                    bPoseChannel *pchan,
229                                                    bConstraint *con,
230                                                    RootPChanMap *root_map)
231 {
232         bSplineIKConstraint *data = (bSplineIKConstraint *)con->data;
233         bPoseChannel *rootchan = BKE_armature_splineik_solver_find_root(pchan, data);
234         OperationKey transforms_key(&ob->id, DEPSNODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_READY);
235         OperationKey solver_key(&ob->id, DEPSNODE_TYPE_EVAL_POSE, rootchan->name, DEG_OPCODE_POSE_SPLINE_IK_SOLVER);
236
237         /* attach owner to IK Solver too
238          * - assume that owner is always part of chain
239          * - see notes on direction of rel below...
240          */
241         add_relation(transforms_key, solver_key, "Spline IK Solver Owner");
242
243         /* attach path dependency to solver */
244         if (data->tar) {
245                 /* TODO(sergey): For until we'll store partial matricies in the depsgraph,
246                  * we create dependency between target object and pose eval component.
247                  * See IK pose for a bit more information.
248                  */
249                 // TODO: the bigggest point here is that we need the curve PATH and not just the general geometry...
250                 ComponentKey target_key(&data->tar->id, DEPSNODE_TYPE_GEOMETRY);
251                 ComponentKey pose_key(&ob->id, DEPSNODE_TYPE_EVAL_POSE);
252                 add_relation(target_key, pose_key, "[Curve.Path -> Spline IK] DepsRel");
253         }
254
255         pchan->flag |= POSE_DONE;
256         OperationKey final_transforms_key(&ob->id, DEPSNODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_DONE);
257         add_relation(solver_key, final_transforms_key, "Spline IK Result");
258
259         root_map->add_bone(pchan->name, rootchan->name);
260
261         /* Walk to the chain's root */
262         //size_t segcount = 0;
263         int segcount = 0;
264
265         for (bPoseChannel *parchan = pchan->parent; parchan; parchan = parchan->parent) {
266                 /* Make Spline IK solver dependent on this bone's result,
267                  * since it can only run after the standard results
268                  * of the bone are know. Validate links step on the
269                  * bone will ensure that users of this bone only
270                  * grab the result with IK solver results...
271                  */
272                 if (parchan != pchan) {
273                         OperationKey parent_key(&ob->id, DEPSNODE_TYPE_BONE, parchan->name, DEG_OPCODE_BONE_READY);
274                         add_relation(parent_key, solver_key, "Spline IK Solver Update");
275
276                         OperationKey done_key(&ob->id, DEPSNODE_TYPE_BONE, parchan->name, DEG_OPCODE_BONE_DONE);
277                         add_relation(solver_key, done_key, "IK Chain Result");
278                 }
279                 parchan->flag |= POSE_DONE;
280
281                 OperationKey final_transforms_key(&ob->id, DEPSNODE_TYPE_BONE, parchan->name, DEG_OPCODE_BONE_DONE);
282                 add_relation(solver_key, final_transforms_key, "Spline IK Solver Result");
283
284                 root_map->add_bone(parchan->name, rootchan->name);
285
286                 /* continue up chain, until we reach target number of items... */
287                 segcount++;
288                 if ((segcount == data->chainlen) || (segcount > 255)) break;  /* 255 is weak */
289         }
290
291         OperationKey flush_key(&ob->id, DEPSNODE_TYPE_EVAL_POSE, DEG_OPCODE_POSE_DONE);
292         add_relation(solver_key, flush_key, "PoseEval Result-Bone Link");
293 }
294
295 /* Pose/Armature Bones Graph */
296 void DepsgraphRelationBuilder::build_rig(Scene *scene, Object *ob)
297 {
298         /* Armature-Data */
299         bArmature *arm = (bArmature *)ob->data;
300
301         // TODO: selection status?
302
303         /* attach links between pose operations */
304         OperationKey init_key(&ob->id, DEPSNODE_TYPE_EVAL_POSE, DEG_OPCODE_POSE_INIT);
305         OperationKey flush_key(&ob->id, DEPSNODE_TYPE_EVAL_POSE, DEG_OPCODE_POSE_DONE);
306
307         add_relation(init_key, flush_key, "[Pose Init -> Pose Cleanup]");
308
309         /* Make sure pose is up-to-date with armature updates. */
310         OperationKey armature_key(&arm->id,
311                                   DEPSNODE_TYPE_PARAMETERS,
312                                   DEG_OPCODE_PLACEHOLDER,
313                                   "Armature Eval");
314         add_relation(armature_key, init_key, "Data dependency");
315
316         if (needs_animdata_node(&ob->id)) {
317                 ComponentKey animation_key(&ob->id, DEPSNODE_TYPE_ANIMATION);
318                 add_relation(animation_key, init_key, "Rig Animation");
319         }
320
321         /* IK Solvers...
322          * - These require separate processing steps are pose-level
323          *   to be executed between chains of bones (i.e. once the
324          *   base transforms of a bunch of bones is done)
325          *
326          * - We build relations for these before the dependencies
327          *   between ops in the same component as it is necessary
328          *   to check whether such bones are in the same IK chain
329          *   (or else we get weird issues with either in-chain
330          *   references, or with bones being parented to IK'd bones)
331          *
332          * Unsolved Issues:
333          * - Care is needed to ensure that multi-headed trees work out the same as in ik-tree building
334          * - Animated chain-lengths are a problem...
335          */
336         RootPChanMap root_map;
337         bool pose_depends_on_local_transform = false;
338         LINKLIST_FOREACH (bPoseChannel *, pchan, &ob->pose->chanbase) {
339                 LINKLIST_FOREACH (bConstraint *, con, &pchan->constraints) {
340                         switch (con->type) {
341                                 case CONSTRAINT_TYPE_KINEMATIC:
342                                         build_ik_pose(ob, pchan, con, &root_map);
343                                         pose_depends_on_local_transform = true;
344                                         break;
345
346                                 case CONSTRAINT_TYPE_SPLINEIK:
347                                         build_splineik_pose(ob, pchan, con, &root_map);
348                                         pose_depends_on_local_transform = true;
349                                         break;
350
351                                 /* Constraints which needs world's matrix for transform.
352                                  * TODO(sergey): More constraints here?
353                                  */
354                                 case CONSTRAINT_TYPE_ROTLIKE:
355                                 case CONSTRAINT_TYPE_SIZELIKE:
356                                 case CONSTRAINT_TYPE_LOCLIKE:
357                                 case CONSTRAINT_TYPE_TRANSLIKE:
358                                         /* TODO(sergey): Add used space check. */
359                                         pose_depends_on_local_transform = true;
360                                         break;
361
362                                 default:
363                                         break;
364                         }
365                 }
366         }
367         //root_map.print_debug();
368
369         if (pose_depends_on_local_transform) {
370                 /* TODO(sergey): Once partial updates are possible use relation between
371                  * object transform and solver itself in it's build function.
372                  */
373                 ComponentKey pose_key(&ob->id, DEPSNODE_TYPE_EVAL_POSE);
374                 ComponentKey local_transform_key(&ob->id, DEPSNODE_TYPE_TRANSFORM);
375                 add_relation(local_transform_key, pose_key, "Local Transforms");
376         }
377
378
379         /* links between operations for each bone */
380         LINKLIST_FOREACH (bPoseChannel *, pchan, &ob->pose->chanbase) {
381                 OperationKey bone_local_key(&ob->id, DEPSNODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_LOCAL);
382                 OperationKey bone_pose_key(&ob->id, DEPSNODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_POSE_PARENT);
383                 OperationKey bone_ready_key(&ob->id, DEPSNODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_READY);
384                 OperationKey bone_done_key(&ob->id, DEPSNODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_DONE);
385
386                 pchan->flag &= ~POSE_DONE;
387
388                 /* pose init to bone local */
389                 add_relation(init_key, bone_local_key, "PoseEval Source-Bone Link");
390
391                 /* local to pose parenting operation */
392                 add_relation(bone_local_key, bone_pose_key, "Bone Local - PoseSpace Link");
393
394                 /* parent relation */
395                 if (pchan->parent != NULL) {
396                         eDepsOperation_Code parent_key_opcode;
397
398                         /* NOTE: this difference in handling allows us to prevent lockups while ensuring correct poses for separate chains */
399                         if (root_map.has_common_root(pchan->name, pchan->parent->name)) {
400                                 parent_key_opcode = DEG_OPCODE_BONE_READY;
401                         }
402                         else {
403                                 parent_key_opcode = DEG_OPCODE_BONE_DONE;
404                         }
405
406                         OperationKey parent_key(&ob->id, DEPSNODE_TYPE_BONE, pchan->parent->name, parent_key_opcode);
407                         add_relation(parent_key, bone_pose_key, "[Parent Bone -> Child Bone]");
408                 }
409
410                 /* constraints */
411                 if (pchan->constraints.first != NULL) {
412                         /* constraints stack and constraint dependencies */
413                         build_constraints(scene, &ob->id, DEPSNODE_TYPE_BONE, pchan->name, &pchan->constraints, &root_map);
414
415                         /* pose -> constraints */
416                         OperationKey constraints_key(&ob->id, DEPSNODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_CONSTRAINTS);
417                         add_relation(bone_pose_key, constraints_key, "Constraints Stack");
418
419                         /* constraints -> ready */
420                         // TODO: when constraint stack is exploded, this step should occur before the first IK solver
421                         add_relation(constraints_key, bone_ready_key, "Constraints -> Ready");
422                 }
423                 else {
424                         /* pose -> ready */
425                         add_relation(bone_pose_key, bone_ready_key, "Pose -> Ready");
426                 }
427
428                 /* bone ready -> done
429                  * NOTE: For bones without IK, this is all that's needed.
430                  *       For IK chains however, an additional rel is created from IK to done,
431                  *       with transitive reduction removing this one...
432                  */
433                 add_relation(bone_ready_key, bone_done_key, "Ready -> Done");
434
435                 /* assume that all bones must be done for the pose to be ready (for deformers) */
436                 add_relation(bone_done_key, flush_key, "PoseEval Result-Bone Link");
437         }
438 }
439
440 void DepsgraphRelationBuilder::build_proxy_rig(Object *ob)
441 {
442         OperationKey pose_init_key(&ob->id, DEPSNODE_TYPE_EVAL_POSE, DEG_OPCODE_POSE_INIT);
443         OperationKey pose_done_key(&ob->id, DEPSNODE_TYPE_EVAL_POSE, DEG_OPCODE_POSE_DONE);
444         LINKLIST_FOREACH (bPoseChannel *, pchan, &ob->pose->chanbase) {
445                 OperationKey bone_local_key(&ob->id, DEPSNODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_LOCAL);
446                 OperationKey bone_ready_key(&ob->id, DEPSNODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_READY);
447                 OperationKey bone_done_key(&ob->id, DEPSNODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_DONE);
448                 add_relation(pose_init_key, bone_local_key, "Pose Init -> Bone Local");
449                 add_relation(bone_local_key, bone_ready_key, "Local -> Ready");
450                 add_relation(bone_ready_key, bone_done_key, "Ready -> Done");
451                 add_relation(bone_done_key, pose_done_key, "Bone Done -> Pose Done");
452         }
453 }
454
455 }  // namespace DEG