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