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