Merge branch 'blender2.7'
[blender.git] / source / blender / editors / armature / pose_transform.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): Blender Foundation, 2002-2009 full recode.
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/armature/pose_transform.c
27  *  \ingroup edarmature
28  */
29
30 #include "DNA_anim_types.h"
31 #include "DNA_armature_types.h"
32 #include "DNA_object_types.h"
33 #include "DNA_scene_types.h"
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_blenlib.h"
38 #include "BLI_math.h"
39 #include "BLI_string_utils.h"
40
41 #include "BKE_action.h"
42 #include "BKE_animsys.h"
43 #include "BKE_appdir.h"
44 #include "BKE_armature.h"
45 #include "BKE_blender_copybuffer.h"
46 #include "BKE_context.h"
47 #include "BKE_deform.h"
48 #include "BKE_global.h"
49 #include "BKE_idprop.h"
50 #include "BKE_layer.h"
51 #include "BKE_main.h"
52 #include "BKE_object.h"
53 #include "BKE_report.h"
54
55 #include "DEG_depsgraph.h"
56 #include "DEG_depsgraph_query.h"
57
58 #include "RNA_access.h"
59 #include "RNA_define.h"
60
61 #include "WM_api.h"
62 #include "WM_types.h"
63
64 #include "ED_armature.h"
65 #include "ED_keyframing.h"
66 #include "ED_screen.h"
67 #include "ED_util.h"
68
69 #include "armature_intern.h"
70
71
72 /* ********************************************** */
73 /* Pose Apply */
74
75 /* helper for apply_armature_pose2bones - fixes parenting of objects that are bone-parented to armature */
76 static void applyarmature_fix_boneparents(const bContext *C, Scene *scene, Object *armob)
77 {
78         Depsgraph *depsgraph = CTX_data_depsgraph(C);
79         Main *bmain = CTX_data_main(C);
80         Object workob, *ob;
81
82         /* go through all objects in database */
83         for (ob = bmain->object.first; ob; ob = ob->id.next) {
84                 /* if parent is bone in this armature, apply corrections */
85                 if ((ob->parent == armob) && (ob->partype == PARBONE)) {
86                         /* apply current transform from parent (not yet destroyed),
87                          * then calculate new parent inverse matrix
88                          */
89                         BKE_object_apply_mat4(ob, ob->obmat, false, false);
90
91                         BKE_object_workob_calc_parent(depsgraph, scene, ob, &workob);
92                         invert_m4_m4(ob->parentinv, workob.obmat);
93                 }
94         }
95 }
96
97 /* set the current pose as the restpose */
98 static int apply_armature_pose2bones_exec(bContext *C, wmOperator *op)
99 {
100         Main *bmain = CTX_data_main(C);
101         Depsgraph *depsgraph = CTX_data_depsgraph(C);
102         Scene *scene = CTX_data_scene(C);
103         Object *ob = BKE_object_pose_armature_get(CTX_data_active_object(C)); // must be active object, not edit-object
104         const Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob);
105         bArmature *arm = BKE_armature_from_object(ob);
106         bPose *pose;
107         bPoseChannel *pchan;
108         EditBone *curbone;
109
110         /* don't check if editmode (should be done by caller) */
111         if (ob->type != OB_ARMATURE)
112                 return OPERATOR_CANCELLED;
113         if (BKE_object_obdata_is_libdata(ob)) {
114                 BKE_report(op->reports, RPT_ERROR, "Cannot apply pose to lib-linked armature"); /* error_libdata(); */
115                 return OPERATOR_CANCELLED;
116         }
117
118         /* helpful warnings... */
119         /* TODO: add warnings to be careful about actions, applying deforms first, etc. */
120         if (ob->adt && ob->adt->action)
121                 BKE_report(op->reports, RPT_WARNING,
122                            "Actions on this armature will be destroyed by this new rest pose as the "
123                            "transforms stored are relative to the old rest pose");
124
125         /* Get editbones of active armature to alter */
126         ED_armature_to_edit(arm);
127
128         /* get pose of active object and move it out of posemode */
129         pose = ob->pose;
130
131         for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
132                 const bPoseChannel *pchan_eval = BKE_pose_channel_find_name(ob_eval->pose, pchan->name);
133                 curbone = ED_armature_ebone_find_name(arm->edbo, pchan->name);
134
135                 /* simply copy the head/tail values from pchan over to curbone */
136                 copy_v3_v3(curbone->head, pchan_eval->pose_head);
137                 copy_v3_v3(curbone->tail, pchan_eval->pose_tail);
138
139                 /* fix roll:
140                  * 1. find auto-calculated roll value for this bone now
141                  * 2. remove this from the 'visual' y-rotation
142                  */
143                 {
144                         float premat[3][3], imat[3][3], pmat[3][3], tmat[3][3];
145                         float delta[3], eul[3];
146
147                         /* obtain new auto y-rotation */
148                         sub_v3_v3v3(delta, curbone->tail, curbone->head);
149                         vec_roll_to_mat3(delta, 0.0f, premat);
150                         invert_m3_m3(imat, premat);
151
152                         /* get pchan 'visual' matrix */
153                         copy_m3_m4(pmat, pchan_eval->pose_mat);
154
155                         /* remove auto from visual and get euler rotation */
156                         mul_m3_m3m3(tmat, imat, pmat);
157                         mat3_to_eul(eul, tmat);
158
159                         /* just use this euler-y as new roll value */
160                         curbone->roll = eul[1];
161                 }
162
163                 /* combine pose and rest values for bendy bone settings,
164                  * then clear the pchan values (so we don't get a double-up)
165                  */
166                 if (pchan->bone->segments > 1) {
167                         /* combine rest/pose values  */
168                         curbone->curveInX += pchan_eval->curveInX;
169                         curbone->curveInY += pchan_eval->curveInY;
170                         curbone->curveOutX += pchan_eval->curveOutX;
171                         curbone->curveOutY += pchan_eval->curveOutY;
172                         curbone->roll1 += pchan_eval->roll1;
173                         curbone->roll2 += pchan_eval->roll2;
174                         curbone->ease1 += pchan_eval->ease1;
175                         curbone->ease2 += pchan_eval->ease2;
176                         curbone->scaleIn += pchan_eval->scaleIn;
177                         curbone->scaleOut += pchan_eval->scaleOut;
178
179                         /* reset pose values */
180                         pchan->curveInX = pchan->curveOutX = 0.0f;
181                         pchan->curveInY = pchan->curveOutY = 0.0f;
182                         pchan->roll1 = pchan->roll2 = 0.0f;
183                         pchan->ease1 = pchan->ease2 = 0.0f;
184                         pchan->scaleIn = pchan->scaleOut = 1.0f;
185                 }
186
187                 /* clear transform values for pchan */
188                 zero_v3(pchan->loc);
189                 zero_v3(pchan->eul);
190                 unit_qt(pchan->quat);
191                 unit_axis_angle(pchan->rotAxis, &pchan->rotAngle);
192                 pchan->size[0] = pchan->size[1] = pchan->size[2] = 1.0f;
193
194                 /* set anim lock */
195                 curbone->flag |= BONE_UNKEYED;
196         }
197
198         /* convert editbones back to bones, and then free the edit-data */
199         ED_armature_from_edit(bmain, arm);
200         ED_armature_edit_free(arm);
201
202         /* flush positions of posebones */
203         BKE_pose_where_is(depsgraph, scene, ob);
204
205         /* fix parenting of objects which are bone-parented */
206         applyarmature_fix_boneparents(C, scene, ob);
207
208         /* note, notifier might evolve */
209         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
210         DEG_id_tag_update(&ob->id, ID_RECALC_COPY_ON_WRITE);
211
212         return OPERATOR_FINISHED;
213 }
214
215 void POSE_OT_armature_apply(wmOperatorType *ot)
216 {
217         /* identifiers */
218         ot->name = "Apply Pose as Rest Pose";
219         ot->idname = "POSE_OT_armature_apply";
220         ot->description = "Apply the current pose as the new rest pose";
221
222         /* callbacks */
223         ot->exec = apply_armature_pose2bones_exec;
224         ot->poll = ED_operator_posemode;
225
226         /* flags */
227         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
228 }
229
230
231 /* set the current pose as the restpose */
232 static int pose_visual_transform_apply_exec(bContext *C, wmOperator *UNUSED(op))
233 {
234         ViewLayer *view_layer = CTX_data_view_layer(C);
235         View3D *v3d = CTX_wm_view3d(C);
236         Depsgraph *depsgraph = CTX_data_depsgraph(C);
237
238         FOREACH_OBJECT_IN_MODE_BEGIN(view_layer, v3d, OB_ARMATURE, OB_MODE_POSE, ob)
239         {
240                 /* loop over all selected pchans
241                  *
242                  * TODO, loop over children before parents if multiple bones
243                  * at once are to be predictable*/
244                 FOREACH_PCHAN_SELECTED_IN_OBJECT_BEGIN (ob, pchan)
245                 {
246                         const Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob);
247                         bPoseChannel *pchan_eval = BKE_pose_channel_find_name(ob_eval->pose, pchan->name);
248                         float delta_mat[4][4];
249
250                         /* chan_mat already contains the delta transform from rest pose to pose-mode pose
251                          * as that is baked into there so that B-Bones will work. Once we've set this as the
252                          * new raw-transform components, don't recalc the poses yet, otherwise IK result will
253                          * change, thus changing the result we may be trying to record.
254                          */
255                         /* XXX For some reason, we can't use pchan->chan_mat here, gives odd rotation/offset (see T38251).
256                          *     Using pchan->pose_mat and bringing it back in bone space seems to work as expected!
257                          */
258                         BKE_armature_mat_pose_to_bone(pchan_eval, pchan_eval->pose_mat, delta_mat);
259
260                         BKE_pchan_apply_mat4(pchan, delta_mat, true);
261                 }
262                 FOREACH_PCHAN_SELECTED_IN_OBJECT_END;
263
264                 DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
265
266                 /* note, notifier might evolve */
267                 WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
268         }
269         FOREACH_OBJECT_IN_MODE_END;
270
271         return OPERATOR_FINISHED;
272 }
273
274 void POSE_OT_visual_transform_apply(wmOperatorType *ot)
275 {
276         /* identifiers */
277         ot->name = "Apply Visual Transform to Pose";
278         ot->idname = "POSE_OT_visual_transform_apply";
279         ot->description = "Apply final constrained position of pose bones to their transform";
280
281         /* callbacks */
282         ot->exec = pose_visual_transform_apply_exec;
283         ot->poll = ED_operator_posemode;
284
285         /* flags */
286         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
287 }
288
289 /* ********************************************** */
290 /* Copy/Paste */
291
292 /* This function is used to indicate that a bone is selected
293  * and needs to be included in copy buffer (used to be for inserting keys)
294  */
295 static void set_pose_keys(Object *ob)
296 {
297         bArmature *arm = ob->data;
298         bPoseChannel *chan;
299
300         if (ob->pose) {
301                 for (chan = ob->pose->chanbase.first; chan; chan = chan->next) {
302                         Bone *bone = chan->bone;
303                         if ((bone) && (bone->flag & BONE_SELECTED) && (arm->layer & bone->layer))
304                                 chan->flag |= POSE_KEY;
305                         else
306                                 chan->flag &= ~POSE_KEY;
307                 }
308         }
309 }
310
311 /**
312  * Perform paste pose, for a single bone.
313  *
314  * \param ob: Object where bone to paste to lives
315  * \param chan: Bone that pose to paste comes from
316  * \param selOnly: Only paste on selected bones
317  * \param flip: Flip on x-axis
318  * \return Whether the bone that we pasted to if we succeeded
319  */
320 static bPoseChannel *pose_bone_do_paste(Object *ob, bPoseChannel *chan, const bool selOnly, const bool flip)
321 {
322         bPoseChannel *pchan;
323         char name[MAXBONENAME];
324         short paste_ok;
325
326         /* get the name - if flipping, we must flip this first */
327         if (flip)
328                 BLI_string_flip_side_name(name, chan->name, false, sizeof(name));
329         else
330                 BLI_strncpy(name, chan->name, sizeof(name));
331
332         /* only copy when:
333          *  1) channel exists - poses are not meant to add random channels to anymore
334          *  2) if selection-masking is on, channel is selected - only selected bones get pasted on, allowing making both sides symmetrical
335          */
336         pchan = BKE_pose_channel_find_name(ob->pose, name);
337
338         if (selOnly)
339                 paste_ok = ((pchan) && (pchan->bone->flag & BONE_SELECTED));
340         else
341                 paste_ok = (pchan != NULL);
342
343         /* continue? */
344         if (paste_ok) {
345                 /* only loc rot size
346                  * - only copies transform info for the pose
347                  */
348                 copy_v3_v3(pchan->loc, chan->loc);
349                 copy_v3_v3(pchan->size, chan->size);
350                 pchan->flag = chan->flag;
351
352                 /* check if rotation modes are compatible (i.e. do they need any conversions) */
353                 if (pchan->rotmode == chan->rotmode) {
354                         /* copy the type of rotation in use */
355                         if (pchan->rotmode > 0) {
356                                 copy_v3_v3(pchan->eul, chan->eul);
357                         }
358                         else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
359                                 copy_v3_v3(pchan->rotAxis, chan->rotAxis);
360                                 pchan->rotAngle = chan->rotAngle;
361                         }
362                         else {
363                                 copy_qt_qt(pchan->quat, chan->quat);
364                         }
365                 }
366                 else if (pchan->rotmode > 0) {
367                         /* quat/axis-angle to euler */
368                         if (chan->rotmode == ROT_MODE_AXISANGLE)
369                                 axis_angle_to_eulO(pchan->eul, pchan->rotmode, chan->rotAxis, chan->rotAngle);
370                         else
371                                 quat_to_eulO(pchan->eul, pchan->rotmode, chan->quat);
372                 }
373                 else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
374                         /* quat/euler to axis angle */
375                         if (chan->rotmode > 0)
376                                 eulO_to_axis_angle(pchan->rotAxis, &pchan->rotAngle, chan->eul, chan->rotmode);
377                         else
378                                 quat_to_axis_angle(pchan->rotAxis, &pchan->rotAngle, chan->quat);
379                 }
380                 else {
381                         /* euler/axis-angle to quat */
382                         if (chan->rotmode > 0)
383                                 eulO_to_quat(pchan->quat, chan->eul, chan->rotmode);
384                         else
385                                 axis_angle_to_quat(pchan->quat, chan->rotAxis, pchan->rotAngle);
386                 }
387
388                 /* B-Bone posing options should also be included... */
389                 pchan->curveInX = chan->curveInX;
390                 pchan->curveInY = chan->curveInY;
391                 pchan->curveOutX = chan->curveOutX;
392                 pchan->curveOutY = chan->curveOutY;
393
394                 pchan->roll1 = chan->roll1;
395                 pchan->roll2 = chan->roll2;
396                 pchan->ease1 = chan->ease1;
397                 pchan->ease2 = chan->ease2;
398                 pchan->scaleIn = chan->scaleIn;
399                 pchan->scaleOut = chan->scaleOut;
400
401                 /* paste flipped pose? */
402                 if (flip) {
403                         pchan->loc[0] *= -1;
404
405                         pchan->curveInX *= -1;
406                         pchan->curveOutX *= -1;
407                         pchan->roll1 *= -1; // XXX?
408                         pchan->roll2 *= -1; // XXX?
409
410                         /* has to be done as eulers... */
411                         if (pchan->rotmode > 0) {
412                                 pchan->eul[1] *= -1;
413                                 pchan->eul[2] *= -1;
414                         }
415                         else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
416                                 float eul[3];
417
418                                 axis_angle_to_eulO(eul, EULER_ORDER_DEFAULT, pchan->rotAxis, pchan->rotAngle);
419                                 eul[1] *= -1;
420                                 eul[2] *= -1;
421                                 eulO_to_axis_angle(pchan->rotAxis, &pchan->rotAngle, eul, EULER_ORDER_DEFAULT);
422                         }
423                         else {
424                                 float eul[3];
425
426                                 normalize_qt(pchan->quat);
427                                 quat_to_eul(eul, pchan->quat);
428                                 eul[1] *= -1;
429                                 eul[2] *= -1;
430                                 eul_to_quat(pchan->quat, eul);
431                         }
432                 }
433
434                 /* ID properties */
435                 if (chan->prop) {
436                         if (pchan->prop) {
437                                 /* if we have existing properties on a bone, just copy over the values of
438                                  * matching properties (i.e. ones which will have some impact) on to the
439                                  * target instead of just blinding replacing all [
440                                  */
441                                 IDP_SyncGroupValues(pchan->prop, chan->prop);
442                         }
443                         else {
444                                 /* no existing properties, so assume that we want copies too? */
445                                 pchan->prop = IDP_CopyProperty(chan->prop);
446                         }
447                 }
448         }
449
450         /* return whether paste went ahead */
451         return pchan;
452 }
453
454 /* ---- */
455
456 static int pose_copy_exec(bContext *C, wmOperator *op)
457 {
458         Object *ob = BKE_object_pose_armature_get(CTX_data_active_object(C));
459         char str[FILE_MAX];
460         /* Sanity checking. */
461         if (ELEM(NULL, ob, ob->pose)) {
462                 BKE_report(op->reports, RPT_ERROR, "No pose to copy");
463                 return OPERATOR_CANCELLED;
464         }
465         /* Sets chan->flag to POSE_KEY if bone selected. */
466         set_pose_keys(ob);
467         /* Construct a local bmain and only put object and it's data into it,
468          * o this way we don't expand any other objects into the copy buffer
469          * file.
470          *
471          * TODO(sergey): Find an easier way to tell copy buffer to only store
472          * data we are actually interested in. Maybe pass it a flag to skip
473          * any datablock expansion?
474          */
475         Main *temp_bmain = BKE_main_new();
476         Object ob_copy = *ob;
477         bArmature arm_copy = *((bArmature *)ob->data);
478         ob_copy.data = &arm_copy;
479         BLI_addtail(&temp_bmain->object, &ob_copy);
480         BLI_addtail(&temp_bmain->armature, &arm_copy);
481         /* begin copy buffer on a temp bmain. */
482         BKE_copybuffer_begin(temp_bmain);
483         /* Store the whole object to the copy buffer because pose can't be
484          * existing on it's own.
485          */
486         BKE_copybuffer_tag_ID(&ob_copy.id);
487         BLI_make_file_string("/", str, BKE_tempdir_base(), "copybuffer_pose.blend");
488         BKE_copybuffer_save(temp_bmain, str, op->reports);
489         /* We clear the lists so no datablocks gets freed,
490          * This is required because objects in temp bmain shares same pointers
491          * as the real ones.
492          */
493         BLI_listbase_clear(&temp_bmain->object);
494         BLI_listbase_clear(&temp_bmain->armature);
495         BKE_main_free(temp_bmain);
496         /* We are all done! */
497         BKE_report(op->reports, RPT_INFO, "Copied pose to buffer");
498         return OPERATOR_FINISHED;
499 }
500
501 void POSE_OT_copy(wmOperatorType *ot)
502 {
503         /* identifiers */
504         ot->name = "Copy Pose";
505         ot->idname = "POSE_OT_copy";
506         ot->description = "Copies the current pose of the selected bones to copy/paste buffer";
507
508         /* api callbacks */
509         ot->exec = pose_copy_exec;
510         ot->poll = ED_operator_posemode;
511
512         /* flag */
513         ot->flag = OPTYPE_REGISTER;
514 }
515
516 /* ---- */
517
518 static int pose_paste_exec(bContext *C, wmOperator *op)
519 {
520         Object *ob = BKE_object_pose_armature_get(CTX_data_active_object(C));
521         Scene *scene = CTX_data_scene(C);
522         bPoseChannel *chan;
523         const bool flip = RNA_boolean_get(op->ptr, "flipped");
524         bool selOnly = RNA_boolean_get(op->ptr, "selected_mask");
525
526         /* Get KeyingSet to use. */
527         KeyingSet *ks = ANIM_get_keyingset_for_autokeying(scene, ANIM_KS_WHOLE_CHARACTER_ID);
528
529         /* Sanity checks. */
530         if (ELEM(NULL, ob, ob->pose)) {
531                 return OPERATOR_CANCELLED;
532         }
533
534         /* Read copy buffer .blend file. */
535         char str[FILE_MAX];
536         Main *tmp_bmain = BKE_main_new();
537         BLI_make_file_string("/", str, BKE_tempdir_base(), "copybuffer_pose.blend");
538         if (!BKE_copybuffer_read(tmp_bmain, str, op->reports)) {
539                 BKE_report(op->reports, RPT_ERROR, "Copy buffer is empty");
540                 BKE_main_free(tmp_bmain);
541                 return OPERATOR_CANCELLED;
542         }
543         /* Make sure data from this file is usable for pose paste. */
544         if (BLI_listbase_count_at_most(&tmp_bmain->object, 2) != 1) {
545                 BKE_report(op->reports, RPT_ERROR, "Copy buffer is not from pose mode");
546                 BKE_main_free(tmp_bmain);
547                 return OPERATOR_CANCELLED;
548         }
549
550         Object *object_from = tmp_bmain->object.first;
551         bPose *pose_from = object_from->pose;
552         if (pose_from == NULL) {
553                 BKE_report(op->reports, RPT_ERROR, "Copy buffer has no pose");
554                 BKE_main_free(tmp_bmain);
555                 return OPERATOR_CANCELLED;
556         }
557
558         /* If selOnly option is enabled, if user hasn't selected any bones,
559          * just go back to default behavior to be more in line with other
560          * pose tools.
561          */
562         if (selOnly) {
563                 if (CTX_DATA_COUNT(C, selected_pose_bones) == 0) {
564                         selOnly = false;
565                 }
566         }
567
568         /* Safely merge all of the channels in the buffer pose into any
569          * existing pose.
570          */
571         for (chan = pose_from->chanbase.first; chan; chan = chan->next) {
572                 if (chan->flag & POSE_KEY) {
573                         /* Try to perform paste on this bone. */
574                         bPoseChannel *pchan = pose_bone_do_paste(ob, chan, selOnly, flip);
575                         if (pchan != NULL) {
576                                 /* Keyframing tagging for successful paste, */
577                                 ED_autokeyframe_pchan(C, scene, ob, pchan, ks);
578                         }
579                 }
580         }
581         BKE_main_free(tmp_bmain);
582
583         /* Update event for pose and deformation children. */
584         DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
585
586         /* Recalculate paths if any of the bones have paths... */
587         if ((ob->pose->avs.path_bakeflag & MOTIONPATH_BAKE_HAS_PATHS)) {
588                 ED_pose_recalculate_paths(C, scene, ob, false);
589         }
590
591         /* Notifiers for updates, */
592         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
593
594         return OPERATOR_FINISHED;
595 }
596
597 void POSE_OT_paste(wmOperatorType *ot)
598 {
599         PropertyRNA *prop;
600
601         /* identifiers */
602         ot->name = "Paste Pose";
603         ot->idname = "POSE_OT_paste";
604         ot->description = "Paste the stored pose on to the current pose";
605
606         /* api callbacks */
607         ot->exec = pose_paste_exec;
608         ot->poll = ED_operator_posemode;
609
610         /* flag */
611         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
612
613         /* properties */
614         prop = RNA_def_boolean(ot->srna, "flipped", false, "Flipped on X-Axis", "Paste the stored pose flipped on to current pose");
615         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
616
617         RNA_def_boolean(ot->srna, "selected_mask", false, "On Selected Only", "Only paste the stored pose on to selected bones in the current pose");
618 }
619
620 /* ********************************************** */
621 /* Clear Pose Transforms */
622
623 /* clear scale of pose-channel */
624 static void pchan_clear_scale(bPoseChannel *pchan)
625 {
626         if ((pchan->protectflag & OB_LOCK_SCALEX) == 0)
627                 pchan->size[0] = 1.0f;
628         if ((pchan->protectflag & OB_LOCK_SCALEY) == 0)
629                 pchan->size[1] = 1.0f;
630         if ((pchan->protectflag & OB_LOCK_SCALEZ) == 0)
631                 pchan->size[2] = 1.0f;
632
633         pchan->ease1 = 0.0f;
634         pchan->ease2 = 0.0f;
635         pchan->scaleIn = 1.0f;
636         pchan->scaleOut = 1.0f;
637 }
638
639 /* clear location of pose-channel */
640 static void pchan_clear_loc(bPoseChannel *pchan)
641 {
642         if ((pchan->protectflag & OB_LOCK_LOCX) == 0)
643                 pchan->loc[0] = 0.0f;
644         if ((pchan->protectflag & OB_LOCK_LOCY) == 0)
645                 pchan->loc[1] = 0.0f;
646         if ((pchan->protectflag & OB_LOCK_LOCZ) == 0)
647                 pchan->loc[2] = 0.0f;
648 }
649
650 /* clear rotation of pose-channel */
651 static void pchan_clear_rot(bPoseChannel *pchan)
652 {
653         if (pchan->protectflag & (OB_LOCK_ROTX | OB_LOCK_ROTY | OB_LOCK_ROTZ | OB_LOCK_ROTW)) {
654                 /* check if convert to eulers for locking... */
655                 if (pchan->protectflag & OB_LOCK_ROT4D) {
656                         /* perform clamping on a component by component basis */
657                         if (pchan->rotmode == ROT_MODE_AXISANGLE) {
658                                 if ((pchan->protectflag & OB_LOCK_ROTW) == 0)
659                                         pchan->rotAngle = 0.0f;
660                                 if ((pchan->protectflag & OB_LOCK_ROTX) == 0)
661                                         pchan->rotAxis[0] = 0.0f;
662                                 if ((pchan->protectflag & OB_LOCK_ROTY) == 0)
663                                         pchan->rotAxis[1] = 0.0f;
664                                 if ((pchan->protectflag & OB_LOCK_ROTZ) == 0)
665                                         pchan->rotAxis[2] = 0.0f;
666
667                                 /* check validity of axis - axis should never be 0,0,0 (if so, then we make it rotate about y) */
668                                 if (IS_EQF(pchan->rotAxis[0], pchan->rotAxis[1]) && IS_EQF(pchan->rotAxis[1], pchan->rotAxis[2]))
669                                         pchan->rotAxis[1] = 1.0f;
670                         }
671                         else if (pchan->rotmode == ROT_MODE_QUAT) {
672                                 if ((pchan->protectflag & OB_LOCK_ROTW) == 0)
673                                         pchan->quat[0] = 1.0f;
674                                 if ((pchan->protectflag & OB_LOCK_ROTX) == 0)
675                                         pchan->quat[1] = 0.0f;
676                                 if ((pchan->protectflag & OB_LOCK_ROTY) == 0)
677                                         pchan->quat[2] = 0.0f;
678                                 if ((pchan->protectflag & OB_LOCK_ROTZ) == 0)
679                                         pchan->quat[3] = 0.0f;
680                         }
681                         else {
682                                 /* the flag may have been set for the other modes, so just ignore the extra flag... */
683                                 if ((pchan->protectflag & OB_LOCK_ROTX) == 0)
684                                         pchan->eul[0] = 0.0f;
685                                 if ((pchan->protectflag & OB_LOCK_ROTY) == 0)
686                                         pchan->eul[1] = 0.0f;
687                                 if ((pchan->protectflag & OB_LOCK_ROTZ) == 0)
688                                         pchan->eul[2] = 0.0f;
689                         }
690                 }
691                 else {
692                         /* perform clamping using euler form (3-components) */
693                         float eul[3], oldeul[3], quat1[4] = {0};
694                         float qlen = 0.0f;
695
696                         if (pchan->rotmode == ROT_MODE_QUAT) {
697                                 qlen = normalize_qt_qt(quat1, pchan->quat);
698                                 quat_to_eul(oldeul, quat1);
699                         }
700                         else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
701                                 axis_angle_to_eulO(oldeul, EULER_ORDER_DEFAULT, pchan->rotAxis, pchan->rotAngle);
702                         }
703                         else {
704                                 copy_v3_v3(oldeul, pchan->eul);
705                         }
706
707                         eul[0] = eul[1] = eul[2] = 0.0f;
708
709                         if (pchan->protectflag & OB_LOCK_ROTX)
710                                 eul[0] = oldeul[0];
711                         if (pchan->protectflag & OB_LOCK_ROTY)
712                                 eul[1] = oldeul[1];
713                         if (pchan->protectflag & OB_LOCK_ROTZ)
714                                 eul[2] = oldeul[2];
715
716                         if (pchan->rotmode == ROT_MODE_QUAT) {
717                                 eul_to_quat(pchan->quat, eul);
718
719                                 /* restore original quat size */
720                                 mul_qt_fl(pchan->quat, qlen);
721
722                                 /* quaternions flip w sign to accumulate rotations correctly */
723                                 if ((quat1[0] < 0.0f && pchan->quat[0] > 0.0f) || (quat1[0] > 0.0f && pchan->quat[0] < 0.0f)) {
724                                         mul_qt_fl(pchan->quat, -1.0f);
725                                 }
726                         }
727                         else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
728                                 eulO_to_axis_angle(pchan->rotAxis, &pchan->rotAngle, eul, EULER_ORDER_DEFAULT);
729                         }
730                         else {
731                                 copy_v3_v3(pchan->eul, eul);
732                         }
733                 }
734         }       /* Duplicated in source/blender/editors/object/object_transform.c */
735         else {
736                 if (pchan->rotmode == ROT_MODE_QUAT) {
737                         unit_qt(pchan->quat);
738                 }
739                 else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
740                         /* by default, make rotation of 0 radians around y-axis (roll) */
741                         unit_axis_angle(pchan->rotAxis, &pchan->rotAngle);
742                 }
743                 else {
744                         zero_v3(pchan->eul);
745                 }
746         }
747
748         /* Clear also Bendy Bone stuff - Roll is obvious, but Curve X/Y stuff is also kindof rotational in nature... */
749         pchan->roll1 = 0.0f;
750         pchan->roll2 = 0.0f;
751
752         pchan->curveInX = 0.0f;
753         pchan->curveInY = 0.0f;
754         pchan->curveOutX = 0.0f;
755         pchan->curveOutY = 0.0f;
756 }
757
758 /* clear loc/rot/scale of pose-channel */
759 static void pchan_clear_transforms(bPoseChannel *pchan)
760 {
761         pchan_clear_loc(pchan);
762         pchan_clear_rot(pchan);
763         pchan_clear_scale(pchan);
764 }
765
766 /* --------------- */
767
768 /* generic exec for clear-pose operators */
769 static int pose_clear_transform_generic_exec(bContext *C, wmOperator *op,
770                                              void (*clear_func)(bPoseChannel *), const char default_ksName[])
771 {
772         Scene *scene = CTX_data_scene(C);
773         bool changed_multi = false;
774
775         /* sanity checks */
776         if (ELEM(NULL, clear_func, default_ksName)) {
777                 BKE_report(op->reports, RPT_ERROR, "Programming error: missing clear transform function or keying set name");
778                 return OPERATOR_CANCELLED;
779         }
780
781         /* only clear relevant transforms for selected bones */
782         ViewLayer *view_layer = CTX_data_view_layer(C);
783         View3D *v3d = CTX_wm_view3d(C);
784         FOREACH_OBJECT_IN_MODE_BEGIN (view_layer, v3d, OB_ARMATURE, OB_MODE_POSE, ob_iter)
785         {
786                 Object *ob_eval = DEG_get_evaluated_object(CTX_data_depsgraph(C), ob_iter); // XXX: UGLY HACK (for autokey + clear transforms)
787                 ListBase dsources = {NULL, NULL};
788                 bool changed = false;
789
790                 FOREACH_PCHAN_SELECTED_IN_OBJECT_BEGIN (ob_iter, pchan)
791                 {
792                         /* run provided clearing function */
793                         clear_func(pchan);
794                         changed = true;
795
796                         /* do auto-keyframing as appropriate */
797                         if (autokeyframe_cfra_can_key(scene, &ob_iter->id)) {
798                                 /* clear any unkeyed tags */
799                                 if (pchan->bone) {
800                                         pchan->bone->flag &= ~BONE_UNKEYED;
801                                 }
802                                 /* tag for autokeying later */
803                                 ANIM_relative_keyingset_add_source(&dsources, &ob_iter->id, &RNA_PoseBone, pchan);
804
805 #if 1                   /* XXX: Ugly Hack - Run clearing function on evaluated copy of pchan */
806                                 bPoseChannel *pchan_eval = BKE_pose_channel_find_name(ob_eval->pose, pchan->name);
807                                 clear_func(pchan_eval);
808 #endif
809                         }
810                         else {
811                                 /* add unkeyed tags */
812                                 if (pchan->bone) {
813                                         pchan->bone->flag |= BONE_UNKEYED;
814                                 }
815                         }
816                 }
817                 FOREACH_PCHAN_SELECTED_IN_OBJECT_END;
818
819                 if (changed) {
820                         changed_multi = true;
821
822                         /* perform autokeying on the bones if needed */
823                         if (!BLI_listbase_is_empty(&dsources)) {
824                                 /* get KeyingSet to use */
825                                 KeyingSet *ks = ANIM_get_keyingset_for_autokeying(scene, default_ksName);
826
827                                 /* insert keyframes */
828                                 ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
829
830                                 /* now recalculate paths */
831                                 if ((ob_iter->pose->avs.path_bakeflag & MOTIONPATH_BAKE_HAS_PATHS)) {
832                                         ED_pose_recalculate_paths(C, scene, ob_iter, false);
833                                 }
834
835                                 BLI_freelistN(&dsources);
836                         }
837
838                         DEG_id_tag_update(&ob_iter->id, ID_RECALC_GEOMETRY);
839
840                         /* note, notifier might evolve */
841                         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, ob_iter);
842                 }
843         }
844         FOREACH_OBJECT_IN_MODE_END;
845
846         return changed_multi ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
847 }
848
849 /* --------------- */
850
851 static int pose_clear_scale_exec(bContext *C, wmOperator *op)
852 {
853         return pose_clear_transform_generic_exec(C, op, pchan_clear_scale, ANIM_KS_SCALING_ID);
854 }
855
856 void POSE_OT_scale_clear(wmOperatorType *ot)
857 {
858         /* identifiers */
859         ot->name = "Clear Pose Scale";
860         ot->idname = "POSE_OT_scale_clear";
861         ot->description = "Reset scaling of selected bones to their default values";
862
863         /* api callbacks */
864         ot->exec = pose_clear_scale_exec;
865         ot->poll = ED_operator_posemode;
866
867         /* flags */
868         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
869 }
870
871
872 static int pose_clear_rot_exec(bContext *C, wmOperator *op)
873 {
874         return pose_clear_transform_generic_exec(C, op, pchan_clear_rot, ANIM_KS_ROTATION_ID);
875 }
876
877 void POSE_OT_rot_clear(wmOperatorType *ot)
878 {
879         /* identifiers */
880         ot->name = "Clear Pose Rotation";
881         ot->idname = "POSE_OT_rot_clear";
882         ot->description = "Reset rotations of selected bones to their default values";
883
884         /* api callbacks */
885         ot->exec = pose_clear_rot_exec;
886         ot->poll = ED_operator_posemode;
887
888         /* flags */
889         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
890 }
891
892
893 static int pose_clear_loc_exec(bContext *C, wmOperator *op)
894 {
895         return pose_clear_transform_generic_exec(C, op, pchan_clear_loc, ANIM_KS_LOCATION_ID);
896 }
897
898 void POSE_OT_loc_clear(wmOperatorType *ot)
899 {
900         /* identifiers */
901         ot->name = "Clear Pose Location";
902         ot->idname = "POSE_OT_loc_clear";
903         ot->description = "Reset locations of selected bones to their default values";
904
905         /* api callbacks */
906         ot->exec = pose_clear_loc_exec;
907         ot->poll = ED_operator_posemode;
908
909         /* flags */
910         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
911 }
912
913
914 static int pose_clear_transforms_exec(bContext *C, wmOperator *op)
915 {
916         return pose_clear_transform_generic_exec(C, op, pchan_clear_transforms, ANIM_KS_LOC_ROT_SCALE_ID);
917 }
918
919 void POSE_OT_transforms_clear(wmOperatorType *ot)
920 {
921         /* identifiers */
922         ot->name = "Clear Pose Transforms";
923         ot->idname = "POSE_OT_transforms_clear";
924         ot->description = "Reset location, rotation, and scaling of selected bones to their default values";
925
926         /* api callbacks */
927         ot->exec = pose_clear_transforms_exec;
928         ot->poll = ED_operator_posemode;
929
930         /* flags */
931         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
932 }
933
934 /* ********************************************** */
935 /* Clear User Transforms */
936
937 static int pose_clear_user_transforms_exec(bContext *C, wmOperator *op)
938 {
939         ViewLayer *view_layer = CTX_data_view_layer(C);
940         View3D *v3d = CTX_wm_view3d(C);
941         Scene *scene = CTX_data_scene(C);
942         float cframe = (float)CFRA;
943         const bool only_select = RNA_boolean_get(op->ptr, "only_selected");
944
945         FOREACH_OBJECT_IN_MODE_BEGIN (view_layer, v3d, OB_ARMATURE, OB_MODE_POSE, ob)
946         {
947                 if ((ob->adt) && (ob->adt->action)) {
948                         /* XXX: this is just like this to avoid contaminating anything else;
949                          * just pose values should change, so this should be fine
950                          */
951                         bPose *dummyPose = NULL;
952                         Object workob = {{NULL}};
953                         bPoseChannel *pchan;
954
955                         /* execute animation step for current frame using a dummy copy of the pose */
956                         BKE_pose_copy_data(&dummyPose, ob->pose, 0);
957
958                         BLI_strncpy(workob.id.name, "OB<ClearTfmWorkOb>", sizeof(workob.id.name));
959                         workob.type = OB_ARMATURE;
960                         workob.data = ob->data;
961                         workob.adt = ob->adt;
962                         workob.pose = dummyPose;
963
964                         BKE_animsys_evaluate_animdata(NULL, scene, &workob.id, workob.adt, cframe, ADT_RECALC_ANIM);
965
966                         /* copy back values, but on selected bones only  */
967                         for (pchan = dummyPose->chanbase.first; pchan; pchan = pchan->next) {
968                                 pose_bone_do_paste(ob, pchan, only_select, 0);
969                         }
970
971                         /* free temp data - free manually as was copied without constraints */
972                         for (pchan = dummyPose->chanbase.first; pchan; pchan = pchan->next) {
973                                 if (pchan->prop) {
974                                         IDP_FreeProperty(pchan->prop);
975                                         MEM_freeN(pchan->prop);
976                                 }
977                         }
978
979                         /* was copied without constraints */
980                         BLI_freelistN(&dummyPose->chanbase);
981                         MEM_freeN(dummyPose);
982                 }
983                 else {
984                         /* no animation, so just reset whole pose to rest pose
985                          * (cannot just restore for selected though)
986                          */
987                         BKE_pose_rest(ob->pose);
988                 }
989
990                 /* notifiers and updates */
991                 DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
992                 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, ob);
993         }
994         FOREACH_OBJECT_IN_MODE_END;
995
996         return OPERATOR_FINISHED;
997 }
998
999 void POSE_OT_user_transforms_clear(wmOperatorType *ot)
1000 {
1001         /* identifiers */
1002         ot->name = "Clear User Transforms";
1003         ot->idname = "POSE_OT_user_transforms_clear";
1004         ot->description = "Reset pose on selected bones to keyframed state";
1005
1006         /* callbacks */
1007         ot->exec = pose_clear_user_transforms_exec;
1008         ot->poll = ED_operator_posemode;
1009
1010         /* flags */
1011         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1012
1013         /* properties */
1014         RNA_def_boolean(ot->srna, "only_selected", true, "Only Selected", "Only visible/selected bones");
1015 }