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