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