2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/editors/transform/transform_conversions.c
29 * \ingroup edtransform
41 #include "DNA_anim_types.h"
42 #include "DNA_armature_types.h"
43 #include "DNA_lattice_types.h"
44 #include "DNA_mesh_types.h"
45 #include "DNA_meta_types.h"
46 #include "DNA_node_types.h"
47 #include "DNA_screen_types.h"
48 #include "DNA_space_types.h"
49 #include "DNA_sequence_types.h"
50 #include "DNA_view3d_types.h"
51 #include "DNA_constraint_types.h"
52 #include "DNA_scene_types.h"
53 #include "DNA_meshdata_types.h"
54 #include "DNA_gpencil_types.h"
55 #include "DNA_movieclip_types.h"
56 #include "DNA_mask_types.h"
58 #include "MEM_guardedalloc.h"
61 #include "BLI_blenlib.h"
62 #include "BLI_array.h"
63 #include "BLI_utildefines.h"
64 #include "BLI_smallhash.h"
66 #include "BKE_DerivedMesh.h"
67 #include "BKE_action.h"
68 #include "BKE_armature.h"
69 #include "BKE_bmesh.h"
70 #include "BKE_constraint.h"
71 #include "BKE_context.h"
72 #include "BKE_curve.h"
73 #include "BKE_depsgraph.h"
74 #include "BKE_fcurve.h"
75 #include "BKE_global.h"
76 #include "BKE_gpencil.h"
79 #include "BKE_modifier.h"
80 #include "BKE_movieclip.h"
83 #include "BKE_object.h"
84 #include "BKE_particle.h"
85 #include "BKE_pointcache.h"
86 #include "BKE_report.h"
87 #include "BKE_scene.h"
88 #include "BKE_sequencer.h"
89 #include "BKE_tessmesh.h"
90 #include "BKE_tracking.h"
94 #include "ED_anim_api.h"
95 #include "ED_armature.h"
96 #include "ED_particle.h"
98 #include "ED_keyframing.h"
99 #include "ED_keyframes_edit.h"
100 #include "ED_object.h"
101 #include "ED_markers.h"
104 #include "ED_types.h"
105 #include "ED_uvedit.h"
108 #include "ED_util.h" /* for crazyspace correction */
110 #include "WM_api.h" /* for WM_event_add_notifier to deal with stabilization nodes */
111 #include "WM_types.h"
113 #include "UI_view2d.h"
115 #include "RNA_access.h"
117 #include "transform.h"
120 #include "BLO_sys_types.h" // for intptr_t support
122 /* local function prototype - for Object/Bone Constraints */
123 static short constraints_list_needinv(TransInfo *t, ListBase *list);
125 /* ************************** Functions *************************** */
127 static void qsort_trans_data(TransInfo *t, TransData *head, TransData *tail, TransData *temp)
129 TransData *ihead = head;
130 TransData *itail = tail;
133 while (head < tail) {
134 if (t->flag & T_PROP_CONNECTED) {
135 while ((tail->dist >= temp->dist) && (head < tail))
139 while ((tail->rdist >= temp->rdist) && (head < tail))
148 if (t->flag & T_PROP_CONNECTED) {
149 while ((head->dist <= temp->dist) && (head < tail))
153 while ((head->rdist <= temp->rdist) && (head < tail))
165 qsort_trans_data(t, ihead, head-1, temp);
168 qsort_trans_data(t, head+1, itail, temp);
172 void sort_trans_data_dist(TransInfo *t)
175 TransData *start = t->data;
178 while (i < t->total && start->flag & TD_SELECTED) {
182 qsort_trans_data(t, start, t->data + t->total - 1, &temp);
185 static void sort_trans_data(TransInfo *t)
187 TransData *sel, *unsel;
192 while (sel > unsel) {
193 while (unsel->flag & TD_SELECTED) {
199 while (!(sel->flag & TD_SELECTED)) {
213 /* distance calculated from not-selected vertex to nearest selected vertex
214 * warning; this is loops inside loop, has minor N^2 issues, but by sorting list it is OK */
215 static void set_prop_dist(TransInfo *t, short with_dist)
220 for (a=0, tob= t->data; a<t->total; a++, tob++) {
222 tob->rdist= 0.0f; // init, it was mallocced
224 if ((tob->flag & TD_SELECTED)==0) {
229 tob->rdist = -1.0f; // signal for next loop
231 for (i = 0, td= t->data; i < t->total; i++, td++) {
232 if (td->flag & TD_SELECTED) {
233 sub_v3_v3v3(vec, tob->center, td->center);
234 mul_m3_v3(tob->mtx, vec);
235 dist = normalize_v3(vec);
236 if (tob->rdist == -1.0f) {
239 else if (dist < tob->rdist) {
243 else break; // by definition transdata has selected items in beginning
246 tob->dist = tob->rdist;
252 /* ************************** CONVERSIONS ************************* */
254 /* ********************* texture space ********* */
256 static void createTransTexspace(TransInfo *t)
258 Scene *scene = t->scene;
266 if (ob == NULL) { // Shouldn't logically happen, but still...
272 if (id == NULL || !ELEM3(GS(id->name), ID_ME, ID_CU, ID_MB )) {
278 td= t->data= MEM_callocN(sizeof(TransData), "TransTexspace");
279 td->ext= t->ext= MEM_callocN(sizeof(TransDataExtension), "TransTexspace");
281 td->flag= TD_SELECTED;
282 copy_v3_v3(td->center, ob->obmat[3]);
285 copy_m3_m4(td->mtx, ob->obmat);
286 copy_m3_m4(td->axismtx, ob->obmat);
287 normalize_m3(td->axismtx);
288 invert_m3_m3(td->smtx, td->mtx);
290 if (BKE_object_obdata_texspace_get(ob, &texflag, &td->loc, &td->ext->size, &td->ext->rot)) {
291 ob->dtx |= OB_TEXSPACE;
292 *texflag &= ~ME_AUTOSPACE;
295 copy_v3_v3(td->iloc, td->loc);
296 copy_v3_v3(td->ext->irot, td->ext->rot);
297 copy_v3_v3(td->ext->isize, td->ext->size);
300 /* ********************* edge (for crease) ***** */
302 static void createTransEdge(TransInfo *t)
304 BMEditMesh *em = BMEdit_FromObject(t->obedit);
305 TransData *td = NULL;
308 float mtx[3][3], smtx[3][3];
309 int count=0, countsel=0;
310 int propmode = t->flag & T_PROP_EDIT;
312 BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
313 if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN)) {
314 if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) countsel++;
315 if (propmode) count++;
329 td= t->data= MEM_callocN(t->total * sizeof(TransData), "TransCrease");
331 copy_m3_m4(mtx, t->obedit->obmat);
332 invert_m3_m3(smtx, mtx);
334 BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
335 if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN) && (BM_elem_flag_test(eed, BM_ELEM_SELECT) || propmode)) {
336 float *bweight = CustomData_bmesh_get(&em->bm->edata, eed->head.data, CD_BWEIGHT);
337 float *crease = CustomData_bmesh_get(&em->bm->edata, eed->head.data, CD_CREASE);
339 /* need to set center for center calculations */
340 add_v3_v3v3(td->center, eed->v1->co, eed->v2->co);
341 mul_v3_fl(td->center, 0.5f);
344 if (BM_elem_flag_test(eed, BM_ELEM_SELECT))
345 td->flag= TD_SELECTED;
350 copy_m3_m3(td->smtx, smtx);
351 copy_m3_m3(td->mtx, mtx);
354 if (t->mode == TFM_BWEIGHT) {
356 td->ival = bweight ? *bweight : 1.0f;
360 td->ival = crease ? *crease : 0.0f;
368 /* ********************* pose mode ************* */
370 static bKinematicConstraint *has_targetless_ik(bPoseChannel *pchan)
372 bConstraint *con= pchan->constraints.first;
374 for (;con; con= con->next) {
375 if (con->type==CONSTRAINT_TYPE_KINEMATIC && (con->enforce!=0.0f)) {
376 bKinematicConstraint *data= con->data;
380 if (data->tar->type==OB_ARMATURE && data->subtarget[0]==0)
387 static short apply_targetless_ik(Object *ob)
389 bPoseChannel *pchan, *parchan, *chanlist[256];
390 bKinematicConstraint *data;
391 int segcount, apply= 0;
393 /* now we got a difficult situation... we have to find the
394 * target-less IK pchans, and apply transformation to the all
395 * pchans that were in the chain */
397 for (pchan=ob->pose->chanbase.first; pchan; pchan=pchan->next) {
398 data= has_targetless_ik(pchan);
399 if (data && (data->flag & CONSTRAINT_IK_AUTO)) {
401 /* fill the array with the bones of the chain (armature.c does same, keep it synced) */
404 /* exclude tip from chain? */
405 if (!(data->flag & CONSTRAINT_IK_TIP))
406 parchan= pchan->parent;
410 /* Find the chain's root & count the segments needed */
411 for (; parchan; parchan=parchan->parent) {
412 chanlist[segcount]= parchan;
415 if (segcount==data->rootbone || segcount>255) break; // 255 is weak
417 for (;segcount;segcount--) {
419 float rmat[4][4]/*, tmat[4][4], imat[4][4]*/;
421 /* pose_mat(b) = pose_mat(b-1) * offs_bone * channel * constraint * IK */
422 /* we put in channel the entire result of rmat= (channel * constraint * IK) */
423 /* pose_mat(b) = pose_mat(b-1) * offs_bone * rmat */
424 /* rmat = pose_mat(b) * inv(pose_mat(b-1) * offs_bone ) */
426 parchan= chanlist[segcount-1];
428 bone->flag |= BONE_TRANSFORM; /* ensures it gets an auto key inserted */
430 BKE_armature_mat_pose_to_bone(parchan, parchan->pose_mat, rmat);
432 /* apply and decompose, doesn't work for constraints or non-uniform scale well */
434 float rmat3[3][3], qrmat[3][3], imat3[3][3], smat[3][3];
436 copy_m3_m4(rmat3, rmat);
439 /* [#22409] is partially caused by this, as slight numeric error introduced during
440 * the solving process leads to locked-axis values changing. However, we cannot modify
441 * the values here, or else there are huge discreptancies between IK-solver (interactive)
444 if (parchan->rotmode > 0)
445 mat3_to_eulO(parchan->eul, parchan->rotmode, rmat3);
446 else if (parchan->rotmode == ROT_MODE_AXISANGLE)
447 mat3_to_axis_angle(parchan->rotAxis, &parchan->rotAngle, rmat3);
449 mat3_to_quat(parchan->quat, rmat3);
451 /* for size, remove rotation */
452 /* causes problems with some constraints (so apply only if needed) */
453 if (data->flag & CONSTRAINT_IK_STRETCH) {
454 if (parchan->rotmode > 0)
455 eulO_to_mat3(qrmat, parchan->eul, parchan->rotmode);
456 else if (parchan->rotmode == ROT_MODE_AXISANGLE)
457 axis_angle_to_mat3(qrmat, parchan->rotAxis, parchan->rotAngle);
459 quat_to_mat3(qrmat, parchan->quat);
461 invert_m3_m3(imat3, qrmat);
462 mul_m3_m3m3(smat, rmat3, imat3);
463 mat3_to_size(parchan->size, smat);
466 /* causes problems with some constraints (e.g. childof), so disable this */
467 /* as it is IK shouldn't affect location directly */
468 /* copy_v3_v3(parchan->loc, rmat[3]); */
474 data->flag &= ~CONSTRAINT_IK_AUTO;
481 static void add_pose_transdata(TransInfo *t, bPoseChannel *pchan, Object *ob, TransData *td)
483 Bone *bone= pchan->bone;
484 float pmat[3][3], omat[3][3];
485 float cmat[3][3], tmat[3][3];
488 copy_v3_v3(vec, pchan->pose_mat[3]);
489 copy_v3_v3(td->center, vec);
492 td->flag = TD_SELECTED;
493 if (bone->flag & BONE_HINGE_CHILD_TRANSFORM) {
494 td->flag |= TD_NOCENTER;
497 if (bone->flag & BONE_TRANSFORM_CHILD) {
498 td->flag |= TD_NOCENTER;
499 td->flag |= TD_NO_LOC;
502 td->protectflag= pchan->protectflag;
504 td->loc = pchan->loc;
505 copy_v3_v3(td->iloc, pchan->loc);
507 td->ext->size= pchan->size;
508 copy_v3_v3(td->ext->isize, pchan->size);
510 if (pchan->rotmode > 0) {
511 td->ext->rot= pchan->eul;
512 td->ext->rotAxis= NULL;
513 td->ext->rotAngle= NULL;
516 copy_v3_v3(td->ext->irot, pchan->eul);
518 else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
520 td->ext->rotAxis= pchan->rotAxis;
521 td->ext->rotAngle= &pchan->rotAngle;
524 td->ext->irotAngle= pchan->rotAngle;
525 copy_v3_v3(td->ext->irotAxis, pchan->rotAxis);
529 td->ext->rotAxis= NULL;
530 td->ext->rotAngle= NULL;
531 td->ext->quat= pchan->quat;
533 copy_qt_qt(td->ext->iquat, pchan->quat);
535 td->ext->rotOrder= pchan->rotmode;
538 /* proper way to get parent transform + own transform + constraints transform */
539 copy_m3_m4(omat, ob->obmat);
541 /* New code, using "generic" BKE_pchan_to_pose_mat(). */
543 float rotscale_mat[4][4], loc_mat[4][4];
546 BKE_pchan_to_pose_mat(pchan, rotscale_mat, loc_mat);
547 if (t->mode == TFM_TRANSLATION)
548 copy_m3_m4(pmat, loc_mat);
550 copy_m3_m4(pmat, rotscale_mat);
552 /* Grrr! Exceptional case: When translating pose bones that are either Hinge or NoLocal,
553 * and want align snapping, we just need both loc_mat and rotscale_mat.
554 * So simply always store rotscale mat in td->ext, and always use it to apply rotations...
555 * Ugly to need such hacks! :/ */
556 copy_m3_m4(rpmat, rotscale_mat);
558 if (constraints_list_needinv(t, &pchan->constraints)) {
559 copy_m3_m4(tmat, pchan->constinv);
560 invert_m3_m3(cmat, tmat);
561 mul_serie_m3(td->mtx, pmat, omat, cmat, NULL, NULL, NULL, NULL, NULL);
562 mul_serie_m3(td->ext->r_mtx, rpmat, omat, cmat, NULL,NULL,NULL,NULL,NULL);
565 mul_serie_m3(td->mtx, pmat, omat, NULL, NULL,NULL,NULL,NULL,NULL);
566 mul_serie_m3(td->ext->r_mtx, rpmat, omat, NULL, NULL,NULL,NULL,NULL,NULL);
568 invert_m3_m3(td->ext->r_smtx, td->ext->r_mtx);
571 invert_m3_m3(td->smtx, td->mtx);
573 /* exceptional case: rotate the pose bone which also applies transformation
574 * when a parentless bone has BONE_NO_LOCAL_LOCATION [] */
575 if (!ELEM(t->mode, TFM_TRANSLATION, TFM_RESIZE) && (pchan->bone->flag & BONE_NO_LOCAL_LOCATION)) {
577 /* same as td->smtx but without pchan->bone->bone_mat */
578 td->flag |= TD_PBONE_LOCAL_MTX_C;
579 mul_m3_m3m3(td->ext->l_smtx, pchan->bone->bone_mat, td->smtx);
582 td->flag |= TD_PBONE_LOCAL_MTX_P;
586 /* for axismat we use bone's own transform */
587 copy_m3_m4(pmat, pchan->pose_mat);
588 mul_m3_m3m3(td->axismtx, omat, pmat);
589 normalize_m3(td->axismtx);
591 if (t->mode==TFM_BONESIZE) {
592 bArmature *arm= t->poseobj->data;
594 if (arm->drawtype==ARM_ENVELOPE) {
596 td->val= &bone->dist;
597 td->ival= bone->dist;
600 // abusive storage of scale in the loc pointer :)
601 td->loc= &bone->xwidth;
602 copy_v3_v3(td->iloc, td->loc);
607 /* in this case we can do target-less IK grabbing */
608 if (t->mode==TFM_TRANSLATION) {
609 bKinematicConstraint *data= has_targetless_ik(pchan);
611 if (data->flag & CONSTRAINT_IK_TIP) {
612 copy_v3_v3(data->grabtarget, pchan->pose_tail);
615 copy_v3_v3(data->grabtarget, pchan->pose_head);
617 td->loc = data->grabtarget;
618 copy_v3_v3(td->iloc, td->loc);
619 data->flag |= CONSTRAINT_IK_AUTO;
621 /* only object matrix correction */
622 copy_m3_m3(td->mtx, omat);
623 invert_m3_m3(td->smtx, td->mtx);
627 /* store reference to first constraint */
628 td->con= pchan->constraints.first;
631 static void bone_children_clear_transflag(int mode, short around, ListBase *lb)
633 Bone *bone= lb->first;
635 for ( ; bone;bone= bone->next) {
636 if ((bone->flag & BONE_HINGE) && (bone->flag & BONE_CONNECTED)) {
637 bone->flag |= BONE_HINGE_CHILD_TRANSFORM;
639 else if ((bone->flag & BONE_TRANSFORM) &&
640 (mode == TFM_ROTATION || mode == TFM_TRACKBALL) &&
641 (around == V3D_LOCAL))
643 bone->flag |= BONE_TRANSFORM_CHILD;
646 bone->flag &= ~BONE_TRANSFORM;
649 bone_children_clear_transflag(mode, around, &bone->childbase);
653 /* sets transform flags in the bones
654 * returns total number of bones with BONE_TRANSFORM */
655 int count_set_pose_transflags(int *out_mode, short around, Object *ob)
657 bArmature *arm= ob->data;
660 int mode = *out_mode;
661 int hastranslation = 0;
664 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
666 if (PBONE_VISIBLE(arm, bone)) {
667 if ((bone->flag & BONE_SELECTED))
668 bone->flag |= BONE_TRANSFORM;
670 bone->flag &= ~BONE_TRANSFORM;
672 bone->flag &= ~BONE_HINGE_CHILD_TRANSFORM;
673 bone->flag &= ~BONE_TRANSFORM_CHILD;
676 bone->flag &= ~BONE_TRANSFORM;
679 /* make sure no bone can be transformed when a parent is transformed */
680 /* since pchans are depsgraph sorted, the parents are in beginning of list */
681 if (mode != TFM_BONESIZE) {
682 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
684 if (bone->flag & BONE_TRANSFORM)
685 bone_children_clear_transflag(mode, around, &bone->childbase);
688 /* now count, and check if we have autoIK or have to switch from translate to rotate */
691 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
693 if (bone->flag & BONE_TRANSFORM) {
696 if (mode == TFM_TRANSLATION) {
697 if (has_targetless_ik(pchan) == NULL) {
698 if (pchan->parent && (pchan->bone->flag & BONE_CONNECTED)) {
699 if (pchan->bone->flag & BONE_HINGE_CHILD_TRANSFORM)
702 else if ((pchan->protectflag & OB_LOCK_LOC)!=OB_LOCK_LOC)
711 /* if there are no translatable bones, do rotation */
712 if (mode == TFM_TRANSLATION && !hastranslation) {
713 *out_mode = TFM_ROTATION;
720 /* -------- Auto-IK ---------- */
722 /* adjust pose-channel's auto-ik chainlen */
723 static void pchan_autoik_adjust (bPoseChannel *pchan, short chainlen)
727 /* don't bother to search if no valid constraints */
728 if ((pchan->constflag & (PCHAN_HAS_IK|PCHAN_HAS_TARGET))==0)
731 /* check if pchan has ik-constraint */
732 for (con= pchan->constraints.first; con; con= con->next) {
733 if (con->type == CONSTRAINT_TYPE_KINEMATIC && (con->enforce!=0.0f)) {
734 bKinematicConstraint *data= con->data;
736 /* only accept if a temporary one (for auto-ik) */
737 if (data->flag & CONSTRAINT_IK_TEMP) {
738 /* chainlen is new chainlen, but is limited by maximum chainlen */
739 if ((chainlen==0) || (chainlen > data->max_rootbone))
740 data->rootbone= data->max_rootbone;
742 data->rootbone= chainlen;
748 /* change the chain-length of auto-ik */
749 void transform_autoik_update(TransInfo *t, short mode)
751 short *chainlen= &t->settings->autoik_chainlen;
754 /* mode determines what change to apply to chainlen */
756 /* mode=1 is from WHEELMOUSEDOWN... increases len */
759 else if (mode == -1) {
760 /* mode==-1 is from WHEELMOUSEUP... decreases len */
761 if (*chainlen > 0) (*chainlen)--;
764 /* sanity checks (don't assume t->poseobj is set, or that it is an armature) */
765 if (ELEM(NULL, t->poseobj, t->poseobj->pose))
768 /* apply to all pose-channels */
769 for (pchan=t->poseobj->pose->chanbase.first; pchan; pchan=pchan->next) {
770 pchan_autoik_adjust(pchan, *chainlen);
774 /* frees temporal IKs */
775 static void pose_grab_with_ik_clear(Object *ob)
777 bKinematicConstraint *data;
779 bConstraint *con, *next;
781 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
782 /* clear all temporary lock flags */
783 pchan->ikflag &= ~(BONE_IK_NO_XDOF_TEMP|BONE_IK_NO_YDOF_TEMP|BONE_IK_NO_ZDOF_TEMP);
785 pchan->constflag &= ~(PCHAN_HAS_IK|PCHAN_HAS_TARGET);
787 /* remove all temporary IK-constraints added */
788 for (con= pchan->constraints.first; con; con= next) {
790 if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
792 if (data->flag & CONSTRAINT_IK_TEMP) {
793 BLI_remlink(&pchan->constraints, con);
794 MEM_freeN(con->data);
798 pchan->constflag |= PCHAN_HAS_IK;
799 if (data->tar==NULL || (data->tar->type==OB_ARMATURE && data->subtarget[0]==0))
800 pchan->constflag |= PCHAN_HAS_TARGET;
806 /* adds the IK to pchan - returns if added */
807 static short pose_grab_with_ik_add(bPoseChannel *pchan)
809 bKinematicConstraint *targetless = NULL;
810 bKinematicConstraint *data;
817 /* Rule: not if there's already an IK on this channel */
818 for (con= pchan->constraints.first; con; con= con->next) {
819 if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
822 if (data->tar==NULL || (data->tar->type==OB_ARMATURE && data->subtarget[0]=='\0')) {
823 /* make reference to constraint to base things off later (if it's the last targetless constraint encountered) */
824 targetless = (bKinematicConstraint *)con->data;
826 /* but, if this is a targetless IK, we make it auto anyway (for the children loop) */
827 if (con->enforce!=0.0f) {
828 data->flag |= CONSTRAINT_IK_AUTO;
830 /* if no chain length has been specified, just make things obey standard rotation locks too */
831 if (data->rootbone == 0) {
832 for (; pchan; pchan=pchan->parent) {
833 /* here, we set ik-settings for bone from pchan->protectflag */
834 // XXX: careful with quats/axis-angle rotations where we're locking 4d components
835 if (pchan->protectflag & OB_LOCK_ROTX) pchan->ikflag |= BONE_IK_NO_XDOF_TEMP;
836 if (pchan->protectflag & OB_LOCK_ROTY) pchan->ikflag |= BONE_IK_NO_YDOF_TEMP;
837 if (pchan->protectflag & OB_LOCK_ROTZ) pchan->ikflag |= BONE_IK_NO_ZDOF_TEMP;
845 if ((con->flag & CONSTRAINT_DISABLE)==0 && (con->enforce!=0.0f))
850 con = add_pose_constraint(NULL, pchan, "TempConstraint", CONSTRAINT_TYPE_KINEMATIC);
851 pchan->constflag |= (PCHAN_HAS_IK|PCHAN_HAS_TARGET); /* for draw, but also for detecting while pose solving */
854 /* if exists, use values from last targetless (but disabled) IK-constraint as base */
858 data->flag= CONSTRAINT_IK_TIP;
859 data->flag |= CONSTRAINT_IK_TEMP|CONSTRAINT_IK_AUTO;
860 copy_v3_v3(data->grabtarget, pchan->pose_tail);
861 data->rootbone= 0; /* watch-it! has to be 0 here, since we're still on the same bone for the first time through the loop [#25885] */
863 /* we only include bones that are part of a continual connected chain */
865 /* here, we set ik-settings for bone from pchan->protectflag */
866 // XXX: careful with quats/axis-angle rotations where we're locking 4d components
867 if (pchan->protectflag & OB_LOCK_ROTX) pchan->ikflag |= BONE_IK_NO_XDOF_TEMP;
868 if (pchan->protectflag & OB_LOCK_ROTY) pchan->ikflag |= BONE_IK_NO_YDOF_TEMP;
869 if (pchan->protectflag & OB_LOCK_ROTZ) pchan->ikflag |= BONE_IK_NO_ZDOF_TEMP;
871 /* now we count this pchan as being included */
874 /* continue to parent, but only if we're connected to it */
875 if (pchan->bone->flag & BONE_CONNECTED)
876 pchan = pchan->parent;
881 /* make a copy of maximum chain-length */
882 data->max_rootbone= data->rootbone;
887 /* bone is a candidate to get IK, but we don't do it if it has children connected */
888 static short pose_grab_with_ik_children(bPose *pose, Bone *bone)
891 short wentdeeper=0, added=0;
893 /* go deeper if children & children are connected */
894 for (bonec= bone->childbase.first; bonec; bonec= bonec->next) {
895 if (bonec->flag & BONE_CONNECTED) {
897 added+= pose_grab_with_ik_children(pose, bonec);
901 bPoseChannel *pchan= BKE_pose_channel_find_name(pose, bone->name);
903 added+= pose_grab_with_ik_add(pchan);
909 /* main call which adds temporal IK chains */
910 static short pose_grab_with_ik(Object *ob)
913 bPoseChannel *pchan, *parent;
917 if ((ob==NULL) || (ob->pose==NULL) || (ob->mode & OB_MODE_POSE)==0)
922 /* Rule: allow multiple Bones (but they must be selected, and only one ik-solver per chain should get added) */
923 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
924 if (pchan->bone->layer & arm->layer) {
925 if (pchan->bone->flag & BONE_SELECTED) {
926 /* Rule: no IK for solitatry (unconnected) bones */
927 for (bonec=pchan->bone->childbase.first; bonec; bonec=bonec->next) {
928 if (bonec->flag & BONE_CONNECTED) {
932 if ((pchan->bone->flag & BONE_CONNECTED)==0 && (bonec == NULL))
935 /* rule: if selected Bone is not a root bone, it gets a temporal IK */
937 /* only adds if there's no IK yet (and no parent bone was selected) */
938 for (parent= pchan->parent; parent; parent= parent->parent) {
939 if (parent->bone->flag & BONE_SELECTED)
943 tot_ik += pose_grab_with_ik_add(pchan);
946 /* rule: go over the children and add IK to the tips */
947 tot_ik += pose_grab_with_ik_children(ob->pose, pchan->bone);
953 return (tot_ik) ? 1 : 0;
957 /* only called with pose mode active object now */
958 static void createTransPose(TransInfo *t, Object *ob)
963 TransDataExtension *tdx;
969 /* check validity of state */
970 arm= BKE_armature_from_object(ob);
971 if ((arm==NULL) || (ob->pose==NULL)) return;
973 if (arm->flag & ARM_RESTPOS) {
974 if (ELEM(t->mode, TFM_DUMMY, TFM_BONESIZE)==0) {
975 // XXX use transform operator reports
976 // BKE_report(op->reports, RPT_ERROR, "Can't select linked when sync selection is enabled");
981 /* do we need to add temporal IK chains? */
982 if ((arm->flag & ARM_AUTO_IK) && t->mode==TFM_TRANSLATION) {
983 ik_on= pose_grab_with_ik(ob);
984 if (ik_on) t->flag |= T_AUTOIK;
987 /* set flags and count total (warning, can change transform to rotate) */
988 t->total = count_set_pose_transflags(&t->mode, t->around, ob);
990 if (t->total == 0) return;
993 t->poseobj= ob; /* we also allow non-active objects to be transformed, in weightpaint */
995 /* init trans data */
996 td = t->data = MEM_callocN(t->total*sizeof(TransData), "TransPoseBone");
997 tdx = t->ext = MEM_callocN(t->total*sizeof(TransDataExtension), "TransPoseBoneExt");
998 for (i=0; i<t->total; i++, td++, tdx++) {
1003 /* use pose channels to fill trans data */
1005 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1006 if (pchan->bone->flag & BONE_TRANSFORM) {
1007 add_pose_transdata(t, pchan, ob, td);
1012 if (td != (t->data+t->total)) {
1013 // XXX use transform operator reports
1014 // BKE_report(op->reports, RPT_DEBUG, "Bone selection count error");
1017 /* initialize initial auto=ik chainlen's? */
1018 if (ik_on) transform_autoik_update(t, 0);
1021 /* ********************* armature ************** */
1023 static void createTransArmatureVerts(TransInfo *t)
1026 bArmature *arm= t->obedit->data;
1027 ListBase *edbo = arm->edbo;
1029 float mtx[3][3], smtx[3][3], delta[3], bonemat[3][3];
1031 /* special hack for envelope drawmode and scaling:
1032 * to allow scaling the size of the envelope around single points,
1033 * mode should become TFM_BONE_ENVELOPE in this case
1035 // TODO: maybe we need a separate hotkey for it, but this is consistent with 2.4x for now
1036 if ((t->mode == TFM_RESIZE) && (arm->drawtype==ARM_ENVELOPE))
1037 t->mode= TFM_BONE_ENVELOPE;
1040 for (ebo = edbo->first; ebo; ebo = ebo->next) {
1041 if (EBONE_VISIBLE(arm, ebo) && !(ebo->flag & BONE_EDITMODE_LOCKED)) {
1042 if (t->mode == TFM_BONESIZE) {
1043 if (ebo->flag & BONE_SELECTED)
1046 else if (t->mode == TFM_BONE_ROLL) {
1047 if (ebo->flag & BONE_SELECTED)
1051 if (ebo->flag & BONE_TIPSEL)
1053 if (ebo->flag & BONE_ROOTSEL)
1059 if (!t->total) return;
1061 copy_m3_m4(mtx, t->obedit->obmat);
1062 invert_m3_m3(smtx, mtx);
1064 td = t->data = MEM_callocN(t->total*sizeof(TransData), "TransEditBone");
1066 for (ebo = edbo->first; ebo; ebo = ebo->next) {
1067 ebo->oldlength = ebo->length; // length==0.0 on extrude, used for scaling radius of bone points
1069 if (EBONE_VISIBLE(arm, ebo) && !(ebo->flag & BONE_EDITMODE_LOCKED)) {
1070 if (t->mode==TFM_BONE_ENVELOPE) {
1071 if (ebo->flag & BONE_ROOTSEL) {
1072 td->val= &ebo->rad_head;
1075 copy_v3_v3(td->center, ebo->head);
1076 td->flag= TD_SELECTED;
1078 copy_m3_m3(td->smtx, smtx);
1079 copy_m3_m3(td->mtx, mtx);
1087 if (ebo->flag & BONE_TIPSEL) {
1088 td->val= &ebo->rad_tail;
1090 copy_v3_v3(td->center, ebo->tail);
1091 td->flag= TD_SELECTED;
1093 copy_m3_m3(td->smtx, smtx);
1094 copy_m3_m3(td->mtx, mtx);
1104 else if (t->mode==TFM_BONESIZE) {
1105 if (ebo->flag & BONE_SELECTED) {
1106 if (arm->drawtype==ARM_ENVELOPE) {
1108 td->val= &ebo->dist;
1109 td->ival= ebo->dist;
1112 // abusive storage of scale in the loc pointer :)
1113 td->loc= &ebo->xwidth;
1114 copy_v3_v3(td->iloc, td->loc);
1117 copy_v3_v3(td->center, ebo->head);
1118 td->flag= TD_SELECTED;
1120 /* use local bone matrix */
1121 sub_v3_v3v3(delta, ebo->tail, ebo->head);
1122 vec_roll_to_mat3(delta, ebo->roll, bonemat);
1123 mul_m3_m3m3(td->mtx, mtx, bonemat);
1124 invert_m3_m3(td->smtx, td->mtx);
1126 copy_m3_m3(td->axismtx, td->mtx);
1127 normalize_m3(td->axismtx);
1135 else if (t->mode==TFM_BONE_ROLL) {
1136 if (ebo->flag & BONE_SELECTED) {
1138 td->val= &(ebo->roll);
1139 td->ival= ebo->roll;
1141 copy_v3_v3(td->center, ebo->head);
1142 td->flag= TD_SELECTED;
1151 if (ebo->flag & BONE_TIPSEL) {
1152 copy_v3_v3(td->iloc, ebo->tail);
1153 copy_v3_v3(td->center, (t->around==V3D_LOCAL) ? ebo->head : td->iloc);
1155 td->flag= TD_SELECTED;
1156 if (ebo->flag & BONE_EDITMODE_LOCKED)
1157 td->protectflag = OB_LOCK_LOC|OB_LOCK_ROT|OB_LOCK_SCALE;
1159 copy_m3_m3(td->smtx, smtx);
1160 copy_m3_m3(td->mtx, mtx);
1162 sub_v3_v3v3(delta, ebo->tail, ebo->head);
1163 vec_roll_to_mat3(delta, ebo->roll, td->axismtx);
1165 if ((ebo->flag & BONE_ROOTSEL) == 0) {
1175 if (ebo->flag & BONE_ROOTSEL) {
1176 copy_v3_v3(td->iloc, ebo->head);
1177 copy_v3_v3(td->center, td->iloc);
1179 td->flag= TD_SELECTED;
1180 if (ebo->flag & BONE_EDITMODE_LOCKED)
1181 td->protectflag = OB_LOCK_LOC|OB_LOCK_ROT|OB_LOCK_SCALE;
1183 copy_m3_m3(td->smtx, smtx);
1184 copy_m3_m3(td->mtx, mtx);
1186 sub_v3_v3v3(delta, ebo->tail, ebo->head);
1187 vec_roll_to_mat3(delta, ebo->roll, td->axismtx);
1189 td->extra = ebo; /* to fix roll */
1202 /* ********************* meta elements ********* */
1204 static void createTransMBallVerts(TransInfo *t)
1206 MetaBall *mb = (MetaBall*)t->obedit->data;
1209 TransDataExtension *tx;
1210 float mtx[3][3], smtx[3][3];
1211 int count=0, countsel=0;
1212 int propmode = t->flag & T_PROP_EDIT;
1215 for (ml= mb->editelems->first; ml; ml= ml->next) {
1216 if (ml->flag & SELECT) countsel++;
1217 if (propmode) count++;
1220 /* note: in prop mode we need at least 1 selected */
1221 if (countsel==0) return;
1223 if (propmode) t->total = count;
1224 else t->total = countsel;
1226 td = t->data= MEM_callocN(t->total*sizeof(TransData), "TransObData(MBall EditMode)");
1227 tx = t->ext = MEM_callocN(t->total*sizeof(TransDataExtension), "MetaElement_TransExtension");
1229 copy_m3_m4(mtx, t->obedit->obmat);
1230 invert_m3_m3(smtx, mtx);
1232 for (ml= mb->editelems->first; ml; ml= ml->next) {
1233 if (propmode || (ml->flag & SELECT)) {
1235 copy_v3_v3(td->iloc, td->loc);
1236 copy_v3_v3(td->center, td->loc);
1238 if (ml->flag & SELECT) td->flag= TD_SELECTED | TD_USEQUAT | TD_SINGLESIZE;
1239 else td->flag= TD_USEQUAT;
1241 copy_m3_m3(td->smtx, smtx);
1242 copy_m3_m3(td->mtx, mtx);
1246 /* Radius of MetaElem (mass of MetaElem influence) */
1247 if (ml->flag & MB_SCALE_RAD) {
1256 /* expx/expy/expz determine "shape" of some MetaElem types */
1257 tx->size = &ml->expx;
1258 tx->isize[0] = ml->expx;
1259 tx->isize[1] = ml->expy;
1260 tx->isize[2] = ml->expz;
1262 /* quat is used for rotation of MetaElem */
1263 tx->quat = ml->quat;
1264 copy_qt_qt(tx->iquat, ml->quat);
1274 /* ********************* curve/surface ********* */
1276 static void calc_distanceCurveVerts(TransData *head, TransData *tail)
1278 TransData *td, *td_near = NULL;
1279 for (td = head; td<=tail; td++) {
1280 if (td->flag & TD_SELECTED) {
1286 dist = len_v3v3(td_near->center, td->center);
1287 if (dist < (td-1)->dist) {
1288 td->dist = (td-1)->dist;
1295 td->dist = MAXFLOAT;
1296 td->flag |= TD_NOTCONNECTED;
1300 for (td = tail; td>=head; td--) {
1301 if (td->flag & TD_SELECTED) {
1307 dist = len_v3v3(td_near->center, td->center);
1308 if (td->flag & TD_NOTCONNECTED || dist < td->dist || (td+1)->dist < td->dist) {
1309 td->flag &= ~TD_NOTCONNECTED;
1310 if (dist < (td+1)->dist) {
1311 td->dist = (td+1)->dist;
1321 /* Utility function for getting the handle data from bezier's */
1322 static TransDataCurveHandleFlags *initTransDataCurveHandles(TransData *td, struct BezTriple *bezt)
1324 TransDataCurveHandleFlags *hdata;
1325 td->flag |= TD_BEZTRIPLE;
1326 hdata = td->hdata = MEM_mallocN(sizeof(TransDataCurveHandleFlags), "CuHandle Data");
1327 hdata->ih1 = bezt->h1;
1328 hdata->h1 = &bezt->h1;
1329 hdata->ih2 = bezt->h2; /* in case the second is not selected */
1330 hdata->h2 = &bezt->h2;
1334 static void createTransCurveVerts(bContext *C, TransInfo *t)
1336 Object *obedit= CTX_data_edit_object(C);
1337 Curve *cu= obedit->data;
1338 TransData *td = NULL;
1342 float mtx[3][3], smtx[3][3];
1344 int count=0, countsel=0;
1345 int propmode = t->flag & T_PROP_EDIT;
1346 short hide_handles = (cu->drawflag & CU_HIDE_HANDLES);
1350 if (cu->editnurb==NULL) return;
1352 /* count total of vertices, check identical as in 2nd loop for making transdata! */
1353 nurbs= BKE_curve_editNurbs_get(cu);
1354 for (nu= nurbs->first; nu; nu= nu->next) {
1355 if (nu->type == CU_BEZIER) {
1356 for (a=0, bezt= nu->bezt; a<nu->pntsu; a++, bezt++) {
1357 if (bezt->hide==0) {
1359 if (bezt->f2 & SELECT) countsel+=3;
1360 if (propmode) count+= 3;
1363 if (bezt->f1 & SELECT) countsel++;
1364 if (bezt->f2 & SELECT) countsel++;
1365 if (bezt->f3 & SELECT) countsel++;
1366 if (propmode) count+= 3;
1372 for (a= nu->pntsu*nu->pntsv, bp= nu->bp; a>0; a--, bp++) {
1374 if (propmode) count++;
1375 if (bp->f1 & SELECT) countsel++;
1380 /* note: in prop mode we need at least 1 selected */
1381 if (countsel==0) return;
1383 if (propmode) t->total = count;
1384 else t->total = countsel;
1385 t->data= MEM_callocN(t->total*sizeof(TransData), "TransObData(Curve EditMode)");
1387 copy_m3_m4(mtx, t->obedit->obmat);
1388 invert_m3_m3(smtx, mtx);
1391 for (nu= nurbs->first; nu; nu= nu->next) {
1392 if (nu->type == CU_BEZIER) {
1393 TransData *head, *tail;
1395 for (a=0, bezt= nu->bezt; a<nu->pntsu; a++, bezt++) {
1396 if (bezt->hide==0) {
1397 TransDataCurveHandleFlags *hdata = NULL;
1400 ((bezt->f2 & SELECT) && hide_handles) ||
1401 ((bezt->f1 & SELECT) && hide_handles == 0))
1403 copy_v3_v3(td->iloc, bezt->vec[0]);
1404 td->loc= bezt->vec[0];
1405 copy_v3_v3(td->center, bezt->vec[(hide_handles || bezt->f2 & SELECT) ? 1:0]);
1407 if (bezt->f2 & SELECT) td->flag= TD_SELECTED;
1411 if (bezt->f1 & SELECT) td->flag= TD_SELECTED;
1417 hdata = initTransDataCurveHandles(td, bezt);
1419 copy_m3_m3(td->smtx, smtx);
1420 copy_m3_m3(td->mtx, mtx);
1427 /* This is the Curve Point, the other two are handles */
1428 if (propmode || (bezt->f2 & SELECT)) {
1429 copy_v3_v3(td->iloc, bezt->vec[1]);
1430 td->loc= bezt->vec[1];
1431 copy_v3_v3(td->center, td->loc);
1432 if (bezt->f2 & SELECT) td->flag= TD_SELECTED;
1436 if (t->mode==TFM_CURVE_SHRINKFATTEN) { /* || t->mode==TFM_RESIZE) {*/ /* TODO - make points scale */
1437 td->val = &(bezt->radius);
1438 td->ival = bezt->radius;
1440 else if (t->mode==TFM_TILT) {
1441 td->val = &(bezt->alfa);
1442 td->ival = bezt->alfa;
1448 copy_m3_m3(td->smtx, smtx);
1449 copy_m3_m3(td->mtx, mtx);
1451 if ((bezt->f1&SELECT)==0 && (bezt->f3&SELECT)==0)
1452 /* If the middle is selected but the sides arnt, this is needed */
1453 if (hdata==NULL) { /* if the handle was not saved by the previous handle */
1454 hdata = initTransDataCurveHandles(td, bezt);
1462 ((bezt->f2 & SELECT) && hide_handles) ||
1463 ((bezt->f3 & SELECT) && hide_handles == 0))
1465 copy_v3_v3(td->iloc, bezt->vec[2]);
1466 td->loc= bezt->vec[2];
1467 copy_v3_v3(td->center, bezt->vec[(hide_handles || bezt->f2 & SELECT) ? 1:2]);
1469 if (bezt->f2 & SELECT) td->flag= TD_SELECTED;
1473 if (bezt->f3 & SELECT) td->flag= TD_SELECTED;
1479 if (hdata==NULL) { /* if the handle was not saved by the previous handle */
1480 hdata = initTransDataCurveHandles(td, bezt);
1483 copy_m3_m3(td->smtx, smtx);
1484 copy_m3_m3(td->mtx, mtx);
1491 else if (propmode && head != tail) {
1492 calc_distanceCurveVerts(head, tail-1);
1496 if (propmode && head != tail)
1497 calc_distanceCurveVerts(head, tail-1);
1499 /* TODO - in the case of tilt and radius we can also avoid allocating the initTransDataCurveHandles
1500 * but for now just don't change handle types */
1501 if (ELEM(t->mode, TFM_CURVE_SHRINKFATTEN, TFM_TILT) == 0) {
1502 /* sets the handles based on their selection, do this after the data is copied to the TransData */
1503 BKE_nurb_handles_test(nu);
1507 TransData *head, *tail;
1509 for (a= nu->pntsu*nu->pntsv, bp= nu->bp; a>0; a--, bp++) {
1511 if (propmode || (bp->f1 & SELECT)) {
1512 copy_v3_v3(td->iloc, bp->vec);
1514 copy_v3_v3(td->center, td->loc);
1515 if (bp->f1 & SELECT) td->flag= TD_SELECTED;
1519 if (t->mode==TFM_CURVE_SHRINKFATTEN || t->mode==TFM_RESIZE) {
1520 td->val = &(bp->radius);
1521 td->ival = bp->radius;
1524 td->val = &(bp->alfa);
1525 td->ival = bp->alfa;
1528 copy_m3_m3(td->smtx, smtx);
1529 copy_m3_m3(td->mtx, mtx);
1536 else if (propmode && head != tail) {
1537 calc_distanceCurveVerts(head, tail-1);
1541 if (propmode && head != tail)
1542 calc_distanceCurveVerts(head, tail-1);
1547 /* ********************* lattice *************** */
1549 static void createTransLatticeVerts(TransInfo *t)
1551 Lattice *latt = ((Lattice*)t->obedit->data)->editlatt->latt;
1552 TransData *td = NULL;
1554 float mtx[3][3], smtx[3][3];
1556 int count=0, countsel=0;
1557 int propmode = t->flag & T_PROP_EDIT;
1560 a = latt->pntsu * latt->pntsv * latt->pntsw;
1563 if (bp->f1 & SELECT) countsel++;
1564 if (propmode) count++;
1569 /* note: in prop mode we need at least 1 selected */
1570 if (countsel==0) return;
1572 if (propmode) t->total = count;
1573 else t->total = countsel;
1574 t->data= MEM_callocN(t->total*sizeof(TransData), "TransObData(Lattice EditMode)");
1576 copy_m3_m4(mtx, t->obedit->obmat);
1577 invert_m3_m3(smtx, mtx);
1581 a = latt->pntsu * latt->pntsv * latt->pntsw;
1583 if (propmode || (bp->f1 & SELECT)) {
1585 copy_v3_v3(td->iloc, bp->vec);
1587 copy_v3_v3(td->center, td->loc);
1588 if (bp->f1 & SELECT) td->flag= TD_SELECTED;
1590 copy_m3_m3(td->smtx, smtx);
1591 copy_m3_m3(td->mtx, mtx);
1604 /* ******************* particle edit **************** */
1605 static void createTransParticleVerts(bContext *C, TransInfo *t)
1607 TransData *td = NULL;
1608 TransDataExtension *tx;
1609 Base *base = CTX_data_active_base(C);
1610 Object *ob = CTX_data_active_object(C);
1611 ParticleEditSettings *pset = PE_settings(t->scene);
1612 PTCacheEdit *edit = PE_get_current(t->scene, ob);
1613 ParticleSystem *psys = NULL;
1614 ParticleSystemModifierData *psmd = NULL;
1615 PTCacheEditPoint *point;
1616 PTCacheEditKey *key;
1618 int i, k, transformparticle;
1619 int count = 0, hasselected = 0;
1620 int propmode = t->flag & T_PROP_EDIT;
1622 if (edit==NULL || t->settings->particle.selectmode==SCE_SELECT_PATH) return;
1627 psmd = psys_get_modifier(ob, psys);
1629 base->flag |= BA_HAS_RECALC_DATA;
1631 for (i=0, point=edit->points; i<edit->totpoint; i++, point++) {
1632 point->flag &= ~PEP_TRANSFORM;
1633 transformparticle= 0;
1635 if ((point->flag & PEP_HIDE)==0) {
1636 for (k=0, key=point->keys; k<point->totkey; k++, key++) {
1637 if ((key->flag&PEK_HIDE)==0) {
1638 if (key->flag&PEK_SELECT) {
1640 transformparticle= 1;
1643 transformparticle= 1;
1648 if (transformparticle) {
1649 count += point->totkey;
1650 point->flag |= PEP_TRANSFORM;
1654 /* note: in prop mode we need at least 1 selected */
1655 if (hasselected==0) return;
1658 td = t->data = MEM_callocN(t->total * sizeof(TransData), "TransObData(Particle Mode)");
1660 if (t->mode == TFM_BAKE_TIME)
1661 tx = t->ext = MEM_callocN(t->total * sizeof(TransDataExtension), "Particle_TransExtension");
1667 invert_m4_m4(ob->imat, ob->obmat);
1669 for (i=0, point=edit->points; i<edit->totpoint; i++, point++) {
1670 TransData *head, *tail;
1673 if (!(point->flag & PEP_TRANSFORM)) continue;
1675 if (psys && !(psys->flag & PSYS_GLOBAL_HAIR))
1676 psys_mat_hair_to_global(ob, psmd->dm, psys->part->from, psys->particles + i, mat);
1678 for (k=0, key=point->keys; k<point->totkey; k++, key++) {
1679 if (key->flag & PEK_USE_WCO) {
1680 copy_v3_v3(key->world_co, key->co);
1681 mul_m4_v3(mat, key->world_co);
1682 td->loc = key->world_co;
1687 copy_v3_v3(td->iloc, td->loc);
1688 copy_v3_v3(td->center, td->loc);
1690 if (key->flag & PEK_SELECT)
1691 td->flag |= TD_SELECTED;
1693 td->flag |= TD_SKIP;
1698 /* don't allow moving roots */
1699 if (k==0 && pset->flag & PE_LOCK_FIRST && (!psys || !(psys->flag & PSYS_GLOBAL_HAIR)))
1700 td->protectflag |= OB_LOCK_LOC;
1704 if (t->mode == TFM_BAKE_TIME) {
1705 td->val = key->time;
1706 td->ival = *(key->time);
1707 /* abuse size and quat for min/max values */
1708 td->flag |= TD_NO_EXT;
1709 if (k==0) tx->size = NULL;
1710 else tx->size = (key - 1)->time;
1712 if (k == point->totkey - 1) tx->quat = NULL;
1713 else tx->quat = (key + 1)->time;
1721 if (propmode && head != tail)
1722 calc_distanceCurveVerts(head, tail - 1);
1726 void flushTransParticles(TransInfo *t)
1728 Scene *scene = t->scene;
1730 PTCacheEdit *edit = PE_get_current(scene, ob);
1731 ParticleSystem *psys = edit->psys;
1732 ParticleSystemModifierData *psmd = NULL;
1733 PTCacheEditPoint *point;
1734 PTCacheEditKey *key;
1736 float mat[4][4], imat[4][4], co[3];
1737 int i, k, propmode = t->flag & T_PROP_EDIT;
1740 psmd = psys_get_modifier(ob, psys);
1742 /* we do transform in world space, so flush world space position
1743 * back to particle local space (only for hair particles) */
1745 for (i=0, point=edit->points; i<edit->totpoint; i++, point++, td++) {
1746 if (!(point->flag & PEP_TRANSFORM)) continue;
1748 if (psys && !(psys->flag & PSYS_GLOBAL_HAIR)) {
1749 psys_mat_hair_to_global(ob, psmd->dm, psys->part->from, psys->particles + i, mat);
1750 invert_m4_m4(imat, mat);
1752 for (k=0, key=point->keys; k<point->totkey; k++, key++) {
1753 copy_v3_v3(co, key->world_co);
1754 mul_m4_v3(imat, co);
1757 /* optimization for proportional edit */
1758 if (!propmode || !compare_v3v3(key->co, co, 0.0001f)) {
1759 copy_v3_v3(key->co, co);
1760 point->flag |= PEP_EDIT_RECALC;
1765 point->flag |= PEP_EDIT_RECALC;
1768 PE_update_object(scene, OBACT, 1);
1771 /* ********************* mesh ****************** */
1773 /* proportional distance based on connectivity */
1774 #define THRESHOLDFACTOR (1.0f-0.0001f)
1776 /* I did this wrong, it should be a breadth-first search
1777 * but instead it's a depth-first search, fudged
1778 * to report shortest distances. I have no idea how fast
1779 * or slow this is. */
1780 static void editmesh_set_connectivity_distance(BMEditMesh *em, float mtx[][3], float *dists)
1782 BMVert **queue = NULL;
1783 float *dqueue = NULL;
1784 int *tots = MEM_callocN(sizeof(int)*em->bm->totvert, "tots editmesh_set_connectivity_distance");
1785 BLI_array_declare(queue);
1786 BLI_array_declare(dqueue);
1787 SmallHash svisit, *visit=&svisit;
1792 fill_vn_fl(dists, em->bm->totvert, FLT_MAX);
1794 BM_mesh_elem_index_ensure(em->bm, BM_VERT);
1796 BLI_smallhash_init(visit);
1798 BM_ITER_MESH (v, &viter, em->bm, BM_VERTS_OF_MESH) {
1799 if (BM_elem_flag_test(v, BM_ELEM_SELECT)==0 || BM_elem_flag_test(v, BM_ELEM_HIDDEN))
1803 BLI_smallhash_insert(visit, (uintptr_t)v, NULL);
1804 BLI_array_append(queue, v);
1805 BLI_array_append(dqueue, 0.0f);
1806 dists[BM_elem_index_get(v)] = 0.0f;
1810 while (start < BLI_array_count(queue)) {
1819 BM_ITER_ELEM (e, &eiter, v2, BM_EDGES_OF_VERT) {
1821 v3 = BM_edge_other_vert(e, v2);
1823 if (BM_elem_flag_test(v3, BM_ELEM_SELECT) || BM_elem_flag_test(v3, BM_ELEM_HIDDEN))
1826 sub_v3_v3v3(vec, v2->co, v3->co);
1827 mul_m3_v3(mtx, vec);
1829 d2 = d + len_v3(vec);
1831 if (dists[BM_elem_index_get(v3)] != FLT_MAX)
1832 dists[BM_elem_index_get(v3)] = MIN2(d2, dists[BM_elem_index_get(v3)]);
1834 dists[BM_elem_index_get(v3)] = d2;
1836 tots[BM_elem_index_get(v3)] = 1;
1838 if (BLI_smallhash_haskey(visit, (uintptr_t)v3))
1841 BLI_smallhash_insert(visit, (uintptr_t)v3, NULL);
1843 BLI_array_append(queue, v3);
1844 BLI_array_append(dqueue, d2);
1850 BLI_smallhash_release(visit);
1852 for (i=0; i<em->bm->totvert; i++) {
1854 dists[i] /= (float)tots[i];
1857 BLI_array_free(queue);
1858 BLI_array_free(dqueue);
1862 /* loop-in-a-loop I know, but we need it! (ton) */
1863 static void get_face_center(float cent_r[3], BMVert *eve)
1869 BM_ITER_ELEM (efa, &iter, eve, BM_FACES_OF_VERT) {
1870 if (BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
1871 BM_face_calc_center_mean(efa, cent_r);
1877 static void get_edge_center(float cent_r[3], BMVert *eve)
1882 BM_ITER_ELEM (eed, &iter, eve, BM_EDGES_OF_VERT) {
1883 if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
1884 mid_v3_v3v3(cent_r, eed->v1->co, eed->v2->co);
1890 /* way to overwrite what data is edited with transform */
1891 static void VertsToTransData(TransInfo *t, TransData *td, TransDataExtension *tx,
1892 BMEditMesh *em, BMVert *eve, float *bweight)
1896 // td->loc = key->co;
1900 copy_v3_v3(td->center, td->loc);
1902 if (t->around==V3D_LOCAL) {
1903 if (em->selectmode & SCE_SELECT_FACE)
1904 get_face_center(td->center, eve);
1905 else if (em->selectmode & SCE_SELECT_EDGE)
1906 get_edge_center(td->center, eve);
1908 copy_v3_v3(td->iloc, td->loc);
1911 copy_v3_v3(td->axismtx[2], eve->no);
1917 td->axismtx[1][2] = 0.0f;
1922 if (t->mode == TFM_BWEIGHT) {
1924 td->ival = bweight ? *(bweight) : 1.0f;
1926 else if (t->mode == TFM_SKIN_RESIZE) {
1927 MVertSkin *vs = CustomData_bmesh_get(&em->bm->vdata,
1930 /* skin node size */
1932 copy_v3_v3(tx->isize, vs->radius);
1933 tx->size = vs->radius;
1934 td->val = vs->radius;
1938 static void createTransEditVerts(bContext *C, TransInfo *t)
1940 ToolSettings *ts = CTX_data_tool_settings(C);
1941 TransData *tob = NULL;
1942 TransDataExtension *tx = NULL;
1943 BMEditMesh *em = BMEdit_FromObject(t->obedit);
1947 BMVert *eve_act = NULL;
1948 float *mappedcos = NULL, *quats= NULL;
1949 float mtx[3][3], smtx[3][3], (*defmats)[3][3] = NULL, (*defcos)[3] = NULL;
1951 int count=0, countsel=0, a, totleft;
1952 int propmode = (t->flag & T_PROP_EDIT) ? (t->flag & (T_PROP_EDIT | T_PROP_CONNECTED)) : 0;
1954 char *selstate = NULL;
1955 short selectmode = ts->selectmode;
1957 if (t->flag & T_MIRROR) {
1958 EDBM_verts_mirror_cache_begin(em, TRUE);
1962 /* edge slide forces edge select */
1963 if (t->mode == TFM_EDGE_SLIDE) {
1964 selectmode = SCE_SELECT_EDGE;
1967 /* BMESH_TODO, writing into the index values is BAD!, means we cant
1968 * use the values for vertex mirror - campbell */
1970 // transform now requires awareness for select mode, so we tag the f1 flags in verts
1971 if (selectmode & SCE_SELECT_VERTEX) {
1972 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
1973 BM_elem_flag_set(eve, BM_ELEM_TAG, BM_elem_flag_test(eve, BM_ELEM_SELECT));
1976 else if (selectmode & SCE_SELECT_EDGE) {
1979 eve = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL);
1980 for ( ; eve; eve=BM_iter_step(&iter)) BM_elem_flag_disable(eve, BM_ELEM_TAG);
1982 eed = BM_iter_new(&iter, bm, BM_EDGES_OF_MESH, NULL);
1983 for ( ; eed; eed=BM_iter_step(&iter)) {
1984 if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
1985 BM_elem_flag_enable(eed->v1, BM_ELEM_TAG);
1986 BM_elem_flag_enable(eed->v2, BM_ELEM_TAG);
1992 eve = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL);
1993 for ( ; eve; eve=BM_iter_step(&iter)) BM_elem_flag_disable(eve, BM_ELEM_TAG);
1995 efa = BM_iter_new(&iter, bm, BM_FACES_OF_MESH, NULL);
1996 for ( ; efa; efa=BM_iter_step(&iter)) {
1997 if (BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
2001 l = BM_iter_new(&liter, bm, BM_LOOPS_OF_FACE, efa);
2002 for (; l; l=BM_iter_step(&liter)) {
2003 BM_elem_flag_enable(l->v, BM_ELEM_TAG);
2009 /* now we can count. we store selection state in selstate, since
2010 * get_crazy_mapped_editverts messes up the index state of the
2012 selstate = MEM_callocN(sizeof(*selstate) * bm->totvert, __func__);
2013 eve = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL);
2014 for (a=0; eve; eve=BM_iter_step(&iter), a++) {
2015 if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
2016 if (BM_elem_flag_test(eve, BM_ELEM_TAG)) {
2020 if (propmode) count++;
2024 /* note: in prop mode we need at least 1 selected */
2025 if (countsel == 0) {
2030 if (em->bm->selected.last) {
2031 BMEditSelection *ese = em->bm->selected.last;
2032 if (ese->htype == BM_VERT) {
2033 eve_act = (BMVert *)ese->ele;
2041 /* allocating scratch arrays */
2042 if (propmode & T_PROP_CONNECTED)
2043 dists = MEM_mallocN(em->bm->totvert * sizeof(float), "scratch nears");
2045 else t->total = countsel;
2047 tob= t->data= MEM_callocN(t->total*sizeof(TransData), "TransObData(Mesh EditMode)");
2048 if (t->mode == TFM_SKIN_RESIZE) {
2049 tx = t->ext = MEM_callocN(t->total * sizeof(TransDataExtension),
2053 copy_m3_m4(mtx, t->obedit->obmat);
2054 invert_m3_m3(smtx, mtx);
2056 if (propmode & T_PROP_CONNECTED) {
2057 editmesh_set_connectivity_distance(em, mtx, dists);
2060 /* detect CrazySpace [tm] */
2061 if (modifiers_getCageIndex(t->scene, t->obedit, NULL, 1)>=0) {
2062 if (modifiers_isCorrectableDeformed(t->obedit)) {
2063 /* check if we can use deform matrices for modifier from the
2064 * start up to stack, they are more accurate than quats */
2065 totleft= editbmesh_get_first_deform_matrices(t->scene, t->obedit, em, &defmats, &defcos);
2067 /* if we still have more modifiers, also do crazyspace
2068 * correction with quats, relative to the coordinates after
2069 * the modifiers that support deform matrices (defcos) */
2071 mappedcos= crazyspace_get_mapped_editverts(t->scene, t->obedit);
2072 quats= MEM_mallocN((t->total)*sizeof(float)*4, "crazy quats");
2073 crazyspace_set_quats_editmesh(em, (float*)defcos, mappedcos, quats); /* BMESH_TODO, abuses vertex index, should use an int array */
2075 MEM_freeN(mappedcos);
2083 /* find out which half we do */
2085 eve = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL);
2086 for (a=0; eve; eve=BM_iter_step(&iter), a++) {
2087 if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN) && selstate[a] && eve->co[0]!=0.0f) {
2088 if (eve->co[0] < 0.0f) {
2097 eve = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL);
2098 for (a=0; eve; eve=BM_iter_step(&iter), a++) {
2099 if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
2100 if (propmode || selstate[a]) {
2101 float *bweight = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_BWEIGHT);
2103 VertsToTransData(t, tob, tx, em, eve, bweight);
2108 if (selstate[a]) tob->flag |= TD_SELECTED;
2111 if (eve == eve_act) tob->flag |= TD_ACTIVE;
2114 if (propmode & T_PROP_CONNECTED) {
2115 tob->dist = dists[a];
2118 tob->flag |= TD_NOTCONNECTED;
2119 tob->dist = MAXFLOAT;
2124 if (defmats || (quats && BM_elem_index_get(eve) != -1)) {
2125 float mat[3][3], qmat[3][3], imat[3][3];
2127 /* use both or either quat and defmat correction */
2128 if (quats && BM_elem_index_get(eve) != -1) {
2129 quat_to_mat3(qmat, quats + 4*BM_elem_index_get(eve));
2132 mul_serie_m3(mat, mtx, qmat, defmats[a],
2133 NULL, NULL, NULL, NULL, NULL);
2135 mul_m3_m3m3(mat, mtx, qmat);
2138 mul_m3_m3m3(mat, mtx, defmats[a]);
2140 invert_m3_m3(imat, mat);
2142 copy_m3_m3(tob->smtx, imat);
2143 copy_m3_m3(tob->mtx, mat);
2146 copy_m3_m3(tob->smtx, smtx);
2147 copy_m3_m3(tob->mtx, mtx);
2151 if ((mirror>0 && tob->iloc[0]>0.0f) || (mirror<0 && tob->iloc[0]<0.0f)) {
2152 BMVert *vmir= EDBM_verts_mirror_get(em, eve); //t->obedit, em, eve, tob->iloc, a);
2153 if (vmir && vmir != eve) {
2164 for (a = 0; a < t->total; a++, tob++ ) {
2165 if (ABS(tob->loc[0]) <= 0.00001f) {
2166 tob->flag |= TD_MIRROR_EDGE;
2172 /* crazy space free */
2180 MEM_freeN(selstate);
2182 if (t->flag & T_MIRROR) {
2183 EDBM_verts_mirror_cache_end(em);
2188 /* *** NODE EDITOR *** */
2189 void flushTransNodes(TransInfo *t)
2194 /* flush to 2d vector from internally used 3d vector */
2195 for (a=0, td= t->data2d; a<t->total; a++, td++) {
2196 td->loc2d[0]= td->loc[0];
2197 td->loc2d[1]= td->loc[1];
2200 /* handle intersection with noodles */
2202 ED_node_link_intersect_test(t->sa, 1);
2206 /* *** SEQUENCE EDITOR *** */
2208 /* commented _only_ because the meta may have animation data which
2209 * needs moving too [#28158] */
2211 #define SEQ_TX_NESTED_METAS
2213 void flushTransSeq(TransInfo *t)
2215 ListBase *seqbasep= BKE_sequencer_editing_get(t->scene, FALSE)->seqbasep; /* Editing null check already done */
2216 int a, new_frame, old_start;
2217 TransData *td= NULL;
2218 TransData2D *td2d= NULL;
2219 TransDataSeq *tdsq= NULL;
2224 /* prevent updating the same seq twice
2225 * if the transdata order is changed this will mess up
2226 * but so will TransDataSeq */
2227 Sequence *seq_prev= NULL;
2229 /* flush to 2d vector from internally used 3d vector */
2230 for (a=0, td= t->data, td2d= t->data2d; a<t->total; a++, td++, td2d++) {
2231 tdsq= (TransDataSeq *)td->extra;
2233 old_start = seq->start;
2234 new_frame= (int)floor(td2d->loc[0] + 0.5f);
2236 switch (tdsq->sel_flag) {
2238 #ifdef SEQ_TX_NESTED_METAS
2239 if ((seq->depth != 0 || seq_tx_test(seq))) /* for meta's, their children move */
2240 seq->start= new_frame - tdsq->start_offset;
2242 if (seq->type != SEQ_META && (seq->depth != 0 || seq_tx_test(seq))) /* for meta's, their children move */
2243 seq->start= new_frame - tdsq->start_offset;
2245 if (seq->depth==0) {
2246 seq->machine= (int)floor(td2d->loc[1] + 0.5f);
2247 CLAMP(seq->machine, 1, MAXSEQ);
2250 case SEQ_LEFTSEL: /* no vertical transform */
2251 seq_tx_set_final_left(seq, new_frame);
2252 seq_tx_handle_xlimits(seq, tdsq->flag&SEQ_LEFTSEL, tdsq->flag&SEQ_RIGHTSEL);
2253 seq_single_fix(seq); /* todo - move this into aftertrans update? - old seq tx needed it anyway */
2255 case SEQ_RIGHTSEL: /* no vertical transform */
2256 seq_tx_set_final_right(seq, new_frame);
2257 seq_tx_handle_xlimits(seq, tdsq->flag&SEQ_LEFTSEL, tdsq->flag&SEQ_RIGHTSEL);
2258 seq_single_fix(seq); /* todo - move this into aftertrans update? - old seq tx needed it anyway */
2262 if (seq != seq_prev) {
2263 if (seq->depth==0) {
2264 /* Calculate this strip and all nested strips
2265 * children are ALWAYS transformed first
2266 * so we don't need to do this in another loop. */
2267 calc_sequence(t->scene, seq);
2270 calc_sequence_disp(t->scene, seq);
2273 if (tdsq->sel_flag == SELECT)
2274 seq_offset_animdata(t->scene, seq, seq->start - old_start);
2280 if (ELEM(t->mode, TFM_SEQ_SLIDE, TFM_TIME_TRANSLATE)) { /* originally TFM_TIME_EXTEND, transform changes */
2281 /* Special annoying case here, need to calc metas with TFM_TIME_EXTEND only */
2283 /* calc all meta's then effects [#27953] */
2284 for (seq = seqbasep->first; seq; seq = seq->next) {
2285 if (seq->type == SEQ_META && seq->flag & SELECT) {
2286 calc_sequence(t->scene, seq);
2289 for (seq = seqbasep->first; seq; seq = seq->next) {
2290 if (seq->seq1 || seq->seq2 || seq->seq3) {
2291 calc_sequence(t->scene, seq);
2296 /* need to do the overlap check in a new loop otherwise adjacent strips
2297 * will not be updated and we'll get false positives */
2299 for (a=0, td= t->data, td2d= t->data2d; a<t->total; a++, td++, td2d++) {
2301 tdsq= (TransDataSeq *)td->extra;
2304 if (seq != seq_prev) {
2305 if (seq->depth==0) {
2306 /* test overlap, displayes red outline */
2307 seq->flag &= ~SEQ_OVERLAP;
2308 if (seq_test_overlap(seqbasep, seq)) {
2309 seq->flag |= SEQ_OVERLAP;
2317 /* ********************* UV ****************** */
2319 static void UVsToTransData(SpaceImage *sima, TransData *td, TransData2D *td2d, float *uv, int selected)
2323 ED_space_image_uv_aspect(sima, &aspx, &aspy);
2325 /* uv coords are scaled by aspects. this is needed for rotations and
2326 * proportional editing to be consistent with the stretched uv coords
2327 * that are displayed. this also means that for display and numinput,
2328 * and when the the uv coords are flushed, these are converted each time */
2329 td2d->loc[0] = uv[0]*aspx;
2330 td2d->loc[1] = uv[1]*aspy;
2331 td2d->loc[2] = 0.0f;
2335 td->loc = td2d->loc;
2336 copy_v3_v3(td->center, td->loc);
2337 copy_v3_v3(td->iloc, td->loc);
2339 memset(td->axismtx, 0, sizeof(td->axismtx));
2340 td->axismtx[2][2] = 1.0f;
2342 td->ext= NULL; td->val= NULL;
2345 td->flag |= TD_SELECTED;
2355 static void createTransUVs(bContext *C, TransInfo *t)
2357 SpaceImage *sima = CTX_wm_space_image(C);
2358 Image *ima = CTX_data_edit_image(C);
2359 Scene *scene = t->scene;
2360 TransData *td = NULL;
2361 TransData2D *td2d = NULL;
2364 BMEditMesh *em = BMEdit_FromObject(t->obedit);
2368 int count=0, countsel=0;
2369 int propmode = t->flag & T_PROP_EDIT;
2371 if (!ED_space_image_show_uvedit(sima, t->obedit)) return;
2374 BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
2375 tf= CustomData_bmesh_get(&em->bm->pdata, efa->head.data, CD_MTEXPOLY);
2377 if (!uvedit_face_visible_test(scene, ima, efa, tf)) {
2378 BM_elem_flag_disable(efa, BM_ELEM_TAG);
2382 BM_elem_flag_enable(efa, BM_ELEM_TAG);
2383 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
2384 if (uvedit_uv_select_test(em, scene, l))
2392 /* note: in prop mode we need at least 1 selected */
2393 if (countsel==0) return;
2395 t->total= (propmode)? count: countsel;
2396 t->data= MEM_callocN(t->total*sizeof(TransData), "TransObData(UV Editing)");
2397 /* for each 2d uv coord a 3d vector is allocated, so that they can be
2398 * treated just as if they were 3d verts */
2399 t->data2d= MEM_callocN(t->total*sizeof(TransData2D), "TransObData2D(UV Editing)");
2401 if (sima->flag & SI_CLIP_UV)
2402 t->flag |= T_CLIP_UV;
2407 BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
2408 if (!BM_elem_flag_test(efa, BM_ELEM_TAG))
2411 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
2412 if (!propmode && !uvedit_uv_select_test(em, scene, l))
2415 luv = CustomData_bmesh_get(&em->bm->ldata, l->head.data, CD_MLOOPUV);
2416 UVsToTransData(sima, td++, td2d++, luv->uv, uvedit_uv_select_test(em, scene, l));
2420 if (sima->flag & SI_LIVE_UNWRAP)
2421 ED_uvedit_live_unwrap_begin(t->scene, t->obedit);
2424 void flushTransUVs(TransInfo *t)
2426 SpaceImage *sima = t->sa->spacedata.first;
2428 int a, width, height;
2429 float aspx, aspy, invx, invy;
2431 ED_space_image_uv_aspect(sima, &aspx, &aspy);
2432 ED_space_image_size(sima, &width, &height);
2436 /* flush to 2d vector from internally used 3d vector */
2437 for (a=0, td= t->data2d; a<t->total; a++, td++) {
2438 td->loc2d[0]= td->loc[0]*invx;
2439 td->loc2d[1]= td->loc[1]*invy;
2441 if ((sima->flag & SI_PIXELSNAP) && (t->state != TRANS_CANCEL)) {
2442 td->loc2d[0]= (float)floor(width*td->loc2d[0] + 0.5f)/width;
2443 td->loc2d[1]= (float)floor(height*td->loc2d[1] + 0.5f)/height;
2448 int clipUVTransform(TransInfo *t, float *vec, int resize)
2451 int a, clipx=1, clipy=1;
2452 float aspx, aspy, min[2], max[2];
2454 ED_space_image_uv_aspect(t->sa->spacedata.first, &aspx, &aspy);
2455 min[0]= min[1]= 0.0f;
2456 max[0]= aspx; max[1]= aspy;
2458 for (a=0, td= t->data; a<t->total; a++, td++) {
2459 DO_MINMAX2(td->loc, min, max);
2463 if (min[0] < 0.0f && t->center[0] > 0.0f && t->center[0] < aspx*0.5f)
2464 vec[0] *= t->center[0]/(t->center[0] - min[0]);
2465 else if (max[0] > aspx && t->center[0] < aspx)
2466 vec[0] *= (t->center[0] - aspx)/(t->center[0] - max[0]);
2470 if (min[1] < 0.0f && t->center[1] > 0.0f && t->center[1] < aspy*0.5f)
2471 vec[1] *= t->center[1]/(t->center[1] - min[1]);
2472 else if (max[1] > aspy && t->center[1] < aspy)
2473 vec[1] *= (t->center[1] - aspy)/(t->center[1] - max[1]);
2480 else if (max[0] > aspx)
2481 vec[0] -= max[0]-aspx;
2487 else if (max[1] > aspy)
2488 vec[1] -= max[1]-aspy;
2493 return (clipx || clipy);
2496 /* ********************* ANIMATION EDITORS (GENERAL) ************************* */
2498 /* This function tests if a point is on the "mouse" side of the cursor/frame-marking */
2499 static short FrameOnMouseSide(char side, float frame, float cframe)
2501 /* both sides, so it doesn't matter */
2502 if (side == 'B') return 1;
2504 /* only on the named side */
2506 return (frame >= cframe) ? 1 : 0;
2508 return (frame <= cframe) ? 1 : 0;
2511 /* ********************* NLA EDITOR ************************* */
2513 static void createTransNlaData(bContext *C, TransInfo *t)
2515 Scene *scene= t->scene;
2516 SpaceNla *snla = NULL;
2517 TransData *td = NULL;
2518 TransDataNla *tdn = NULL;
2521 ListBase anim_data = {NULL, NULL};
2527 /* determine what type of data we are operating on */
2528 if (ANIM_animdata_get_context(C, &ac) == 0)
2530 snla = (SpaceNla *)ac.sl;
2533 filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_FOREDIT);
2534 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
2536 /* which side of the current frame should be allowed */
2537 if (t->mode == TFM_TIME_EXTEND) {
2538 /* only side on which mouse is gets transformed */
2539 float xmouse, ymouse;
2541 UI_view2d_region_to_view(&ac.ar->v2d, t->imval[0], t->imval[1], &xmouse, &ymouse);
2542 t->frame_side= (xmouse > CFRA) ? 'R' : 'L';
2545 /* normal transform - both sides of current frame are considered */
2546 t->frame_side = 'B';
2549 /* loop 1: count how many strips are selected (consider each strip as 2 points) */
2550 for (ale= anim_data.first; ale; ale= ale->next) {
2551 NlaTrack *nlt= (NlaTrack *)ale->data;
2554 /* make some meta-strips for chains of selected strips */
2555 BKE_nlastrips_make_metas(&nlt->strips, 1);
2557 /* only consider selected strips */
2558 for (strip= nlt->strips.first; strip; strip= strip->next) {
2559 // TODO: we can make strips have handles later on...
2560 /* transition strips can't get directly transformed */
2561 if (strip->type != NLASTRIP_TYPE_TRANSITION) {
2562 if (strip->flag & NLASTRIP_FLAG_SELECT) {
2563 if (FrameOnMouseSide(t->frame_side, strip->start, (float)CFRA)) count++;
2564 if (FrameOnMouseSide(t->frame_side, strip->end, (float)CFRA)) count++;
2570 /* stop if trying to build list if nothing selected */
2572 /* cleanup temp list */
2573 BLI_freelistN(&anim_data);
2577 /* allocate memory for data */
2580 t->data= MEM_callocN(t->total*sizeof(TransData), "TransData(NLA Editor)");
2582 t->customData= MEM_callocN(t->total*sizeof(TransDataNla), "TransDataNla (NLA Editor)");
2584 t->flag |= T_FREE_CUSTOMDATA;
2586 /* loop 2: build transdata array */
2587 for (ale= anim_data.first; ale; ale= ale->next) {
2588 /* only if a real NLA-track */
2589 if (ale->type == ANIMTYPE_NLATRACK) {
2590 AnimData *adt = ale->adt;
2591 NlaTrack *nlt= (NlaTrack *)ale->data;
2594 /* only consider selected strips */
2595 for (strip= nlt->strips.first; strip; strip= strip->next) {
2596 // TODO: we can make strips have handles later on...
2597 /* transition strips can't get directly transformed */
2598 if (strip->type != NLASTRIP_TYPE_TRANSITION) {
2599 if (strip->flag & NLASTRIP_FLAG_SELECT) {
2600 /* our transform data is constructed as follows:
2601 * - only the handles on the right side of the current-frame get included
2602 * - td structs are transform-elements operated on by the transform system
2603 * and represent a single handle. The storage/pointer used (val or loc) depends on
2604 * whether we're scaling or transforming. Ultimately though, the handles
2605 * the td writes to will simply be a dummy in tdn
2606 * - for each strip being transformed, a single tdn struct is used, so in some
2607 * cases, there will need to be 1 of these tdn elements in the array skipped...
2609 float center[3], yval;
2611 /* firstly, init tdn settings */
2613 tdn->oldTrack= tdn->nlt= nlt;
2615 tdn->trackIndex= BLI_findindex(&adt->nla_tracks, nlt);
2617 yval= (float)(tdn->trackIndex * NLACHANNEL_STEP(snla));
2619 tdn->h1[0]= strip->start;
2621 tdn->h2[0]= strip->end;
2624 center[0]= (float)CFRA;
2628 /* set td's based on which handles are applicable */
2629 if (FrameOnMouseSide(t->frame_side, strip->start, (float)CFRA)) {
2630 /* just set tdn to assume that it only has one handle for now */
2633 /* now, link the transform data up to this data */
2634 if (ELEM(t->mode, TFM_TRANSLATION, TFM_TIME_EXTEND)) {
2636 copy_v3_v3(td->iloc, tdn->h1);
2638 /* store all the other gunk that is required by transform */
2639 copy_v3_v3(td->center, center);
2640 memset(td->axismtx, 0, sizeof(td->axismtx));
2641 td->axismtx[2][2] = 1.0f;
2643 td->ext= NULL; td->val= NULL;
2645 td->flag |= TD_SELECTED;
2652 /* time scaling only needs single value */
2653 td->val= &tdn->h1[0];
2654 td->ival= tdn->h1[0];
2660 if (FrameOnMouseSide(t->frame_side, strip->end, (float)CFRA)) {
2661 /* if tdn is already holding the start handle, then we're doing both, otherwise, only end */
2662 tdn->handle= (tdn->handle) ? 2 : 1;
2664 /* now, link the transform data up to this data */
2665 if (ELEM(t->mode, TFM_TRANSLATION, TFM_TIME_EXTEND)) {
2667 copy_v3_v3(td->iloc, tdn->h2);
2669 /* store all the other gunk that is required by transform */
2670 copy_v3_v3(td->center, center);
2671 memset(td->axismtx, 0, sizeof(td->axismtx));
2672 td->axismtx[2][2] = 1.0f;
2674 td->ext= NULL; td->val= NULL;
2676 td->flag |= TD_SELECTED;
2683 /* time scaling only needs single value */
2684 td->val= &tdn->h2[0];
2685 td->ival= tdn->h2[0];
2692 /* if both handles were used, skip the next tdn (i.e. leave it blank) since the counting code is dumb...
2693 * otherwise, just advance to the next one...
2695 if (tdn->handle == 2)
2705 /* cleanup temp list */
2706 BLI_freelistN(&anim_data);
2709 /* ********************* ACTION EDITOR ****************** */
2711 /* Called by special_aftertrans_update to make sure selected gp-frames replace
2712 * any other gp-frames which may reside on that frame (that are not selected).
2713 * It also makes sure gp-frames are still stored in chronological order after
2716 static void posttrans_gpd_clean (bGPdata *gpd)
2720 for (gpl= gpd->layers.first; gpl; gpl= gpl->next) {
2721 ListBase sel_buffer = {NULL, NULL};
2722 bGPDframe *gpf, *gpfn;
2723 bGPDframe *gfs, *gfsn;
2725 /* loop 1: loop through and isolate selected gp-frames to buffer
2726 * (these need to be sorted as they are isolated)
2728 for (gpf= gpl->frames.first; gpf; gpf= gpfn) {
2732 if (gpf->flag & GP_FRAME_SELECT) {
2733 BLI_remlink(&gpl->frames, gpf);
2735 /* find place to add them in buffer
2736 * - go backwards as most frames will still be in order,
2737 * so doing it this way will be faster
2739 for (gfs= sel_buffer.last; gfs; gfs= gfs->prev) {
2740 /* if current (gpf) occurs after this one in buffer, add! */
2741 if (gfs->framenum < gpf->framenum) {
2742 BLI_insertlinkafter(&sel_buffer, gfs, gpf);
2748 BLI_addhead(&sel_buffer, gpf);
2752 /* error checking: it is unlikely, but may be possible to have none selected */
2753 if (sel_buffer.first == NULL)
2756 /* if all were selected (i.e. gpl->frames is empty), then just transfer sel-buf over */
2757 if (gpl->frames.first == NULL) {
2758 gpl->frames.first= sel_buffer.first;
2759 gpl->frames.last= sel_buffer.last;
2764 /* loop 2: remove duplicates of frames in buffers */
2765 for (gpf= gpl->frames.first; gpf && sel_buffer.first; gpf= gpfn) {
2768 /* loop through sel_buffer, emptying stuff from front of buffer if ok */
2769 for (gfs= sel_buffer.first; gfs && gpf; gfs= gfsn) {
2772 /* if this buffer frame needs to go before current, add it! */