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