Armature drawing: custom shape scale options
[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
489         chan->custom_scale = 1.0f;
490
491         /* init vars to prevent math errors */
492         unit_qt(chan->quat);
493         unit_axis_angle(chan->rotAxis, &chan->rotAngle);
494         chan->size[0] = chan->size[1] = chan->size[2] = 1.0f;
495         
496         chan->limitmin[0] = chan->limitmin[1] = chan->limitmin[2] = -180.0f;
497         chan->limitmax[0] = chan->limitmax[1] = chan->limitmax[2] = 180.0f;
498         chan->stiffness[0] = chan->stiffness[1] = chan->stiffness[2] = 0.0f;
499         chan->ikrotweight = chan->iklinweight = 0.0f;
500         unit_m4(chan->constinv);
501         
502         chan->protectflag = OB_LOCK_ROT4D;  /* lock by components by default */
503         
504         BLI_addtail(&pose->chanbase, chan);
505         BKE_pose_channels_hash_free(pose);
506         
507         return chan;
508 }
509
510 #ifndef NDEBUG
511 bool BKE_pose_channels_is_valid(const bPose *pose)
512 {
513         if (pose->chanhash) {
514                 bPoseChannel *pchan;
515                 for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
516                         if (BLI_ghash_lookup(pose->chanhash, pchan->name) != pchan) {
517                                 return false;
518                         }
519                 }
520         }
521
522         return true;
523 }
524
525 #endif
526
527 /**
528  * Find the active posechannel for an object (we can't just use pose, as layer info is in armature)
529  *
530  * \note: Object, not bPose is used here, as we need layer info from Armature)
531  */
532 bPoseChannel *BKE_pose_channel_active(Object *ob)
533 {
534         bArmature *arm = (ob) ? ob->data : NULL;
535         bPoseChannel *pchan;
536
537         if (ELEM(NULL, ob, ob->pose, arm)) {
538                 return NULL;
539         }
540
541         /* find active */
542         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
543                 if ((pchan->bone) && (pchan->bone == arm->act_bone) && (pchan->bone->layer & arm->layer))
544                         return pchan;
545         }
546         
547         return NULL;
548 }
549
550 /**
551  * \see #ED_armature_bone_get_mirrored (edit-mode, matching function)
552  */
553 bPoseChannel *BKE_pose_channel_get_mirrored(const bPose *pose, const char *name)
554 {
555         char name_flip[MAXBONENAME];
556
557         BKE_deform_flip_side_name(name_flip, name, false);
558
559         if (!STREQ(name_flip, name)) {
560                 return BKE_pose_channel_find_name(pose, name_flip);
561         }
562
563         return NULL;
564 }
565
566 const char *BKE_pose_ikparam_get_name(bPose *pose)
567 {
568         if (pose) {
569                 switch (pose->iksolver) {
570                         case IKSOLVER_STANDARD:
571                                 return NULL;
572                         case IKSOLVER_ITASC:
573                                 return "bItasc";
574                 }
575         }
576         return NULL;
577 }
578
579 /**
580  * Allocate a new pose on the heap, and copy the src pose and it's channels
581  * into the new pose. *dst is set to the newly allocated structure, and assumed to be NULL.
582  *
583  * \param dst  Should be freed already, makes entire duplicate.
584  */
585 void BKE_pose_copy_data(bPose **dst, bPose *src, const bool copy_constraints)
586 {
587         bPose *outPose;
588         bPoseChannel *pchan;
589         ListBase listb;
590
591         if (!src) {
592                 *dst = NULL;
593                 return;
594         }
595         
596         outPose = MEM_callocN(sizeof(bPose), "pose");
597         
598         BLI_duplicatelist(&outPose->chanbase, &src->chanbase);
599
600         outPose->iksolver = src->iksolver;
601         outPose->ikdata = NULL;
602         outPose->ikparam = MEM_dupallocN(src->ikparam);
603         outPose->avs = src->avs;
604         
605         for (pchan = outPose->chanbase.first; pchan; pchan = pchan->next) {
606
607                 if (pchan->custom) {
608                         id_us_plus(&pchan->custom->id);
609                 }
610
611                 /* warning, O(n2) here, but it's a rarely used feature. */
612                 if (pchan->custom_tx) {
613                         pchan->custom_tx = BKE_pose_channel_find_name(outPose, pchan->custom_tx->name);
614                 }
615
616                 if (copy_constraints) {
617                         BKE_constraints_copy(&listb, &pchan->constraints, true);  // BKE_constraints_copy NULLs listb
618                         pchan->constraints = listb;
619                         pchan->mpath = NULL; /* motion paths should not get copied yet... */
620                 }
621                 
622                 if (pchan->prop) {
623                         pchan->prop = IDP_CopyProperty(pchan->prop);
624                 }
625         }
626
627         /* for now, duplicate Bone Groups too when doing this */
628         if (copy_constraints) {
629                 BLI_duplicatelist(&outPose->agroups, &src->agroups);
630         }
631         
632         *dst = outPose;
633 }
634
635 void BKE_pose_itasc_init(bItasc *itasc)
636 {
637         if (itasc) {
638                 itasc->iksolver = IKSOLVER_ITASC;
639                 itasc->minstep = 0.01f;
640                 itasc->maxstep = 0.06f;
641                 itasc->numiter = 100;
642                 itasc->numstep = 4;
643                 itasc->precision = 0.005f;
644                 itasc->flag = ITASC_AUTO_STEP | ITASC_INITIAL_REITERATION;
645                 itasc->feedback = 20.0f;
646                 itasc->maxvel = 50.0f;
647                 itasc->solver = ITASC_SOLVER_SDLS;
648                 itasc->dampmax = 0.5;
649                 itasc->dampeps = 0.15;
650         }
651 }
652 void BKE_pose_ikparam_init(bPose *pose)
653 {
654         bItasc *itasc;
655         switch (pose->iksolver) {
656                 case IKSOLVER_ITASC:
657                         itasc = MEM_callocN(sizeof(bItasc), "itasc");
658                         BKE_pose_itasc_init(itasc);
659                         pose->ikparam = itasc;
660                         break;
661                 case IKSOLVER_STANDARD:
662                 default:
663                         pose->ikparam = NULL;
664                         break;
665         }
666 }
667
668
669 /* only for real IK, not for auto-IK */
670 static bool pose_channel_in_IK_chain(Object *ob, bPoseChannel *pchan, int level)
671 {
672         bConstraint *con;
673         Bone *bone;
674         
675         /* No need to check if constraint is active (has influence),
676          * since all constraints with CONSTRAINT_IK_AUTO are active */
677         for (con = pchan->constraints.first; con; con = con->next) {
678                 if (con->type == CONSTRAINT_TYPE_KINEMATIC) {
679                         bKinematicConstraint *data = con->data;
680                         if ((data->rootbone == 0) || (data->rootbone > level)) {
681                                 if ((data->flag & CONSTRAINT_IK_AUTO) == 0)
682                                         return true;
683                         }
684                 }
685         }
686         for (bone = pchan->bone->childbase.first; bone; bone = bone->next) {
687                 pchan = BKE_pose_channel_find_name(ob->pose, bone->name);
688                 if (pchan && pose_channel_in_IK_chain(ob, pchan, level + 1))
689                         return true;
690         }
691         return false;
692 }
693
694 bool BKE_pose_channel_in_IK_chain(Object *ob, bPoseChannel *pchan)
695 {
696         return pose_channel_in_IK_chain(ob, pchan, 0);
697 }
698
699 /**
700  * Removes the hash for quick lookup of channels, must
701  * be done when adding/removing channels.
702  */
703 void BKE_pose_channels_hash_make(bPose *pose) 
704 {
705         if (!pose->chanhash) {
706                 bPoseChannel *pchan;
707                 
708                 pose->chanhash = BLI_ghash_str_new("make_pose_chan gh");
709                 for (pchan = pose->chanbase.first; pchan; pchan = pchan->next)
710                         BLI_ghash_insert(pose->chanhash, pchan->name, pchan);
711         }
712 }
713
714 void BKE_pose_channels_hash_free(bPose *pose) 
715 {
716         if (pose->chanhash) {
717                 BLI_ghash_free(pose->chanhash, NULL, NULL);
718                 pose->chanhash = NULL;
719         }
720 }
721
722 /**
723  * Selectively remove pose channels.
724  */
725 void BKE_pose_channels_remove(
726         Object *ob,
727         bool (*filter_fn)(const char *bone_name, void *user_data), void *user_data)
728 {
729         /*  First erase any associated pose channel */
730         if (ob->pose) {
731                 bPoseChannel *pchan, *pchan_next;
732                 bConstraint *con;
733
734                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan_next) {
735                         pchan_next = pchan->next;
736
737                         if (filter_fn(pchan->name, user_data)) {
738                                 BKE_pose_channel_free(pchan);
739                                 if (ob->pose->chanhash) {
740                                         BLI_ghash_remove(ob->pose->chanhash, pchan->name, NULL, NULL);
741                                 }
742                                 BLI_freelinkN(&ob->pose->chanbase, pchan);
743                         }
744                         else {
745                                 for (con = pchan->constraints.first; con; con = con->next) {
746                                         const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con);
747                                         ListBase targets = {NULL, NULL};
748                                         bConstraintTarget *ct;
749
750                                         if (cti && cti->get_constraint_targets) {
751                                                 cti->get_constraint_targets(con, &targets);
752
753                                                 for (ct = targets.first; ct; ct = ct->next) {
754                                                         if (ct->tar == ob) {
755                                                                 if (ct->subtarget[0]) {
756                                                                         if (filter_fn(ct->subtarget, user_data)) {
757                                                                                 con->flag |= CONSTRAINT_DISABLE;
758                                                                                 ct->subtarget[0] = 0;
759                                                                         }
760                                                                 }
761                                                         }
762                                                 }
763
764                                                 if (cti->flush_constraint_targets)
765                                                         cti->flush_constraint_targets(con, &targets, 0);
766                                         }
767                                 }
768                         }
769                 }
770         }
771 }
772
773 /**
774  * Deallocates a pose channel.
775  * Does not free the pose channel itself.
776  */
777 void BKE_pose_channel_free_ex(bPoseChannel *pchan, bool do_id_user)
778 {
779         if (pchan->custom) {
780                 if (do_id_user) {
781                         id_us_min(&pchan->custom->id);
782                 }
783                 pchan->custom = NULL;
784         }
785
786         if (pchan->mpath) {
787                 animviz_free_motionpath(pchan->mpath);
788                 pchan->mpath = NULL;
789         }
790
791         BKE_constraints_free_ex(&pchan->constraints, do_id_user);
792         
793         if (pchan->prop) {
794                 IDP_FreeProperty(pchan->prop);
795                 MEM_freeN(pchan->prop);
796         }
797 }
798
799 void BKE_pose_channel_free(bPoseChannel *pchan)
800 {
801         BKE_pose_channel_free_ex(pchan, true);
802 }
803
804 /**
805  * Removes and deallocates all channels from a pose.
806  * Does not free the pose itself.
807  */
808 void BKE_pose_channels_free_ex(bPose *pose, bool do_id_user)
809 {
810         bPoseChannel *pchan;
811         
812         if (pose->chanbase.first) {
813                 for (pchan = pose->chanbase.first; pchan; pchan = pchan->next)
814                         BKE_pose_channel_free_ex(pchan, do_id_user);
815                 
816                 BLI_freelistN(&pose->chanbase);
817         }
818
819         BKE_pose_channels_hash_free(pose);
820 }
821
822 void BKE_pose_channels_free(bPose *pose)
823 {
824         BKE_pose_channels_free_ex(pose, true);
825 }
826
827 /**
828  * Removes and deallocates all data from a pose, and also frees the pose.
829  */
830 void BKE_pose_free_ex(bPose *pose, bool do_id_user)
831 {
832         if (pose) {
833                 /* free pose-channels */
834                 BKE_pose_channels_free_ex(pose, do_id_user);
835                 
836                 /* free pose-groups */
837                 if (pose->agroups.first)
838                         BLI_freelistN(&pose->agroups);
839                 
840                 /* free IK solver state */
841                 BIK_clear_data(pose);
842                 
843                 /* free IK solver param */
844                 if (pose->ikparam)
845                         MEM_freeN(pose->ikparam);
846                 
847                 /* free pose */
848                 MEM_freeN(pose);
849         }
850 }
851
852 void BKE_pose_free(bPose *pose)
853 {
854         BKE_pose_free_ex(pose, true);
855 }
856
857 static void copy_pose_channel_data(bPoseChannel *pchan, const bPoseChannel *chan)
858 {
859         bConstraint *pcon, *con;
860         
861         copy_v3_v3(pchan->loc, chan->loc);
862         copy_v3_v3(pchan->size, chan->size);
863         copy_v3_v3(pchan->eul, chan->eul);
864         copy_v3_v3(pchan->rotAxis, chan->rotAxis);
865         pchan->rotAngle = chan->rotAngle;
866         copy_qt_qt(pchan->quat, chan->quat);
867         pchan->rotmode = chan->rotmode;
868         copy_m4_m4(pchan->chan_mat, (float(*)[4])chan->chan_mat);
869         copy_m4_m4(pchan->pose_mat, (float(*)[4])chan->pose_mat);
870         pchan->flag = chan->flag;
871         
872         con = chan->constraints.first;
873         for (pcon = pchan->constraints.first; pcon && con; pcon = pcon->next, con = con->next) {
874                 pcon->enforce = con->enforce;
875                 pcon->headtail = con->headtail;
876         }
877 }
878
879 /**
880  * Copy the internal members of each pose channel including constraints
881  * and ID-Props, used when duplicating bones in editmode.
882  * (unlike copy_pose_channel_data which only).
883  *
884  * \note use when copying bones in editmode (on returned value from #BKE_pose_channel_verify)
885  */
886 void BKE_pose_channel_copy_data(bPoseChannel *pchan, const bPoseChannel *pchan_from)
887 {
888         /* copy transform locks */
889         pchan->protectflag = pchan_from->protectflag;
890
891         /* copy rotation mode */
892         pchan->rotmode = pchan_from->rotmode;
893
894         /* copy bone group */
895         pchan->agrp_index = pchan_from->agrp_index;
896
897         /* ik (dof) settings */
898         pchan->ikflag = pchan_from->ikflag;
899         copy_v3_v3(pchan->limitmin, pchan_from->limitmin);
900         copy_v3_v3(pchan->limitmax, pchan_from->limitmax);
901         copy_v3_v3(pchan->stiffness, pchan_from->stiffness);
902         pchan->ikstretch = pchan_from->ikstretch;
903         pchan->ikrotweight = pchan_from->ikrotweight;
904         pchan->iklinweight = pchan_from->iklinweight;
905
906         /* constraints */
907         BKE_constraints_copy(&pchan->constraints, &pchan_from->constraints, true);
908
909         /* id-properties */
910         if (pchan->prop) {
911                 /* unlikely but possible it exists */
912                 IDP_FreeProperty(pchan->prop);
913                 MEM_freeN(pchan->prop);
914                 pchan->prop = NULL;
915         }
916         if (pchan_from->prop) {
917                 pchan->prop = IDP_CopyProperty(pchan_from->prop);
918         }
919
920         /* custom shape */
921         pchan->custom = pchan_from->custom;
922         if (pchan->custom) {
923                 id_us_plus(&pchan->custom->id);
924         }
925 }
926
927
928 /* checks for IK constraint, Spline IK, and also for Follow-Path constraint.
929  * can do more constraints flags later 
930  */
931 /* pose should be entirely OK */
932 void BKE_pose_update_constraint_flags(bPose *pose)
933 {
934         bPoseChannel *pchan, *parchan;
935         bConstraint *con;
936         
937         /* clear */
938         for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
939                 pchan->constflag = 0;
940         }
941         pose->flag &= ~POSE_CONSTRAINTS_TIMEDEPEND;
942         
943         /* detect */
944         for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
945                 for (con = pchan->constraints.first; con; con = con->next) {
946                         if (con->type == CONSTRAINT_TYPE_KINEMATIC) {
947                                 bKinematicConstraint *data = (bKinematicConstraint *)con->data;
948                                 
949                                 pchan->constflag |= PCHAN_HAS_IK;
950                                 
951                                 if (data->tar == NULL || (data->tar->type == OB_ARMATURE && data->subtarget[0] == 0))
952                                         pchan->constflag |= PCHAN_HAS_TARGET;
953                                 
954                                 /* negative rootbone = recalc rootbone index. used in do_versions */
955                                 if (data->rootbone < 0) {
956                                         data->rootbone = 0;
957                                         
958                                         if (data->flag & CONSTRAINT_IK_TIP) parchan = pchan;
959                                         else parchan = pchan->parent;
960                                         
961                                         while (parchan) {
962                                                 data->rootbone++;
963                                                 if ((parchan->bone->flag & BONE_CONNECTED) == 0)
964                                                         break;
965                                                 parchan = parchan->parent;
966                                         }
967                                 }
968                         }
969                         else if (con->type == CONSTRAINT_TYPE_FOLLOWPATH) {
970                                 bFollowPathConstraint *data = (bFollowPathConstraint *)con->data;
971                                 
972                                 /* for drawing constraint colors when color set allows this */
973                                 pchan->constflag |= PCHAN_HAS_CONST;
974                                 
975                                 /* if we have a valid target, make sure that this will get updated on frame-change
976                                  * (needed for when there is no anim-data for this pose)
977                                  */
978                                 if ((data->tar) && (data->tar->type == OB_CURVE))
979                                         pose->flag |= POSE_CONSTRAINTS_TIMEDEPEND;
980                         }
981                         else if (con->type == CONSTRAINT_TYPE_SPLINEIK)
982                                 pchan->constflag |= PCHAN_HAS_SPLINEIK;
983                         else 
984                                 pchan->constflag |= PCHAN_HAS_CONST;
985                 }
986         }
987         pose->flag &= ~POSE_CONSTRAINTS_NEED_UPDATE_FLAGS;
988 }
989
990 void BKE_pose_tag_update_constraint_flags(bPose *pose)
991 {
992         pose->flag |= POSE_CONSTRAINTS_NEED_UPDATE_FLAGS;
993 }
994
995 /* Clears all BONE_UNKEYED flags for every pose channel in every pose 
996  * This should only be called on frame changing, when it is acceptable to
997  * do this. Otherwise, these flags should not get cleared as poses may get lost.
998  */
999 void framechange_poses_clear_unkeyed(void)
1000 {
1001         Object *ob;
1002         bPose *pose;
1003         bPoseChannel *pchan;
1004         
1005         /* This needs to be done for each object that has a pose */
1006         /* TODO: proxies may/may not be correctly handled here... (this needs checking) */
1007         for (ob = G.main->object.first; ob; ob = ob->id.next) {
1008                 /* we only need to do this on objects with a pose */
1009                 if ((pose = ob->pose)) {
1010                         for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
1011                                 if (pchan->bone) 
1012                                         pchan->bone->flag &= ~BONE_UNKEYED;
1013                         }
1014                 }
1015         }
1016 }
1017
1018 /* ************************** Bone Groups ************************** */
1019
1020 /* Adds a new bone-group (name may be NULL) */
1021 bActionGroup *BKE_pose_add_group(bPose *pose, const char *name)
1022 {
1023         bActionGroup *grp;
1024         
1025         if (!name) {
1026                 name = DATA_("Group");
1027         }
1028         
1029         grp = MEM_callocN(sizeof(bActionGroup), "PoseGroup");
1030         BLI_strncpy(grp->name, name, sizeof(grp->name));
1031         BLI_addtail(&pose->agroups, grp);
1032         BLI_uniquename(&pose->agroups, grp, name, '.', offsetof(bActionGroup, name), sizeof(grp->name));
1033         
1034         pose->active_group = BLI_listbase_count(&pose->agroups);
1035         
1036         return grp;
1037 }
1038
1039 /* Remove the given bone-group (expects 'virtual' index (+1 one, used by active_group etc.))
1040  * index might be invalid ( < 1), in which case it will be find from grp. */
1041 void BKE_pose_remove_group(bPose *pose, bActionGroup *grp, const int index)
1042 {
1043         bPoseChannel *pchan;
1044         int idx = index;
1045         
1046         if (idx < 1) {
1047                 idx = BLI_findindex(&pose->agroups, grp) + 1;
1048         }
1049         
1050         BLI_assert(idx > 0);
1051         
1052         /* adjust group references (the trouble of using indices!):
1053          *  - firstly, make sure nothing references it
1054          *  - also, make sure that those after this item get corrected
1055          */
1056         for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
1057                 if (pchan->agrp_index == idx)
1058                         pchan->agrp_index = 0;
1059                 else if (pchan->agrp_index > idx)
1060                         pchan->agrp_index--;
1061         }
1062
1063         /* now, remove it from the pose */
1064         BLI_freelinkN(&pose->agroups, grp);
1065         if (pose->active_group >= idx) {
1066                 const bool has_groups = !BLI_listbase_is_empty(&pose->agroups);
1067                 pose->active_group--;
1068                 if (pose->active_group == 0 && has_groups) {
1069                         pose->active_group = 1;
1070                 }
1071                 else if (pose->active_group < 0 || !has_groups) {
1072                         pose->active_group = 0;
1073                 }
1074         }
1075 }
1076
1077 /* Remove the indexed bone-group (expects 'virtual' index (+1 one, used by active_group etc.)) */
1078 void BKE_pose_remove_group_index(bPose *pose, const int index)
1079 {
1080         bActionGroup *grp = NULL;
1081         
1082         /* get group to remove */
1083         grp = BLI_findlink(&pose->agroups, index - 1);
1084         if (grp) {
1085                 BKE_pose_remove_group(pose, grp, index);
1086         }
1087 }
1088
1089 /* ************** F-Curve Utilities for Actions ****************** */
1090
1091 /* Check if the given action has any keyframes */
1092 bool action_has_motion(const bAction *act)
1093 {
1094         FCurve *fcu;
1095         
1096         /* return on the first F-Curve that has some keyframes/samples defined */
1097         if (act) {
1098                 for (fcu = act->curves.first; fcu; fcu = fcu->next) {
1099                         if (fcu->totvert)
1100                                 return true;
1101                 }
1102         }
1103         
1104         /* nothing found */
1105         return false;
1106 }
1107
1108 /* Calculate the extents of given action */
1109 void calc_action_range(const bAction *act, float *start, float *end, short incl_modifiers)
1110 {
1111         FCurve *fcu;
1112         float min = 999999999.0f, max = -999999999.0f;
1113         short foundvert = 0, foundmod = 0;
1114
1115         if (act) {
1116                 for (fcu = act->curves.first; fcu; fcu = fcu->next) {
1117                         /* if curve has keyframes, consider them first */
1118                         if (fcu->totvert) {
1119                                 float nmin, nmax;
1120                                 
1121                                 /* get extents for this curve */
1122                                 /* TODO: allow enabling/disabling this? */
1123                                 calc_fcurve_range(fcu, &nmin, &nmax, false, true);
1124                                 
1125                                 /* compare to the running tally */
1126                                 min = min_ff(min, nmin);
1127                                 max = max_ff(max, nmax);
1128                                 
1129                                 foundvert = 1;
1130                         }
1131                         
1132                         /* if incl_modifiers is enabled, need to consider modifiers too
1133                          *      - only really care about the last modifier
1134                          */
1135                         if ((incl_modifiers) && (fcu->modifiers.last)) {
1136                                 FModifier *fcm = fcu->modifiers.last;
1137                                 
1138                                 /* only use the maximum sensible limits of the modifiers if they are more extreme */
1139                                 switch (fcm->type) {
1140                                         case FMODIFIER_TYPE_LIMITS: /* Limits F-Modifier */
1141                                         {
1142                                                 FMod_Limits *fmd = (FMod_Limits *)fcm->data;
1143                                                 
1144                                                 if (fmd->flag & FCM_LIMIT_XMIN) {
1145                                                         min = min_ff(min, fmd->rect.xmin);
1146                                                 }
1147                                                 if (fmd->flag & FCM_LIMIT_XMAX) {
1148                                                         max = max_ff(max, fmd->rect.xmax);
1149                                                 }
1150                                                 break;
1151                                         }
1152                                         case FMODIFIER_TYPE_CYCLES: /* Cycles F-Modifier */
1153                                         {
1154                                                 FMod_Cycles *fmd = (FMod_Cycles *)fcm->data;
1155                                                 
1156                                                 if (fmd->before_mode != FCM_EXTRAPOLATE_NONE)
1157                                                         min = MINAFRAMEF;
1158                                                 if (fmd->after_mode != FCM_EXTRAPOLATE_NONE)
1159                                                         max = MAXFRAMEF;
1160                                                 break;
1161                                         }
1162                                         /* TODO: function modifier may need some special limits */
1163                                                 
1164                                         default: /* all other standard modifiers are on the infinite range... */
1165                                                 min = MINAFRAMEF;
1166                                                 max = MAXFRAMEF;
1167                                                 break;
1168                                 }
1169                                 
1170                                 foundmod = 1;
1171                         }
1172                 }
1173         }
1174         
1175         if (foundvert || foundmod) {
1176                 if (min == max) max += 1.0f;
1177                 *start = min;
1178                 *end = max;
1179         }
1180         else {
1181                 *start = 0.0f;
1182                 *end = 1.0f;
1183         }
1184 }
1185
1186 /* Return flags indicating which transforms the given object/posechannel has 
1187  *      - if 'curves' is provided, a list of links to these curves are also returned
1188  */
1189 short action_get_item_transforms(bAction *act, Object *ob, bPoseChannel *pchan, ListBase *curves)
1190 {
1191         PointerRNA ptr;
1192         FCurve *fcu;
1193         char *basePath = NULL;
1194         short flags = 0;
1195         
1196         /* build PointerRNA from provided data to obtain the paths to use */
1197         if (pchan)
1198                 RNA_pointer_create((ID *)ob, &RNA_PoseBone, pchan, &ptr);
1199         else if (ob)
1200                 RNA_id_pointer_create((ID *)ob, &ptr);
1201         else
1202                 return 0;
1203                 
1204         /* get the basic path to the properties of interest */
1205         basePath = RNA_path_from_ID_to_struct(&ptr);
1206         if (basePath == NULL)
1207                 return 0;
1208                 
1209         /* search F-Curves for the given properties 
1210          *      - we cannot use the groups, since they may not be grouped in that way...
1211          */
1212         for (fcu = act->curves.first; fcu; fcu = fcu->next) {
1213                 const char *bPtr = NULL, *pPtr = NULL;
1214                 
1215                 /* if enough flags have been found, we can stop checking unless we're also getting the curves */
1216                 if ((flags == ACT_TRANS_ALL) && (curves == NULL))
1217                         break;
1218                         
1219                 /* just in case... */
1220                 if (fcu->rna_path == NULL)
1221                         continue;
1222                 
1223                 /* step 1: check for matching base path */
1224                 bPtr = strstr(fcu->rna_path, basePath);
1225                 
1226                 if (bPtr) {
1227                         /* we must add len(basePath) bytes to the match so that we are at the end of the 
1228                          * base path so that we don't get false positives with these strings in the names
1229                          */
1230                         bPtr += strlen(basePath);
1231                         
1232                         /* step 2: check for some property with transforms 
1233                          *      - to speed things up, only check for the ones not yet found 
1234                          *    unless we're getting the curves too
1235                          *      - if we're getting the curves, the BLI_genericNodeN() creates a LinkData
1236                          *        node wrapping the F-Curve, which then gets added to the list
1237                          *      - once a match has been found, the curve cannot possibly be any other one
1238                          */
1239                         if ((curves) || (flags & ACT_TRANS_LOC) == 0) {
1240                                 pPtr = strstr(bPtr, "location");
1241                                 if (pPtr) {
1242                                         flags |= ACT_TRANS_LOC;
1243                                         
1244                                         if (curves) 
1245                                                 BLI_addtail(curves, BLI_genericNodeN(fcu));
1246                                         continue;
1247                                 }
1248                         }
1249                         
1250                         if ((curves) || (flags & ACT_TRANS_SCALE) == 0) {
1251                                 pPtr = strstr(bPtr, "scale");
1252                                 if (pPtr) {
1253                                         flags |= ACT_TRANS_SCALE;
1254                                         
1255                                         if (curves) 
1256                                                 BLI_addtail(curves, BLI_genericNodeN(fcu));
1257                                         continue;
1258                                 }
1259                         }
1260                         
1261                         if ((curves) || (flags & ACT_TRANS_ROT) == 0) {
1262                                 pPtr = strstr(bPtr, "rotation");
1263                                 if (pPtr) {
1264                                         flags |= ACT_TRANS_ROT;
1265                                         
1266                                         if (curves) 
1267                                                 BLI_addtail(curves, BLI_genericNodeN(fcu));
1268                                         continue;
1269                                 }
1270                         }
1271                         
1272                         if ((curves) || (flags & ACT_TRANS_PROP) == 0) {
1273                                 /* custom properties only */
1274                                 pPtr = strstr(bPtr, "[\""); /* extra '"' comment here to keep my texteditor functionlist working :) */
1275                                 if (pPtr) {
1276                                         flags |= ACT_TRANS_PROP;
1277                                         
1278                                         if (curves)
1279                                                 BLI_addtail(curves, BLI_genericNodeN(fcu));
1280                                         continue;
1281                                 }
1282                         }
1283                 }
1284         }
1285         
1286         /* free basePath */
1287         MEM_freeN(basePath);
1288         
1289         /* return flags found */
1290         return flags;
1291 }
1292
1293 /* ************** Pose Management Tools ****************** */
1294
1295 /* Copy the data from the action-pose (src) into the pose */
1296 /* both args are assumed to be valid */
1297 /* exported to game engine */
1298 /* Note! this assumes both poses are aligned, this isn't always true when dealing with user poses */
1299 void extract_pose_from_pose(bPose *pose, const bPose *src)
1300 {
1301         const bPoseChannel *schan;
1302         bPoseChannel *pchan = pose->chanbase.first;
1303
1304         if (pose == src) {
1305                 printf("extract_pose_from_pose source and target are the same\n");
1306                 return;
1307         }
1308
1309         for (schan = src->chanbase.first; (schan && pchan); schan = schan->next, pchan = pchan->next) {
1310                 copy_pose_channel_data(pchan, schan);
1311         }
1312 }
1313
1314 /* for do_all_pose_actions, clears the pose. Now also exported for proxy and tools */
1315 void BKE_pose_rest(bPose *pose)
1316 {
1317         bPoseChannel *pchan;
1318         
1319         if (!pose)
1320                 return;
1321         
1322         memset(pose->stride_offset, 0, sizeof(pose->stride_offset));
1323         memset(pose->cyclic_offset, 0, sizeof(pose->cyclic_offset));
1324         
1325         for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
1326                 zero_v3(pchan->loc);
1327                 zero_v3(pchan->eul);
1328                 unit_qt(pchan->quat);
1329                 unit_axis_angle(pchan->rotAxis, &pchan->rotAngle);
1330                 pchan->size[0] = pchan->size[1] = pchan->size[2] = 1.0f;
1331
1332                 pchan->flag &= ~(POSE_LOC | POSE_ROT | POSE_SIZE);
1333         }
1334 }
1335
1336 /* both poses should be in sync */
1337 bool BKE_pose_copy_result(bPose *to, bPose *from)
1338 {
1339         bPoseChannel *pchanto, *pchanfrom;
1340         
1341         if (to == NULL || from == NULL) {
1342                 printf("Pose copy error, pose to:%p from:%p\n", (void *)to, (void *)from); /* debug temp */
1343                 return false;
1344         }
1345
1346         if (to == from) {
1347                 printf("BKE_pose_copy_result source and target are the same\n");
1348                 return false;
1349         }
1350
1351
1352         for (pchanfrom = from->chanbase.first; pchanfrom; pchanfrom = pchanfrom->next) {
1353                 pchanto = BKE_pose_channel_find_name(to, pchanfrom->name);
1354                 if (pchanto) {
1355                         copy_m4_m4(pchanto->pose_mat, pchanfrom->pose_mat);
1356                         copy_m4_m4(pchanto->chan_mat, pchanfrom->chan_mat);
1357                         
1358                         /* used for local constraints */
1359                         copy_v3_v3(pchanto->loc, pchanfrom->loc);
1360                         copy_qt_qt(pchanto->quat, pchanfrom->quat);
1361                         copy_v3_v3(pchanto->eul, pchanfrom->eul);
1362                         copy_v3_v3(pchanto->size, pchanfrom->size);
1363                         
1364                         copy_v3_v3(pchanto->pose_head, pchanfrom->pose_head);
1365                         copy_v3_v3(pchanto->pose_tail, pchanfrom->pose_tail);
1366                         
1367                         pchanto->rotmode = pchanfrom->rotmode;
1368                         pchanto->flag = pchanfrom->flag;
1369                         pchanto->protectflag = pchanfrom->protectflag;
1370                 }
1371         }
1372         return true;
1373 }
1374
1375 /* Tag pose for recalc. Also tag all related data to be recalc. */
1376 void BKE_pose_tag_recalc(Main *bmain, bPose *pose)
1377 {
1378         pose->flag |= POSE_RECALC;
1379         /* Depsgraph components depends on actual pose state,
1380          * if pose was changed depsgraph is to be updated as well.
1381          */
1382         DAG_relations_tag_update(bmain);
1383 }
1384
1385 /* For the calculation of the effects of an Action at the given frame on an object 
1386  * This is currently only used for the Action Constraint 
1387  */
1388 void what_does_obaction(Object *ob, Object *workob, bPose *pose, bAction *act, char groupname[], float cframe)
1389 {
1390         bActionGroup *agrp = BKE_action_group_find_name(act, groupname);
1391         
1392         /* clear workob */
1393         BKE_object_workob_clear(workob);
1394         
1395         /* init workob */
1396         copy_m4_m4(workob->obmat, ob->obmat);
1397         copy_m4_m4(workob->parentinv, ob->parentinv);
1398         copy_m4_m4(workob->constinv, ob->constinv);
1399         workob->parent = ob->parent;
1400         
1401         workob->rotmode = ob->rotmode;
1402         
1403         workob->trackflag = ob->trackflag;
1404         workob->upflag = ob->upflag;
1405         
1406         workob->partype = ob->partype;
1407         workob->par1 = ob->par1;
1408         workob->par2 = ob->par2;
1409         workob->par3 = ob->par3;
1410
1411         workob->constraints.first = ob->constraints.first;
1412         workob->constraints.last = ob->constraints.last;
1413         
1414         workob->pose = pose; /* need to set pose too, since this is used for both types of Action Constraint */
1415
1416         BLI_strncpy(workob->parsubstr, ob->parsubstr, sizeof(workob->parsubstr));
1417         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 */
1418         
1419         /* if we're given a group to use, it's likely to be more efficient (though a bit more dangerous) */
1420         if (agrp) {
1421                 /* specifically evaluate this group only */
1422                 PointerRNA id_ptr;
1423                 
1424                 /* get RNA-pointer for the workob's ID */
1425                 RNA_id_pointer_create(&workob->id, &id_ptr);
1426                 
1427                 /* execute action for this group only */
1428                 animsys_evaluate_action_group(&id_ptr, act, agrp, NULL, cframe);
1429         }
1430         else {
1431                 AnimData adt = {NULL};
1432                 
1433                 /* init animdata, and attach to workob */
1434                 workob->adt = &adt;
1435                 
1436                 adt.recalc = ADT_RECALC_ANIM;
1437                 adt.action = act;
1438                 
1439                 /* execute effects of Action on to workob (or it's PoseChannels) */
1440                 BKE_animsys_evaluate_animdata(NULL, &workob->id, &adt, cframe, ADT_RECALC_ANIM);
1441         }
1442 }
1443