Code cleanup: use bool for static methods
[blender.git] / source / blender / editors / transform / transform_generics.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/transform/transform_generics.c
29  *  \ingroup edtransform
30  */
31
32 #include <string.h>
33 #include <math.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_sys_types.h" /* for intptr_t support */
38
39 #include "DNA_anim_types.h"
40 #include "DNA_armature_types.h"
41 #include "DNA_lattice_types.h"
42 #include "DNA_screen_types.h"
43 #include "DNA_sequence_types.h"
44 #include "DNA_space_types.h"
45 #include "DNA_scene_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_mesh_types.h"
48 #include "DNA_meshdata_types.h"
49 #include "DNA_view3d_types.h"
50 #include "DNA_modifier_types.h"
51 #include "DNA_movieclip_types.h"
52 #include "DNA_mask_types.h"
53
54 #include "BLI_math.h"
55 #include "BLI_blenlib.h"
56 #include "BLI_rand.h"
57 #include "BLI_utildefines.h"
58
59 #include "BLF_translation.h"
60
61 #include "RNA_access.h"
62
63 #include "BIF_gl.h"
64 #include "BIF_glutil.h"
65
66 #include "BIK_api.h"
67
68 #include "BKE_animsys.h"
69 #include "BKE_action.h"
70 #include "BKE_armature.h"
71 #include "BKE_curve.h"
72 #include "BKE_depsgraph.h"
73 #include "BKE_displist.h"
74 #include "BKE_fcurve.h"
75 #include "BKE_lattice.h"
76 #include "BKE_mesh.h"
77 #include "BKE_nla.h"
78 #include "BKE_context.h"
79 #include "BKE_sequencer.h"
80 #include "BKE_editmesh.h"
81 #include "BKE_tracking.h"
82 #include "BKE_mask.h"
83
84 #include "ED_anim_api.h"
85 #include "ED_armature.h"
86 #include "ED_image.h"
87 #include "ED_keyframing.h"
88 #include "ED_markers.h"
89 #include "ED_mesh.h"
90 #include "ED_particle.h"
91 #include "ED_screen_types.h"
92 #include "ED_space_api.h"
93 #include "ED_uvedit.h"
94 #include "ED_view3d.h"
95 #include "ED_curve.h" /* for curve_editnurbs */
96 #include "ED_clip.h"
97 #include "ED_screen.h"
98
99 #include "WM_types.h"
100 #include "WM_api.h"
101
102 #include "UI_resources.h"
103
104 #include "transform.h"
105
106 /* ************************** Functions *************************** */
107
108 void getViewVector(TransInfo *t, float coord[3], float vec[3])
109 {
110         if (t->persp != RV3D_ORTHO) {
111                 float p1[4], p2[4];
112                 
113                 copy_v3_v3(p1, coord);
114                 p1[3] = 1.0f;
115                 copy_v3_v3(p2, p1);
116                 p2[3] = 1.0f;
117                 mul_m4_v4(t->viewmat, p2);
118                 
119                 p2[0] = 2.0f * p2[0];
120                 p2[1] = 2.0f * p2[1];
121                 p2[2] = 2.0f * p2[2];
122                 
123                 mul_m4_v4(t->viewinv, p2);
124                 
125                 sub_v3_v3v3(vec, p1, p2);
126         }
127         else {
128                 copy_v3_v3(vec, t->viewinv[2]);
129         }
130         normalize_v3(vec);
131 }
132
133 /* ************************** GENERICS **************************** */
134
135
136 static void clipMirrorModifier(TransInfo *t, Object *ob)
137 {
138         ModifierData *md = ob->modifiers.first;
139         float tolerance[3] = {0.0f, 0.0f, 0.0f};
140         int axis = 0;
141         
142         for (; md; md = md->next) {
143                 if ((md->type == eModifierType_Mirror) && (md->mode & eModifierMode_Realtime)) {
144                         MirrorModifierData *mmd = (MirrorModifierData *) md;
145                         
146                         if (mmd->flag & MOD_MIR_CLIPPING) {
147                                 axis = 0;
148                                 if (mmd->flag & MOD_MIR_AXIS_X) {
149                                         axis |= 1;
150                                         tolerance[0] = mmd->tolerance;
151                                 }
152                                 if (mmd->flag & MOD_MIR_AXIS_Y) {
153                                         axis |= 2;
154                                         tolerance[1] = mmd->tolerance;
155                                 }
156                                 if (mmd->flag & MOD_MIR_AXIS_Z) {
157                                         axis |= 4;
158                                         tolerance[2] = mmd->tolerance;
159                                 }
160                                 if (axis) {
161                                         float mtx[4][4], imtx[4][4];
162                                         int i;
163                                         TransData *td = t->data;
164                                         
165                                         if (mmd->mirror_ob) {
166                                                 float obinv[4][4];
167                                                 
168                                                 invert_m4_m4(obinv, mmd->mirror_ob->obmat);
169                                                 mul_m4_m4m4(mtx, obinv, ob->obmat);
170                                                 invert_m4_m4(imtx, mtx);
171                                         }
172                                         
173                                         for (i = 0; i < t->total; i++, td++) {
174                                                 int clip;
175                                                 float loc[3], iloc[3];
176                                                 
177                                                 if (td->flag & TD_NOACTION)
178                                                         break;
179                                                 if (td->loc == NULL)
180                                                         break;
181                                                 
182                                                 if (td->flag & TD_SKIP)
183                                                         continue;
184                                                 
185                                                 copy_v3_v3(loc,  td->loc);
186                                                 copy_v3_v3(iloc, td->iloc);
187                                                 
188                                                 if (mmd->mirror_ob) {
189                                                         mul_m4_v3(mtx, loc);
190                                                         mul_m4_v3(mtx, iloc);
191                                                 }
192                                                 
193                                                 clip = 0;
194                                                 if (axis & 1) {
195                                                         if (fabsf(iloc[0]) <= tolerance[0] ||
196                                                             loc[0] * iloc[0] < 0.0f)
197                                                         {
198                                                                 loc[0] = 0.0f;
199                                                                 clip = 1;
200                                                         }
201                                                 }
202                                                 
203                                                 if (axis & 2) {
204                                                         if (fabsf(iloc[1]) <= tolerance[1] ||
205                                                             loc[1] * iloc[1] < 0.0f)
206                                                         {
207                                                                 loc[1] = 0.0f;
208                                                                 clip = 1;
209                                                         }
210                                                 }
211                                                 if (axis & 4) {
212                                                         if (fabsf(iloc[2]) <= tolerance[2] ||
213                                                             loc[2] * iloc[2] < 0.0f)
214                                                         {
215                                                                 loc[2] = 0.0f;
216                                                                 clip = 1;
217                                                         }
218                                                 }
219                                                 if (clip) {
220                                                         if (mmd->mirror_ob) {
221                                                                 mul_m4_v3(imtx, loc);
222                                                         }
223                                                         copy_v3_v3(td->loc, loc);
224                                                 }
225                                         }
226                                 }
227                                 
228                         }
229                 }
230         }
231 }
232
233 /* assumes obedit set to mesh object */
234 static void editbmesh_apply_to_mirror(TransInfo *t)
235 {
236         TransData *td = t->data;
237         BMVert *eve;
238         int i;
239         
240         for (i = 0; i < t->total; i++, td++) {
241                 if (td->flag & TD_NOACTION)
242                         break;
243                 if (td->loc == NULL)
244                         break;
245                 if (td->flag & TD_SKIP)
246                         continue;
247                 
248                 eve = td->extra;
249                 if (eve) {
250                         eve->co[0] = -td->loc[0];
251                         eve->co[1] = td->loc[1];
252                         eve->co[2] = td->loc[2];
253                 }
254                 
255                 if (td->flag & TD_MIRROR_EDGE) {
256                         td->loc[0] = 0;
257                 }
258         }
259 }
260
261 /* for the realtime animation recording feature, handle overlapping data */
262 static void animrecord_check_state(Scene *scene, ID *id, wmTimer *animtimer)
263 {
264         ScreenAnimData *sad = (animtimer) ? animtimer->customdata : NULL;
265         
266         /* sanity checks */
267         if (ELEM3(NULL, scene, id, sad))
268                 return;
269         
270         /* check if we need a new strip if:
271          *  - if animtimer is running
272          *      - we're not only keying for available channels
273          *      - the option to add new actions for each round is not enabled
274          */
275         if (IS_AUTOKEY_FLAG(scene, INSERTAVAIL) == 0 && (scene->toolsettings->autokey_flag & ANIMRECORD_FLAG_WITHNLA)) {
276                 /* if playback has just looped around, we need to add a new NLA track+strip to allow a clean pass to occur */
277                 if ((sad) && (sad->flag & ANIMPLAY_FLAG_JUMPED)) {
278                         AnimData *adt = BKE_animdata_from_id(id);
279                         
280                         /* perform push-down manually with some differences 
281                          * NOTE: BKE_nla_action_pushdown() sync warning...
282                          */
283                         if ((adt->action) && !(adt->flag & ADT_NLA_EDIT_ON)) {
284                                 float astart, aend;
285                                 
286                                 /* only push down if action is more than 1-2 frames long */
287                                 calc_action_range(adt->action, &astart, &aend, 1);
288                                 if (aend > astart + 2.0f) {
289                                         NlaStrip *strip = add_nlastrip_to_stack(adt, adt->action);
290                                         
291                                         /* clear reference to action now that we've pushed it onto the stack */
292                                         adt->action->id.us--;
293                                         adt->action = NULL;
294                                         
295                                         /* adjust blending + extend so that they will behave correctly */
296                                         strip->extendmode = NLASTRIP_EXTEND_NOTHING;
297                                         strip->flag &= ~(NLASTRIP_FLAG_AUTO_BLENDS | NLASTRIP_FLAG_SELECT | NLASTRIP_FLAG_ACTIVE);
298                                         
299                                         /* also, adjust the AnimData's action extend mode to be on 
300                                          * 'nothing' so that previous result still play 
301                                          */
302                                         adt->act_extendmode = NLASTRIP_EXTEND_NOTHING;
303                                 }
304                         }
305                 }
306         }
307 }
308
309 static bool fcu_test_selected(FCurve *fcu)
310 {
311         BezTriple *bezt = fcu->bezt;
312         unsigned int i;
313
314         if (bezt == NULL) /* ignore baked */
315                 return 0;
316
317         for (i = 0; i < fcu->totvert; i++, bezt++) {
318                 if (BEZSELECTED(bezt)) return 1;
319         }
320
321         return 0;
322 }
323
324 /* helper for recalcData() - for Action Editor transforms */
325 static void recalcData_actedit(TransInfo *t)
326 {
327         Scene *scene = t->scene;
328         SpaceAction *saction = (SpaceAction *)t->sa->spacedata.first;
329         
330         bAnimContext ac = {NULL};
331         ListBase anim_data = {NULL, NULL};
332         bAnimListElem *ale;
333         int filter;
334         
335         /* initialize relevant anim-context 'context' data from TransInfo data */
336         /* NOTE: sync this with the code in ANIM_animdata_get_context() */
337         ac.scene = t->scene;
338         ac.obact = OBACT;
339         ac.sa = t->sa;
340         ac.ar = t->ar;
341         ac.sl = (t->sa) ? t->sa->spacedata.first : NULL;
342         ac.spacetype = (t->sa) ? t->sa->spacetype : 0;
343         ac.regiontype = (t->ar) ? t->ar->regiontype : 0;
344         
345         ANIM_animdata_context_getdata(&ac);
346         
347         /* perform flush */
348         if (ELEM(ac.datatype, ANIMCONT_GPENCIL, ANIMCONT_MASK)) {
349                 /* flush transform values back to actual coordinates */
350                 flushTransIntFrameActionData(t);
351         }
352         else {
353                 /* get animdata blocks visible in editor, assuming that these will be the ones where things changed */
354                 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_ANIMDATA);
355                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
356                 
357                 /* just tag these animdata-blocks to recalc, assuming that some data there changed 
358                  * BUT only do this if realtime updates are enabled
359                  */
360                 if ((saction->flag & SACTION_NOREALTIMEUPDATES) == 0) {
361                         for (ale = anim_data.first; ale; ale = ale->next) {
362                                 /* set refresh tags for objects using this animation */
363                                 ANIM_list_elem_update(t->scene, ale);
364                         }
365                 }
366                 
367                 /* now free temp channels */
368                 BLI_freelistN(&anim_data);
369         }
370 }
371 /* helper for recalcData() - for Graph Editor transforms */
372 static void recalcData_graphedit(TransInfo *t)
373 {
374         SpaceIpo *sipo = (SpaceIpo *)t->sa->spacedata.first;
375         Scene *scene;
376         
377         ListBase anim_data = {NULL, NULL};
378         bAnimContext ac = {NULL};
379         int filter;
380         
381         bAnimListElem *ale;
382         int dosort = 0;
383
384         /* initialize relevant anim-context 'context' data from TransInfo data */
385         /* NOTE: sync this with the code in ANIM_animdata_get_context() */
386         scene = ac.scene = t->scene;
387         ac.obact = OBACT;
388         ac.sa = t->sa;
389         ac.ar = t->ar;
390         ac.sl = (t->sa) ? t->sa->spacedata.first : NULL;
391         ac.spacetype = (t->sa) ? t->sa->spacetype : 0;
392         ac.regiontype = (t->ar) ? t->ar->regiontype : 0;
393         
394         ANIM_animdata_context_getdata(&ac);
395         
396         /* do the flush first */
397         flushTransGraphData(t);
398         
399         /* get curves to check if a re-sort is needed */
400         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_CURVE_VISIBLE);
401         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
402         
403         /* now test if there is a need to re-sort */
404         for (ale = anim_data.first; ale; ale = ale->next) {
405                 FCurve *fcu = (FCurve *)ale->key_data;
406                 
407                 /* ignore unselected fcurves */
408                 if (!fcu_test_selected(fcu))
409                         continue;
410
411                 /* watch it: if the time is wrong: do not correct handles yet */
412                 if (test_time_fcurve(fcu))
413                         dosort++;
414                 else
415                         calchandles_fcurve(fcu);
416                 
417                 /* set refresh tags for objects using this animation,
418                  * BUT only if realtime updates are enabled  
419                  */
420                 if ((sipo->flag & SIPO_NOREALTIMEUPDATES) == 0)
421                         ANIM_list_elem_update(t->scene, ale);
422         }
423         
424         /* do resort and other updates? */
425         if (dosort) remake_graph_transdata(t, &anim_data);
426         
427         /* now free temp channels */
428         BLI_freelistN(&anim_data);
429 }
430
431 /* helper for recalcData() - for NLA Editor transforms */
432 static void recalcData_nla(TransInfo *t)
433 {
434         TransDataNla *tdn = (TransDataNla *)t->customData;
435         SpaceNla *snla = (SpaceNla *)t->sa->spacedata.first;
436         Scene *scene = t->scene;
437         double secf = FPS;
438         int i;
439         
440         /* for each strip we've got, perform some additional validation of the values that got set before
441          * using RNA to set the value (which does some special operations when setting these values to make
442          * sure that everything works ok)
443          */
444         for (i = 0; i < t->total; i++, tdn++) {
445                 NlaStrip *strip = tdn->strip;
446                 PointerRNA strip_ptr;
447                 short pExceeded, nExceeded, iter;
448                 int delta_y1, delta_y2;
449                 
450                 /* if this tdn has no handles, that means it is just a dummy that should be skipped */
451                 if (tdn->handle == 0)
452                         continue;
453                 
454                 /* set refresh tags for objects using this animation,
455                  * BUT only if realtime updates are enabled  
456                  */
457                 if ((snla->flag & SNLA_NOREALTIMEUPDATES) == 0)
458                         ANIM_id_update(t->scene, tdn->id);
459                 
460                 /* if canceling transform, just write the values without validating, then move on */
461                 if (t->state == TRANS_CANCEL) {
462                         /* clear the values by directly overwriting the originals, but also need to restore
463                          * endpoints of neighboring transition-strips
464                          */
465                         
466                         /* start */
467                         strip->start = tdn->h1[0];
468                         
469                         if ((strip->prev) && (strip->prev->type == NLASTRIP_TYPE_TRANSITION))
470                                 strip->prev->end = tdn->h1[0];
471                         
472                         /* end */
473                         strip->end = tdn->h2[0];
474                         
475                         if ((strip->next) && (strip->next->type == NLASTRIP_TYPE_TRANSITION))
476                                 strip->next->start = tdn->h2[0];
477                         
478                         /* flush transforms to child strips (since this should be a meta) */
479                         BKE_nlameta_flush_transforms(strip);
480                         
481                         /* restore to original track (if needed) */
482                         if (tdn->oldTrack != tdn->nlt) {
483                                 /* just append to end of list for now, since strips get sorted in special_aftertrans_update() */
484                                 BLI_remlink(&tdn->nlt->strips, strip);
485                                 BLI_addtail(&tdn->oldTrack->strips, strip);
486                         }
487                         
488                         continue;
489                 }
490                 
491                 /* firstly, check if the proposed transform locations would overlap with any neighboring strips
492                  * (barring transitions) which are absolute barriers since they are not being moved
493                  *
494                  * this is done as a iterative procedure (done 5 times max for now)
495                  */
496                 for (iter = 0; iter < 5; iter++) {
497                         pExceeded = ((strip->prev) && (strip->prev->type != NLASTRIP_TYPE_TRANSITION) && (tdn->h1[0] < strip->prev->end));
498                         nExceeded = ((strip->next) && (strip->next->type != NLASTRIP_TYPE_TRANSITION) && (tdn->h2[0] > strip->next->start));
499                         
500                         if ((pExceeded && nExceeded) || (iter == 4)) {
501                                 /* both endpoints exceeded (or iteration ping-pong'd meaning that we need a compromise)
502                                  *      - simply crop strip to fit within the bounds of the strips bounding it
503                                  *      - if there were no neighbors, clear the transforms (make it default to the strip's current values)
504                                  */
505                                 if (strip->prev && strip->next) {
506                                         tdn->h1[0] = strip->prev->end;
507                                         tdn->h2[0] = strip->next->start;
508                                 }
509                                 else {
510                                         tdn->h1[0] = strip->start;
511                                         tdn->h2[0] = strip->end;
512                                 }
513                         }
514                         else if (nExceeded) {
515                                 /* move backwards */
516                                 float offset = tdn->h2[0] - strip->next->start;
517                                 
518                                 tdn->h1[0] -= offset;
519                                 tdn->h2[0] -= offset;
520                         }
521                         else if (pExceeded) {
522                                 /* more forwards */
523                                 float offset = strip->prev->end - tdn->h1[0];
524                                 
525                                 tdn->h1[0] += offset;
526                                 tdn->h2[0] += offset;
527                         }
528                         else /* all is fine and well */
529                                 break;
530                 }
531                 
532                 /* handle auto-snapping */
533                 switch (snla->autosnap) {
534                         case SACTSNAP_FRAME: /* snap to nearest frame/time  */
535                                 if (snla->flag & SNLA_DRAWTIME) {
536                                         tdn->h1[0] = (float)(floor(((double)tdn->h1[0] / secf) + 0.5) * secf);
537                                         tdn->h2[0] = (float)(floor(((double)tdn->h2[0] / secf) + 0.5) * secf);
538                                 }
539                                 else {
540                                         tdn->h1[0] = floorf(tdn->h1[0] + 0.5f);
541                                         tdn->h2[0] = floorf(tdn->h2[0] + 0.5f);
542                                 }
543                                 break;
544                         
545                         case SACTSNAP_MARKER: /* snap to nearest marker */
546                                 tdn->h1[0] = (float)ED_markers_find_nearest_marker_time(&t->scene->markers, tdn->h1[0]);
547                                 tdn->h2[0] = (float)ED_markers_find_nearest_marker_time(&t->scene->markers, tdn->h2[0]);
548                                 break;
549                 }
550                 
551                 /* Use RNA to write the values to ensure that constraints on these are obeyed
552                  * (e.g. for transition strips, the values are taken from the neighbours)
553                  * 
554                  * NOTE: we write these twice to avoid truncation errors which can arise when
555                  * moving the strips a large distance using numeric input [#33852] 
556                  */
557                 RNA_pointer_create(NULL, &RNA_NlaStrip, strip, &strip_ptr);
558                 
559                 RNA_float_set(&strip_ptr, "frame_start", tdn->h1[0]);
560                 RNA_float_set(&strip_ptr, "frame_end", tdn->h2[0]);
561                 
562                 RNA_float_set(&strip_ptr, "frame_start", tdn->h1[0]);
563                 RNA_float_set(&strip_ptr, "frame_end", tdn->h2[0]);
564                 
565                 /* flush transforms to child strips (since this should be a meta) */
566                 BKE_nlameta_flush_transforms(strip);
567                 
568                 
569                 /* now, check if we need to try and move track
570                  *      - we need to calculate both, as only one may have been altered by transform if only 1 handle moved
571                  */
572                 delta_y1 = ((int)tdn->h1[1] / NLACHANNEL_STEP(snla) - tdn->trackIndex);
573                 delta_y2 = ((int)tdn->h2[1] / NLACHANNEL_STEP(snla) - tdn->trackIndex);
574                 
575                 if (delta_y1 || delta_y2) {
576                         NlaTrack *track;
577                         int delta = (delta_y2) ? delta_y2 : delta_y1;
578                         int n;
579                         
580                         /* move in the requested direction, checking at each layer if there's space for strip to pass through,
581                          * stopping on the last track available or that we're able to fit in
582                          */
583                         if (delta > 0) {
584                                 for (track = tdn->nlt->next, n = 0; (track) && (n < delta); track = track->next, n++) {
585                                         /* check if space in this track for the strip */
586                                         if (BKE_nlatrack_has_space(track, strip->start, strip->end)) {
587                                                 /* move strip to this track */
588                                                 BLI_remlink(&tdn->nlt->strips, strip);
589                                                 BKE_nlatrack_add_strip(track, strip);
590                                                 
591                                                 tdn->nlt = track;
592                                                 tdn->trackIndex++;
593                                         }
594                                         else /* can't move any further */
595                                                 break;
596                                 }
597                         }
598                         else {
599                                 /* make delta 'positive' before using it, since we now know to go backwards */
600                                 delta = -delta;
601                                 
602                                 for (track = tdn->nlt->prev, n = 0; (track) && (n < delta); track = track->prev, n++) {
603                                         /* check if space in this track for the strip */
604                                         if (BKE_nlatrack_has_space(track, strip->start, strip->end)) {
605                                                 /* move strip to this track */
606                                                 BLI_remlink(&tdn->nlt->strips, strip);
607                                                 BKE_nlatrack_add_strip(track, strip);
608                                                 
609                                                 tdn->nlt = track;
610                                                 tdn->trackIndex--;
611                                         }
612                                         else /* can't move any further */
613                                                 break;
614                                 }
615                         }
616                 }
617         }
618 }
619
620 static void recalcData_mask_common(TransInfo *t)
621 {
622         Mask *mask = CTX_data_edit_mask(t->context);
623
624         flushTransMasking(t);
625
626         DAG_id_tag_update(&mask->id, 0);
627 }
628
629 /* helper for recalcData() - for Image Editor transforms */
630 static void recalcData_image(TransInfo *t)
631 {
632         if (t->options & CTX_MASK) {
633                 recalcData_mask_common(t);
634         }
635         else if (t->obedit && t->obedit->type == OB_MESH) {
636                 SpaceImage *sima = t->sa->spacedata.first;
637                 
638                 flushTransUVs(t);
639                 if (sima->flag & SI_LIVE_UNWRAP)
640                         ED_uvedit_live_unwrap_re_solve();
641                 
642                 DAG_id_tag_update(t->obedit->data, 0);
643         }
644 }
645
646 /* helper for recalcData() - for Movie Clip transforms */
647 static void recalcData_spaceclip(TransInfo *t)
648 {
649         SpaceClip *sc = t->sa->spacedata.first;
650
651         if (ED_space_clip_check_show_trackedit(sc)) {
652                 MovieClip *clip = ED_space_clip_get_clip(sc);
653                 ListBase *tracksbase = BKE_tracking_get_active_tracks(&clip->tracking);
654                 MovieTrackingTrack *track;
655                 int framenr = ED_space_clip_get_clip_frame_number(sc);
656
657                 flushTransTracking(t);
658
659                 track = tracksbase->first;
660                 while (track) {
661                         if (TRACK_VIEW_SELECTED(sc, track) && (track->flag & TRACK_LOCKED) == 0) {
662                                 MovieTrackingMarker *marker = BKE_tracking_marker_get(track, framenr);
663
664                                 if (t->mode == TFM_TRANSLATION) {
665                                         if (TRACK_AREA_SELECTED(track, TRACK_AREA_PAT))
666                                                 BKE_tracking_marker_clamp(marker, CLAMP_PAT_POS);
667                                         if (TRACK_AREA_SELECTED(track, TRACK_AREA_SEARCH))
668                                                 BKE_tracking_marker_clamp(marker, CLAMP_SEARCH_POS);
669                                 }
670                                 else if (t->mode == TFM_RESIZE) {
671                                         if (TRACK_AREA_SELECTED(track, TRACK_AREA_PAT))
672                                                 BKE_tracking_marker_clamp(marker, CLAMP_PAT_DIM);
673                                         if (TRACK_AREA_SELECTED(track, TRACK_AREA_SEARCH))
674                                                 BKE_tracking_marker_clamp(marker, CLAMP_SEARCH_DIM);
675                                 }
676                                 else if (t->mode == TFM_ROTATION) {
677                                         if (TRACK_AREA_SELECTED(track, TRACK_AREA_PAT))
678                                                 BKE_tracking_marker_clamp(marker, CLAMP_PAT_POS);
679                                 }
680                         }
681
682                         track = track->next;
683                 }
684
685                 DAG_id_tag_update(&clip->id, 0);
686         }
687         else if (t->options & CTX_MASK) {
688                 recalcData_mask_common(t);
689         }
690 }
691
692 /* helper for recalcData() - for 3d-view transforms */
693 static void recalcData_view3d(TransInfo *t)
694 {
695         Base *base = t->scene->basact;
696         
697         if (t->obedit) {
698                 if (ELEM(t->obedit->type, OB_CURVE, OB_SURF)) {
699                         Curve *cu = t->obedit->data;
700                         ListBase *nurbs = BKE_curve_editNurbs_get(cu);
701                         Nurb *nu = nurbs->first;
702                         
703                         if (t->state != TRANS_CANCEL) {
704                                 clipMirrorModifier(t, t->obedit);
705                                 applyProject(t);
706                         }
707                         
708                         DAG_id_tag_update(t->obedit->data, 0);  /* sets recalc flags */
709                                 
710                         if (t->state == TRANS_CANCEL) {
711                                 while (nu) {
712                                         BKE_nurb_handles_calc(nu); /* Cant do testhandlesNurb here, it messes up the h1 and h2 flags */
713                                         nu = nu->next;
714                                 }
715                         }
716                         else {
717                                 /* Normal updating */
718                                 while (nu) {
719                                         BKE_nurb_test2D(nu);
720                                         BKE_nurb_handles_calc(nu);
721                                         nu = nu->next;
722                                 }
723                         }
724                 }
725                 else if (t->obedit->type == OB_LATTICE) {
726                         Lattice *la = t->obedit->data;
727                         
728                         if (t->state != TRANS_CANCEL) {
729                                 applyProject(t);
730                         }
731                         
732                         DAG_id_tag_update(t->obedit->data, 0);  /* sets recalc flags */
733                         
734                         if (la->editlatt->latt->flag & LT_OUTSIDE) outside_lattice(la->editlatt->latt);
735                 }
736                 else if (t->obedit->type == OB_MESH) {
737                         BMEditMesh *em = BKE_editmesh_from_object(t->obedit);
738                         /* mirror modifier clipping? */
739                         if (t->state != TRANS_CANCEL) {
740                                 /* apply clipping after so we never project past the clip plane [#25423] */
741                                 applyProject(t);
742                                 clipMirrorModifier(t, t->obedit);
743                         }
744                         if ((t->options & CTX_NO_MIRROR) == 0 && (t->flag & T_MIRROR))
745                                 editbmesh_apply_to_mirror(t);
746                                 
747                         DAG_id_tag_update(t->obedit->data, 0);  /* sets recalc flags */
748                         
749                         EDBM_mesh_normals_update(em);
750                         BKE_editmesh_tessface_calc(em);
751                 }
752                 else if (t->obedit->type == OB_ARMATURE) { /* no recalc flag, does pose */
753                         bArmature *arm = t->obedit->data;
754                         ListBase *edbo = arm->edbo;
755                         EditBone *ebo;
756                         TransData *td = t->data;
757                         int i;
758                         
759                         if (t->state != TRANS_CANCEL) {
760                                 applyProject(t);
761                         }
762                         
763                         /* Ensure all bones are correctly adjusted */
764                         for (ebo = edbo->first; ebo; ebo = ebo->next) {
765                                 
766                                 if ((ebo->flag & BONE_CONNECTED) && ebo->parent) {
767                                         /* If this bone has a parent tip that has been moved */
768                                         if (ebo->parent->flag & BONE_TIPSEL) {
769                                                 copy_v3_v3(ebo->head, ebo->parent->tail);
770                                                 if (t->mode == TFM_BONE_ENVELOPE) ebo->rad_head = ebo->parent->rad_tail;
771                                         }
772                                         /* If this bone has a parent tip that has NOT been moved */
773                                         else {
774                                                 copy_v3_v3(ebo->parent->tail, ebo->head);
775                                                 if (t->mode == TFM_BONE_ENVELOPE) ebo->parent->rad_tail = ebo->rad_head;
776                                         }
777                                 }
778                                 
779                                 /* on extrude bones, oldlength==0.0f, so we scale radius of points */
780                                 ebo->length = len_v3v3(ebo->head, ebo->tail);
781                                 if (ebo->oldlength == 0.0f) {
782                                         ebo->rad_head = 0.25f * ebo->length;
783                                         ebo->rad_tail = 0.10f * ebo->length;
784                                         ebo->dist = 0.25f * ebo->length;
785                                         if (ebo->parent) {
786                                                 if (ebo->rad_head > ebo->parent->rad_tail)
787                                                         ebo->rad_head = ebo->parent->rad_tail;
788                                         }
789                                 }
790                                 else if (t->mode != TFM_BONE_ENVELOPE) {
791                                         /* if bones change length, lets do that for the deform distance as well */
792                                         ebo->dist *= ebo->length / ebo->oldlength;
793                                         ebo->rad_head *= ebo->length / ebo->oldlength;
794                                         ebo->rad_tail *= ebo->length / ebo->oldlength;
795                                         ebo->oldlength = ebo->length;
796                                 }
797                         }
798                         
799                         if (!ELEM3(t->mode, TFM_BONE_ROLL, TFM_BONE_ENVELOPE, TFM_BONESIZE)) {
800                                 /* fix roll */
801                                 for (i = 0; i < t->total; i++, td++) {
802                                         if (td->extra) {
803                                                 float vec[3], up_axis[3];
804                                                 float qrot[4];
805                                                 float roll;
806                                                 
807                                                 ebo = td->extra;
808
809                                                 if (t->state == TRANS_CANCEL) {
810                                                         /* restore roll */
811                                                         ebo->roll = td->ival;
812                                                 }
813                                                 else {
814                                                         copy_v3_v3(up_axis, td->axismtx[2]);
815                                                         
816                                                         if (t->mode != TFM_ROTATION) {
817                                                                 sub_v3_v3v3(vec, ebo->tail, ebo->head);
818                                                                 normalize_v3(vec);
819                                                                 rotation_between_vecs_to_quat(qrot, td->axismtx[1], vec);
820                                                                 mul_qt_v3(qrot, up_axis);
821                                                         }
822                                                         else {
823                                                                 mul_m3_v3(t->mat, up_axis);
824                                                         }
825                                                         
826                                                         /* roll has a tendency to flip in certain orientations - [#34283], [#33974] */
827                                                         roll = ED_rollBoneToVector(ebo, up_axis, false);
828                                                         ebo->roll = angle_compat_rad(roll, td->ival);
829                                                 }
830                                         }
831                                 }
832                         }
833                         
834                         if (arm->flag & ARM_MIRROR_EDIT)
835                                 transform_armature_mirror_update(t->obedit);
836                         
837                 }
838                 else {
839                         if (t->state != TRANS_CANCEL) {
840                                 applyProject(t);
841                         }
842                         DAG_id_tag_update(t->obedit->data, 0);  /* sets recalc flags */
843                 }
844         }
845         else if ((t->flag & T_POSE) && t->poseobj) {
846                 Object *ob = t->poseobj;
847                 bArmature *arm = ob->data;
848                 
849                 /* if animtimer is running, and the object already has animation data,
850                  * check if the auto-record feature means that we should record 'samples'
851                  * (i.e. uneditable animation values)
852                  *
853                  * context is needed for keying set poll() functions.
854                  */
855                 // TODO: autokeyframe calls need some setting to specify to add samples (FPoints) instead of keyframes?
856                 if ((t->animtimer) && (t->context) && IS_AUTOKEY_ON(t->scene)) {
857                         int targetless_ik = (t->flag & T_AUTOIK); // XXX this currently doesn't work, since flags aren't set yet!
858                         
859                         animrecord_check_state(t->scene, &ob->id, t->animtimer);
860                         autokeyframe_pose_cb_func(t->context, t->scene, (View3D *)t->view, ob, t->mode, targetless_ik);
861                 }
862                 
863                 /* old optimize trick... this enforces to bypass the depgraph */
864                 if (!(arm->flag & ARM_DELAYDEFORM)) {
865                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);  /* sets recalc flags */
866                         /* transformation of pose may affect IK tree, make sure it is rebuilt */
867                         BIK_clear_data(ob->pose);
868                 }
869                 else
870                         BKE_pose_where_is(t->scene, ob);
871         }
872         else if (base && (base->object->mode & OB_MODE_PARTICLE_EDIT) && PE_get_current(t->scene, base->object)) {
873                 if (t->state != TRANS_CANCEL) {
874                         applyProject(t);
875                 }
876                 flushTransParticles(t);
877         }
878         else {
879                 int i;
880                 
881                 if (t->state != TRANS_CANCEL) {
882                         applyProject(t);
883                 }
884                 
885                 for (i = 0; i < t->total; i++) {
886                         TransData *td = t->data + i;
887                         Object *ob = td->ob;
888                         
889                         if (td->flag & TD_NOACTION)
890                                 break;
891                         
892                         if (td->flag & TD_SKIP)
893                                 continue;
894                         
895                         /* if animtimer is running, and the object already has animation data,
896                          * check if the auto-record feature means that we should record 'samples'
897                          * (i.e. uneditable animation values)
898                          */
899                         // TODO: autokeyframe calls need some setting to specify to add samples (FPoints) instead of keyframes?
900                         if ((t->animtimer) && IS_AUTOKEY_ON(t->scene)) {
901                                 animrecord_check_state(t->scene, &ob->id, t->animtimer);
902                                 autokeyframe_ob_cb_func(t->context, t->scene, (View3D *)t->view, ob, t->mode);
903                         }
904                         
905                         /* sets recalc flags fully, instead of flushing existing ones 
906                          * otherwise proxies don't function correctly
907                          */
908                         DAG_id_tag_update(&ob->id, OB_RECALC_OB);
909
910                         if (t->flag & T_TEXTURE)
911                                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
912                 }
913         }
914 }
915
916 /* helper for recalcData() - for sequencer transforms */
917 static void recalcData_sequencer(TransInfo *t)
918 {
919         TransData *td;
920         int a;
921         Sequence *seq_prev = NULL;
922
923         for (a = 0, td = t->data; a < t->total; a++, td++) {
924                 TransDataSeq *tdsq = (TransDataSeq *) td->extra;
925                 Sequence *seq = tdsq->seq;
926
927                 if (seq != seq_prev) {
928                         BKE_sequence_invalidate_dependent(t->scene, seq);
929                 }
930
931                 seq_prev = seq;
932         }
933
934         BKE_sequencer_preprocessed_cache_cleanup();
935
936         flushTransSeq(t);
937 }
938
939 /* called for updating while transform acts, once per redraw */
940 void recalcData(TransInfo *t)
941 {
942         if (t->spacetype == SPACE_NODE) {
943                 flushTransNodes(t);
944         }
945         else if (t->spacetype == SPACE_SEQ) {
946                 recalcData_sequencer(t);
947         }
948         else if (t->spacetype == SPACE_ACTION) {
949                 recalcData_actedit(t);
950         }
951         else if (t->spacetype == SPACE_IPO) {
952                 recalcData_graphedit(t);
953         }
954         else if (t->spacetype == SPACE_NLA) {
955                 recalcData_nla(t);
956         }
957         else if (t->spacetype == SPACE_IMAGE) {
958                 recalcData_image(t);
959         }
960         else if (t->spacetype == SPACE_VIEW3D) {
961                 recalcData_view3d(t);
962         }
963         else if (t->spacetype == SPACE_CLIP) {
964                 recalcData_spaceclip(t);
965         }
966 }
967
968 void drawLine(TransInfo *t, const float center[3], const float dir[3], char axis, short options)
969 {
970         float v1[3], v2[3], v3[3];
971         unsigned char col[3], col2[3];
972
973         if (t->spacetype == SPACE_VIEW3D) {
974                 View3D *v3d = t->view;
975                 
976                 glPushMatrix();
977                 
978                 //if (t->obedit) glLoadMatrixf(t->obedit->obmat);       // sets opengl viewing
979                 
980                 
981                 copy_v3_v3(v3, dir);
982                 mul_v3_fl(v3, v3d->far);
983                 
984                 sub_v3_v3v3(v2, center, v3);
985                 add_v3_v3v3(v1, center, v3);
986                 
987                 if (options & DRAWLIGHT) {
988                         col[0] = col[1] = col[2] = 220;
989                 }
990                 else {
991                         UI_GetThemeColor3ubv(TH_GRID, col);
992                 }
993                 UI_make_axis_color(col, col2, axis);
994                 glColor3ubv(col2);
995                 
996                 setlinestyle(0);
997                 glBegin(GL_LINE_STRIP);
998                 glVertex3fv(v1);
999                 glVertex3fv(v2);
1000                 glEnd();
1001                 
1002                 glPopMatrix();
1003         }
1004 }
1005
1006 /**
1007  * Free data before switching to another mode.
1008  */
1009 void resetTransModal(TransInfo *t)
1010 {
1011         if (t->mode == TFM_EDGE_SLIDE) {
1012                 freeEdgeSlideVerts(t);
1013         }
1014         else if (t->mode == TFM_VERT_SLIDE) {
1015                 freeVertSlideVerts(t);
1016         }
1017 }
1018
1019 void resetTransRestrictions(TransInfo *t)
1020 {
1021         t->flag &= ~T_ALL_RESTRICTIONS;
1022 }
1023
1024 static int initTransInfo_edit_pet_to_flag(const int proportional)
1025 {
1026         switch (proportional) {
1027                 case PROP_EDIT_ON:
1028                         return T_PROP_EDIT;
1029                 case PROP_EDIT_CONNECTED:
1030                         return T_PROP_EDIT | T_PROP_CONNECTED;
1031                 case PROP_EDIT_PROJECTED:
1032                         return T_PROP_EDIT | T_PROP_PROJECTED;
1033                 default:
1034                         return 0;
1035         }
1036 }
1037
1038 /**
1039  * Setup internal data, mouse, vectors
1040  *
1041  * \note \a op and \a event can be NULL
1042  */
1043 void initTransInfo(bContext *C, TransInfo *t, wmOperator *op, const wmEvent *event)
1044 {
1045         Scene *sce = CTX_data_scene(C);
1046         ToolSettings *ts = CTX_data_tool_settings(C);
1047         ARegion *ar = CTX_wm_region(C);
1048         ScrArea *sa = CTX_wm_area(C);
1049         Object *obedit = CTX_data_edit_object(C);
1050         PropertyRNA *prop;
1051         
1052         t->scene = sce;
1053         t->sa = sa;
1054         t->ar = ar;
1055         t->obedit = obedit;
1056         t->settings = ts;
1057         t->reports = op ? op->reports : NULL;
1058
1059         if (obedit) {
1060                 copy_m3_m4(t->obedit_mat, obedit->obmat);
1061                 normalize_m3(t->obedit_mat);
1062         }
1063
1064         t->data = NULL;
1065         t->ext = NULL;
1066         
1067         t->helpline = HLP_NONE;
1068         
1069         t->flag = 0;
1070         
1071         t->redraw = TREDRAW_HARD;  /* redraw first time */
1072         
1073         if (event) {
1074                 copy_v2_v2_int(t->imval, event->mval);
1075                 t->event_type = event->type;
1076         }
1077         else {
1078                 t->imval[0] = 0;
1079                 t->imval[1] = 0;
1080         }
1081         
1082         t->con.imval[0] = t->imval[0];
1083         t->con.imval[1] = t->imval[1];
1084         
1085         t->mval[0] = t->imval[0];
1086         t->mval[1] = t->imval[1];
1087         
1088         t->transform        = NULL;
1089         t->handleEvent      = NULL;
1090         
1091         t->total            = 0;
1092         
1093         t->val = 0.0f;
1094
1095         zero_v3(t->vec);
1096         zero_v3(t->center);
1097
1098         unit_m3(t->mat);
1099         
1100         /* if there's an event, we're modal */
1101         if (event) {
1102                 t->flag |= T_MODAL;
1103         }
1104
1105         /* Crease needs edge flag */
1106         if (ELEM(t->mode, TFM_CREASE, TFM_BWEIGHT)) {
1107                 t->options |= CTX_EDGE;
1108         }
1109
1110         t->remove_on_cancel = false;
1111
1112         if (op && (prop = RNA_struct_find_property(op->ptr, "remove_on_cancel")) && RNA_property_is_set(op->ptr, prop)) {
1113                 if (RNA_property_boolean_get(op->ptr, prop)) {
1114                         t->remove_on_cancel = true;
1115                 }
1116         }
1117
1118         /* Assign the space type, some exceptions for running in different mode */
1119         if (sa == NULL) {
1120                 /* background mode */
1121                 t->spacetype = SPACE_EMPTY;
1122         }
1123         else if ((ar == NULL) && (sa->spacetype == SPACE_VIEW3D)) {
1124                 /* running in the text editor */
1125                 t->spacetype = SPACE_EMPTY;
1126         }
1127         else {
1128                 /* normal operation */
1129                 t->spacetype = sa->spacetype;
1130         }
1131
1132
1133         if (t->spacetype == SPACE_VIEW3D) {
1134                 View3D *v3d = sa->spacedata.first;
1135                 bScreen *animscreen = ED_screen_animation_playing(CTX_wm_manager(C));
1136                 
1137                 t->view = v3d;
1138                 t->animtimer = (animscreen) ? animscreen->animtimer : NULL;
1139                 
1140                 /* turn manipulator off during transform */
1141                 // FIXME: but don't do this when USING the manipulator...
1142                 if (t->flag & T_MODAL) {
1143                         t->twtype = v3d->twtype;
1144                         v3d->twtype = 0;
1145                 }
1146
1147                 if (v3d->flag & V3D_ALIGN) t->flag |= T_V3D_ALIGN;
1148                 t->around = v3d->around;
1149                 
1150                 /* bend always uses the cursor */
1151                 if (t->mode == TFM_BEND) {
1152                         t->around = V3D_CURSOR;
1153                 }
1154
1155                 if (op && ((prop = RNA_struct_find_property(op->ptr, "constraint_orientation")) &&
1156                            RNA_property_is_set(op->ptr, prop)))
1157                 {
1158                         t->current_orientation = RNA_property_enum_get(op->ptr, prop);
1159
1160                         if (t->current_orientation >= V3D_MANIP_CUSTOM + BIF_countTransformOrientation(C)) {
1161                                 t->current_orientation = V3D_MANIP_GLOBAL;
1162                         }
1163                 }
1164                 else {
1165                         t->current_orientation = v3d->twmode;
1166                 }
1167
1168                 /* exceptional case */
1169                 if (t->around == V3D_LOCAL && (t->settings->selectmode & SCE_SELECT_FACE)) {
1170                         if (ELEM3(t->mode, TFM_ROTATION, TFM_RESIZE, TFM_TRACKBALL)) {
1171                                 t->options |= CTX_NO_PET;
1172                         }
1173                 }
1174
1175                 /* initialize UV transform from */
1176                 if (op && ((prop = RNA_struct_find_property(op->ptr, "correct_uv")))) {
1177                         if (RNA_property_is_set(op->ptr, prop)) {
1178                                 if (RNA_property_boolean_get(op->ptr, prop)) {
1179                                         t->settings->uvcalc_flag |= UVCALC_TRANSFORM_CORRECT;
1180                                 }
1181                                 else {
1182                                         t->settings->uvcalc_flag &= ~UVCALC_TRANSFORM_CORRECT;
1183                                 }
1184                         }
1185                         else {
1186                                 RNA_property_boolean_set(op->ptr, prop, t->settings->uvcalc_flag & UVCALC_TRANSFORM_CORRECT);
1187                         }
1188                 }
1189
1190         }
1191         else if (t->spacetype == SPACE_IMAGE) {
1192                 SpaceImage *sima = sa->spacedata.first;
1193                 // XXX for now, get View2D from the active region
1194                 t->view = &ar->v2d;
1195                 t->around = sima->around;
1196
1197                 if (ED_space_image_show_uvedit(sima, t->obedit)) {
1198                         /* UV transform */
1199                 }
1200                 else if (sima->mode == SI_MODE_MASK) {
1201                         t->options |= CTX_MASK;
1202                 }
1203                 else {
1204                         /* image not in uv edit, nor in mask mode, can happen for some tools */
1205                 }
1206         }
1207         else if (t->spacetype == SPACE_NODE) {
1208                 // XXX for now, get View2D from the active region
1209                 t->view = &ar->v2d;
1210                 t->around = V3D_CENTER;
1211         }
1212         else if (t->spacetype == SPACE_IPO) {
1213                 SpaceIpo *sipo = sa->spacedata.first;
1214                 t->view = &ar->v2d;
1215                 t->around = sipo->around;
1216         }
1217         else if (t->spacetype == SPACE_CLIP) {
1218                 SpaceClip *sclip = sa->spacedata.first;
1219                 t->view = &ar->v2d;
1220                 t->around = sclip->around;
1221
1222                 if (ED_space_clip_check_show_trackedit(sclip))
1223                         t->options |= CTX_MOVIECLIP;
1224                 else if (ED_space_clip_check_show_maskedit(sclip))
1225                         t->options |= CTX_MASK;
1226         }
1227         else {
1228                 if (ar) {
1229                         // XXX for now, get View2D  from the active region
1230                         t->view = &ar->v2d;
1231                         // XXX for now, the center point is the midpoint of the data
1232                 }
1233                 else {
1234                         t->view = NULL;
1235                 }
1236                 t->around = V3D_CENTER;
1237         }
1238         
1239         if (op && ((prop = RNA_struct_find_property(op->ptr, "release_confirm")) &&
1240                    RNA_property_is_set(op->ptr, prop)))
1241         {
1242                 if (RNA_property_boolean_get(op->ptr, prop)) {
1243                         t->flag |= T_RELEASE_CONFIRM;
1244                 }
1245         }
1246         else {
1247                 if (U.flag & USER_RELEASECONFIRM) {
1248                         t->flag |= T_RELEASE_CONFIRM;
1249                 }
1250         }
1251
1252         if (op && ((prop = RNA_struct_find_property(op->ptr, "mirror")) &&
1253                    RNA_property_is_set(op->ptr, prop)))
1254         {
1255                 if (RNA_property_boolean_get(op->ptr, prop)) {
1256                         t->flag |= T_MIRROR;
1257                         t->mirror = 1;
1258                 }
1259         }
1260         // Need stuff to take it from edit mesh or whatnot here
1261         else if (t->spacetype == SPACE_VIEW3D) {
1262                 if (t->obedit && t->obedit->type == OB_MESH && (((Mesh *)t->obedit->data)->editflag & ME_EDIT_MIRROR_X)) {
1263                         t->flag |= T_MIRROR;
1264                         t->mirror = 1;
1265                 }
1266         }
1267         
1268         /* setting PET flag only if property exist in operator. Otherwise, assume it's not supported */
1269         if (op && (prop = RNA_struct_find_property(op->ptr, "proportional"))) {
1270                 if (RNA_property_is_set(op->ptr, prop)) {
1271                         t->flag |= initTransInfo_edit_pet_to_flag(RNA_property_enum_get(op->ptr, prop));
1272                 }
1273                 else {
1274                         /* use settings from scene only if modal */
1275                         if (t->flag & T_MODAL) {
1276                                 if ((t->options & CTX_NO_PET) == 0) {
1277                                         if (t->obedit) {
1278                                                 t->flag |= initTransInfo_edit_pet_to_flag(ts->proportional);
1279                                         }
1280                                         else if (t->options & CTX_MASK) {
1281                                                 if (ts->proportional_mask) {
1282                                                         t->flag |= T_PROP_EDIT;
1283
1284                                                         if (ts->proportional == PROP_EDIT_CONNECTED) {
1285                                                                 t->flag |= T_PROP_CONNECTED;
1286                                                         }
1287                                                 }
1288                                         }
1289                                         else if (t->obedit == NULL && ts->proportional_objects) {
1290                                                 t->flag |= T_PROP_EDIT;
1291                                         }
1292                                 }
1293                         }
1294                 }
1295                 
1296                 if (op && ((prop = RNA_struct_find_property(op->ptr, "proportional_size")) &&
1297                            RNA_property_is_set(op->ptr, prop)))
1298                 {
1299                         t->prop_size = RNA_property_float_get(op->ptr, prop);
1300                 }
1301                 else {
1302                         t->prop_size = ts->proportional_size;
1303                 }
1304                 
1305                 
1306                 /* TRANSFORM_FIX_ME rna restrictions */
1307                 if (t->prop_size <= 0.00001f) {
1308                         printf("Proportional size (%f) under 0.00001, resetting to 1!\n", t->prop_size);
1309                         t->prop_size = 1.0f;
1310                 }
1311                 
1312                 if (op && ((prop = RNA_struct_find_property(op->ptr, "proportional_edit_falloff")) &&
1313                            RNA_property_is_set(op->ptr, prop)))
1314                 {
1315                         t->prop_mode = RNA_property_enum_get(op->ptr, prop);
1316                 }
1317                 else {
1318                         t->prop_mode = ts->prop_mode;
1319                 }
1320         }
1321         else { /* add not pet option to context when not available */
1322                 t->options |= CTX_NO_PET;
1323         }
1324         
1325         // Mirror is not supported with PET, turn it off.
1326 #if 0
1327         if (t->flag & T_PROP_EDIT) {
1328                 t->flag &= ~T_MIRROR;
1329         }
1330 #endif
1331
1332         setTransformViewMatrices(t);
1333         initNumInput(&t->num);
1334 }
1335
1336 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
1337 void postTrans(bContext *C, TransInfo *t)
1338 {
1339         TransData *td;
1340         
1341         if (t->draw_handle_view)
1342                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle_view);
1343         if (t->draw_handle_apply)
1344                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle_apply);
1345         if (t->draw_handle_pixel)
1346                 ED_region_draw_cb_exit(t->ar->type, t->draw_handle_pixel);
1347         if (t->draw_handle_cursor)
1348                 WM_paint_cursor_end(CTX_wm_manager(C), t->draw_handle_cursor);
1349
1350         if (t->customFree) {
1351                 /* Can take over freeing t->data and data2d etc... */
1352                 t->customFree(t);
1353                 BLI_assert(t->customData == NULL);
1354         }
1355         else if ((t->customData != NULL) && (t->flag & T_FREE_CUSTOMDATA)) {
1356                 MEM_freeN(t->customData);
1357                 t->customData = NULL;
1358         }
1359
1360         /* postTrans can be called when nothing is selected, so data is NULL already */
1361         if (t->data) {
1362                 
1363                 /* free data malloced per trans-data */
1364                 if ((t->obedit && ELEM(t->obedit->type, OB_CURVE, OB_SURF)) ||
1365                     (t->spacetype == SPACE_IPO))
1366                 {
1367                         int a;
1368                         for (a = 0, td = t->data; a < t->total; a++, td++) {
1369                                 if (td->flag & TD_BEZTRIPLE) {
1370                                         MEM_freeN(td->hdata);
1371                                 }
1372                         }
1373                 }
1374                 MEM_freeN(t->data);
1375         }
1376         
1377         BLI_freelistN(&t->tsnap.points);
1378
1379         if (t->ext) MEM_freeN(t->ext);
1380         if (t->data2d) {
1381                 MEM_freeN(t->data2d);
1382                 t->data2d = NULL;
1383         }
1384         
1385         if (t->spacetype == SPACE_IMAGE) {
1386                 if (t->options & CTX_MASK) {
1387                         /* pass */
1388                 }
1389                 else {
1390                         SpaceImage *sima = t->sa->spacedata.first;
1391                         if (sima->flag & SI_LIVE_UNWRAP)
1392                                 ED_uvedit_live_unwrap_end(t->state == TRANS_CANCEL);
1393                 }
1394         }
1395         else if (t->spacetype == SPACE_VIEW3D) {
1396                 View3D *v3d = t->sa->spacedata.first;
1397                 /* restore manipulator */
1398                 if (t->flag & T_MODAL) {
1399                         v3d->twtype = t->twtype;
1400                 }
1401         }
1402         
1403         if (t->mouse.data) {
1404                 MEM_freeN(t->mouse.data);
1405         }
1406 }
1407
1408 void applyTransObjects(TransInfo *t)
1409 {
1410         TransData *td;
1411         
1412         for (td = t->data; td < t->data + t->total; td++) {
1413                 copy_v3_v3(td->iloc, td->loc);
1414                 if (td->ext->rot) {
1415                         copy_v3_v3(td->ext->irot, td->ext->rot);
1416                 }
1417                 if (td->ext->size) {
1418                         copy_v3_v3(td->ext->isize, td->ext->size);
1419                 }
1420         }
1421         recalcData(t);
1422 }
1423
1424 static void restoreElement(TransData *td)
1425 {
1426         /* TransData for crease has no loc */
1427         if (td->loc) {
1428                 copy_v3_v3(td->loc, td->iloc);
1429         }
1430         if (td->val) {
1431                 *td->val = td->ival;
1432         }
1433
1434         if (td->ext && (td->flag & TD_NO_EXT) == 0) {
1435                 if (td->ext->rot) {
1436                         copy_v3_v3(td->ext->rot, td->ext->irot);
1437                 }
1438                 if (td->ext->rotAngle) {
1439                         *td->ext->rotAngle = td->ext->irotAngle;
1440                 }
1441                 if (td->ext->rotAxis) {
1442                         copy_v3_v3(td->ext->rotAxis, td->ext->irotAxis);
1443                 }
1444                 /* XXX, drotAngle & drotAxis not used yet */
1445                 if (td->ext->size) {
1446                         copy_v3_v3(td->ext->size, td->ext->isize);
1447                 }
1448                 if (td->ext->quat) {
1449                         copy_qt_qt(td->ext->quat, td->ext->iquat);
1450                 }
1451         }
1452         
1453         if (td->flag & TD_BEZTRIPLE) {
1454                 *(td->hdata->h1) = td->hdata->ih1;
1455                 *(td->hdata->h2) = td->hdata->ih2;
1456         }
1457 }
1458
1459 void restoreTransObjects(TransInfo *t)
1460 {
1461         TransData *td;
1462         TransData2D *td2d;
1463
1464         for (td = t->data; td < t->data + t->total; td++) {
1465                 restoreElement(td);
1466         }
1467         
1468         for (td2d = t->data2d; t->data2d && td2d < t->data2d + t->total; td2d++) {
1469                 if (td2d->h1) {
1470                         td2d->h1[0] = td2d->ih1[0];
1471                         td2d->h1[1] = td2d->ih1[1];
1472                 }
1473                 if (td2d->h2) {
1474                         td2d->h2[0] = td2d->ih2[0];
1475                         td2d->h2[1] = td2d->ih2[1];
1476                 }
1477         }
1478
1479         unit_m3(t->mat);
1480         
1481         recalcData(t);
1482 }
1483
1484 void calculateCenter2D(TransInfo *t)
1485 {
1486         if (t->flag & (T_EDIT | T_POSE)) {
1487                 Object *ob = t->obedit ? t->obedit : t->poseobj;
1488                 float vec[3];
1489                 
1490                 copy_v3_v3(vec, t->center);
1491                 mul_m4_v3(ob->obmat, vec);
1492                 projectFloatView(t, vec, t->center2d);
1493         }
1494         else {
1495                 projectFloatView(t, t->center, t->center2d);
1496         }
1497 }
1498
1499 void calculateCenterCursor(TransInfo *t)
1500 {
1501         const float *cursor;
1502         
1503         cursor = ED_view3d_cursor3d_get(t->scene, t->view);
1504         copy_v3_v3(t->center, cursor);
1505         
1506         /* If edit or pose mode, move cursor in local space */
1507         if (t->flag & (T_EDIT | T_POSE)) {
1508                 Object *ob = t->obedit ? t->obedit : t->poseobj;
1509                 float mat[3][3], imat[3][3];
1510                 
1511                 sub_v3_v3v3(t->center, t->center, ob->obmat[3]);
1512                 copy_m3_m4(mat, ob->obmat);
1513                 invert_m3_m3(imat, mat);
1514                 mul_m3_v3(imat, t->center);
1515         }
1516         
1517         calculateCenter2D(t);
1518 }
1519
1520 void calculateCenterCursor2D(TransInfo *t)
1521 {
1522         float aspx = 1.0, aspy = 1.0;
1523         float *cursor = NULL;
1524         
1525         if (t->spacetype == SPACE_IMAGE) {
1526                 SpaceImage *sima = (SpaceImage *)t->sa->spacedata.first;
1527                 if (t->options & CTX_MASK) {
1528                         ED_space_image_get_aspect(sima, &aspx, &aspy);
1529                 }
1530                 else {
1531                         ED_space_image_get_uv_aspect(sima, &aspx, &aspy);
1532                 }
1533                 cursor = sima->cursor;
1534         }
1535         else if (t->spacetype == SPACE_CLIP) {
1536                 SpaceClip *space_clip = (SpaceClip *) t->sa->spacedata.first;
1537                 if (t->options & CTX_MOVIECLIP) {
1538                         ED_space_clip_get_aspect_dimension_aware(space_clip, &aspx, &aspy);
1539                 }
1540                 else {
1541                         ED_space_clip_get_aspect(space_clip, &aspx, &aspy);
1542                 }
1543                 cursor = space_clip->cursor;
1544         }
1545         
1546         if (cursor) {
1547                 if (t->options & CTX_MASK) {
1548                         float co[2];
1549                         float frame_size[2];
1550
1551                         if (t->spacetype == SPACE_IMAGE) {
1552                                 SpaceImage *sima = (SpaceImage *)t->sa->spacedata.first;
1553                                 ED_space_image_get_size_fl(sima, frame_size);
1554                                 BKE_mask_coord_from_frame(co, cursor, frame_size);
1555                                 ED_space_image_get_aspect(sima, &aspx, &aspy);
1556                         }
1557                         else if (t->spacetype == SPACE_CLIP) {
1558                                 SpaceClip *space_clip = (SpaceClip *) t->sa->spacedata.first;
1559                                 ED_space_clip_get_size_fl(space_clip, frame_size);
1560                                 BKE_mask_coord_from_frame(co, cursor, frame_size);
1561                                 ED_space_clip_get_aspect(space_clip, &aspx, &aspy);
1562                         }
1563                         else {
1564                                 BLI_assert(!"Shall not happen");
1565                         }
1566
1567                         t->center[0] = co[0] * aspx;
1568                         t->center[1] = co[1] * aspy;
1569                 }
1570                 else {
1571                         t->center[0] = cursor[0] * aspx;
1572                         t->center[1] = cursor[1] * aspy;
1573                 }
1574         }
1575         
1576         calculateCenter2D(t);
1577 }
1578
1579 static void calculateCenterCursorGraph2D(TransInfo *t)
1580 {
1581         SpaceIpo *sipo = (SpaceIpo *)t->sa->spacedata.first;
1582         Scene *scene = t->scene;
1583         
1584         /* cursor is combination of current frame, and graph-editor cursor value */
1585         t->center[0] = (float)(scene->r.cfra);
1586         t->center[1] = sipo->cursorVal;
1587         
1588         calculateCenter2D(t);
1589 }
1590
1591 void calculateCenterMedian(TransInfo *t)
1592 {
1593         float partial[3] = {0.0f, 0.0f, 0.0f};
1594         int total = 0;
1595         int i;
1596         
1597         for (i = 0; i < t->total; i++) {
1598                 if (t->data[i].flag & TD_SELECTED) {
1599                         if (!(t->data[i].flag & TD_NOCENTER)) {
1600                                 add_v3_v3(partial, t->data[i].center);
1601                                 total++;
1602                         }
1603                 }
1604         }
1605         if (i)
1606                 mul_v3_fl(partial, 1.0f / total);
1607         copy_v3_v3(t->center, partial);
1608         
1609         calculateCenter2D(t);
1610 }
1611
1612 void calculateCenterBound(TransInfo *t)
1613 {
1614         float max[3];
1615         float min[3];
1616         int i;
1617         for (i = 0; i < t->total; i++) {
1618                 if (i) {
1619                         if (t->data[i].flag & TD_SELECTED) {
1620                                 if (!(t->data[i].flag & TD_NOCENTER))
1621                                         minmax_v3v3_v3(min, max, t->data[i].center);
1622                         }
1623                 }
1624                 else {
1625                         copy_v3_v3(max, t->data[i].center);
1626                         copy_v3_v3(min, t->data[i].center);
1627                 }
1628         }
1629         mid_v3_v3v3(t->center, min, max);
1630
1631         calculateCenter2D(t);
1632 }
1633
1634 void calculateCenter(TransInfo *t)
1635 {
1636         switch (t->around) {
1637                 case V3D_CENTER:
1638                         calculateCenterBound(t);
1639                         break;
1640                 case V3D_CENTROID:
1641                         calculateCenterMedian(t);
1642                         break;
1643                 case V3D_CURSOR:
1644                         if (ELEM(t->spacetype, SPACE_IMAGE, SPACE_CLIP))
1645                                 calculateCenterCursor2D(t);
1646                         else if (t->spacetype == SPACE_IPO)
1647                                 calculateCenterCursorGraph2D(t);
1648                         else
1649                                 calculateCenterCursor(t);
1650                         break;
1651                 case V3D_LOCAL:
1652                         /* Individual element center uses median center for helpline and such */
1653                         calculateCenterMedian(t);
1654                         break;
1655                 case V3D_ACTIVE:
1656                 {
1657                         /* set median, and if if if... do object center */
1658                 
1659                         /* EDIT MODE ACTIVE EDITMODE ELEMENT */
1660
1661                         if (t->obedit) {
1662                                 if (t->obedit && t->obedit->type == OB_MESH) {
1663                                         BMEditSelection ese;
1664                                         BMEditMesh *em = BKE_editmesh_from_object(t->obedit);
1665
1666                                         if (BM_select_history_active_get(em->bm, &ese)) {
1667                                                 BM_editselection_center(&ese, t->center);
1668                                                 calculateCenter2D(t);
1669                                                 break;
1670                                         }
1671                                 }
1672                                 else if (ELEM(t->obedit->type, OB_CURVE, OB_SURF)) {
1673                                         float center[3];
1674                                         Curve *cu = (Curve *)t->obedit->data;
1675
1676                                         if (ED_curve_active_center(cu, center)) {
1677                                                 copy_v3_v3(t->center, center);
1678                                                 calculateCenter2D(t);
1679                                                 break;
1680                                         }
1681                                 }
1682                                 else if (t->obedit && t->obedit->type == OB_LATTICE) {
1683                                         BPoint *actbp = BKE_lattice_active_point_get(t->obedit->data);
1684
1685                                         if (actbp) {
1686                                                 copy_v3_v3(t->center, actbp->vec);
1687                                                 calculateCenter2D(t);
1688                                                 break;
1689                                         }
1690                                 }
1691                         } /* END EDIT MODE ACTIVE ELEMENT */
1692
1693                         calculateCenterMedian(t);
1694                         if ((t->flag & (T_EDIT | T_POSE)) == 0) {
1695                                 Scene *scene = t->scene;
1696                                 Object *ob = OBACT;
1697                                 if (ob) {
1698                                         copy_v3_v3(t->center, ob->obmat[3]);
1699                                         projectFloatView(t, t->center, t->center2d);
1700                                 }
1701                         }
1702                         break;
1703                 }
1704         }
1705         
1706         /* setting constraint center */
1707         copy_v3_v3(t->con.center, t->center);
1708         if (t->flag & (T_EDIT | T_POSE)) {
1709                 Object *ob = t->obedit ? t->obedit : t->poseobj;
1710                 mul_m4_v3(ob->obmat, t->con.center);
1711         }
1712         
1713         /* for panning from cameraview */
1714         if (t->flag & T_OBJECT) {
1715                 if (t->spacetype == SPACE_VIEW3D && t->ar && t->ar->regiontype == RGN_TYPE_WINDOW) {
1716                         View3D *v3d = t->view;
1717                         Scene *scene = t->scene;
1718                         RegionView3D *rv3d = t->ar->regiondata;
1719                         
1720                         if (v3d->camera == OBACT && rv3d->persp == RV3D_CAMOB) {
1721                                 float axis[3];
1722                                 /* persinv is nasty, use viewinv instead, always right */
1723                                 copy_v3_v3(axis, t->viewinv[2]);
1724                                 normalize_v3(axis);
1725                                 
1726                                 /* 6.0 = 6 grid units */
1727                                 axis[0] = t->center[0] - 6.0f * axis[0];
1728                                 axis[1] = t->center[1] - 6.0f * axis[1];
1729                                 axis[2] = t->center[2] - 6.0f * axis[2];
1730                                 
1731                                 projectFloatView(t, axis, t->center2d);
1732                                 
1733                                 /* rotate only needs correct 2d center, grab needs ED_view3d_calc_zfac() value */
1734                                 if (t->mode == TFM_TRANSLATION) {
1735                                         copy_v3_v3(t->center, axis);
1736                                         copy_v3_v3(t->con.center, t->center);
1737                                 }
1738                         }
1739                 }
1740         }
1741         
1742         if (t->spacetype == SPACE_VIEW3D) {
1743                 /* ED_view3d_calc_zfac() defines a factor for perspective depth correction, used in ED_view3d_win_to_delta() */
1744                 float vec[3];
1745                 if (t->flag & (T_EDIT | T_POSE)) {
1746                         Object *ob = t->obedit ? t->obedit : t->poseobj;
1747                         mul_v3_m4v3(vec, ob->obmat, t->center);
1748                 }
1749                 else {
1750                         copy_v3_v3(vec, t->center);
1751                 }
1752
1753                 /* zfac is only used convertViewVec only in cases operator was invoked in RGN_TYPE_WINDOW
1754                  * and never used in other cases.
1755                  *
1756                  * We need special case here as well, since ED_view3d_calc_zfac will crahs when called
1757                  * for a region different from RGN_TYPE_WINDOW.
1758                  */
1759                 if (t->ar->regiontype == RGN_TYPE_WINDOW) {
1760                         t->zfac = ED_view3d_calc_zfac(t->ar->regiondata, vec, NULL);
1761                 }
1762                 else {
1763                         t->zfac = 0.0f;
1764                 }
1765         }
1766 }
1767
1768 void calculatePropRatio(TransInfo *t)
1769 {
1770         TransData *td = t->data;
1771         int i;
1772         float dist;
1773         short connected = t->flag & T_PROP_CONNECTED;
1774
1775         if (t->flag & T_PROP_EDIT) {
1776                 for (i = 0; i < t->total; i++, td++) {
1777                         if (td->flag & TD_SELECTED) {
1778                                 td->factor = 1.0f;
1779                         }
1780                         else if (t->flag & T_MIRROR && td->loc[0] * t->mirror < -0.00001f) {
1781                                 td->flag |= TD_SKIP;
1782                                 td->factor = 0.0f;
1783                                 restoreElement(td);
1784                         }
1785                         else if ((connected && (td->flag & TD_NOTCONNECTED || td->dist > t->prop_size)) ||
1786                                  (connected == 0 && td->rdist > t->prop_size))
1787                         {
1788                                 /*
1789                                  * The elements are sorted according to their dist member in the array,
1790                                  * that means we can stop when it finds one element outside of the propsize.
1791                                  * do not set 'td->flag |= TD_NOACTION', the prop circle is being changed.
1792                                  */
1793                                 
1794                                 td->factor = 0.0f;
1795                                 restoreElement(td);
1796                         }
1797                         else {
1798                                 /* Use rdist for falloff calculations, it is the real distance */
1799                                 td->flag &= ~TD_NOACTION;
1800                                 
1801                                 if (connected)
1802                                         dist = (t->prop_size - td->dist) / t->prop_size;
1803                                 else
1804                                         dist = (t->prop_size - td->rdist) / t->prop_size;
1805
1806                                 /*
1807                                  * Clamp to positive numbers.
1808                                  * Certain corner cases with connectivity and individual centers
1809                                  * can give values of rdist larger than propsize.
1810                                  */
1811                                 if (dist < 0.0f)
1812                                         dist = 0.0f;
1813                                 
1814                                 switch (t->prop_mode) {
1815                                         case PROP_SHARP:
1816                                                 td->factor = dist * dist;
1817                                                 break;
1818                                         case PROP_SMOOTH:
1819                                                 td->factor = 3.0f * dist * dist - 2.0f * dist * dist * dist;
1820                                                 break;
1821                                         case PROP_ROOT:
1822                                                 td->factor = (float)sqrt(dist);
1823                                                 break;
1824                                         case PROP_LIN:
1825                                                 td->factor = dist;
1826                                                 break;
1827                                         case PROP_CONST:
1828                                                 td->factor = 1.0f;
1829                                                 break;
1830                                         case PROP_SPHERE:
1831                                                 td->factor = (float)sqrt(2 * dist - dist * dist);
1832                                                 break;
1833                                         case PROP_RANDOM:
1834                                                 td->factor = BLI_frand() * dist;
1835                                                 break;
1836                                         default:
1837                                                 td->factor = 1;
1838                                                 break;
1839                                 }
1840                         }
1841                 }
1842                 switch (t->prop_mode) {
1843                         case PROP_SHARP:
1844                                 strcpy(t->proptext, IFACE_("(Sharp)"));
1845                                 break;
1846                         case PROP_SMOOTH:
1847                                 strcpy(t->proptext, IFACE_("(Smooth)"));
1848                                 break;
1849                         case PROP_ROOT:
1850                                 strcpy(t->proptext, IFACE_("(Root)"));
1851                                 break;
1852                         case PROP_LIN:
1853                                 strcpy(t->proptext, IFACE_("(Linear)"));
1854                                 break;
1855                         case PROP_CONST:
1856                                 strcpy(t->proptext, IFACE_("(Constant)"));
1857                                 break;
1858                         case PROP_SPHERE:
1859                                 strcpy(t->proptext, IFACE_("(Sphere)"));
1860                                 break;
1861                         case PROP_RANDOM:
1862                                 strcpy(t->proptext, IFACE_("(Random)"));
1863                                 break;
1864                         default:
1865                                 t->proptext[0] = '\0';
1866                                 break;
1867                 }
1868         }
1869         else {
1870                 for (i = 0; i < t->total; i++, td++) {
1871                         td->factor = 1.0;
1872                 }
1873                 t->proptext[0] = '\0';
1874         }
1875 }