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