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