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