5b1a6ea7a51eacd3e855c94caf295a0076c20723
[blender-staging.git] / source / blender / blenkernel / intern / action.c
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) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Full recode, Ton Roosendaal, Crete 2005
22  *                               Full recode, Joshua Leung, 2009
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/blenkernel/intern/action.c
28  *  \ingroup bke
29  */
30
31
32 #include <string.h>
33 #include <math.h>
34 #include <stdlib.h>
35 #include <stddef.h>     
36
37 #include "MEM_guardedalloc.h"
38
39 #include "DNA_anim_types.h"
40 #include "DNA_armature_types.h"
41 #include "DNA_constraint_types.h"
42 #include "DNA_scene_types.h"
43 #include "DNA_object_types.h"
44
45 #include "BLI_blenlib.h"
46 #include "BLI_math.h"
47 #include "BLI_utildefines.h"
48 #include "BLI_ghash.h"
49
50 #include "BLT_translation.h"
51
52 #include "BKE_action.h"
53 #include "BKE_anim.h"
54 #include "BKE_animsys.h"
55 #include "BKE_constraint.h"
56 #include "BKE_deform.h"
57 #include "BKE_depsgraph.h"
58 #include "BKE_fcurve.h"
59 #include "BKE_global.h"
60 #include "BKE_idprop.h"
61 #include "BKE_library.h"
62 #include "BKE_main.h"
63 #include "BKE_object.h"
64
65 #include "BIK_api.h"
66
67 #include "RNA_access.h"
68
69 /* *********************** NOTE ON POSE AND ACTION **********************
70  *
71  * - Pose is the local (object level) component of armature. The current
72  *   object pose is saved in files, and (will be) is presorted for dependency
73  * - Actions have fewer (or other) channels, and write data to a Pose
74  * - Currently ob->pose data is controlled in BKE_pose_where_is only. The (recalc)
75  *   event system takes care of calling that
76  * - The NLA system (here too) uses Poses as interpolation format for Actions
77  * - Therefore we assume poses to be static, and duplicates of poses have channels in
78  *   same order, for quick interpolation reasons
79  *
80  * ****************************** (ton) ************************************ */
81
82 /* ***************** Library data level operations on action ************** */
83
84 bAction *add_empty_action(Main *bmain, const char name[])
85 {
86         bAction *act;
87         
88         act = BKE_libblock_alloc(bmain, ID_AC, name);
89         
90         return act;
91 }       
92
93 /* .................................. */
94
95 /* temp data for BKE_action_make_local */
96 typedef struct tMakeLocalActionContext {
97         bAction *act;       /* original action */
98         bAction *act_new;   /* new action */
99         
100         bool is_lib;        /* some action users were libraries */
101         bool is_local;      /* some action users were not libraries */
102 } tMakeLocalActionContext;
103
104 /* helper function for BKE_action_make_local() - local/lib init step */
105 static void make_localact_init_cb(ID *id, AnimData *adt, void *mlac_ptr)
106 {
107         tMakeLocalActionContext *mlac = (tMakeLocalActionContext *)mlac_ptr;
108         
109         if (adt->action == mlac->act) {
110                 if (id->lib) mlac->is_lib = true;
111                 else mlac->is_local = true;
112         }
113 }
114
115 /* helper function for BKE_action_make_local() - change references */
116 static void make_localact_apply_cb(ID *id, AnimData *adt, void *mlac_ptr)
117 {
118         tMakeLocalActionContext *mlac = (tMakeLocalActionContext *)mlac_ptr;
119         
120         if (adt->action == mlac->act) {
121                 if (id->lib == NULL) {
122                         adt->action = mlac->act_new;
123                         
124                         id_us_plus(&mlac->act_new->id);
125                         id_us_min(&mlac->act->id);
126                 }
127         }
128 }
129
130 // does copy_fcurve...
131 void BKE_action_make_local(bAction *act)
132 {
133         tMakeLocalActionContext mlac = {act, NULL, false, false};
134         Main *bmain = G.main;
135         
136         if (act->id.lib == NULL)
137                 return;
138         
139         /* XXX: double-check this; it used to be just single-user check, but that was when fake-users were still default */
140         if ((act->id.flag & LIB_FAKEUSER) && (act->id.us <= 1)) {
141                 id_clear_lib_data(bmain, &act->id);
142                 return;
143         }
144         
145         BKE_animdata_main_cb(bmain, make_localact_init_cb, &mlac);
146         
147         if (mlac.is_local && mlac.is_lib == false) {
148                 id_clear_lib_data(bmain, &act->id);
149         }
150         else if (mlac.is_local && mlac.is_lib) {
151                 mlac.act_new = BKE_action_copy(act);
152                 mlac.act_new->id.us = 0;
153
154                 BKE_id_lib_local_paths(bmain, act->id.lib, &mlac.act_new->id);
155
156                 BKE_animdata_main_cb(bmain, make_localact_apply_cb, &mlac);
157         }
158 }
159
160 /* .................................. */
161
162 void BKE_action_free(bAction *act)
163 {
164         /* sanity check */
165         if (act == NULL)
166                 return;
167         
168         /* Free F-Curves */
169         free_fcurves(&act->curves);
170         
171         /* Free groups */
172         if (act->groups.first)
173                 BLI_freelistN(&act->groups);
174                 
175         /* Free pose-references (aka local markers) */
176         if (act->markers.first)
177                 BLI_freelistN(&act->markers);
178 }
179
180 /* .................................. */
181
182 bAction *BKE_action_copy(bAction *src)
183 {
184         bAction *dst = NULL;
185         bActionGroup *dgrp, *sgrp;
186         FCurve *dfcu, *sfcu;
187         
188         if (src == NULL) 
189                 return NULL;
190         dst = BKE_libblock_copy(&src->id);
191         
192         /* duplicate the lists of groups and markers */
193         BLI_duplicatelist(&dst->groups, &src->groups);
194         BLI_duplicatelist(&dst->markers, &src->markers);
195         
196         /* copy F-Curves, fixing up the links as we go */
197         BLI_listbase_clear(&dst->curves);
198         
199         for (sfcu = src->curves.first; sfcu; sfcu = sfcu->next) {
200                 /* duplicate F-Curve */
201                 dfcu = copy_fcurve(sfcu);
202                 BLI_addtail(&dst->curves, dfcu);
203                 
204                 /* fix group links (kindof bad list-in-list search, but this is the most reliable way) */
205                 for (dgrp = dst->groups.first, sgrp = src->groups.first; dgrp && sgrp; dgrp = dgrp->next, sgrp = sgrp->next) {
206                         if (sfcu->grp == sgrp) {
207                                 dfcu->grp = dgrp;
208                                 
209                                 if (dgrp->channels.first == sfcu)
210                                         dgrp->channels.first = dfcu;
211                                 if (dgrp->channels.last == sfcu)
212                                         dgrp->channels.last = dfcu;
213                                         
214                                 break;
215                         }
216                 }
217         }
218         
219         if (src->id.lib) {
220                 BKE_id_lib_local_paths(G.main, src->id.lib, &dst->id);
221         }
222
223         return dst;
224 }
225
226 /* *************** Action Groups *************** */
227
228 /* Get the active action-group for an Action */
229 bActionGroup *get_active_actiongroup(bAction *act)
230 {
231         bActionGroup *agrp = NULL;
232         
233         if (act && act->groups.first) {
234                 for (agrp = act->groups.first; agrp; agrp = agrp->next) {
235                         if (agrp->flag & AGRP_ACTIVE)
236                                 break;
237                 }
238         }
239         
240         return agrp;
241 }
242
243 /* Make the given Action-Group the active one */
244 void set_active_action_group(bAction *act, bActionGroup *agrp, short select)
245 {
246         bActionGroup *grp;
247         
248         /* sanity checks */
249         if (act == NULL)
250                 return;
251         
252         /* Deactive all others */
253         for (grp = act->groups.first; grp; grp = grp->next) {
254                 if ((grp == agrp) && (select))
255                         grp->flag |= AGRP_ACTIVE;
256                 else
257                         grp->flag &= ~AGRP_ACTIVE;
258         }
259 }
260
261 /* Sync colors used for action/bone group with theme settings */
262 void action_group_colors_sync(bActionGroup *grp, const bActionGroup *ref_grp)
263 {
264         /* only do color copying if using a custom color (i.e. not default color)  */
265         if (grp->customCol) {
266                 if (grp->customCol > 0) {
267                         /* copy theme colors on-to group's custom color in case user tries to edit color */
268                         bTheme *btheme = U.themes.first;
269                         ThemeWireColor *col_set = &btheme->tarm[(grp->customCol - 1)];
270                         
271                         memcpy(&grp->cs, col_set, sizeof(ThemeWireColor));
272                 }
273                 else {
274                         /* if a reference group is provided, use the custom color from there... */
275                         if (ref_grp) {
276                                 /* assumption: reference group has a color set */
277                                 memcpy(&grp->cs, &ref_grp->cs, sizeof(ThemeWireColor));
278                         }
279                         /* otherwise, init custom color with a generic/placeholder color set if
280                          * no previous theme color was used that we can just keep using
281                          */
282                         else if (grp->cs.solid[0] == 0) {
283                                 /* define for setting colors in theme below */
284                                 rgba_char_args_set(grp->cs.solid, 0xff, 0x00, 0x00, 255);
285                                 rgba_char_args_set(grp->cs.select, 0x81, 0xe6, 0x14, 255);
286                                 rgba_char_args_set(grp->cs.active, 0x18, 0xb6, 0xe0, 255);
287                         }
288                 }
289         }
290 }
291
292 /* Add a new action group with the given name to the action */
293 bActionGroup *action_groups_add_new(bAction *act, const char name[])
294 {
295         bActionGroup *agrp;
296         
297         /* sanity check: must have action and name */
298         if (ELEM(NULL, act, name))
299                 return NULL;
300         
301         /* allocate a new one */
302         agrp = MEM_callocN(sizeof(bActionGroup), "bActionGroup");
303         
304         /* make it selected, with default name */
305         agrp->flag = AGRP_SELECTED;
306         BLI_strncpy(agrp->name, name[0] ? name : DATA_("Group"), sizeof(agrp->name));
307         
308         /* add to action, and validate */
309         BLI_addtail(&act->groups, agrp);
310         BLI_uniquename(&act->groups, agrp, DATA_("Group"), '.', offsetof(bActionGroup, name), sizeof(agrp->name));
311         
312         /* return the new group */
313         return agrp;
314 }
315
316 /* Add given channel into (active) group 
317  *      - assumes that channel is not linked to anything anymore
318  *      - always adds at the end of the group 
319  */
320 void action_groups_add_channel(bAction *act, bActionGroup *agrp, FCurve *fcurve)
321 {       
322         /* sanity checks */
323         if (ELEM(NULL, act, agrp, fcurve))
324                 return;
325         
326         /* if no channels anywhere, just add to two lists at the same time */
327         if (BLI_listbase_is_empty(&act->curves)) {
328                 fcurve->next = fcurve->prev = NULL;
329                 
330                 agrp->channels.first = agrp->channels.last = fcurve;
331                 act->curves.first = act->curves.last = fcurve;
332         }
333         
334         /* if the group already has channels, the F-Curve can simply be added to the list 
335          * (i.e. as the last channel in the group)
336          */
337         else if (agrp->channels.first) {
338                 /* if the group's last F-Curve is the action's last F-Curve too, 
339                  * then set the F-Curve as the last for the action first so that
340                  * the lists will be in sync after linking
341                  */
342                 if (agrp->channels.last == act->curves.last)
343                         act->curves.last = fcurve;
344                         
345                 /* link in the given F-Curve after the last F-Curve in the group,
346                  * which means that it should be able to fit in with the rest of the
347                  * list seamlessly
348                  */
349                 BLI_insertlinkafter(&agrp->channels, agrp->channels.last, fcurve);
350         }
351         
352         /* otherwise, need to find the nearest F-Curve in group before/after current to link with */
353         else {
354                 bActionGroup *grp;
355                 
356                 /* firstly, link this F-Curve to the group */
357                 agrp->channels.first = agrp->channels.last = fcurve;
358                 
359                 /* step through the groups preceding this one, finding the F-Curve there to attach this one after */
360                 for (grp = agrp->prev; grp; grp = grp->prev) {
361                         /* if this group has F-Curves, we want weave the given one in right after the last channel there,
362                          * but via the Action's list not this group's list
363                          *      - this is so that the F-Curve is in the right place in the Action,
364                          *        but won't be included in the previous group
365                          */
366                         if (grp->channels.last) {
367                                 /* once we've added, break here since we don't need to search any further... */
368                                 BLI_insertlinkafter(&act->curves, grp->channels.last, fcurve);
369                                 break;
370                         }
371                 }
372                 
373                 /* if grp is NULL, that means we fell through, and this F-Curve should be added as the new first
374                  * since group is (effectively) the first group. Thus, the existing first F-Curve becomes the 
375                  * second in the chain, etc. etc.
376                  */
377                 if (grp == NULL)
378                         BLI_insertlinkbefore(&act->curves, act->curves.first, fcurve);
379         }
380         
381         /* set the F-Curve's new group */
382         fcurve->grp = agrp;
383 }       
384
385 /* Remove the given channel from all groups */
386 void action_groups_remove_channel(bAction *act, FCurve *fcu)
387 {
388         /* sanity checks */
389         if (ELEM(NULL, act, fcu))
390                 return;
391         
392         /* check if any group used this directly */
393         if (fcu->grp) {
394                 bActionGroup *agrp = fcu->grp;
395                 
396                 if (agrp->channels.first == agrp->channels.last) {
397                         if (agrp->channels.first == fcu) {
398                                 BLI_listbase_clear(&agrp->channels);
399                         }
400                 }
401                 else if (agrp->channels.first == fcu) {
402                         if ((fcu->next) && (fcu->next->grp == agrp))
403                                 agrp->channels.first = fcu->next;
404                         else
405                                 agrp->channels.first = NULL;
406                 }
407                 else if (agrp->channels.last == fcu) {
408                         if ((fcu->prev) && (fcu->prev->grp == agrp))
409                                 agrp->channels.last = fcu->prev;
410                         else
411                                 agrp->channels.last = NULL;
412                 }
413                 
414                 fcu->grp = NULL;
415         }
416         
417         /* now just remove from list */
418         BLI_remlink(&act->curves, fcu);
419 }
420
421 /* Find a group with the given name */
422 bActionGroup *BKE_action_group_find_name(bAction *act, const char name[])
423 {
424         /* sanity checks */
425         if (ELEM(NULL, act, act->groups.first, name) || (name[0] == 0))
426                 return NULL;
427                 
428         /* do string comparisons */
429         return BLI_findstring(&act->groups, name, offsetof(bActionGroup, name));
430 }
431
432 /* Clear all 'temp' flags on all groups */
433 void action_groups_clear_tempflags(bAction *act)
434 {
435         bActionGroup *agrp;
436         
437         /* sanity checks */
438         if (ELEM(NULL, act, act->groups.first))
439                 return;
440                 
441         /* flag clearing loop */
442         for (agrp = act->groups.first; agrp; agrp = agrp->next)
443                 agrp->flag &= ~AGRP_TEMP;
444 }
445
446 /* *************** Pose channels *************** */
447
448 /**
449  * Return a pointer to the pose channel of the given name
450  * from this pose.
451  */
452 bPoseChannel *BKE_pose_channel_find_name(const bPose *pose, const char *name)
453 {
454         if (ELEM(NULL, pose, name) || (name[0] == '\0'))
455                 return NULL;
456         
457         if (pose->chanhash)
458                 return BLI_ghash_lookup(pose->chanhash, (const void *)name);
459         
460         return BLI_findstring(&((const bPose *)pose)->chanbase, name, offsetof(bPoseChannel, name));
461 }
462
463 /**
464  * Looks to see if the channel with the given name
465  * already exists in this pose - if not a new one is
466  * allocated and initialized.
467  *
468  * \note Use with care, not on Armature poses but for temporal ones.
469  * \note (currently used for action constraints and in rebuild_pose).
470  */
471 bPoseChannel *BKE_pose_channel_verify(bPose *pose, const char *name)
472 {
473         bPoseChannel *chan;
474         
475         if (pose == NULL)
476                 return NULL;
477         
478         /* See if this channel exists */
479         chan = BLI_findstring(&pose->chanbase, name, offsetof(bPoseChannel, name));
480         if (chan) {
481                 return chan;
482         }
483
484         /* If not, create it and add it */
485         chan = MEM_callocN(sizeof(bPoseChannel), "verifyPoseChannel");
486         
487         BLI_strncpy(chan->name, name, sizeof(chan->name));
488         /* init vars to prevent math errors */
489         unit_qt(chan->quat);
490         unit_axis_angle(chan->rotAxis, &chan->rotAngle);
491         chan->size[0] = chan->size[1] = chan->size[2] = 1.0f;
492         
493         chan->limitmin[0] = chan->limitmin[1] = chan->limitmin[2] = -180.0f;
494         chan->limitmax[0] = chan->limitmax[1] = chan->limitmax[2] = 180.0f;
495         chan->stiffness[0] = chan->stiffness[1] = chan->stiffness[2] = 0.0f;
496         chan->ikrotweight = chan->iklinweight = 0.0f;
497         unit_m4(chan->constinv);
498         
499         chan->protectflag = OB_LOCK_ROT4D;  /* lock by components by default */
500         
501         BLI_addtail(&pose->chanbase, chan);
502         BKE_pose_channels_hash_free(pose);
503         
504         return chan;
505 }
506
507 #ifndef NDEBUG
508 bool BKE_pose_channels_is_valid(const bPose *pose)
509 {
510         if (pose->chanhash) {
511                 bPoseChannel *pchan;
512                 for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
513                         if (BLI_ghash_lookup(pose->chanhash, pchan->name) != pchan) {
514                                 return false;
515                         }
516                 }
517         }
518
519         return true;
520 }
521
522 #endif
523
524 /**
525  * Find the active posechannel for an object (we can't just use pose, as layer info is in armature)
526  *
527  * \note: Object, not bPose is used here, as we need layer info from Armature)
528  */
529 bPoseChannel *BKE_pose_channel_active(Object *ob)
530 {
531         bArmature *arm = (ob) ? ob->data : NULL;
532         bPoseChannel *pchan;
533
534         if (ELEM(NULL, ob, ob->pose, arm)) {
535                 return NULL;
536         }
537
538         /* find active */
539         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
540                 if ((pchan->bone) && (pchan->bone == arm->act_bone) && (pchan->bone->layer & arm->layer))
541                         return pchan;
542         }
543         
544         return NULL;
545 }
546
547 /**
548  * \see #ED_armature_bone_get_mirrored (edit-mode, matching function)
549  */
550 bPoseChannel *BKE_pose_channel_get_mirrored(const bPose *pose, const char *name)
551 {
552         char name_flip[MAXBONENAME];
553
554         BKE_deform_flip_side_name(name_flip, name, false);
555
556         if (!STREQ(name_flip, name)) {
557                 return BKE_pose_channel_find_name(pose, name_flip);
558         }
559
560         return NULL;
561 }
562
563 const char *BKE_pose_ikparam_get_name(bPose *pose)
564 {
565         if (pose) {
566                 switch (pose->iksolver) {
567                         case IKSOLVER_STANDARD:
568                                 return NULL;
569                         case IKSOLVER_ITASC:
570                                 return "bItasc";
571                 }
572         }
573         return NULL;
574 }
575
576 /**
577  * Allocate a new pose on the heap, and copy the src pose and it's channels
578  * into the new pose. *dst is set to the newly allocated structure, and assumed to be NULL.
579  *
580  * \param dst  Should be freed already, makes entire duplicate.
581  */
582 void BKE_pose_copy_data(bPose **dst, bPose *src, const bool copy_constraints)
583 {
584         bPose *outPose;
585         bPoseChannel *pchan;
586         ListBase listb;
587
588         if (!src) {
589                 *dst = NULL;
590                 return;
591         }
592         
593         outPose = MEM_callocN(sizeof(bPose), "pose");
594         
595         BLI_duplicatelist(&outPose->chanbase, &src->chanbase);
596
597         outPose->iksolver = src->iksolver;
598         outPose->ikdata = NULL;
599         outPose->ikparam = MEM_dupallocN(src->ikparam);
600         outPose->avs = src->avs;
601         
602         for (pchan = outPose->chanbase.first; pchan; pchan = pchan->next) {
603
604                 if (pchan->custom) {
605                         id_us_plus(&pchan->custom->id);
606                 }
607
608                 /* warning, O(n2) here, but it's a rarely used feature. */
609                 if (pchan->custom_tx) {
610                         pchan->custom_tx = BKE_pose_channel_find_name(outPose, pchan->custom_tx->name);
611                 }
612
613                 if (copy_constraints) {
614                         BKE_constraints_copy(&listb, &pchan->constraints, true);  // BKE_constraints_copy NULLs listb
615                         pchan->constraints = listb;
616                         pchan->mpath = NULL; /* motion paths should not get copied yet... */
617                 }
618                 
619                 if (pchan->prop) {
620                         pchan->prop = IDP_CopyProperty(pchan->prop);
621                 }
622         }
623
624         /* for now, duplicate Bone Groups too when doing this */
625         if (copy_constraints) {
626                 BLI_duplicatelist(&outPose->agroups, &src->agroups);
627         }
628         
629         *dst = outPose;
630 }
631
632 void BKE_pose_itasc_init(bItasc *itasc)
633 {
634         if (itasc) {
635                 itasc->iksolver = IKSOLVER_ITASC;
636                 itasc->minstep = 0.01f;
637                 itasc->maxstep = 0.06f;
638                 itasc->numiter = 100;
639                 itasc->numstep = 4;
640                 itasc->precision = 0.005f;
641                 itasc->flag = ITASC_AUTO_STEP | ITASC_INITIAL_REITERATION;
642                 itasc->feedback = 20.0f;
643                 itasc->maxvel = 50.0f;
644                 itasc->solver = ITASC_SOLVER_SDLS;
645                 itasc->dampmax = 0.5;
646                 itasc->dampeps = 0.15;
647         }
648 }
649 void BKE_pose_ikparam_init(bPose *pose)
650 {
651         bItasc *itasc;
652         switch (pose->iksolver) {
653                 case IKSOLVER_ITASC:
654                         itasc = MEM_callocN(sizeof(bItasc), "itasc");
655                         BKE_pose_itasc_init(itasc);
656                         pose->ikparam = itasc;
657                         break;
658                 case IKSOLVER_STANDARD:
659                 default:
660                         pose->ikparam = NULL;
661                         break;
662         }
663 }
664
665
666 /* only for real IK, not for auto-IK */
667 static bool pose_channel_in_IK_chain(Object *ob, bPoseChannel *pchan, int level)
668 {
669         bConstraint *con;
670         Bone *bone;
671         
672         /* No need to check if constraint is active (has influence),
673          * since all constraints with CONSTRAINT_IK_AUTO are active */
674         for (con = pchan->constraints.first; con; con = con->next) {
675                 if (con->type == CONSTRAINT_TYPE_KINEMATIC) {
676                         bKinematicConstraint *data = con->data;
677                         if ((data->rootbone == 0) || (data->rootbone > level)) {
678                                 if ((data->flag & CONSTRAINT_IK_AUTO) == 0)
679                                         return true;
680                         }
681                 }
682         }
683         for (bone = pchan->bone->childbase.first; bone; bone = bone->next) {
684                 pchan = BKE_pose_channel_find_name(ob->pose, bone->name);
685                 if (pchan && pose_channel_in_IK_chain(ob, pchan, level + 1))
686                         return true;
687         }
688         return false;
689 }
690
691 bool BKE_pose_channel_in_IK_chain(Object *ob, bPoseChannel *pchan)
692 {
693         return pose_channel_in_IK_chain(ob, pchan, 0);
694 }
695
696 /**
697  * Removes the hash for quick lookup of channels, must
698  * be done when adding/removing channels.
699  */
700 void BKE_pose_channels_hash_make(bPose *pose) 
701 {
702         if (!pose->chanhash) {
703                 bPoseChannel *pchan;
704                 
705                 pose->chanhash = BLI_ghash_str_new("make_pose_chan gh");
706                 for (pchan = pose->chanbase.first; pchan; pchan = pchan->next)
707                         BLI_ghash_insert(pose->chanhash, pchan->name, pchan);
708         }
709 }
710
711 void BKE_pose_channels_hash_free(bPose *pose) 
712 {
713         if (pose->chanhash) {
714                 BLI_ghash_free(pose->chanhash, NULL, NULL);
715                 pose->chanhash = NULL;
716         }
717 }
718
719 /**
720  * Selectively remove pose channels.
721  */
722 void BKE_pose_channels_remove(
723         Object *ob,
724         bool (*filter_fn)(const char *bone_name, void *user_data), void *user_data)
725 {
726         /*  First erase any associated pose channel */
727         if (ob->pose) {
728                 bPoseChannel *pchan, *pchan_next;
729                 bConstraint *con;
730
731                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan_next) {
732                         pchan_next = pchan->next;
733
734                         if (filter_fn(pchan->name, user_data)) {
735                                 BKE_pose_channel_free(pchan);
736                                 if (ob->pose->chanhash) {
737                                         BLI_ghash_remove(ob->pose->chanhash, pchan->name, NULL, NULL);
738                                 }
739                                 BLI_freelinkN(&ob->pose->chanbase, pchan);
740                         }
741                         else {
742                                 for (con = pchan->constraints.first; con; con = con->next) {
743                                         const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con);
744                                         ListBase targets = {NULL, NULL};
745                                         bConstraintTarget *ct;
746
747                                         if (cti && cti->get_constraint_targets) {
748                                                 cti->get_constraint_targets(con, &targets);
749
750                                                 for (ct = targets.first; ct; ct = ct->next) {
751                                                         if (ct->tar == ob) {
752                                                                 if (ct->subtarget[0]) {
753                                                                         if (filter_fn(ct->subtarget, user_data)) {
754                                                                                 con->flag |= CONSTRAINT_DISABLE;
755                                                                                 ct->subtarget[0] = 0;
756                                                                         }
757                                                                 }
758                                                         }
759                                                 }
760
761                                                 if (cti->flush_constraint_targets)
762                                                         cti->flush_constraint_targets(con, &targets, 0);
763                                         }
764                                 }
765                         }
766                 }
767         }
768 }
769
770 /**
771  * Deallocates a pose channel.
772  * Does not free the pose channel itself.
773  */
774 void BKE_pose_channel_free_ex(bPoseChannel *pchan, bool do_id_user)
775 {
776         if (pchan->custom) {
777                 if (do_id_user) {
778                         id_us_min(&pchan->custom->id);
779                 }
780                 pchan->custom = NULL;
781         }
782
783         if (pchan->mpath) {
784                 animviz_free_motionpath(pchan->mpath);
785                 pchan->mpath = NULL;
786         }
787
788         BKE_constraints_free_ex(&pchan->constraints, do_id_user);
789         
790         if (pchan->prop) {
791                 IDP_FreeProperty(pchan->prop);
792                 MEM_freeN(pchan->prop);
793         }
794 }
795
796 void BKE_pose_channel_free(bPoseChannel *pchan)
797 {
798         BKE_pose_channel_free_ex(pchan, true);
799 }
800
801 /**
802  * Removes and deallocates all channels from a pose.
803  * Does not free the pose itself.
804  */
805 void BKE_pose_channels_free_ex(bPose *pose, bool do_id_user)
806 {
807         bPoseChannel *pchan;
808         
809         if (pose->chanbase.first) {
810                 for (pchan = pose->chanbase.first; pchan; pchan = pchan->next)
811                         BKE_pose_channel_free_ex(pchan, do_id_user);
812                 
813                 BLI_freelistN(&pose->chanbase);
814         }
815
816         BKE_pose_channels_hash_free(pose);
817 }
818
819 void BKE_pose_channels_free(bPose *pose)
820 {
821         BKE_pose_channels_free_ex(pose, true);
822 }
823
824 /**
825  * Removes and deallocates all data from a pose, and also frees the pose.
826  */
827 void BKE_pose_free_ex(bPose *pose, bool do_id_user)
828 {
829         if (pose) {
830                 /* free pose-channels */
831                 BKE_pose_channels_free_ex(pose, do_id_user);
832                 
833                 /* free pose-groups */
834                 if (pose->agroups.first)
835                         BLI_freelistN(&pose->agroups);
836                 
837                 /* free IK solver state */
838                 BIK_clear_data(pose);
839                 
840                 /* free IK solver param */
841                 if (pose->ikparam)
842                         MEM_freeN(pose->ikparam);
843                 
844                 /* free pose */
845                 MEM_freeN(pose);
846         }
847 }
848
849 void BKE_pose_free(bPose *pose)
850 {
851         BKE_pose_free_ex(pose, true);
852 }
853
854 static void copy_pose_channel_data(bPoseChannel *pchan, const bPoseChannel *chan)
855 {
856         bConstraint *pcon, *con;
857         
858         copy_v3_v3(pchan->loc, chan->loc);
859         copy_v3_v3(pchan->size, chan->size);
860         copy_v3_v3(pchan->eul, chan->eul);
861         copy_v3_v3(pchan->rotAxis, chan->rotAxis);
862         pchan->rotAngle = chan->rotAngle;
863         copy_qt_qt(pchan->quat, chan->quat);
864         pchan->rotmode = chan->rotmode;
865         copy_m4_m4(pchan->chan_mat, (float(*)[4])chan->chan_mat);
866         copy_m4_m4(pchan->pose_mat, (float(*)[4])chan->pose_mat);
867         pchan->flag = chan->flag;
868         
869         con = chan->constraints.first;
870         for (pcon = pchan->constraints.first; pcon && con; pcon = pcon->next, con = con->next) {
871                 pcon->enforce = con->enforce;
872                 pcon->headtail = con->headtail;
873         }
874 }
875
876 /**
877  * Copy the internal members of each pose channel including constraints
878  * and ID-Props, used when duplicating bones in editmode.
879  * (unlike copy_pose_channel_data which only).
880  *
881  * \note use when copying bones in editmode (on returned value from #BKE_pose_channel_verify)
882  */
883 void BKE_pose_channel_copy_data(bPoseChannel *pchan, const bPoseChannel *pchan_from)
884 {
885         /* copy transform locks */
886         pchan->protectflag = pchan_from->protectflag;
887
888         /* copy rotation mode */
889         pchan->rotmode = pchan_from->rotmode;
890
891         /* copy bone group */
892         pchan->agrp_index = pchan_from->agrp_index;
893
894         /* ik (dof) settings */
895         pchan->ikflag = pchan_from->ikflag;
896         copy_v3_v3(pchan->limitmin, pchan_from->limitmin);
897         copy_v3_v3(pchan->limitmax, pchan_from->limitmax);
898         copy_v3_v3(pchan->stiffness, pchan_from->stiffness);
899         pchan->ikstretch = pchan_from->ikstretch;
900         pchan->ikrotweight = pchan_from->ikrotweight;
901         pchan->iklinweight = pchan_from->iklinweight;
902
903         /* constraints */
904         BKE_constraints_copy(&pchan->constraints, &pchan_from->constraints, true);
905
906         /* id-properties */
907         if (pchan->prop) {
908                 /* unlikely but possible it exists */
909                 IDP_FreeProperty(pchan->prop);
910                 MEM_freeN(pchan->prop);
911                 pchan->prop = NULL;
912         }
913         if (pchan_from->prop) {
914                 pchan->prop = IDP_CopyProperty(pchan_from->prop);
915         }
916
917         /* custom shape */
918         pchan->custom = pchan_from->custom;
919         if (pchan->custom) {
920                 id_us_plus(&pchan->custom->id);
921         }
922 }
923
924
925 /* checks for IK constraint, Spline IK, and also for Follow-Path constraint.
926  * can do more constraints flags later 
927  */
928 /* pose should be entirely OK */
929 void BKE_pose_update_constraint_flags(bPose *pose)
930 {
931         bPoseChannel *pchan, *parchan;
932         bConstraint *con;
933         
934         /* clear */
935         for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
936                 pchan->constflag = 0;
937         }
938         pose->flag &= ~POSE_CONSTRAINTS_TIMEDEPEND;
939         
940         /* detect */
941         for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
942                 for (con = pchan->constraints.first; con; con = con->next) {
943                         if (con->type == CONSTRAINT_TYPE_KINEMATIC) {
944                                 bKinematicConstraint *data = (bKinematicConstraint *)con->data;
945                                 
946                                 pchan->constflag |= PCHAN_HAS_IK;
947                                 
948                                 if (data->tar == NULL || (data->tar->type == OB_ARMATURE && data->subtarget[0] == 0))
949                                         pchan->constflag |= PCHAN_HAS_TARGET;
950                                 
951                                 /* negative rootbone = recalc rootbone index. used in do_versions */
952                                 if (data->rootbone < 0) {
953                                         data->rootbone = 0;
954                                         
955                                         if (data->flag & CONSTRAINT_IK_TIP) parchan = pchan;
956                                         else parchan = pchan->parent;
957                                         
958                                         while (parchan) {
959                                                 data->rootbone++;
960                                                 if ((parchan->bone->flag & BONE_CONNECTED) == 0)
961                                                         break;
962                                                 parchan = parchan->parent;
963                                         }
964                                 }
965                         }
966                         else if (con->type == CONSTRAINT_TYPE_FOLLOWPATH) {
967                                 bFollowPathConstraint *data = (bFollowPathConstraint *)con->data;
968                                 
969                                 /* for drawing constraint colors when color set allows this */
970                                 pchan->constflag |= PCHAN_HAS_CONST;
971                                 
972                                 /* if we have a valid target, make sure that this will get updated on frame-change
973                                  * (needed for when there is no anim-data for this pose)
974                                  */
975                                 if ((data->tar) && (data->tar->type == OB_CURVE))
976                                         pose->flag |= POSE_CONSTRAINTS_TIMEDEPEND;
977                         }
978                         else if (con->type == CONSTRAINT_TYPE_SPLINEIK)
979                                 pchan->constflag |= PCHAN_HAS_SPLINEIK;
980                         else 
981                                 pchan->constflag |= PCHAN_HAS_CONST;
982                 }
983         }
984         pose->flag &= ~POSE_CONSTRAINTS_NEED_UPDATE_FLAGS;
985 }
986
987 void BKE_pose_tag_update_constraint_flags(bPose *pose)
988 {
989         pose->flag |= POSE_CONSTRAINTS_NEED_UPDATE_FLAGS;
990 }
991
992 /* Clears all BONE_UNKEYED flags for every pose channel in every pose 
993  * This should only be called on frame changing, when it is acceptable to
994  * do this. Otherwise, these flags should not get cleared as poses may get lost.
995  */
996 void framechange_poses_clear_unkeyed(void)
997 {
998         Object *ob;
999         bPose *pose;
1000         bPoseChannel *pchan;
1001         
1002         /* This needs to be done for each object that has a pose */
1003         /* TODO: proxies may/may not be correctly handled here... (this needs checking) */
1004         for (ob = G.main->object.first; ob; ob = ob->id.next) {
1005                 /* we only need to do this on objects with a pose */
1006                 if ((pose = ob->pose)) {
1007                         for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
1008                                 if (pchan->bone) 
1009                                         pchan->bone->flag &= ~BONE_UNKEYED;
1010                         }
1011                 }
1012         }
1013 }
1014
1015 /* ************************** Bone Groups ************************** */
1016
1017 /* Adds a new bone-group (name may be NULL) */
1018 bActionGroup *BKE_pose_add_group(bPose *pose, const char *name)
1019 {
1020         bActionGroup *grp;
1021         
1022         if (!name) {
1023                 name = DATA_("Group");
1024         }
1025         
1026         grp = MEM_callocN(sizeof(bActionGroup), "PoseGroup");
1027         BLI_strncpy(grp->name, name, sizeof(grp->name));
1028         BLI_addtail(&pose->agroups, grp);
1029         BLI_uniquename(&pose->agroups, grp, name, '.', offsetof(bActionGroup, name), sizeof(grp->name));
1030         
1031         pose->active_group = BLI_listbase_count(&pose->agroups);
1032         
1033         return grp;
1034 }
1035
1036 /* Remove the given bone-group (expects 'virtual' index (+1 one, used by active_group etc.))
1037  * index might be invalid ( < 1), in which case it will be find from grp. */
1038 void BKE_pose_remove_group(bPose *pose, bActionGroup *grp, const int index)
1039 {
1040         bPoseChannel *pchan;
1041         int idx = index;
1042         
1043         if (idx < 1) {
1044                 idx = BLI_findindex(&pose->agroups, grp) + 1;
1045         }
1046         
1047         BLI_assert(idx > 0);
1048         
1049         /* adjust group references (the trouble of using indices!):
1050          *  - firstly, make sure nothing references it
1051          *  - also, make sure that those after this item get corrected
1052          */
1053         for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
1054                 if (pchan->agrp_index == idx)
1055                         pchan->agrp_index = 0;
1056                 else if (pchan->agrp_index > idx)
1057                         pchan->agrp_index--;
1058         }
1059
1060         /* now, remove it from the pose */
1061         BLI_freelinkN(&pose->agroups, grp);
1062         if (pose->active_group >= idx) {
1063                 const bool has_groups = !BLI_listbase_is_empty(&pose->agroups);
1064                 pose->active_group--;
1065                 if (pose->active_group == 0 && has_groups) {
1066                         pose->active_group = 1;
1067                 }
1068                 else if (pose->active_group < 0 || !has_groups) {
1069                         pose->active_group = 0;
1070                 }
1071         }
1072 }
1073
1074 /* Remove the indexed bone-group (expects 'virtual' index (+1 one, used by active_group etc.)) */
1075 void BKE_pose_remove_group_index(bPose *pose, const int index)
1076 {
1077         bActionGroup *grp = NULL;
1078         
1079         /* get group to remove */
1080         grp = BLI_findlink(&pose->agroups, index - 1);
1081         if (grp) {
1082                 BKE_pose_remove_group(pose, grp, index);
1083         }
1084 }
1085
1086 /* ************** F-Curve Utilities for Actions ****************** */
1087
1088 /* Check if the given action has any keyframes */
1089 bool action_has_motion(const bAction *act)
1090 {
1091         FCurve *fcu;
1092         
1093         /* return on the first F-Curve that has some keyframes/samples defined */
1094         if (act) {
1095                 for (fcu = act->curves.first; fcu; fcu = fcu->next) {
1096                         if (fcu->totvert)
1097                                 return true;
1098                 }
1099         }
1100         
1101         /* nothing found */
1102         return false;
1103 }
1104
1105 /* Calculate the extents of given action */
1106 void calc_action_range(const bAction *act, float *start, float *end, short incl_modifiers)
1107 {
1108         FCurve *fcu;
1109         float min = 999999999.0f, max = -999999999.0f;
1110         short foundvert = 0, foundmod = 0;
1111
1112         if (act) {
1113                 for (fcu = act->curves.first; fcu; fcu = fcu->next) {
1114                         /* if curve has keyframes, consider them first */
1115                         if (fcu->totvert) {
1116                                 float nmin, nmax;
1117                                 
1118                                 /* get extents for this curve */
1119                                 /* TODO: allow enabling/disabling this? */
1120                                 calc_fcurve_range(fcu, &nmin, &nmax, false, true);
1121                                 
1122                                 /* compare to the running tally */
1123                                 min = min_ff(min, nmin);
1124                                 max = max_ff(max, nmax);
1125                                 
1126                                 foundvert = 1;
1127                         }
1128                         
1129                         /* if incl_modifiers is enabled, need to consider modifiers too
1130                          *      - only really care about the last modifier
1131                          */
1132                         if ((incl_modifiers) && (fcu->modifiers.last)) {
1133                                 FModifier *fcm = fcu->modifiers.last;
1134                                 
1135                                 /* only use the maximum sensible limits of the modifiers if they are more extreme */
1136                                 switch (fcm->type) {
1137                                         case FMODIFIER_TYPE_LIMITS: /* Limits F-Modifier */
1138                                         {
1139                                                 FMod_Limits *fmd = (FMod_Limits *)fcm->data;
1140                                                 
1141                                                 if (fmd->flag & FCM_LIMIT_XMIN) {
1142                                                         min = min_ff(min, fmd->rect.xmin);
1143                                                 }
1144                                                 if (fmd->flag & FCM_LIMIT_XMAX) {
1145                                                         max = max_ff(max, fmd->rect.xmax);
1146                                                 }
1147                                                 break;
1148                                         }
1149                                         case FMODIFIER_TYPE_CYCLES: /* Cycles F-Modifier */
1150                                         {
1151                                                 FMod_Cycles *fmd = (FMod_Cycles *)fcm->data;
1152                                                 
1153                                                 if (fmd->before_mode != FCM_EXTRAPOLATE_NONE)
1154                                                         min = MINAFRAMEF;
1155                                                 if (fmd->after_mode != FCM_EXTRAPOLATE_NONE)
1156                                                         max = MAXFRAMEF;
1157                                                 break;
1158                                         }
1159                                         /* TODO: function modifier may need some special limits */
1160                                                 
1161                                         default: /* all other standard modifiers are on the infinite range... */
1162                                                 min = MINAFRAMEF;
1163                                                 max = MAXFRAMEF;
1164                                                 break;
1165                                 }
1166                                 
1167                                 foundmod = 1;
1168                         }
1169                 }
1170         }
1171         
1172         if (foundvert || foundmod) {
1173                 if (min == max) max += 1.0f;
1174                 *start = min;
1175                 *end = max;
1176         }
1177         else {
1178                 *start = 0.0f;
1179                 *end = 1.0f;
1180         }
1181 }
1182
1183 /* Return flags indicating which transforms the given object/posechannel has 
1184  *      - if 'curves' is provided, a list of links to these curves are also returned
1185  */
1186 short action_get_item_transforms(bAction *act, Object *ob, bPoseChannel *pchan, ListBase *curves)
1187 {
1188         PointerRNA ptr;
1189         FCurve *fcu;
1190         char *basePath = NULL;
1191         short flags = 0;
1192         
1193         /* build PointerRNA from provided data to obtain the paths to use */
1194         if (pchan)
1195                 RNA_pointer_create((ID *)ob, &RNA_PoseBone, pchan, &ptr);
1196         else if (ob)
1197                 RNA_id_pointer_create((ID *)ob, &ptr);
1198         else
1199                 return 0;
1200                 
1201         /* get the basic path to the properties of interest */
1202         basePath = RNA_path_from_ID_to_struct(&ptr);
1203         if (basePath == NULL)
1204                 return 0;
1205                 
1206         /* search F-Curves for the given properties 
1207          *      - we cannot use the groups, since they may not be grouped in that way...
1208          */
1209         for (fcu = act->curves.first; fcu; fcu = fcu->next) {
1210                 const char *bPtr = NULL, *pPtr = NULL;
1211                 
1212                 /* if enough flags have been found, we can stop checking unless we're also getting the curves */
1213                 if ((flags == ACT_TRANS_ALL) && (curves == NULL))
1214                         break;
1215                         
1216                 /* just in case... */
1217                 if (fcu->rna_path == NULL)
1218                         continue;
1219                 
1220                 /* step 1: check for matching base path */
1221                 bPtr = strstr(fcu->rna_path, basePath);
1222                 
1223                 if (bPtr) {
1224                         /* we must add len(basePath) bytes to the match so that we are at the end of the 
1225                          * base path so that we don't get false positives with these strings in the names
1226                          */
1227                         bPtr += strlen(basePath);
1228                         
1229                         /* step 2: check for some property with transforms 
1230                          *      - to speed things up, only check for the ones not yet found 
1231                          *    unless we're getting the curves too
1232                          *      - if we're getting the curves, the BLI_genericNodeN() creates a LinkData
1233                          *        node wrapping the F-Curve, which then gets added to the list
1234                          *      - once a match has been found, the curve cannot possibly be any other one
1235                          */
1236                         if ((curves) || (flags & ACT_TRANS_LOC) == 0) {
1237                                 pPtr = strstr(bPtr, "location");
1238                                 if (pPtr) {
1239                                         flags |= ACT_TRANS_LOC;
1240                                         
1241                                         if (curves) 
1242                                                 BLI_addtail(curves, BLI_genericNodeN(fcu));
1243                                         continue;
1244                                 }
1245                         }
1246                         
1247                         if ((curves) || (flags & ACT_TRANS_SCALE) == 0) {
1248                                 pPtr = strstr(bPtr, "scale");
1249                                 if (pPtr) {
1250                                         flags |= ACT_TRANS_SCALE;
1251                                         
1252                                         if (curves) 
1253                                                 BLI_addtail(curves, BLI_genericNodeN(fcu));
1254                                         continue;
1255                                 }
1256                         }
1257                         
1258                         if ((curves) || (flags & ACT_TRANS_ROT) == 0) {
1259                                 pPtr = strstr(bPtr, "rotation");
1260                                 if (pPtr) {
1261                                         flags |= ACT_TRANS_ROT;
1262                                         
1263                                         if (curves) 
1264                                                 BLI_addtail(curves, BLI_genericNodeN(fcu));
1265                                         continue;
1266                                 }
1267                         }
1268                         
1269                         if ((curves) || (flags & ACT_TRANS_PROP) == 0) {
1270                                 /* custom properties only */
1271                                 pPtr = strstr(bPtr, "[\""); /* extra '"' comment here to keep my texteditor functionlist working :) */
1272                                 if (pPtr) {
1273                                         flags |= ACT_TRANS_PROP;
1274                                         
1275                                         if (curves)
1276                                                 BLI_addtail(curves, BLI_genericNodeN(fcu));
1277                                         continue;
1278                                 }
1279                         }
1280                 }
1281         }
1282         
1283         /* free basePath */
1284         MEM_freeN(basePath);
1285         
1286         /* return flags found */
1287         return flags;
1288 }
1289
1290 /* ************** Pose Management Tools ****************** */
1291
1292 /* Copy the data from the action-pose (src) into the pose */
1293 /* both args are assumed to be valid */
1294 /* exported to game engine */
1295 /* Note! this assumes both poses are aligned, this isn't always true when dealing with user poses */
1296 void extract_pose_from_pose(bPose *pose, const bPose *src)
1297 {
1298         const bPoseChannel *schan;
1299         bPoseChannel *pchan = pose->chanbase.first;
1300
1301         if (pose == src) {
1302                 printf("extract_pose_from_pose source and target are the same\n");
1303                 return;
1304         }
1305
1306         for (schan = src->chanbase.first; (schan && pchan); schan = schan->next, pchan = pchan->next) {
1307                 copy_pose_channel_data(pchan, schan);
1308         }
1309 }
1310
1311 /* for do_all_pose_actions, clears the pose. Now also exported for proxy and tools */
1312 void BKE_pose_rest(bPose *pose)
1313 {
1314         bPoseChannel *pchan;
1315         
1316         if (!pose)
1317                 return;
1318         
1319         memset(pose->stride_offset, 0, sizeof(pose->stride_offset));
1320         memset(pose->cyclic_offset, 0, sizeof(pose->cyclic_offset));
1321         
1322         for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
1323                 zero_v3(pchan->loc);
1324                 zero_v3(pchan->eul);
1325                 unit_qt(pchan->quat);
1326                 unit_axis_angle(pchan->rotAxis, &pchan->rotAngle);
1327                 pchan->size[0] = pchan->size[1] = pchan->size[2] = 1.0f;
1328
1329                 pchan->flag &= ~(POSE_LOC | POSE_ROT | POSE_SIZE);
1330         }
1331 }
1332
1333 /* both poses should be in sync */
1334 bool BKE_pose_copy_result(bPose *to, bPose *from)
1335 {
1336         bPoseChannel *pchanto, *pchanfrom;
1337         
1338         if (to == NULL || from == NULL) {
1339                 printf("Pose copy error, pose to:%p from:%p\n", (void *)to, (void *)from); /* debug temp */
1340                 return false;
1341         }
1342
1343         if (to == from) {
1344                 printf("BKE_pose_copy_result source and target are the same\n");
1345                 return false;
1346         }
1347
1348
1349         for (pchanfrom = from->chanbase.first; pchanfrom; pchanfrom = pchanfrom->next) {
1350                 pchanto = BKE_pose_channel_find_name(to, pchanfrom->name);
1351                 if (pchanto) {
1352                         copy_m4_m4(pchanto->pose_mat, pchanfrom->pose_mat);
1353                         copy_m4_m4(pchanto->chan_mat, pchanfrom->chan_mat);
1354                         
1355                         /* used for local constraints */
1356                         copy_v3_v3(pchanto->loc, pchanfrom->loc);
1357                         copy_qt_qt(pchanto->quat, pchanfrom->quat);
1358                         copy_v3_v3(pchanto->eul, pchanfrom->eul);
1359                         copy_v3_v3(pchanto->size, pchanfrom->size);
1360                         
1361                         copy_v3_v3(pchanto->pose_head, pchanfrom->pose_head);
1362                         copy_v3_v3(pchanto->pose_tail, pchanfrom->pose_tail);
1363                         
1364                         pchanto->rotmode = pchanfrom->rotmode;
1365                         pchanto->flag = pchanfrom->flag;
1366                         pchanto->protectflag = pchanfrom->protectflag;
1367                 }
1368         }
1369         return true;
1370 }
1371
1372 /* Tag pose for recalc. Also tag all related data to be recalc. */
1373 void BKE_pose_tag_recalc(Main *bmain, bPose *pose)
1374 {
1375         pose->flag |= POSE_RECALC;
1376         /* Depsgraph components depends on actual pose state,
1377          * if pose was changed depsgraph is to be updated as well.
1378          */
1379         DAG_relations_tag_update(bmain);
1380 }
1381
1382 /* For the calculation of the effects of an Action at the given frame on an object 
1383  * This is currently only used for the Action Constraint 
1384  */
1385 void what_does_obaction(Object *ob, Object *workob, bPose *pose, bAction *act, char groupname[], float cframe)
1386 {
1387         bActionGroup *agrp = BKE_action_group_find_name(act, groupname);
1388         
1389         /* clear workob */
1390         BKE_object_workob_clear(workob);
1391         
1392         /* init workob */
1393         copy_m4_m4(workob->obmat, ob->obmat);
1394         copy_m4_m4(workob->parentinv, ob->parentinv);
1395         copy_m4_m4(workob->constinv, ob->constinv);
1396         workob->parent = ob->parent;
1397         
1398         workob->rotmode = ob->rotmode;
1399         
1400         workob->trackflag = ob->trackflag;
1401         workob->upflag = ob->upflag;
1402         
1403         workob->partype = ob->partype;
1404         workob->par1 = ob->par1;
1405         workob->par2 = ob->par2;
1406         workob->par3 = ob->par3;
1407
1408         workob->constraints.first = ob->constraints.first;
1409         workob->constraints.last = ob->constraints.last;
1410         
1411         workob->pose = pose; /* need to set pose too, since this is used for both types of Action Constraint */
1412
1413         BLI_strncpy(workob->parsubstr, ob->parsubstr, sizeof(workob->parsubstr));
1414         BLI_strncpy(workob->id.name, "OB<ConstrWorkOb>", sizeof(workob->id.name)); /* we don't use real object name, otherwise RNA screws with the real thing */
1415         
1416         /* if we're given a group to use, it's likely to be more efficient (though a bit more dangerous) */
1417         if (agrp) {
1418                 /* specifically evaluate this group only */
1419                 PointerRNA id_ptr;
1420                 
1421                 /* get RNA-pointer for the workob's ID */
1422                 RNA_id_pointer_create(&workob->id, &id_ptr);
1423                 
1424                 /* execute action for this group only */
1425                 animsys_evaluate_action_group(&id_ptr, act, agrp, NULL, cframe);
1426         }
1427         else {
1428                 AnimData adt = {NULL};
1429                 
1430                 /* init animdata, and attach to workob */
1431                 workob->adt = &adt;
1432                 
1433                 adt.recalc = ADT_RECALC_ANIM;
1434                 adt.action = act;
1435                 
1436                 /* execute effects of Action on to workob (or it's PoseChannels) */
1437                 BKE_animsys_evaluate_animdata(NULL, &workob->id, &adt, cframe, ADT_RECALC_ANIM);
1438         }
1439 }
1440