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