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