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