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