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"
57 #include "MEM_guardedalloc.h"
60 #include "BLI_blenlib.h"
61 #include "BLI_array.h"
62 #include "BLI_utildefines.h"
63 #include "BLI_smallhash.h"
65 #include "BKE_DerivedMesh.h"
66 #include "BKE_action.h"
67 #include "BKE_armature.h"
68 #include "BKE_bmesh.h"
69 #include "BKE_constraint.h"
70 #include "BKE_context.h"
71 #include "BKE_curve.h"
72 #include "BKE_depsgraph.h"
73 #include "BKE_fcurve.h"
74 #include "BKE_global.h"
75 #include "BKE_gpencil.h"
78 #include "BKE_modifier.h"
79 #include "BKE_movieclip.h"
82 #include "BKE_object.h"
83 #include "BKE_particle.h"
84 #include "BKE_pointcache.h"
85 #include "BKE_report.h"
86 #include "BKE_scene.h"
87 #include "BKE_sequencer.h"
88 #include "BKE_tessmesh.h"
89 #include "BKE_tracking.h"
92 #include "ED_anim_api.h"
93 #include "ED_armature.h"
94 #include "ED_particle.h"
96 #include "ED_keyframing.h"
97 #include "ED_keyframes_edit.h"
98 #include "ED_object.h"
99 #include "ED_markers.h"
102 #include "ED_types.h"
103 #include "ED_uvedit.h"
105 #include "ED_util.h" /* for crazyspace correction */
107 #include "WM_api.h" /* for WM_event_add_notifier to deal with stabilization nodes */
108 #include "WM_types.h"
110 #include "UI_view2d.h"
112 #include "RNA_access.h"
114 #include "transform.h"
117 #include "BLO_sys_types.h" // for intptr_t support
119 /* local function prototype - for Object/Bone Constraints */
120 static short constraints_list_needinv(TransInfo *t, ListBase *list);
122 /* ************************** Functions *************************** */
124 static void qsort_trans_data(TransInfo *t, TransData *head, TransData *tail, TransData *temp)
126 TransData *ihead = head;
127 TransData *itail = tail;
132 if (t->flag & T_PROP_CONNECTED) {
133 while ((tail->dist >= temp->dist) && (head < tail))
137 while ((tail->rdist >= temp->rdist) && (head < tail))
147 if (t->flag & T_PROP_CONNECTED) {
148 while ((head->dist <= temp->dist) && (head < tail))
152 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 (give_obdata_texspace(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 /* XXX Old code. Will remove it later. */
432 if (parchan->parent) {
433 Bone *parbone= parchan->parent->bone;
434 float offs_bone[4][4];
436 /* offs_bone = yoffs(b-1) + root(b) + bonemat(b) */
437 copy_m4_m3(offs_bone, bone->bone_mat);
439 /* The bone's root offset (is in the parent's coordinate system) */
440 copy_v3_v3(offs_bone[3], bone->head);
442 /* Get the length translation of parent (length along y axis) */
443 offs_bone[3][1]+= parbone->length;
445 /* pose_mat(b-1) * offs_bone */
446 if (parchan->bone->flag & BONE_HINGE) {
447 /* the rotation of the parent restposition */
448 copy_m4_m4(rmat, parbone->arm_mat); /* rmat used as temp */
450 /* the location of actual parent transform */
451 copy_v3_v3(rmat[3], offs_bone[3]);
452 offs_bone[3][0]= offs_bone[3][1]= offs_bone[3][2]= 0.0f;
453 mul_m4_v3(parchan->parent->pose_mat, rmat[3]);
455 mult_m4_m4m4(tmat, rmat, offs_bone);
457 else if (parchan->bone->flag & BONE_NO_SCALE) {
458 mult_m4_m4m4(tmat, parchan->parent->pose_mat, offs_bone);
462 mult_m4_m4m4(tmat, parchan->parent->pose_mat, offs_bone);
464 invert_m4_m4(imat, tmat);
467 copy_m4_m3(tmat, bone->bone_mat);
469 copy_v3_v3(tmat[3], bone->head);
470 invert_m4_m4(imat, tmat);
473 mult_m4_m4m4(rmat, imat, parchan->pose_mat);
475 armature_mat_pose_to_bone(parchan, parchan->pose_mat, rmat);
477 /* apply and decompose, doesn't work for constraints or non-uniform scale well */
479 float rmat3[3][3], qrmat[3][3], imat3[3][3], smat[3][3];
481 copy_m3_m4(rmat3, rmat);
484 /* [#22409] is partially caused by this, as slight numeric error introduced during
485 * the solving process leads to locked-axis values changing. However, we cannot modify
486 * the values here, or else there are huge discreptancies between IK-solver (interactive)
489 if (parchan->rotmode > 0)
490 mat3_to_eulO(parchan->eul, parchan->rotmode,rmat3);
491 else if (parchan->rotmode == ROT_MODE_AXISANGLE)
492 mat3_to_axis_angle(parchan->rotAxis, &parchan->rotAngle,rmat3);
494 mat3_to_quat(parchan->quat,rmat3);
496 /* for size, remove rotation */
497 /* causes problems with some constraints (so apply only if needed) */
498 if (data->flag & CONSTRAINT_IK_STRETCH) {
499 if (parchan->rotmode > 0)
500 eulO_to_mat3( qrmat,parchan->eul, parchan->rotmode);
501 else if (parchan->rotmode == ROT_MODE_AXISANGLE)
502 axis_angle_to_mat3( qrmat,parchan->rotAxis, parchan->rotAngle);
504 quat_to_mat3( qrmat,parchan->quat);
506 invert_m3_m3(imat3, qrmat);
507 mul_m3_m3m3(smat, rmat3, imat3);
508 mat3_to_size( parchan->size,smat);
511 /* causes problems with some constraints (e.g. childof), so disable this */
512 /* as it is IK shouldn't affect location directly */
513 /* copy_v3_v3(parchan->loc, rmat[3]); */
519 data->flag &= ~CONSTRAINT_IK_AUTO;
526 static void add_pose_transdata(TransInfo *t, bPoseChannel *pchan, Object *ob, TransData *td)
528 Bone *bone= pchan->bone;
529 float pmat[3][3], omat[3][3];
530 float cmat[3][3], tmat[3][3];
533 copy_v3_v3(vec, pchan->pose_mat[3]);
534 copy_v3_v3(td->center, vec);
537 td->flag = TD_SELECTED;
538 if (bone->flag & BONE_HINGE_CHILD_TRANSFORM)
540 td->flag |= TD_NOCENTER;
543 if (bone->flag & BONE_TRANSFORM_CHILD)
545 td->flag |= TD_NOCENTER;
546 td->flag |= TD_NO_LOC;
549 td->protectflag= pchan->protectflag;
551 td->loc = pchan->loc;
552 copy_v3_v3(td->iloc, pchan->loc);
554 td->ext->size= pchan->size;
555 copy_v3_v3(td->ext->isize, pchan->size);
557 if (pchan->rotmode > 0) {
558 td->ext->rot= pchan->eul;
559 td->ext->rotAxis= NULL;
560 td->ext->rotAngle= NULL;
563 copy_v3_v3(td->ext->irot, pchan->eul);
565 else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
567 td->ext->rotAxis= pchan->rotAxis;
568 td->ext->rotAngle= &pchan->rotAngle;
571 td->ext->irotAngle= pchan->rotAngle;
572 copy_v3_v3(td->ext->irotAxis, pchan->rotAxis);
576 td->ext->rotAxis= NULL;
577 td->ext->rotAngle= NULL;
578 td->ext->quat= pchan->quat;
580 copy_qt_qt(td->ext->iquat, pchan->quat);
582 td->ext->rotOrder= pchan->rotmode;
585 /* proper way to get parent transform + own transform + constraints transform */
586 copy_m3_m4(omat, ob->obmat);
588 /* New code, using "generic" pchan_to_pose_mat(). */
590 float rotscale_mat[4][4], loc_mat[4][4];
592 pchan_to_pose_mat(pchan, rotscale_mat, loc_mat);
593 if (t->mode == TFM_TRANSLATION)
594 copy_m3_m4(pmat, loc_mat);
596 copy_m3_m4(pmat, rotscale_mat);
598 if (constraints_list_needinv(t, &pchan->constraints)) {
599 copy_m3_m4(tmat, pchan->constinv);
600 invert_m3_m3(cmat, tmat);
601 mul_serie_m3(td->mtx, pmat, omat, cmat, NULL,NULL,NULL,NULL,NULL);
604 mul_serie_m3(td->mtx, pmat, omat, NULL, NULL,NULL,NULL,NULL,NULL);
607 /* XXX Old code. Will remove it later. */
609 if (ELEM(t->mode, TFM_TRANSLATION, TFM_RESIZE) && (pchan->bone->flag & BONE_NO_LOCAL_LOCATION))
612 copy_m3_m3(bmat, pchan->bone->bone_mat);
615 if (pchan->bone->flag & BONE_HINGE) {
616 copy_m3_m4(pmat, pchan->parent->bone->arm_mat);
617 if (!(pchan->bone->flag & BONE_NO_SCALE)) {
618 float tsize[3], tsmat[3][3];
619 mat4_to_size(tsize, pchan->parent->pose_mat);
620 size_to_mat3(tsmat, tsize);
621 mul_m3_m3m3(pmat, tsmat, pmat);
625 copy_m3_m4(pmat, pchan->parent->pose_mat);
626 if (pchan->bone->flag & BONE_NO_SCALE)
630 if (constraints_list_needinv(t, &pchan->constraints)) {
631 copy_m3_m4(tmat, pchan->constinv);
632 invert_m3_m3(cmat, tmat);
633 mul_serie_m3(td->mtx, bmat, pmat, omat, cmat, NULL,NULL,NULL,NULL);
636 mul_serie_m3(td->mtx, bmat, pmat, omat, NULL,NULL,NULL,NULL,NULL);
639 if (constraints_list_needinv(t, &pchan->constraints)) {
640 copy_m3_m4(tmat, pchan->constinv);
641 invert_m3_m3(cmat, tmat);
642 mul_serie_m3(td->mtx, bmat, omat, cmat, NULL,NULL,NULL,NULL,NULL);
645 mul_m3_m3m3(td->mtx, omat, bmat);
649 invert_m3_m3(td->smtx, td->mtx);
651 /* exceptional case: rotate the pose bone which also applies transformation
652 * when a parentless bone has BONE_NO_LOCAL_LOCATION [] */
653 if (!ELEM(t->mode, TFM_TRANSLATION, TFM_RESIZE) && (pchan->bone->flag & BONE_NO_LOCAL_LOCATION)) {
655 /* same as td->smtx but without pchan->bone->bone_mat */
656 td->flag |= TD_PBONE_LOCAL_MTX_C;
657 mul_m3_m3m3(td->ext->l_smtx, pchan->bone->bone_mat, td->smtx);
660 td->flag |= TD_PBONE_LOCAL_MTX_P;
664 /* for axismat we use bone's own transform */
665 copy_m3_m4(pmat, pchan->pose_mat);
666 mul_m3_m3m3(td->axismtx, omat, pmat);
667 normalize_m3(td->axismtx);
669 if (t->mode==TFM_BONESIZE) {
670 bArmature *arm= t->poseobj->data;
672 if (arm->drawtype==ARM_ENVELOPE) {
674 td->val= &bone->dist;
675 td->ival= bone->dist;
678 // abusive storage of scale in the loc pointer :)
679 td->loc= &bone->xwidth;
680 copy_v3_v3(td->iloc, td->loc);
685 /* in this case we can do target-less IK grabbing */
686 if (t->mode==TFM_TRANSLATION) {
687 bKinematicConstraint *data= has_targetless_ik(pchan);
689 if (data->flag & CONSTRAINT_IK_TIP) {
690 copy_v3_v3(data->grabtarget, pchan->pose_tail);
693 copy_v3_v3(data->grabtarget, pchan->pose_head);
695 td->loc = data->grabtarget;
696 copy_v3_v3(td->iloc, td->loc);
697 data->flag |= CONSTRAINT_IK_AUTO;
699 /* only object matrix correction */
700 copy_m3_m3(td->mtx, omat);
701 invert_m3_m3(td->smtx, td->mtx);
705 /* store reference to first constraint */
706 td->con= pchan->constraints.first;
709 static void bone_children_clear_transflag(int mode, short around, ListBase *lb)
711 Bone *bone= lb->first;
713 for (;bone;bone= bone->next) {
714 if ((bone->flag & BONE_HINGE) && (bone->flag & BONE_CONNECTED))
716 bone->flag |= BONE_HINGE_CHILD_TRANSFORM;
718 else if ((bone->flag & BONE_TRANSFORM) &&
719 (mode == TFM_ROTATION || mode == TFM_TRACKBALL) &&
720 (around == V3D_LOCAL))
722 bone->flag |= BONE_TRANSFORM_CHILD;
725 bone->flag &= ~BONE_TRANSFORM;
728 bone_children_clear_transflag(mode, around, &bone->childbase);
732 /* sets transform flags in the bones
733 * returns total number of bones with BONE_TRANSFORM */
734 int count_set_pose_transflags(int *out_mode, short around, Object *ob)
736 bArmature *arm= ob->data;
739 int mode = *out_mode;
740 int hastranslation = 0;
743 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
745 if (PBONE_VISIBLE(arm, bone)) {
746 if ((bone->flag & BONE_SELECTED))
747 bone->flag |= BONE_TRANSFORM;
749 bone->flag &= ~BONE_TRANSFORM;
751 bone->flag &= ~BONE_HINGE_CHILD_TRANSFORM;
752 bone->flag &= ~BONE_TRANSFORM_CHILD;
755 bone->flag &= ~BONE_TRANSFORM;
758 /* make sure no bone can be transformed when a parent is transformed */
759 /* since pchans are depsgraph sorted, the parents are in beginning of list */
760 if (mode != TFM_BONESIZE) {
761 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
763 if (bone->flag & BONE_TRANSFORM)
764 bone_children_clear_transflag(mode, around, &bone->childbase);
767 /* now count, and check if we have autoIK or have to switch from translate to rotate */
770 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
772 if (bone->flag & BONE_TRANSFORM) {
775 if (mode == TFM_TRANSLATION) {
776 if ( has_targetless_ik(pchan)==NULL ) {
777 if (pchan->parent && (pchan->bone->flag & BONE_CONNECTED)) {
778 if (pchan->bone->flag & BONE_HINGE_CHILD_TRANSFORM)
781 else if ((pchan->protectflag & OB_LOCK_LOC)!=OB_LOCK_LOC)
790 /* if there are no translatable bones, do rotation */
791 if (mode == TFM_TRANSLATION && !hastranslation)
793 *out_mode = TFM_ROTATION;
800 /* -------- Auto-IK ---------- */
802 /* adjust pose-channel's auto-ik chainlen */
803 static void pchan_autoik_adjust (bPoseChannel *pchan, short chainlen)
807 /* don't bother to search if no valid constraints */
808 if ((pchan->constflag & (PCHAN_HAS_IK|PCHAN_HAS_TARGET))==0)
811 /* check if pchan has ik-constraint */
812 for (con= pchan->constraints.first; con; con= con->next) {
813 if (con->type == CONSTRAINT_TYPE_KINEMATIC && (con->enforce!=0.0f)) {
814 bKinematicConstraint *data= con->data;
816 /* only accept if a temporary one (for auto-ik) */
817 if (data->flag & CONSTRAINT_IK_TEMP) {
818 /* chainlen is new chainlen, but is limited by maximum chainlen */
819 if ((chainlen==0) || (chainlen > data->max_rootbone))
820 data->rootbone= data->max_rootbone;
822 data->rootbone= chainlen;
828 /* change the chain-length of auto-ik */
829 void transform_autoik_update (TransInfo *t, short mode)
831 short *chainlen= &t->settings->autoik_chainlen;
834 /* mode determines what change to apply to chainlen */
836 /* mode=1 is from WHEELMOUSEDOWN... increases len */
839 else if (mode == -1) {
840 /* mode==-1 is from WHEELMOUSEUP... decreases len */
841 if (*chainlen > 0) (*chainlen)--;
844 /* sanity checks (don't assume t->poseobj is set, or that it is an armature) */
845 if (ELEM(NULL, t->poseobj, t->poseobj->pose))
848 /* apply to all pose-channels */
849 for (pchan=t->poseobj->pose->chanbase.first; pchan; pchan=pchan->next) {
850 pchan_autoik_adjust(pchan, *chainlen);
854 /* frees temporal IKs */
855 static void pose_grab_with_ik_clear(Object *ob)
857 bKinematicConstraint *data;
859 bConstraint *con, *next;
861 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
862 /* clear all temporary lock flags */
863 pchan->ikflag &= ~(BONE_IK_NO_XDOF_TEMP|BONE_IK_NO_YDOF_TEMP|BONE_IK_NO_ZDOF_TEMP);
865 pchan->constflag &= ~(PCHAN_HAS_IK|PCHAN_HAS_TARGET);
867 /* remove all temporary IK-constraints added */
868 for (con= pchan->constraints.first; con; con= next) {
870 if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
872 if (data->flag & CONSTRAINT_IK_TEMP) {
873 BLI_remlink(&pchan->constraints, con);
874 MEM_freeN(con->data);
878 pchan->constflag |= PCHAN_HAS_IK;
879 if (data->tar==NULL || (data->tar->type==OB_ARMATURE && data->subtarget[0]==0))
880 pchan->constflag |= PCHAN_HAS_TARGET;
886 /* adds the IK to pchan - returns if added */
887 static short pose_grab_with_ik_add(bPoseChannel *pchan)
889 bKinematicConstraint *targetless = NULL;
890 bKinematicConstraint *data;
897 /* Rule: not if there's already an IK on this channel */
898 for (con= pchan->constraints.first; con; con= con->next) {
899 if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
902 if (data->tar==NULL || (data->tar->type==OB_ARMATURE && data->subtarget[0]=='\0')) {
903 /* make reference to constraint to base things off later (if it's the last targetless constraint encountered) */
904 targetless = (bKinematicConstraint *)con->data;
906 /* but, if this is a targetless IK, we make it auto anyway (for the children loop) */
907 if (con->enforce!=0.0f) {
908 data->flag |= CONSTRAINT_IK_AUTO;
910 /* if no chain length has been specified, just make things obey standard rotation locks too */
911 if (data->rootbone == 0) {
912 for (; pchan; pchan=pchan->parent) {
913 /* here, we set ik-settings for bone from pchan->protectflag */
914 // XXX: careful with quats/axis-angle rotations where we're locking 4d components
915 if (pchan->protectflag & OB_LOCK_ROTX) pchan->ikflag |= BONE_IK_NO_XDOF_TEMP;
916 if (pchan->protectflag & OB_LOCK_ROTY) pchan->ikflag |= BONE_IK_NO_YDOF_TEMP;
917 if (pchan->protectflag & OB_LOCK_ROTZ) pchan->ikflag |= BONE_IK_NO_ZDOF_TEMP;
925 if ((con->flag & CONSTRAINT_DISABLE)==0 && (con->enforce!=0.0f))
930 con = add_pose_constraint(NULL, pchan, "TempConstraint", CONSTRAINT_TYPE_KINEMATIC);
931 pchan->constflag |= (PCHAN_HAS_IK|PCHAN_HAS_TARGET); /* for draw, but also for detecting while pose solving */
934 /* if exists, use values from last targetless (but disabled) IK-constraint as base */
938 data->flag= CONSTRAINT_IK_TIP;
939 data->flag |= CONSTRAINT_IK_TEMP|CONSTRAINT_IK_AUTO;
940 copy_v3_v3(data->grabtarget, pchan->pose_tail);
941 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] */
943 /* we only include bones that are part of a continual connected chain */
945 /* here, we set ik-settings for bone from pchan->protectflag */
946 // XXX: careful with quats/axis-angle rotations where we're locking 4d components
947 if (pchan->protectflag & OB_LOCK_ROTX) pchan->ikflag |= BONE_IK_NO_XDOF_TEMP;
948 if (pchan->protectflag & OB_LOCK_ROTY) pchan->ikflag |= BONE_IK_NO_YDOF_TEMP;
949 if (pchan->protectflag & OB_LOCK_ROTZ) pchan->ikflag |= BONE_IK_NO_ZDOF_TEMP;
951 /* now we count this pchan as being included */
954 /* continue to parent, but only if we're connected to it */
955 if (pchan->bone->flag & BONE_CONNECTED)
956 pchan = pchan->parent;
961 /* make a copy of maximum chain-length */
962 data->max_rootbone= data->rootbone;
967 /* bone is a candidate to get IK, but we don't do it if it has children connected */
968 static short pose_grab_with_ik_children(bPose *pose, Bone *bone)
971 short wentdeeper=0, added=0;
973 /* go deeper if children & children are connected */
974 for (bonec= bone->childbase.first; bonec; bonec= bonec->next) {
975 if (bonec->flag & BONE_CONNECTED) {
977 added+= pose_grab_with_ik_children(pose, bonec);
981 bPoseChannel *pchan= get_pose_channel(pose, bone->name);
983 added+= pose_grab_with_ik_add(pchan);
989 /* main call which adds temporal IK chains */
990 static short pose_grab_with_ik(Object *ob)
993 bPoseChannel *pchan, *parent;
997 if ((ob==NULL) || (ob->pose==NULL) || (ob->mode & OB_MODE_POSE)==0)
1002 /* Rule: allow multiple Bones (but they must be selected, and only one ik-solver per chain should get added) */
1003 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1004 if (pchan->bone->layer & arm->layer) {
1005 if (pchan->bone->flag & BONE_SELECTED) {
1006 /* Rule: no IK for solitatry (unconnected) bones */
1007 for (bonec=pchan->bone->childbase.first; bonec; bonec=bonec->next) {
1008 if (bonec->flag & BONE_CONNECTED) {
1012 if ((pchan->bone->flag & BONE_CONNECTED)==0 && (bonec == NULL))
1015 /* rule: if selected Bone is not a root bone, it gets a temporal IK */
1016 if (pchan->parent) {
1017 /* only adds if there's no IK yet (and no parent bone was selected) */
1018 for (parent= pchan->parent; parent; parent= parent->parent) {
1019 if (parent->bone->flag & BONE_SELECTED)
1023 tot_ik += pose_grab_with_ik_add(pchan);
1026 /* rule: go over the children and add IK to the tips */
1027 tot_ik += pose_grab_with_ik_children(ob->pose, pchan->bone);
1033 return (tot_ik) ? 1 : 0;
1037 /* only called with pose mode active object now */
1038 static void createTransPose(TransInfo *t, Object *ob)
1041 bPoseChannel *pchan;
1043 TransDataExtension *tdx;
1049 /* check validity of state */
1050 arm= get_armature(ob);
1051 if ((arm==NULL) || (ob->pose==NULL)) return;
1053 if (arm->flag & ARM_RESTPOS) {
1054 if (ELEM(t->mode, TFM_DUMMY, TFM_BONESIZE)==0) {
1055 // XXX use transform operator reports
1056 // BKE_report(op->reports, RPT_ERROR, "Can't select linked when sync selection is enabled");
1061 /* do we need to add temporal IK chains? */
1062 if ((arm->flag & ARM_AUTO_IK) && t->mode==TFM_TRANSLATION) {
1063 ik_on= pose_grab_with_ik(ob);
1064 if (ik_on) t->flag |= T_AUTOIK;
1067 /* set flags and count total (warning, can change transform to rotate) */
1068 t->total = count_set_pose_transflags(&t->mode, t->around, ob);
1070 if (t->total == 0) return;
1073 t->poseobj= ob; /* we also allow non-active objects to be transformed, in weightpaint */
1075 /* init trans data */
1076 td = t->data = MEM_callocN(t->total*sizeof(TransData), "TransPoseBone");
1077 tdx = t->ext = MEM_callocN(t->total*sizeof(TransDataExtension), "TransPoseBoneExt");
1078 for (i=0; i<t->total; i++, td++, tdx++) {
1083 /* use pose channels to fill trans data */
1085 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1086 if (pchan->bone->flag & BONE_TRANSFORM) {
1087 add_pose_transdata(t, pchan, ob, td);
1092 if (td != (t->data+t->total)) {
1093 // XXX use transform operator reports
1094 // BKE_report(op->reports, RPT_DEBUG, "Bone selection count error");
1097 /* initialize initial auto=ik chainlen's? */
1098 if (ik_on) transform_autoik_update(t, 0);
1101 /* ********************* armature ************** */
1103 static void createTransArmatureVerts(TransInfo *t)
1106 bArmature *arm= t->obedit->data;
1107 ListBase *edbo = arm->edbo;
1109 float mtx[3][3], smtx[3][3], delta[3], bonemat[3][3];
1111 /* special hack for envelope drawmode and scaling:
1112 * to allow scaling the size of the envelope around single points,
1113 * mode should become TFM_BONE_ENVELOPE in this case
1115 // TODO: maybe we need a separate hotkey for it, but this is consistent with 2.4x for now
1116 if ((t->mode == TFM_RESIZE) && (arm->drawtype==ARM_ENVELOPE))
1117 t->mode= TFM_BONE_ENVELOPE;
1120 for (ebo = edbo->first; ebo; ebo = ebo->next)
1122 if (EBONE_VISIBLE(arm, ebo) && !(ebo->flag & BONE_EDITMODE_LOCKED))
1124 if (t->mode==TFM_BONESIZE)
1126 if (ebo->flag & BONE_SELECTED)
1129 else if (t->mode==TFM_BONE_ROLL) {
1130 if (ebo->flag & BONE_SELECTED)
1134 if (ebo->flag & BONE_TIPSEL)
1136 if (ebo->flag & BONE_ROOTSEL)
1142 if (!t->total) return;
1144 copy_m3_m4(mtx, t->obedit->obmat);
1145 invert_m3_m3(smtx, mtx);
1147 td = t->data = MEM_callocN(t->total*sizeof(TransData), "TransEditBone");
1149 for (ebo = edbo->first; ebo; ebo = ebo->next)
1151 ebo->oldlength = ebo->length; // length==0.0 on extrude, used for scaling radius of bone points
1153 if (EBONE_VISIBLE(arm, ebo) && !(ebo->flag & BONE_EDITMODE_LOCKED))
1155 if (t->mode==TFM_BONE_ENVELOPE)
1157 if (ebo->flag & BONE_ROOTSEL)
1159 td->val= &ebo->rad_head;
1162 copy_v3_v3(td->center, ebo->head);
1163 td->flag= TD_SELECTED;
1165 copy_m3_m3(td->smtx, smtx);
1166 copy_m3_m3(td->mtx, mtx);
1174 if (ebo->flag & BONE_TIPSEL)
1176 td->val= &ebo->rad_tail;
1178 copy_v3_v3(td->center, ebo->tail);
1179 td->flag= TD_SELECTED;
1181 copy_m3_m3(td->smtx, smtx);
1182 copy_m3_m3(td->mtx, mtx);
1192 else if (t->mode==TFM_BONESIZE) {
1193 if (ebo->flag & BONE_SELECTED) {
1194 if (arm->drawtype==ARM_ENVELOPE)
1197 td->val= &ebo->dist;
1198 td->ival= ebo->dist;
1201 // abusive storage of scale in the loc pointer :)
1202 td->loc= &ebo->xwidth;
1203 copy_v3_v3(td->iloc, td->loc);
1206 copy_v3_v3(td->center, ebo->head);
1207 td->flag= TD_SELECTED;
1209 /* use local bone matrix */
1210 sub_v3_v3v3(delta, ebo->tail, ebo->head);
1211 vec_roll_to_mat3(delta, ebo->roll, bonemat);
1212 mul_m3_m3m3(td->mtx, mtx, bonemat);
1213 invert_m3_m3(td->smtx, td->mtx);
1215 copy_m3_m3(td->axismtx, td->mtx);
1216 normalize_m3(td->axismtx);
1224 else if (t->mode==TFM_BONE_ROLL) {
1225 if (ebo->flag & BONE_SELECTED)
1228 td->val= &(ebo->roll);
1229 td->ival= ebo->roll;
1231 copy_v3_v3(td->center, ebo->head);
1232 td->flag= TD_SELECTED;
1241 if (ebo->flag & BONE_TIPSEL)
1243 copy_v3_v3(td->iloc, ebo->tail);
1244 copy_v3_v3(td->center, (t->around==V3D_LOCAL) ? ebo->head : td->iloc);
1246 td->flag= TD_SELECTED;
1247 if (ebo->flag & BONE_EDITMODE_LOCKED)
1248 td->protectflag = OB_LOCK_LOC|OB_LOCK_ROT|OB_LOCK_SCALE;
1250 copy_m3_m3(td->smtx, smtx);
1251 copy_m3_m3(td->mtx, mtx);
1253 sub_v3_v3v3(delta, ebo->tail, ebo->head);
1254 vec_roll_to_mat3(delta, ebo->roll, td->axismtx);
1256 if ((ebo->flag & BONE_ROOTSEL) == 0)
1267 if (ebo->flag & BONE_ROOTSEL)
1269 copy_v3_v3(td->iloc, ebo->head);
1270 copy_v3_v3(td->center, td->iloc);
1272 td->flag= TD_SELECTED;
1273 if (ebo->flag & BONE_EDITMODE_LOCKED)
1274 td->protectflag = OB_LOCK_LOC|OB_LOCK_ROT|OB_LOCK_SCALE;
1276 copy_m3_m3(td->smtx, smtx);
1277 copy_m3_m3(td->mtx, mtx);
1279 sub_v3_v3v3(delta, ebo->tail, ebo->head);
1280 vec_roll_to_mat3(delta, ebo->roll, td->axismtx);
1282 td->extra = ebo; /* to fix roll */
1295 /* ********************* meta elements ********* */
1297 static void createTransMBallVerts(TransInfo *t)
1299 MetaBall *mb = (MetaBall*)t->obedit->data;
1302 TransDataExtension *tx;
1303 float mtx[3][3], smtx[3][3];
1304 int count=0, countsel=0;
1305 int propmode = t->flag & T_PROP_EDIT;
1308 for (ml= mb->editelems->first; ml; ml= ml->next) {
1309 if (ml->flag & SELECT) countsel++;
1310 if (propmode) count++;
1313 /* note: in prop mode we need at least 1 selected */
1314 if (countsel==0) return;
1316 if (propmode) t->total = count;
1317 else t->total = countsel;
1319 td = t->data= MEM_callocN(t->total*sizeof(TransData), "TransObData(MBall EditMode)");
1320 tx = t->ext = MEM_callocN(t->total*sizeof(TransDataExtension), "MetaElement_TransExtension");
1322 copy_m3_m4(mtx, t->obedit->obmat);
1323 invert_m3_m3(smtx, mtx);
1325 for (ml= mb->editelems->first; ml; ml= ml->next) {
1326 if (propmode || (ml->flag & SELECT)) {
1328 copy_v3_v3(td->iloc, td->loc);
1329 copy_v3_v3(td->center, td->loc);
1331 if (ml->flag & SELECT) td->flag= TD_SELECTED | TD_USEQUAT | TD_SINGLESIZE;
1332 else td->flag= TD_USEQUAT;
1334 copy_m3_m3(td->smtx, smtx);
1335 copy_m3_m3(td->mtx, mtx);
1339 /* Radius of MetaElem (mass of MetaElem influence) */
1340 if (ml->flag & MB_SCALE_RAD) {
1349 /* expx/expy/expz determine "shape" of some MetaElem types */
1350 tx->size = &ml->expx;
1351 tx->isize[0] = ml->expx;
1352 tx->isize[1] = ml->expy;
1353 tx->isize[2] = ml->expz;
1355 /* quat is used for rotation of MetaElem */
1356 tx->quat = ml->quat;
1357 copy_qt_qt(tx->iquat, ml->quat);
1367 /* ********************* curve/surface ********* */
1369 static void calc_distanceCurveVerts(TransData *head, TransData *tail)
1371 TransData *td, *td_near = NULL;
1372 for (td = head; td<=tail; td++) {
1373 if (td->flag & TD_SELECTED) {
1379 dist = len_v3v3(td_near->center, td->center);
1380 if (dist < (td-1)->dist) {
1381 td->dist = (td-1)->dist;
1388 td->dist = MAXFLOAT;
1389 td->flag |= TD_NOTCONNECTED;
1393 for (td = tail; td>=head; td--) {
1394 if (td->flag & TD_SELECTED) {
1400 dist = len_v3v3(td_near->center, td->center);
1401 if (td->flag & TD_NOTCONNECTED || dist < td->dist || (td+1)->dist < td->dist) {
1402 td->flag &= ~TD_NOTCONNECTED;
1403 if (dist < (td+1)->dist) {
1404 td->dist = (td+1)->dist;
1414 /* Utility function for getting the handle data from bezier's */
1415 static TransDataCurveHandleFlags *initTransDataCurveHandles(TransData *td, struct BezTriple *bezt)
1417 TransDataCurveHandleFlags *hdata;
1418 td->flag |= TD_BEZTRIPLE;
1419 hdata = td->hdata = MEM_mallocN(sizeof(TransDataCurveHandleFlags), "CuHandle Data");
1420 hdata->ih1 = bezt->h1;
1421 hdata->h1 = &bezt->h1;
1422 hdata->ih2 = bezt->h2; /* in case the second is not selected */
1423 hdata->h2 = &bezt->h2;
1427 static void createTransCurveVerts(bContext *C, TransInfo *t)
1429 Object *obedit= CTX_data_edit_object(C);
1430 Curve *cu= obedit->data;
1431 TransData *td = NULL;
1435 float mtx[3][3], smtx[3][3];
1437 int count=0, countsel=0;
1438 int propmode = t->flag & T_PROP_EDIT;
1439 short hide_handles = (cu->drawflag & CU_HIDE_HANDLES);
1443 if (cu->editnurb==NULL) return;
1445 /* count total of vertices, check identical as in 2nd loop for making transdata! */
1446 nurbs= curve_editnurbs(cu);
1447 for (nu= nurbs->first; nu; nu= nu->next) {
1448 if (nu->type == CU_BEZIER) {
1449 for (a=0, bezt= nu->bezt; a<nu->pntsu; a++, bezt++) {
1450 if (bezt->hide==0) {
1452 if (bezt->f2 & SELECT) countsel+=3;
1453 if (propmode) count+= 3;
1456 if (bezt->f1 & SELECT) countsel++;
1457 if (bezt->f2 & SELECT) countsel++;
1458 if (bezt->f3 & SELECT) countsel++;
1459 if (propmode) count+= 3;
1465 for (a= nu->pntsu*nu->pntsv, bp= nu->bp; a>0; a--, bp++) {
1467 if (propmode) count++;
1468 if (bp->f1 & SELECT) countsel++;
1473 /* note: in prop mode we need at least 1 selected */
1474 if (countsel==0) return;
1476 if (propmode) t->total = count;
1477 else t->total = countsel;
1478 t->data= MEM_callocN(t->total*sizeof(TransData), "TransObData(Curve EditMode)");
1480 copy_m3_m4(mtx, t->obedit->obmat);
1481 invert_m3_m3(smtx, mtx);
1484 for (nu= nurbs->first; nu; nu= nu->next) {
1485 if (nu->type == CU_BEZIER) {
1486 TransData *head, *tail;
1488 for (a=0, bezt= nu->bezt; a<nu->pntsu; a++, bezt++) {
1489 if (bezt->hide==0) {
1490 TransDataCurveHandleFlags *hdata = NULL;
1493 ((bezt->f2 & SELECT) && hide_handles) ||
1494 ((bezt->f1 & SELECT) && hide_handles == 0)
1496 copy_v3_v3(td->iloc, bezt->vec[0]);
1497 td->loc= bezt->vec[0];
1498 copy_v3_v3(td->center, bezt->vec[(hide_handles || bezt->f2 & SELECT) ? 1:0]);
1500 if (bezt->f2 & SELECT) td->flag= TD_SELECTED;
1504 if (bezt->f1 & SELECT) td->flag= TD_SELECTED;
1510 hdata = initTransDataCurveHandles(td, bezt);
1512 copy_m3_m3(td->smtx, smtx);
1513 copy_m3_m3(td->mtx, mtx);
1520 /* This is the Curve Point, the other two are handles */
1521 if (propmode || (bezt->f2 & SELECT)) {
1522 copy_v3_v3(td->iloc, bezt->vec[1]);
1523 td->loc= bezt->vec[1];
1524 copy_v3_v3(td->center, td->loc);
1525 if (bezt->f2 & SELECT) td->flag= TD_SELECTED;
1529 if (t->mode==TFM_CURVE_SHRINKFATTEN) { /* || t->mode==TFM_RESIZE) {*/ /* TODO - make points scale */
1530 td->val = &(bezt->radius);
1531 td->ival = bezt->radius;
1533 else if (t->mode==TFM_TILT) {
1534 td->val = &(bezt->alfa);
1535 td->ival = bezt->alfa;
1541 copy_m3_m3(td->smtx, smtx);
1542 copy_m3_m3(td->mtx, mtx);
1544 if ((bezt->f1&SELECT)==0 && (bezt->f3&SELECT)==0)
1545 /* If the middle is selected but the sides arnt, this is needed */
1546 if (hdata==NULL) { /* if the handle was not saved by the previous handle */
1547 hdata = initTransDataCurveHandles(td, bezt);
1555 ((bezt->f2 & SELECT) && hide_handles) ||
1556 ((bezt->f3 & SELECT) && hide_handles == 0)
1558 copy_v3_v3(td->iloc, bezt->vec[2]);
1559 td->loc= bezt->vec[2];
1560 copy_v3_v3(td->center, bezt->vec[(hide_handles || bezt->f2 & SELECT) ? 1:2]);
1562 if (bezt->f2 & SELECT) td->flag= TD_SELECTED;
1566 if (bezt->f3 & SELECT) td->flag= TD_SELECTED;
1572 if (hdata==NULL) { /* if the handle was not saved by the previous handle */
1573 hdata = initTransDataCurveHandles(td, bezt);
1576 copy_m3_m3(td->smtx, smtx);
1577 copy_m3_m3(td->mtx, mtx);
1584 else if (propmode && head != tail) {
1585 calc_distanceCurveVerts(head, tail-1);
1589 if (propmode && head != tail)
1590 calc_distanceCurveVerts(head, tail-1);
1592 /* TODO - in the case of tilt and radius we can also avoid allocating the initTransDataCurveHandles
1593 * but for now just don't change handle types */
1594 if (ELEM(t->mode, TFM_CURVE_SHRINKFATTEN, TFM_TILT) == 0) {
1595 /* sets the handles based on their selection, do this after the data is copied to the TransData */
1596 testhandlesNurb(nu);
1600 TransData *head, *tail;
1602 for (a= nu->pntsu*nu->pntsv, bp= nu->bp; a>0; a--, bp++) {
1604 if (propmode || (bp->f1 & SELECT)) {
1605 copy_v3_v3(td->iloc, bp->vec);
1607 copy_v3_v3(td->center, td->loc);
1608 if (bp->f1 & SELECT) td->flag= TD_SELECTED;
1612 if (t->mode==TFM_CURVE_SHRINKFATTEN || t->mode==TFM_RESIZE) {
1613 td->val = &(bp->radius);
1614 td->ival = bp->radius;
1617 td->val = &(bp->alfa);
1618 td->ival = bp->alfa;
1621 copy_m3_m3(td->smtx, smtx);
1622 copy_m3_m3(td->mtx, mtx);
1629 else if (propmode && head != tail) {
1630 calc_distanceCurveVerts(head, tail-1);
1634 if (propmode && head != tail)
1635 calc_distanceCurveVerts(head, tail-1);
1640 /* ********************* lattice *************** */
1642 static void createTransLatticeVerts(TransInfo *t)
1644 Lattice *latt = ((Lattice*)t->obedit->data)->editlatt->latt;
1645 TransData *td = NULL;
1647 float mtx[3][3], smtx[3][3];
1649 int count=0, countsel=0;
1650 int propmode = t->flag & T_PROP_EDIT;
1653 a = latt->pntsu * latt->pntsv * latt->pntsw;
1656 if (bp->f1 & SELECT) countsel++;
1657 if (propmode) count++;
1662 /* note: in prop mode we need at least 1 selected */
1663 if (countsel==0) return;
1665 if (propmode) t->total = count;
1666 else t->total = countsel;
1667 t->data= MEM_callocN(t->total*sizeof(TransData), "TransObData(Lattice EditMode)");
1669 copy_m3_m4(mtx, t->obedit->obmat);
1670 invert_m3_m3(smtx, mtx);
1674 a = latt->pntsu * latt->pntsv * latt->pntsw;
1676 if (propmode || (bp->f1 & SELECT)) {
1678 copy_v3_v3(td->iloc, bp->vec);
1680 copy_v3_v3(td->center, td->loc);
1681 if (bp->f1 & SELECT) td->flag= TD_SELECTED;
1683 copy_m3_m3(td->smtx, smtx);
1684 copy_m3_m3(td->mtx, mtx);
1697 /* ******************* particle edit **************** */
1698 static void createTransParticleVerts(bContext *C, TransInfo *t)
1700 TransData *td = NULL;
1701 TransDataExtension *tx;
1702 Base *base = CTX_data_active_base(C);
1703 Object *ob = CTX_data_active_object(C);
1704 ParticleEditSettings *pset = PE_settings(t->scene);
1705 PTCacheEdit *edit = PE_get_current(t->scene, ob);
1706 ParticleSystem *psys = NULL;
1707 ParticleSystemModifierData *psmd = NULL;
1708 PTCacheEditPoint *point;
1709 PTCacheEditKey *key;
1711 int i,k, transformparticle;
1712 int count = 0, hasselected = 0;
1713 int propmode = t->flag & T_PROP_EDIT;
1715 if (edit==NULL || t->settings->particle.selectmode==SCE_SELECT_PATH) return;
1720 psmd = psys_get_modifier(ob,psys);
1722 base->flag |= BA_HAS_RECALC_DATA;
1724 for (i=0, point=edit->points; i<edit->totpoint; i++, point++) {
1725 point->flag &= ~PEP_TRANSFORM;
1726 transformparticle= 0;
1728 if ((point->flag & PEP_HIDE)==0) {
1729 for (k=0, key=point->keys; k<point->totkey; k++, key++) {
1730 if ((key->flag&PEK_HIDE)==0) {
1731 if (key->flag&PEK_SELECT) {
1733 transformparticle= 1;
1736 transformparticle= 1;
1741 if (transformparticle) {
1742 count += point->totkey;
1743 point->flag |= PEP_TRANSFORM;
1747 /* note: in prop mode we need at least 1 selected */
1748 if (hasselected==0) return;
1751 td = t->data = MEM_callocN(t->total * sizeof(TransData), "TransObData(Particle Mode)");
1753 if (t->mode == TFM_BAKE_TIME)
1754 tx = t->ext = MEM_callocN(t->total * sizeof(TransDataExtension), "Particle_TransExtension");
1760 invert_m4_m4(ob->imat,ob->obmat);
1762 for (i=0, point=edit->points; i<edit->totpoint; i++, point++) {
1763 TransData *head, *tail;
1766 if (!(point->flag & PEP_TRANSFORM)) continue;
1768 if (psys && !(psys->flag & PSYS_GLOBAL_HAIR))
1769 psys_mat_hair_to_global(ob, psmd->dm, psys->part->from, psys->particles + i, mat);
1771 for (k=0, key=point->keys; k<point->totkey; k++, key++) {
1772 if (key->flag & PEK_USE_WCO) {
1773 copy_v3_v3(key->world_co, key->co);
1774 mul_m4_v3(mat, key->world_co);
1775 td->loc = key->world_co;
1780 copy_v3_v3(td->iloc, td->loc);
1781 copy_v3_v3(td->center, td->loc);
1783 if (key->flag & PEK_SELECT)
1784 td->flag |= TD_SELECTED;
1786 td->flag |= TD_SKIP;
1791 /* don't allow moving roots */
1792 if (k==0 && pset->flag & PE_LOCK_FIRST && (!psys || !(psys->flag & PSYS_GLOBAL_HAIR)))
1793 td->protectflag |= OB_LOCK_LOC;
1797 if (t->mode == TFM_BAKE_TIME) {
1798 td->val = key->time;
1799 td->ival = *(key->time);
1800 /* abuse size and quat for min/max values */
1801 td->flag |= TD_NO_EXT;
1802 if (k==0) tx->size = NULL;
1803 else tx->size = (key - 1)->time;
1805 if (k == point->totkey - 1) tx->quat = NULL;
1806 else tx->quat = (key + 1)->time;
1814 if (propmode && head != tail)
1815 calc_distanceCurveVerts(head, tail - 1);
1819 void flushTransParticles(TransInfo *t)
1821 Scene *scene = t->scene;
1823 PTCacheEdit *edit = PE_get_current(scene, ob);
1824 ParticleSystem *psys = edit->psys;
1825 ParticleSystemModifierData *psmd = NULL;
1826 PTCacheEditPoint *point;
1827 PTCacheEditKey *key;
1829 float mat[4][4], imat[4][4], co[3];
1830 int i, k, propmode = t->flag & T_PROP_EDIT;
1833 psmd = psys_get_modifier(ob, psys);
1835 /* we do transform in world space, so flush world space position
1836 * back to particle local space (only for hair particles) */
1838 for (i=0, point=edit->points; i<edit->totpoint; i++, point++, td++) {
1839 if (!(point->flag & PEP_TRANSFORM)) continue;
1841 if (psys && !(psys->flag & PSYS_GLOBAL_HAIR)) {
1842 psys_mat_hair_to_global(ob, psmd->dm, psys->part->from, psys->particles + i, mat);
1843 invert_m4_m4(imat,mat);
1845 for (k=0, key=point->keys; k<point->totkey; k++, key++) {
1846 copy_v3_v3(co, key->world_co);
1847 mul_m4_v3(imat, co);
1850 /* optimization for proportional edit */
1851 if (!propmode || !compare_v3v3(key->co, co, 0.0001f)) {
1852 copy_v3_v3(key->co, co);
1853 point->flag |= PEP_EDIT_RECALC;
1858 point->flag |= PEP_EDIT_RECALC;
1861 PE_update_object(scene, OBACT, 1);
1864 /* ********************* mesh ****************** */
1866 /* proportional distance based on connectivity */
1867 #define THRESHOLDFACTOR (1.0f-0.0001f)
1869 /* I did this wrong, it should be a breadth-first search
1870 * but instead it's a depth-first search, fudged
1871 * to report shortest distances. I have no idea how fast
1872 * or slow this is. */
1873 static void editmesh_set_connectivity_distance(BMEditMesh *em, float mtx[][3], float *dists)
1875 BMVert **queue = NULL;
1876 float *dqueue = NULL;
1877 int *tots = MEM_callocN(sizeof(int)*em->bm->totvert, "tots editmesh_set_connectivity_distance");
1878 BLI_array_declare(queue);
1879 BLI_array_declare(dqueue);
1880 SmallHash svisit, *visit=&svisit;
1885 fill_vn_fl(dists, em->bm->totvert, FLT_MAX);
1887 BM_mesh_elem_index_ensure(em->bm, BM_VERT);
1889 BLI_smallhash_init(visit);
1891 BM_ITER_MESH (v, &viter, em->bm, BM_VERTS_OF_MESH) {
1892 if (BM_elem_flag_test(v, BM_ELEM_SELECT)==0 || BM_elem_flag_test(v, BM_ELEM_HIDDEN))
1896 BLI_smallhash_insert(visit, (uintptr_t)v, NULL);
1897 BLI_array_append(queue, v);
1898 BLI_array_append(dqueue, 0.0f);
1899 dists[BM_elem_index_get(v)] = 0.0f;
1903 while (start < BLI_array_count(queue)) {
1912 BM_ITER_ELEM (e, &eiter, v2, BM_EDGES_OF_VERT) {
1914 v3 = BM_edge_other_vert(e, v2);
1916 if (BM_elem_flag_test(v3, BM_ELEM_SELECT) || BM_elem_flag_test(v3, BM_ELEM_HIDDEN))
1919 sub_v3_v3v3(vec, v2->co, v3->co);
1920 mul_m3_v3(mtx, vec);
1922 d2 = d + len_v3(vec);
1924 if (dists[BM_elem_index_get(v3)] != FLT_MAX)
1925 dists[BM_elem_index_get(v3)] = MIN2(d2, dists[BM_elem_index_get(v3)]);
1927 dists[BM_elem_index_get(v3)] = d2;
1929 tots[BM_elem_index_get(v3)] = 1;
1931 if (BLI_smallhash_haskey(visit, (uintptr_t)v3))
1934 BLI_smallhash_insert(visit, (uintptr_t)v3, NULL);
1936 BLI_array_append(queue, v3);
1937 BLI_array_append(dqueue, d2);
1943 BLI_smallhash_release(visit);
1945 for (i=0; i<em->bm->totvert; i++) {
1947 dists[i] /= (float)tots[i];
1950 BLI_array_free(queue);
1951 BLI_array_free(dqueue);
1955 /* loop-in-a-loop I know, but we need it! (ton) */
1956 static void get_face_center(float cent_r[3], BMVert *eve)
1962 BM_ITER_ELEM (efa, &iter, eve, BM_FACES_OF_VERT) {
1963 if (BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
1964 BM_face_calc_center_mean(efa, cent_r);
1970 static void get_edge_center(float cent_r[3], BMVert *eve)
1975 BM_ITER_ELEM (eed, &iter, eve, BM_EDGES_OF_VERT) {
1976 if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
1977 mid_v3_v3v3(cent_r, eed->v1->co, eed->v2->co);
1983 /* way to overwrite what data is edited with transform */
1984 static void VertsToTransData(TransInfo *t, TransData *td, BMEditMesh *em, BMVert *eve, float *bweight)
1988 // td->loc = key->co;
1992 copy_v3_v3(td->center, td->loc);
1994 if (t->around==V3D_LOCAL) {
1995 if (em->selectmode & SCE_SELECT_FACE)
1996 get_face_center(td->center, eve);
1997 else if (em->selectmode & SCE_SELECT_EDGE)
1998 get_edge_center(td->center, eve);
2000 copy_v3_v3(td->iloc, td->loc);
2003 copy_v3_v3(td->axismtx[2], eve->no);
2009 td->axismtx[1][2] = 0.0f;
2014 if (t->mode == TFM_BWEIGHT) {
2016 td->ival = bweight ? *(bweight) : 1.0f;
2020 static void createTransEditVerts(bContext *C, TransInfo *t)
2022 ToolSettings *ts = CTX_data_tool_settings(C);
2023 TransData *tob = NULL;
2024 BMEditMesh *em = BMEdit_FromObject(t->obedit);
2028 BMVert *eve_act = NULL;
2029 float *mappedcos = NULL, *quats= NULL;
2030 float mtx[3][3], smtx[3][3], (*defmats)[3][3] = NULL, (*defcos)[3] = NULL;
2032 int count=0, countsel=0, a, totleft;
2033 int propmode = (t->flag & T_PROP_EDIT) ? (t->flag & (T_PROP_EDIT | T_PROP_CONNECTED)) : 0;
2035 char *selstate = NULL;
2036 short selectmode = ts->selectmode;
2038 if (t->flag & T_MIRROR)
2040 EDBM_verts_mirror_cache_begin(em, TRUE);
2044 /* edge slide forces edge select */
2045 if (t->mode == TFM_EDGE_SLIDE) {
2046 selectmode = SCE_SELECT_EDGE;
2049 /* BMESH_TODO, writing into the index values is BAD!, means we cant
2050 * use the values for vertex mirror - campbell */
2052 // transform now requires awareness for select mode, so we tag the f1 flags in verts
2053 if (selectmode & SCE_SELECT_VERTEX) {
2054 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
2055 BM_elem_flag_set(eve, BM_ELEM_TAG, BM_elem_flag_test(eve, BM_ELEM_SELECT));
2058 else if (selectmode & SCE_SELECT_EDGE) {
2061 eve = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL);
2062 for ( ; eve; eve=BM_iter_step(&iter)) BM_elem_flag_disable(eve, BM_ELEM_TAG);
2064 eed = BM_iter_new(&iter, bm, BM_EDGES_OF_MESH, NULL);
2065 for ( ; eed; eed=BM_iter_step(&iter)) {
2066 if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
2067 BM_elem_flag_enable(eed->v1, BM_ELEM_TAG);
2068 BM_elem_flag_enable(eed->v2, BM_ELEM_TAG);
2074 eve = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL);
2075 for ( ; eve; eve=BM_iter_step(&iter)) BM_elem_flag_disable(eve, BM_ELEM_TAG);
2077 efa = BM_iter_new(&iter, bm, BM_FACES_OF_MESH, NULL);
2078 for ( ; efa; efa=BM_iter_step(&iter)) {
2079 if (BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
2083 l = BM_iter_new(&liter, bm, BM_LOOPS_OF_FACE, efa);
2084 for (; l; l=BM_iter_step(&liter)) {
2085 BM_elem_flag_enable(l->v, BM_ELEM_TAG);
2091 /* now we can count. we store selection state in selstate, since
2092 * get_crazy_mapped_editverts messes up the index state of the
2094 selstate = MEM_callocN(sizeof(*selstate) * bm->totvert, __func__);
2095 eve = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL);
2096 for (a=0; eve; eve=BM_iter_step(&iter), a++) {
2097 if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
2098 if (BM_elem_flag_test(eve, BM_ELEM_TAG)) {
2102 if (propmode) count++;
2106 /* note: in prop mode we need at least 1 selected */
2107 if (countsel == 0) {
2112 if (em->bm->selected.last) {
2113 BMEditSelection *ese = em->bm->selected.last;
2114 if (ese->htype == BM_VERT) {
2115 eve_act = (BMVert *)ese->ele;
2123 /* allocating scratch arrays */
2124 if (propmode & T_PROP_CONNECTED)
2125 dists = MEM_mallocN(em->bm->totvert * sizeof(float), "scratch nears");
2127 else t->total = countsel;
2129 tob= t->data= MEM_callocN(t->total*sizeof(TransData), "TransObData(Mesh EditMode)");
2131 copy_m3_m4(mtx, t->obedit->obmat);
2132 invert_m3_m3(smtx, mtx);
2134 if (propmode & T_PROP_CONNECTED) {
2135 editmesh_set_connectivity_distance(em, mtx, dists);
2138 /* detect CrazySpace [tm] */
2139 if (modifiers_getCageIndex(t->scene, t->obedit, NULL, 1)>=0) {
2140 if (modifiers_isCorrectableDeformed(t->obedit)) {
2141 /* check if we can use deform matrices for modifier from the
2142 * start up to stack, they are more accurate than quats */
2143 totleft= editbmesh_get_first_deform_matrices(t->scene, t->obedit, em, &defmats, &defcos);
2145 /* if we still have more modifiers, also do crazyspace
2146 * correction with quats, relative to the coordinates after
2147 * the modifiers that support deform matrices (defcos) */
2149 mappedcos= crazyspace_get_mapped_editverts(t->scene, t->obedit);
2150 quats= MEM_mallocN( (t->total)*sizeof(float)*4, "crazy quats");
2151 crazyspace_set_quats_editmesh(em, (float*)defcos, mappedcos, quats); /* BMESH_TODO, abuses vertex index, should use an int array */
2153 MEM_freeN(mappedcos);
2161 /* find out which half we do */
2163 eve = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL);
2164 for (a=0; eve; eve=BM_iter_step(&iter), a++) {
2165 if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN) && selstate[a] && eve->co[0]!=0.0f) {
2166 if (eve->co[0]<0.0f)
2176 eve = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL);
2177 for (a=0; eve; eve=BM_iter_step(&iter), a++) {
2178 if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
2179 if (propmode || selstate[a]) {
2180 float *bweight = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_BWEIGHT);
2182 VertsToTransData(t, tob, em, eve, bweight);
2185 if (selstate[a]) tob->flag |= TD_SELECTED;
2188 if (eve == eve_act) tob->flag |= TD_ACTIVE;
2191 if (propmode & T_PROP_CONNECTED) {
2192 tob->dist = dists[a];
2195 tob->flag |= TD_NOTCONNECTED;
2196 tob->dist = MAXFLOAT;
2201 if (defmats || (quats && BM_elem_index_get(eve) != -1)) {
2202 float mat[3][3], qmat[3][3], imat[3][3];
2204 /* use both or either quat and defmat correction */
2205 if (quats && BM_elem_index_get(eve) != -1) {
2206 quat_to_mat3(qmat, quats + 4*BM_elem_index_get(eve));
2209 mul_serie_m3(mat, mtx, qmat, defmats[a],
2210 NULL, NULL, NULL, NULL, NULL);
2212 mul_m3_m3m3(mat, mtx, qmat);
2215 mul_m3_m3m3(mat, mtx, defmats[a]);
2217 invert_m3_m3(imat, mat);
2219 copy_m3_m3(tob->smtx, imat);
2220 copy_m3_m3(tob->mtx, mat);
2223 copy_m3_m3(tob->smtx, smtx);
2224 copy_m3_m3(tob->mtx, mtx);
2228 if ( (mirror>0 && tob->iloc[0]>0.0f) || (mirror<0 && tob->iloc[0]<0.0f)) {
2229 BMVert *vmir= EDBM_verts_mirror_get(em, eve); //t->obedit, em, eve, tob->iloc, a);
2230 if (vmir && vmir != eve) {
2242 for ( a = 0; a < t->total; a++, tob++ )
2244 if (ABS(tob->loc[0]) <= 0.00001f)
2246 tob->flag |= TD_MIRROR_EDGE;
2252 /* crazy space free */
2260 MEM_freeN(selstate);
2262 if (t->flag & T_MIRROR) {
2263 EDBM_verts_mirror_cache_end(em);
2268 /* *** NODE EDITOR *** */
2269 void flushTransNodes(TransInfo *t)
2274 /* flush to 2d vector from internally used 3d vector */
2275 for (a=0, td= t->data2d; a<t->total; a++, td++) {
2276 td->loc2d[0]= td->loc[0];
2277 td->loc2d[1]= td->loc[1];
2280 /* handle intersection with noodles */
2282 ED_node_link_intersect_test(t->sa, 1);
2287 /* *** SEQUENCE EDITOR *** */
2289 /* commented _only_ because the meta may have animation data which
2290 * needs moving too [#28158] */
2292 #define SEQ_TX_NESTED_METAS
2294 void flushTransSeq(TransInfo *t)
2296 ListBase *seqbasep= seq_give_editing(t->scene, FALSE)->seqbasep; /* Editing null check already done */
2297 int a, new_frame, old_start;
2298 TransData *td= NULL;
2299 TransData2D *td2d= NULL;
2300 TransDataSeq *tdsq= NULL;
2305 /* prevent updating the same seq twice
2306 * if the transdata order is changed this will mess up
2307 * but so will TransDataSeq */
2308 Sequence *seq_prev= NULL;
2310 /* flush to 2d vector from internally used 3d vector */
2311 for (a=0, td= t->data, td2d= t->data2d; a<t->total; a++, td++, td2d++) {
2312 tdsq= (TransDataSeq *)td->extra;
2314 old_start = seq->start;
2315 new_frame= (int)floor(td2d->loc[0] + 0.5f);
2317 switch (tdsq->sel_flag) {
2319 #ifdef SEQ_TX_NESTED_METAS
2320 if ((seq->depth != 0 || seq_tx_test(seq))) /* for meta's, their children move */
2321 seq->start= new_frame - tdsq->start_offset;
2323 if (seq->type != SEQ_META && (seq->depth != 0 || seq_tx_test(seq))) /* for meta's, their children move */
2324 seq->start= new_frame - tdsq->start_offset;
2326 if (seq->depth==0) {
2327 seq->machine= (int)floor(td2d->loc[1] + 0.5f);
2328 CLAMP(seq->machine, 1, MAXSEQ);
2331 case SEQ_LEFTSEL: /* no vertical transform */
2332 seq_tx_set_final_left(seq, new_frame);
2333 seq_tx_handle_xlimits(seq, tdsq->flag&SEQ_LEFTSEL, tdsq->flag&SEQ_RIGHTSEL);
2334 seq_single_fix(seq); /* todo - move this into aftertrans update? - old seq tx needed it anyway */
2336 case SEQ_RIGHTSEL: /* no vertical transform */
2337 seq_tx_set_final_right(seq, new_frame);
2338 seq_tx_handle_xlimits(seq, tdsq->flag&SEQ_LEFTSEL, tdsq->flag&SEQ_RIGHTSEL);
2339 seq_single_fix(seq); /* todo - move this into aftertrans update? - old seq tx needed it anyway */
2343 if (seq != seq_prev) {
2344 if (seq->depth==0) {
2345 /* Calculate this strip and all nested strips
2346 * children are ALWAYS transformed first
2347 * so we don't need to do this in another loop. */
2348 calc_sequence(t->scene, seq);
2351 calc_sequence_disp(t->scene, seq);
2354 if (tdsq->sel_flag == SELECT)
2355 seq_offset_animdata(t->scene, seq, seq->start - old_start);
2361 if (ELEM(t->mode, TFM_SEQ_SLIDE, TFM_TIME_TRANSLATE)) { /* originally TFM_TIME_EXTEND, transform changes */
2362 /* Special annoying case here, need to calc metas with TFM_TIME_EXTEND only */
2364 /* calc all meta's then effects [#27953] */
2365 for (seq = seqbasep->first; seq; seq = seq->next) {
2366 if (seq->type == SEQ_META && seq->flag & SELECT) {
2367 calc_sequence(t->scene, seq);
2370 for (seq = seqbasep->first; seq; seq = seq->next) {
2371 if (seq->seq1 || seq->seq2 || seq->seq3) {
2372 calc_sequence(t->scene, seq);
2377 /* need to do the overlap check in a new loop otherwise adjacent strips
2378 * will not be updated and we'll get false positives */
2380 for (a=0, td= t->data, td2d= t->data2d; a<t->total; a++, td++, td2d++) {
2382 tdsq= (TransDataSeq *)td->extra;
2385 if (seq != seq_prev) {
2386 if (seq->depth==0) {
2387 /* test overlap, displayes red outline */
2388 seq->flag &= ~SEQ_OVERLAP;
2389 if ( seq_test_overlap(seqbasep, seq) ) {
2390 seq->flag |= SEQ_OVERLAP;
2398 /* ********************* UV ****************** */
2400 static void UVsToTransData(SpaceImage *sima, TransData *td, TransData2D *td2d, float *uv, int selected)
2404 ED_space_image_uv_aspect(sima, &aspx, &aspy);
2406 /* uv coords are scaled by aspects. this is needed for rotations and
2407 * proportional editing to be consistent with the stretched uv coords
2408 * that are displayed. this also means that for display and numinput,
2409 * and when the the uv coords are flushed, these are converted each time */
2410 td2d->loc[0] = uv[0]*aspx;
2411 td2d->loc[1] = uv[1]*aspy;
2412 td2d->loc[2] = 0.0f;
2416 td->loc = td2d->loc;
2417 copy_v3_v3(td->center, td->loc);
2418 copy_v3_v3(td->iloc, td->loc);
2420 memset(td->axismtx, 0, sizeof(td->axismtx));
2421 td->axismtx[2][2] = 1.0f;
2423 td->ext= NULL; td->val= NULL;
2426 td->flag |= TD_SELECTED;
2436 static void createTransUVs(bContext *C, TransInfo *t)
2438 SpaceImage *sima = CTX_wm_space_image(C);
2439 Image *ima = CTX_data_edit_image(C);
2440 Scene *scene = t->scene;
2441 TransData *td = NULL;
2442 TransData2D *td2d = NULL;
2445 BMEditMesh *em = BMEdit_FromObject(t->obedit);
2449 int count=0, countsel=0;
2450 int propmode = t->flag & T_PROP_EDIT;
2452 if (!ED_space_image_show_uvedit(sima, t->obedit)) return;
2455 BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
2456 tf= CustomData_bmesh_get(&em->bm->pdata, efa->head.data, CD_MTEXPOLY);
2458 if (!uvedit_face_visible(scene, ima, efa, tf)) {
2459 BM_elem_flag_disable(efa, BM_ELEM_TAG);
2463 BM_elem_flag_enable(efa, BM_ELEM_TAG);
2464 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
2465 if (uvedit_uv_selected(em, scene, l))
2473 /* note: in prop mode we need at least 1 selected */
2474 if (countsel==0) return;
2476 t->total= (propmode)? count: countsel;
2477 t->data= MEM_callocN(t->total*sizeof(TransData), "TransObData(UV Editing)");
2478 /* for each 2d uv coord a 3d vector is allocated, so that they can be
2479 * treated just as if they were 3d verts */
2480 t->data2d= MEM_callocN(t->total*sizeof(TransData2D), "TransObData2D(UV Editing)");
2482 if (sima->flag & SI_CLIP_UV)
2483 t->flag |= T_CLIP_UV;
2488 BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
2489 if (!BM_elem_flag_test(efa, BM_ELEM_TAG))
2492 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
2493 if (!propmode && !uvedit_uv_selected(em, scene, l))
2496 luv = CustomData_bmesh_get(&em->bm->ldata, l->head.data, CD_MLOOPUV);
2497 UVsToTransData(sima, td++, td2d++, luv->uv, uvedit_uv_selected(em, scene, l));
2501 if (sima->flag & SI_LIVE_UNWRAP)
2502 ED_uvedit_live_unwrap_begin(t->scene, t->obedit);
2505 void flushTransUVs(TransInfo *t)
2507 SpaceImage *sima = t->sa->spacedata.first;
2509 int a, width, height;
2510 float aspx, aspy, invx, invy;
2512 ED_space_image_uv_aspect(sima, &aspx, &aspy);
2513 ED_space_image_size(sima, &width, &height);
2517 /* flush to 2d vector from internally used 3d vector */
2518 for (a=0, td= t->data2d; a<t->total; a++, td++) {
2519 td->loc2d[0]= td->loc[0]*invx;
2520 td->loc2d[1]= td->loc[1]*invy;
2522 if ((sima->flag & SI_PIXELSNAP) && (t->state != TRANS_CANCEL)) {
2523 td->loc2d[0]= (float)floor(width*td->loc2d[0] + 0.5f)/width;
2524 td->loc2d[1]= (float)floor(height*td->loc2d[1] + 0.5f)/height;
2529 int clipUVTransform(TransInfo *t, float *vec, int resize)
2532 int a, clipx=1, clipy=1;
2533 float aspx, aspy, min[2], max[2];
2535 ED_space_image_uv_aspect(t->sa->spacedata.first, &aspx, &aspy);
2536 min[0]= min[1]= 0.0f;
2537 max[0]= aspx; max[1]= aspy;
2539 for (a=0, td= t->data; a<t->total; a++, td++) {
2540 DO_MINMAX2(td->loc, min, max);
2544 if (min[0] < 0.0f && t->center[0] > 0.0f && t->center[0] < aspx*0.5f)
2545 vec[0] *= t->center[0]/(t->center[0] - min[0]);
2546 else if (max[0] > aspx && t->center[0] < aspx)
2547 vec[0] *= (t->center[0] - aspx)/(t->center[0] - max[0]);
2551 if (min[1] < 0.0f && t->center[1] > 0.0f && t->center[1] < aspy*0.5f)
2552 vec[1] *= t->center[1]/(t->center[1] - min[1]);
2553 else if (max[1] > aspy && t->center[1] < aspy)
2554 vec[1] *= (t->center[1] - aspy)/(t->center[1] - max[1]);
2561 else if (max[0] > aspx)
2562 vec[0] -= max[0]-aspx;
2568 else if (max[1] > aspy)
2569 vec[1] -= max[1]-aspy;
2574 return (clipx || clipy);
2577 /* ********************* ANIMATION EDITORS (GENERAL) ************************* */
2579 /* This function tests if a point is on the "mouse" side of the cursor/frame-marking */
2580 static short FrameOnMouseSide(char side, float frame, float cframe)
2582 /* both sides, so it doesn't matter */
2583 if (side == 'B') return 1;
2585 /* only on the named side */
2587 return (frame >= cframe) ? 1 : 0;
2589 return (frame <= cframe) ? 1 : 0;
2592 /* ********************* NLA EDITOR ************************* */
2594 static void createTransNlaData(bContext *C, TransInfo *t)
2596 Scene *scene= t->scene;
2597 SpaceNla *snla = NULL;
2598 TransData *td = NULL;
2599 TransDataNla *tdn = NULL;
2602 ListBase anim_data = {NULL, NULL};
2608 /* determine what type of data we are operating on */
2609 if (ANIM_animdata_get_context(C, &ac) == 0)
2611 snla = (SpaceNla *)ac.sl;
2614 filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_FOREDIT);
2615 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
2617 /* which side of the current frame should be allowed */
2618 if (t->mode == TFM_TIME_EXTEND) {
2619 /* only side on which mouse is gets transformed */
2620 float xmouse, ymouse;
2622 UI_view2d_region_to_view(&ac.ar->v2d, t->imval[0], t->imval[1], &xmouse, &ymouse);
2623 t->frame_side= (xmouse > CFRA) ? 'R' : 'L';
2626 /* normal transform - both sides of current frame are considered */
2627 t->frame_side = 'B';
2630 /* loop 1: count how many strips are selected (consider each strip as 2 points) */
2631 for (ale= anim_data.first; ale; ale= ale->next) {
2632 NlaTrack *nlt= (NlaTrack *)ale->data;
2635 /* make some meta-strips for chains of selected strips */
2636 BKE_nlastrips_make_metas(&nlt->strips, 1);
2638 /* only consider selected strips */
2639 for (strip= nlt->strips.first; strip; strip= strip->next) {
2640 // TODO: we can make strips have handles later on...
2641 /* transition strips can't get directly transformed */
2642 if (strip->type != NLASTRIP_TYPE_TRANSITION) {
2643 if (strip->flag & NLASTRIP_FLAG_SELECT) {
2644 if (FrameOnMouseSide(t->frame_side, strip->start, (float)CFRA)) count++;
2645 if (FrameOnMouseSide(t->frame_side, strip->end, (float)CFRA)) count++;
2651 /* stop if trying to build list if nothing selected */
2653 /* cleanup temp list */
2654 BLI_freelistN(&anim_data);
2658 /* allocate memory for data */
2661 t->data= MEM_callocN(t->total*sizeof(TransData), "TransData(NLA Editor)");
2663 t->customData= MEM_callocN(t->total*sizeof(TransDataNla), "TransDataNla (NLA Editor)");
2665 t->flag |= T_FREE_CUSTOMDATA;
2667 /* loop 2: build transdata array */
2668 for (ale= anim_data.first; ale; ale= ale->next) {
2669 /* only if a real NLA-track */
2670 if (ale->type == ANIMTYPE_NLATRACK) {
2671 AnimData *adt = ale->adt;
2672 NlaTrack *nlt= (NlaTrack *)ale->data;
2675 /* only consider selected strips */
2676 for (strip= nlt->strips.first; strip; strip= strip->next) {
2677 // TODO: we can make strips have handles later on...
2678 /* transition strips can't get directly transformed */
2679 if (strip->type != NLASTRIP_TYPE_TRANSITION) {
2680 if (strip->flag & NLASTRIP_FLAG_SELECT) {
2681 /* our transform data is constructed as follows:
2682 * - only the handles on the right side of the current-frame get included
2683 * - td structs are transform-elements operated on by the transform system
2684 * and represent a single handle. The storage/pointer used (val or loc) depends on
2685 * whether we're scaling or transforming. Ultimately though, the handles
2686 * the td writes to will simply be a dummy in tdn
2687 * - for each strip being transformed, a single tdn struct is used, so in some
2688 * cases, there will need to be 1 of these tdn elements in the array skipped...
2690 float center[3], yval;
2692 /* firstly, init tdn settings */
2694 tdn->oldTrack= tdn->nlt= nlt;
2696 tdn->trackIndex= BLI_findindex(&adt->nla_tracks, nlt);
2698 yval= (float)(tdn->trackIndex * NLACHANNEL_STEP(snla));
2700 tdn->h1[0]= strip->start;
2702 tdn->h2[0]= strip->end;
2705 center[0]= (float)CFRA;
2709 /* set td's based on which handles are applicable */
2710 if (FrameOnMouseSide(t->frame_side, strip->start, (float)CFRA))
2712 /* just set tdn to assume that it only has one handle for now */
2715 /* now, link the transform data up to this data */
2716 if (ELEM(t->mode, TFM_TRANSLATION, TFM_TIME_EXTEND)) {
2718 copy_v3_v3(td->iloc, tdn->h1);
2720 /* store all the other gunk that is required by transform */
2721 copy_v3_v3(td->center, center);
2722 memset(td->axismtx, 0, sizeof(td->axismtx));
2723 td->axismtx[2][2] = 1.0f;
2725 td->ext= NULL; td->val= NULL;
2727 td->flag |= TD_SELECTED;
2734 /* time scaling only needs single value */
2735 td->val= &tdn->h1[0];
2736 td->ival= tdn->h1[0];
2742 if (FrameOnMouseSide(t->frame_side, strip->end, (float)CFRA))
2744 /* if tdn is already holding the start handle, then we're doing both, otherwise, only end */
2745 tdn->handle= (tdn->handle) ? 2 : 1;
2747 /* now, link the transform data up to this data */
2748 if (ELEM(t->mode, TFM_TRANSLATION, TFM_TIME_EXTEND)) {
2750 copy_v3_v3(td->iloc, tdn->h2);
2752 /* store all the other gunk that is required by transform */
2753 copy_v3_v3(td->center, center);
2754 memset(td->axismtx, 0, sizeof(td->axismtx));
2755 td->axismtx[2][2] = 1.0f;
2757 td->ext= NULL; td->val= NULL;
2759 td->flag |= TD_SELECTED;
2766 /* time scaling only needs single value */
2767 td->val= &tdn->h2[0];
2768 td->ival= tdn->h2[0];