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