Depsgraph: Object data separation, armature
[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 *armature = (bArmature *)object->data;
308         // TODO: selection status?
309         /* Attach links between pose operations. */
310         OperationKey init_key(&object->id, DEG_NODE_TYPE_EVAL_POSE, DEG_OPCODE_POSE_INIT);
311         OperationKey init_ik_key(&object->id, DEG_NODE_TYPE_EVAL_POSE, DEG_OPCODE_POSE_INIT_IK);
312         OperationKey flush_key(&object->id, DEG_NODE_TYPE_EVAL_POSE, DEG_OPCODE_POSE_DONE);
313         add_relation(init_key, init_ik_key, "Pose Init -> Pose Init IK");
314         add_relation(init_ik_key, flush_key, "Pose Init IK -> Pose Cleanup");
315         /* Make sure pose is up-to-date with armature updates. */
316         build_armature(armature);
317         OperationKey armature_key(&armature->id,
318                                   DEG_NODE_TYPE_PARAMETERS,
319                                   DEG_OPCODE_PLACEHOLDER,
320                                   "Armature Eval");
321         add_relation(armature_key, init_key, "Data dependency");
322         /* IK Solvers...
323          * - These require separate processing steps are pose-level
324          *   to be executed between chains of bones (i.e. once the
325          *   base transforms of a bunch of bones is done)
326          *
327          * - We build relations for these before the dependencies
328          *   between ops in the same component as it is necessary
329          *   to check whether such bones are in the same IK chain
330          *   (or else we get weird issues with either in-chain
331          *   references, or with bones being parented to IK'd bones)
332          *
333          * Unsolved Issues:
334          * - Care is needed to ensure that multi-headed trees work out the same as
335          *   in ik-tree building
336          * - Animated chain-lengths are a problem...
337          */
338         RootPChanMap root_map;
339         bool pose_depends_on_local_transform = false;
340         LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) {
341                 LISTBASE_FOREACH (bConstraint *, con, &pchan->constraints) {
342                         switch (con->type) {
343                                 case CONSTRAINT_TYPE_KINEMATIC:
344                                         build_ik_pose(object, pchan, con, &root_map);
345                                         pose_depends_on_local_transform = true;
346                                         break;
347
348                                 case CONSTRAINT_TYPE_SPLINEIK:
349                                         build_splineik_pose(object, pchan, con, &root_map);
350                                         pose_depends_on_local_transform = true;
351                                         break;
352
353                                 /* Constraints which needs world's matrix for transform.
354                                  * TODO(sergey): More constraints here?
355                                  */
356                                 case CONSTRAINT_TYPE_ROTLIKE:
357                                 case CONSTRAINT_TYPE_SIZELIKE:
358                                 case CONSTRAINT_TYPE_LOCLIKE:
359                                 case CONSTRAINT_TYPE_TRANSLIKE:
360                                         /* TODO(sergey): Add used space check. */
361                                         pose_depends_on_local_transform = true;
362                                         break;
363
364                                 default:
365                                         break;
366                         }
367                 }
368         }
369         //root_map.print_debug();
370         if (pose_depends_on_local_transform) {
371                 /* TODO(sergey): Once partial updates are possible use relation between
372                  * object transform and solver itself in it's build function.
373                  */
374                 ComponentKey pose_key(&object->id, DEG_NODE_TYPE_EVAL_POSE);
375                 ComponentKey local_transform_key(&object->id, DEG_NODE_TYPE_TRANSFORM);
376                 add_relation(local_transform_key, pose_key, "Local Transforms");
377         }
378         /* Links between operations for each bone. */
379         LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) {
380                 OperationKey bone_local_key(&object->id, DEG_NODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_LOCAL);
381                 OperationKey bone_pose_key(&object->id, DEG_NODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_POSE_PARENT);
382                 OperationKey bone_ready_key(&object->id, DEG_NODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_READY);
383                 OperationKey bone_done_key(&object->id, DEG_NODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_DONE);
384                 pchan->flag &= ~POSE_DONE;
385                 /* Pose init to bone local. */
386                 add_relation(init_key, bone_local_key, "PoseEval Source-Bone Link");
387                 /* Local to pose parenting operation. */
388                 add_relation(bone_local_key, bone_pose_key, "Bone Local - PoseSpace Link");
389                 /* Parent relation. */
390                 if (pchan->parent != NULL) {
391                         eDepsOperation_Code parent_key_opcode;
392
393                         /* NOTE: this difference in handling allows us to prevent lockups
394                          * while ensuring correct poses for separate chains.
395                          */
396                         if (root_map.has_common_root(pchan->name, pchan->parent->name)) {
397                                 parent_key_opcode = DEG_OPCODE_BONE_READY;
398                         }
399                         else {
400                                 parent_key_opcode = DEG_OPCODE_BONE_DONE;
401                         }
402
403                         OperationKey parent_key(&object->id, DEG_NODE_TYPE_BONE, pchan->parent->name, parent_key_opcode);
404                         add_relation(parent_key, bone_pose_key, "Parent Bone -> Child Bone");
405                 }
406                 /* Buil constraints. */
407                 if (pchan->constraints.first != NULL) {
408                         /* Build relations for indirectly linked objects. */
409                         BuilderWalkUserData data;
410                         data.builder = this;
411                         BKE_constraints_id_loop(&pchan->constraints, constraint_walk, &data);
412
413                         /* constraints stack and constraint dependencies */
414                         build_constraints(&object->id, DEG_NODE_TYPE_BONE, pchan->name, &pchan->constraints, &root_map);
415
416                         /* pose -> constraints */
417                         OperationKey constraints_key(&object->id, DEG_NODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_CONSTRAINTS);
418                         add_relation(bone_pose_key, constraints_key, "Constraints Stack");
419
420                         /* constraints -> ready */
421                         // TODO: when constraint stack is exploded, this step should occur before the first IK solver
422                         add_relation(constraints_key, bone_ready_key, "Constraints -> Ready");
423                 }
424                 else {
425                         /* pose -> ready */
426                         add_relation(bone_pose_key, bone_ready_key, "Pose -> Ready");
427                 }
428
429                 /* bone ready -> done
430                  * NOTE: For bones without IK, this is all that's needed.
431                  *       For IK chains however, an additional rel is created from IK
432                  *       to done, with transitive reduction removing this one..
433                  */
434                 add_relation(bone_ready_key, bone_done_key, "Ready -> Done");
435
436                 /* assume that all bones must be done for the pose to be ready
437                  * (for deformers)
438                  */
439                 add_relation(bone_done_key, flush_key, "PoseEval Result-Bone Link");
440                 /* Custom shape. */
441                 if (pchan->custom != NULL) {
442                         build_object(NULL, pchan->custom);
443                 }
444         }
445 }
446
447 void DepsgraphRelationBuilder::build_proxy_rig(Object *object)
448 {
449         bArmature *armature = (bArmature *)object->data;
450         Object *proxy_from = object->proxy_from;
451         build_armature(armature);
452         OperationKey pose_init_key(&object->id,
453                                    DEG_NODE_TYPE_EVAL_POSE,
454                                    DEG_OPCODE_POSE_INIT);
455         OperationKey pose_done_key(&object->id,
456                                    DEG_NODE_TYPE_EVAL_POSE,
457                                    DEG_OPCODE_POSE_DONE);
458         LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) {
459                 OperationKey bone_local_key(&object->id,
460                                             DEG_NODE_TYPE_BONE, pchan->name,
461                                             DEG_OPCODE_BONE_LOCAL);
462                 OperationKey bone_ready_key(&object->id,
463                                             DEG_NODE_TYPE_BONE,
464                                             pchan->name,
465                                             DEG_OPCODE_BONE_READY);
466                 OperationKey bone_done_key(&object->id,
467                                            DEG_NODE_TYPE_BONE,
468                                            pchan->name,
469                                            DEG_OPCODE_BONE_DONE);
470                 add_relation(pose_init_key, bone_local_key, "Pose Init -> Bone Local");
471                 add_relation(bone_local_key, bone_ready_key, "Local -> Ready");
472                 add_relation(bone_ready_key, bone_done_key, "Ready -> Done");
473                 add_relation(bone_done_key, pose_done_key, "Bone Done -> Pose Done");
474
475                 if (pchan->prop != NULL) {
476                         OperationKey bone_parameters(&object->id,
477                                                      DEG_NODE_TYPE_PARAMETERS,
478                                                      DEG_OPCODE_PARAMETERS_EVAL,
479                                                      pchan->name);
480                         OperationKey from_bone_parameters(&proxy_from->id,
481                                                           DEG_NODE_TYPE_PARAMETERS,
482                                                           DEG_OPCODE_PARAMETERS_EVAL,
483                                                           pchan->name);
484                         add_relation(from_bone_parameters,
485                                      bone_parameters,
486                                      "Proxy Bone Parameters");
487                 }
488         }
489 }
490
491 }  // namespace DEG