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