Welcome to mirror list, hosted at ThFree Co, Russian Federation.

transform_conversions.c « transform « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 12e65e9d287e21cbab74cc4ac0e72f28356f10fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
/*
 * $Id$
 *
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/editors/transform/transform_conversions.c
 *  \ingroup edtransform
 */


#ifndef WIN32
#include <unistd.h>
#else
#include <io.h>
#endif
#include <string.h>
#include <math.h>

#include "DNA_anim_types.h"
#include "DNA_armature_types.h"
#include "DNA_lattice_types.h"
#include "DNA_meta_types.h"
#include "DNA_node_types.h"
#include "DNA_screen_types.h"
#include "DNA_space_types.h"
#include "DNA_sequence_types.h"
#include "DNA_view3d_types.h"
#include "DNA_constraint_types.h"
#include "DNA_scene_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_gpencil_types.h"

#include "MEM_guardedalloc.h"

#include "BKE_action.h"
#include "BKE_armature.h"
#include "BKE_context.h"
#include "BKE_curve.h"
#include "BKE_constraint.h"
#include "BKE_depsgraph.h"
#include "BKE_fcurve.h"
#include "BKE_gpencil.h"
#include "BKE_global.h"
#include "BKE_key.h"
#include "BKE_main.h"
#include "BKE_modifier.h"
#include "BKE_nla.h"
#include "BKE_object.h"
#include "BKE_particle.h"
#include "BKE_sequencer.h"
#include "BKE_pointcache.h"
#include "BKE_bmesh.h"
#include "BKE_scene.h"
#include "BKE_report.h"


#include "ED_anim_api.h"
#include "ED_armature.h"
#include "ED_particle.h"
#include "ED_image.h"
#include "ED_keyframing.h"
#include "ED_keyframes_edit.h"
#include "ED_object.h"
#include "ED_markers.h"
#include "ED_mesh.h"
#include "ED_node.h"
#include "ED_types.h"
#include "ED_uvedit.h"
#include "ED_util.h"  /* for crazyspace correction */

#include "UI_view2d.h"

#include "BLI_math.h"
#include "BLI_blenlib.h"
#include "BLI_editVert.h"
#include "BLI_utildefines.h"

#include "RNA_access.h"

extern ListBase editelems;

#include "transform.h"

#include "BLO_sys_types.h" // for intptr_t support

/* local function prototype - for Object/Bone Constraints */
static short constraints_list_needinv(TransInfo *t, ListBase *list);

/* ************************** Functions *************************** */

static void qsort_trans_data(TransInfo *t, TransData *head, TransData *tail, TransData *temp) {
	TransData *ihead = head;
	TransData *itail = tail;
	*temp = *head;

	while (head < tail)
	{
		if (t->flag & T_PROP_CONNECTED) {
			while ((tail->dist >= temp->dist) && (head < tail))
				tail--;
		}
		else {
			while ((tail->rdist >= temp->rdist) && (head < tail))
				tail--;
		}

		if (head != tail)
		{
			*head = *tail;
			head++;
		}

		if (t->flag & T_PROP_CONNECTED) {
			while ((head->dist <= temp->dist) && (head < tail))
				head++;
		}
		else {
			while ((head->rdist <= temp->rdist) && (head < tail))
				head++;
		}

		if (head != tail)
		{
			*tail = *head;
			tail--;
		}
	}

	*head = *temp;
	if (ihead < head) {
		qsort_trans_data(t, ihead, head-1, temp);
	}
	if (itail > head) {
		qsort_trans_data(t, head+1, itail, temp);
	}
}

void sort_trans_data_dist(TransInfo *t) {
	TransData temp;
	TransData *start = t->data;
	int i = 1;

	while(i < t->total && start->flag & TD_SELECTED) {
		start++;
		i++;
	}
	qsort_trans_data(t, start, t->data + t->total - 1, &temp);
}

static void sort_trans_data(TransInfo *t)
{
	TransData *sel, *unsel;
	TransData temp;
	unsel = t->data;
	sel = t->data;
	sel += t->total - 1;
	while (sel > unsel) {
		while (unsel->flag & TD_SELECTED) {
			unsel++;
			if (unsel == sel) {
				return;
			}
		}
		while (!(sel->flag & TD_SELECTED)) {
			sel--;
			if (unsel == sel) {
				return;
			}
		}
		temp = *unsel;
		*unsel = *sel;
		*sel = temp;
		sel--;
		unsel++;
	}
}

/* distance calculated from not-selected vertex to nearest selected vertex
   warning; this is loops inside loop, has minor N^2 issues, but by sorting list it is OK */
static void set_prop_dist(TransInfo *t, short with_dist)
{
	TransData *tob;
	int a;

	for(a=0, tob= t->data; a<t->total; a++, tob++) {

		tob->rdist= 0.0f; // init, it was mallocced

		if((tob->flag & TD_SELECTED)==0) {
			TransData *td;
			int i;
			float dist, vec[3];

			tob->rdist = -1.0f; // signal for next loop

			for (i = 0, td= t->data; i < t->total; i++, td++) {
				if(td->flag & TD_SELECTED) {
					sub_v3_v3v3(vec, tob->center, td->center);
					mul_m3_v3(tob->mtx, vec);
					dist = normalize_v3(vec);
					if (tob->rdist == -1.0f) {
						tob->rdist = dist;
					}
					else if (dist < tob->rdist) {
						tob->rdist = dist;
					}
				}
				else break;	// by definition transdata has selected items in beginning
			}
			if (with_dist) {
				tob->dist = tob->rdist;
			}
		}
	}
}

/* ************************** CONVERSIONS ************************* */

/* ********************* texture space ********* */

static void createTransTexspace(TransInfo *t)
{
	Scene *scene = t->scene;
	TransData *td;
	Object *ob;
	ID *id;
	short *texflag;

	ob = OBACT;

	if (ob == NULL) { // Shouldn't logically happen, but still...
		t->total = 0;
		return;
	}

	id = ob->data;
	if(id == NULL || !ELEM3( GS(id->name), ID_ME, ID_CU, ID_MB )) {
		t->total = 0;
		return;
	}

	t->total = 1;
	td= t->data= MEM_callocN(sizeof(TransData), "TransTexspace");
	td->ext= t->ext= MEM_callocN(sizeof(TransDataExtension), "TransTexspace");

	td->flag= TD_SELECTED;
	copy_v3_v3(td->center, ob->obmat[3]);
	td->ob = ob;

	copy_m3_m4(td->mtx, ob->obmat);
	copy_m3_m4(td->axismtx, ob->obmat);
	normalize_m3(td->axismtx);
	invert_m3_m3(td->smtx, td->mtx);

	if (give_obdata_texspace(ob, &texflag, &td->loc, &td->ext->size, &td->ext->rot)) {
		ob->dtx |= OB_TEXSPACE;
		*texflag &= ~AUTOSPACE;
	}

	copy_v3_v3(td->iloc, td->loc);
	copy_v3_v3(td->ext->irot, td->ext->rot);
	copy_v3_v3(td->ext->isize, td->ext->size);
}

/* ********************* edge (for crease) ***** */

static void createTransEdge(TransInfo *t) {
	EditMesh *em = ((Mesh *)t->obedit->data)->edit_mesh;
	TransData *td = NULL;
	EditEdge *eed;
	float mtx[3][3], smtx[3][3];
	int count=0, countsel=0;
	int propmode = t->flag & T_PROP_EDIT;

	for(eed= em->edges.first; eed; eed= eed->next) {
		if(eed->h==0) {
			if (eed->f & SELECT) countsel++;
			if (propmode) count++;
		}
	}

	if (countsel == 0)
		return;

	if(propmode) {
		t->total = count;
	}
	else {
		t->total = countsel;
	}

	td= t->data= MEM_callocN(t->total * sizeof(TransData), "TransCrease");

	copy_m3_m4(mtx, t->obedit->obmat);
	invert_m3_m3(smtx, mtx);

	for(eed= em->edges.first; eed; eed= eed->next) {
		if(eed->h==0 && (eed->f & SELECT || propmode)) {
			/* need to set center for center calculations */
			add_v3_v3v3(td->center, eed->v1->co, eed->v2->co);
			mul_v3_fl(td->center, 0.5f);

			td->loc= NULL;
			if (eed->f & SELECT)
				td->flag= TD_SELECTED;
			else
				td->flag= 0;


			copy_m3_m3(td->smtx, smtx);
			copy_m3_m3(td->mtx, mtx);

			td->ext = NULL;
			if (t->mode == TFM_BWEIGHT) {
				td->val = &(eed->bweight);
				td->ival = eed->bweight;
			}
			else {
				td->val = &(eed->crease);
				td->ival = eed->crease;
			}

			td++;
		}
	}
}

/* ********************* pose mode ************* */

static bKinematicConstraint *has_targetless_ik(bPoseChannel *pchan)
{
	bConstraint *con= pchan->constraints.first;

	for(;con; con= con->next) {
		if(con->type==CONSTRAINT_TYPE_KINEMATIC && (con->enforce!=0.0f)) {
			bKinematicConstraint *data= con->data;

			if(data->tar==NULL)
				return data;
			if(data->tar->type==OB_ARMATURE && data->subtarget[0]==0)
				return data;
		}
	}
	return NULL;
}

static short apply_targetless_ik(Object *ob)
{
	bPoseChannel *pchan, *parchan, *chanlist[256];
	bKinematicConstraint *data;
	int segcount, apply= 0;

	/* now we got a difficult situation... we have to find the
	   target-less IK pchans, and apply transformation to the all
	   pchans that were in the chain */

	for (pchan=ob->pose->chanbase.first; pchan; pchan=pchan->next) {
		data= has_targetless_ik(pchan);
		if(data && (data->flag & CONSTRAINT_IK_AUTO)) {

			/* fill the array with the bones of the chain (armature.c does same, keep it synced) */
			segcount= 0;

			/* exclude tip from chain? */
			if(!(data->flag & CONSTRAINT_IK_TIP))
				parchan= pchan->parent;
			else
				parchan= pchan;

			/* Find the chain's root & count the segments needed */
			for (; parchan; parchan=parchan->parent){
				chanlist[segcount]= parchan;
				segcount++;

				if(segcount==data->rootbone || segcount>255) break; // 255 is weak
			}
			for(;segcount;segcount--) {
				Bone *bone;
				float rmat[4][4], tmat[4][4], imat[4][4];

				/* pose_mat(b) = pose_mat(b-1) * offs_bone * channel * constraint * IK  */
				/* we put in channel the entire result of rmat= (channel * constraint * IK) */
				/* pose_mat(b) = pose_mat(b-1) * offs_bone * rmat  */
				/* rmat = pose_mat(b) * inv( pose_mat(b-1) * offs_bone ) */

				parchan= chanlist[segcount-1];
				bone= parchan->bone;
				bone->flag |= BONE_TRANSFORM;	/* ensures it gets an auto key inserted */

				if(parchan->parent) {
					Bone *parbone= parchan->parent->bone;
					float offs_bone[4][4];

					/* offs_bone =  yoffs(b-1) + root(b) + bonemat(b) */
					copy_m4_m3(offs_bone, bone->bone_mat);

					/* The bone's root offset (is in the parent's coordinate system) */
					copy_v3_v3(offs_bone[3], bone->head);

					/* Get the length translation of parent (length along y axis) */
					offs_bone[3][1]+= parbone->length;

					/* pose_mat(b-1) * offs_bone */
					if(parchan->bone->flag & BONE_HINGE) {
						/* the rotation of the parent restposition */
						copy_m4_m4(rmat, parbone->arm_mat);	/* rmat used as temp */

						/* the location of actual parent transform */
						copy_v3_v3(rmat[3], offs_bone[3]);
						offs_bone[3][0]= offs_bone[3][1]= offs_bone[3][2]= 0.0f;
						mul_m4_v3(parchan->parent->pose_mat, rmat[3]);

						mul_m4_m4m4(tmat, offs_bone, rmat);
					}
					else if(parchan->bone->flag & BONE_NO_SCALE) {
						mul_m4_m4m4(tmat, offs_bone, parchan->parent->pose_mat);
						normalize_m4(tmat);
					}
					else
						mul_m4_m4m4(tmat, offs_bone, parchan->parent->pose_mat);

					invert_m4_m4(imat, tmat);
				}
				else {
					copy_m4_m3(tmat, bone->bone_mat);

					copy_v3_v3(tmat[3], bone->head);
					invert_m4_m4(imat, tmat);
				}
				/* result matrix */
				mul_m4_m4m4(rmat, parchan->pose_mat, imat);

				/* apply and decompose, doesn't work for constraints or non-uniform scale well */
				{
					float rmat3[3][3], qrmat[3][3], imat3[3][3], smat[3][3];
					
					copy_m3_m4(rmat3, rmat);
					
					/* rotation */
						/* [#22409] is partially caused by this, as slight numeric error introduced during 
						 * the solving process leads to locked-axis values changing. However, we cannot modify
						 * the values here, or else there are huge discreptancies between IK-solver (interactive)
						 * and applied poses.
						 */
					if (parchan->rotmode > 0)
						mat3_to_eulO(parchan->eul, parchan->rotmode,rmat3);
					else if (parchan->rotmode == ROT_MODE_AXISANGLE)
						mat3_to_axis_angle(parchan->rotAxis, &parchan->rotAngle,rmat3);
					else
						mat3_to_quat(parchan->quat,rmat3);
					
					/* for size, remove rotation */
					/* causes problems with some constraints (so apply only if needed) */
					if (data->flag & CONSTRAINT_IK_STRETCH) {
						if (parchan->rotmode > 0)
							eulO_to_mat3( qrmat,parchan->eul, parchan->rotmode);
						else if (parchan->rotmode == ROT_MODE_AXISANGLE)
							axis_angle_to_mat3( qrmat,parchan->rotAxis, parchan->rotAngle);
						else
							quat_to_mat3( qrmat,parchan->quat);
						
						invert_m3_m3(imat3, qrmat);
						mul_m3_m3m3(smat, rmat3, imat3);
						mat3_to_size( parchan->size,smat);
					}
					
					/* causes problems with some constraints (e.g. childof), so disable this */
					/* as it is IK shouldn't affect location directly */
					/* copy_v3_v3(parchan->loc, rmat[3]); */
				}

			}

			apply= 1;
			data->flag &= ~CONSTRAINT_IK_AUTO;
		}
	}

	return apply;
}

static void add_pose_transdata(TransInfo *t, bPoseChannel *pchan, Object *ob, TransData *td)
{
	Bone *bone= pchan->bone;
	float pmat[3][3], omat[3][3], bmat[3][3];
	float cmat[3][3], tmat[3][3];
	float vec[3];

	copy_v3_v3(vec, pchan->pose_mat[3]);
	copy_v3_v3(td->center, vec);

	td->ob = ob;
	td->flag = TD_SELECTED;
	if (bone->flag & BONE_HINGE_CHILD_TRANSFORM)
	{
		td->flag |= TD_NOCENTER;
	}

	if (bone->flag & BONE_TRANSFORM_CHILD)
	{
		td->flag |= TD_NOCENTER;
		td->flag |= TD_NO_LOC;
	}

	td->protectflag= pchan->protectflag;

	td->loc = pchan->loc;
	copy_v3_v3(td->iloc, pchan->loc);

	td->ext->size= pchan->size;
	copy_v3_v3(td->ext->isize, pchan->size);

	if (pchan->rotmode > 0) {
		td->ext->rot= pchan->eul;
		td->ext->rotAxis= NULL;
		td->ext->rotAngle= NULL;
		td->ext->quat= NULL;
		
		copy_v3_v3(td->ext->irot, pchan->eul);
	}
	else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
		td->ext->rot= NULL;
		td->ext->rotAxis= pchan->rotAxis;
		td->ext->rotAngle= &pchan->rotAngle;
		td->ext->quat= NULL;
		
		td->ext->irotAngle= pchan->rotAngle;
		copy_v3_v3(td->ext->irotAxis, pchan->rotAxis);
	}
	else {
		td->ext->rot= NULL;
		td->ext->rotAxis= NULL;
		td->ext->rotAngle= NULL;
		td->ext->quat= pchan->quat;
		
		QUATCOPY(td->ext->iquat, pchan->quat);
	}
	td->ext->rotOrder= pchan->rotmode;
	

	/* proper way to get parent transform + own transform + constraints transform */
	copy_m3_m4(omat, ob->obmat);

	if (ELEM(t->mode, TFM_TRANSLATION, TFM_RESIZE) && (pchan->bone->flag & BONE_NO_LOCAL_LOCATION))
		unit_m3(bmat);
	else
		copy_m3_m3(bmat, pchan->bone->bone_mat);

	if (pchan->parent) {
		if(pchan->bone->flag & BONE_HINGE)
			copy_m3_m4(pmat, pchan->parent->bone->arm_mat);
		else
			copy_m3_m4(pmat, pchan->parent->pose_mat);

		if (constraints_list_needinv(t, &pchan->constraints)) {
			copy_m3_m4(tmat, pchan->constinv);
			invert_m3_m3(cmat, tmat);
			mul_serie_m3(td->mtx, bmat, pmat, omat, cmat, NULL,NULL,NULL,NULL);    // dang mulserie swaps args
		}
		else
			mul_serie_m3(td->mtx, bmat, pmat, omat, NULL,NULL,NULL,NULL,NULL);    // dang mulserie swaps args
	}
	else {
		if (constraints_list_needinv(t, &pchan->constraints)) {
			copy_m3_m4(tmat, pchan->constinv);
			invert_m3_m3(cmat, tmat);
			mul_serie_m3(td->mtx, bmat, omat, cmat, NULL,NULL,NULL,NULL,NULL);    // dang mulserie swaps args
		}
		else
			mul_m3_m3m3(td->mtx, omat, bmat);  // Mat3MulMat3 has swapped args!
	}

	invert_m3_m3(td->smtx, td->mtx);

	/* exceptional case: rotate the pose bone which also applies transformation
	 * when a parentless bone has BONE_NO_LOCAL_LOCATION [] */
	if (!ELEM(t->mode, TFM_TRANSLATION, TFM_RESIZE) && (pchan->bone->flag & BONE_NO_LOCAL_LOCATION)) {
		if(pchan->parent) {
			/* same as td->smtx but without pchan->bone->bone_mat */
			td->flag |= TD_PBONE_LOCAL_MTX_C;
			mul_m3_m3m3(td->ext->l_smtx, pchan->bone->bone_mat, td->smtx);
		}
		else {
			td->flag |= TD_PBONE_LOCAL_MTX_P;
		}
	}
	
	/* for axismat we use bone's own transform */
	copy_m3_m4(pmat, pchan->pose_mat);
	mul_m3_m3m3(td->axismtx, omat, pmat);
	normalize_m3(td->axismtx);

	if (t->mode==TFM_BONESIZE) {
		bArmature *arm= t->poseobj->data;

		if(arm->drawtype==ARM_ENVELOPE) {
			td->loc= NULL;
			td->val= &bone->dist;
			td->ival= bone->dist;
		}
		else {
			// abusive storage of scale in the loc pointer :)
			td->loc= &bone->xwidth;
			copy_v3_v3(td->iloc, td->loc);
			td->val= NULL;
		}
	}

	/* in this case we can do target-less IK grabbing */
	if (t->mode==TFM_TRANSLATION) {
		bKinematicConstraint *data= has_targetless_ik(pchan);
		if(data) {
			if(data->flag & CONSTRAINT_IK_TIP) {
				copy_v3_v3(data->grabtarget, pchan->pose_tail);
			}
			else {
				copy_v3_v3(data->grabtarget, pchan->pose_head);
			}
			td->loc = data->grabtarget;
			copy_v3_v3(td->iloc, td->loc);
			data->flag |= CONSTRAINT_IK_AUTO;

			/* only object matrix correction */
			copy_m3_m3(td->mtx, omat);
			invert_m3_m3(td->smtx, td->mtx);
		}
	}

	/* store reference to first constraint */
	td->con= pchan->constraints.first;
}

static void bone_children_clear_transflag(int mode, short around, ListBase *lb)
{
	Bone *bone= lb->first;

	for(;bone;bone= bone->next) {
		if((bone->flag & BONE_HINGE) && (bone->flag & BONE_CONNECTED))
		{
			bone->flag |= BONE_HINGE_CHILD_TRANSFORM;
		}
		else if (bone->flag & BONE_TRANSFORM && (mode == TFM_ROTATION || mode == TFM_TRACKBALL) && around == V3D_LOCAL)
		{
			bone->flag |= BONE_TRANSFORM_CHILD;
		}
		else
		{
			bone->flag &= ~BONE_TRANSFORM;
		}

		bone_children_clear_transflag(mode, around, &bone->childbase);
	}
}

/* sets transform flags in the bones
 * returns total number of bones with BONE_TRANSFORM */
int count_set_pose_transflags(int *out_mode, short around, Object *ob)
{
	bArmature *arm= ob->data;
	bPoseChannel *pchan;
	Bone *bone;
	int mode = *out_mode;
	int hastranslation = 0;
	int total = 0;

	for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
		bone = pchan->bone;
		if (PBONE_VISIBLE(arm, bone)) {
			if ((bone->flag & BONE_SELECTED))
				bone->flag |= BONE_TRANSFORM;
			else
				bone->flag &= ~BONE_TRANSFORM;
			
			bone->flag &= ~BONE_HINGE_CHILD_TRANSFORM;
			bone->flag &= ~BONE_TRANSFORM_CHILD;
		}
		else
			bone->flag &= ~BONE_TRANSFORM;
	}

	/* make sure no bone can be transformed when a parent is transformed */
	/* since pchans are depsgraph sorted, the parents are in beginning of list */
	if(mode != TFM_BONESIZE) {
		for(pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
			bone = pchan->bone;
			if(bone->flag & BONE_TRANSFORM)
				bone_children_clear_transflag(mode, around, &bone->childbase);
		}
	}
	/* now count, and check if we have autoIK or have to switch from translate to rotate */
	hastranslation = 0;

	for(pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
		bone = pchan->bone;
		if(bone->flag & BONE_TRANSFORM) {
			total++;
			
			if(mode == TFM_TRANSLATION) {
				if( has_targetless_ik(pchan)==NULL ) {
					if(pchan->parent && (pchan->bone->flag & BONE_CONNECTED)) {
						if(pchan->bone->flag & BONE_HINGE_CHILD_TRANSFORM)
							hastranslation = 1;
					}
					else if((pchan->protectflag & OB_LOCK_LOC)!=OB_LOCK_LOC)
						hastranslation = 1;
				}
				else
					hastranslation = 1;
			}
		}
	}

	/* if there are no translatable bones, do rotation */
	if(mode == TFM_TRANSLATION && !hastranslation)
	{
		*out_mode = TFM_ROTATION;
	}

	return total;
}


/* -------- Auto-IK ---------- */

/* adjust pose-channel's auto-ik chainlen */
static void pchan_autoik_adjust (bPoseChannel *pchan, short chainlen)
{
	bConstraint *con;

	/* don't bother to search if no valid constraints */
	if ((pchan->constflag & (PCHAN_HAS_IK|PCHAN_HAS_TARGET))==0)
		return;

	/* check if pchan has ik-constraint */
	for (con= pchan->constraints.first; con; con= con->next) {
		if (con->type == CONSTRAINT_TYPE_KINEMATIC && (con->enforce!=0.0f)) {
			bKinematicConstraint *data= con->data;
			
			/* only accept if a temporary one (for auto-ik) */
			if (data->flag & CONSTRAINT_IK_TEMP) {
				/* chainlen is new chainlen, but is limited by maximum chainlen */
				if ((chainlen==0) || (chainlen > data->max_rootbone))
					data->rootbone= data->max_rootbone;
				else
					data->rootbone= chainlen;
			}
		}
	}
}

/* change the chain-length of auto-ik */
void transform_autoik_update (TransInfo *t, short mode)
{
	short *chainlen= &t->settings->autoik_chainlen;
	bPoseChannel *pchan;

	/* mode determines what change to apply to chainlen */
	if (mode == 1) {
		/* mode=1 is from WHEELMOUSEDOWN... increases len */
		(*chainlen)++;
	}
	else if (mode == -1) {
		/* mode==-1 is from WHEELMOUSEUP... decreases len */
		if (*chainlen > 0) (*chainlen)--;
	}

	/* sanity checks (don't assume t->poseobj is set, or that it is an armature) */
	if (ELEM(NULL, t->poseobj, t->poseobj->pose))
		return;

	/* apply to all pose-channels */
	for (pchan=t->poseobj->pose->chanbase.first; pchan; pchan=pchan->next) {
		pchan_autoik_adjust(pchan, *chainlen);
	}
}

/* frees temporal IKs */
static void pose_grab_with_ik_clear(Object *ob)
{
	bKinematicConstraint *data;
	bPoseChannel *pchan;
	bConstraint *con, *next;

	for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
		/* clear all temporary lock flags */
		pchan->ikflag &= ~(BONE_IK_NO_XDOF_TEMP|BONE_IK_NO_YDOF_TEMP|BONE_IK_NO_ZDOF_TEMP);
		
		pchan->constflag &= ~(PCHAN_HAS_IK|PCHAN_HAS_TARGET);
		
		/* remove all temporary IK-constraints added */
		for (con= pchan->constraints.first; con; con= next) {
			next= con->next;
			if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
				data= con->data;
				if (data->flag & CONSTRAINT_IK_TEMP) {
					BLI_remlink(&pchan->constraints, con);
					MEM_freeN(con->data);
					MEM_freeN(con);
					continue;
				}
				pchan->constflag |= PCHAN_HAS_IK;
				if(data->tar==NULL || (data->tar->type==OB_ARMATURE && data->subtarget[0]==0))
					pchan->constflag |= PCHAN_HAS_TARGET;
			}
		}
	}
}

/* adds the IK to pchan - returns if added */
static short pose_grab_with_ik_add(bPoseChannel *pchan)
{
	bKinematicConstraint *targetless = NULL;
	bKinematicConstraint *data;
	bConstraint *con;

	/* Sanity check */
	if (pchan == NULL)
		return 0;

	/* Rule: not if there's already an IK on this channel */
	for (con= pchan->constraints.first; con; con= con->next) {
		if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
			data= con->data;
			
			if (data->tar==NULL || (data->tar->type==OB_ARMATURE && data->subtarget[0]=='\0')) {
				/* make reference to constraint to base things off later (if it's the last targetless constraint encountered) */
				targetless = (bKinematicConstraint *)con->data;
				
				/* but, if this is a targetless IK, we make it auto anyway (for the children loop) */
				if (con->enforce!=0.0f) {
					data->flag |= CONSTRAINT_IK_AUTO;
					
					/* if no chain length has been specified, just make things obey standard rotation locks too */
					if (data->rootbone == 0) {
						for (; pchan; pchan=pchan->parent) {
							/* here, we set ik-settings for bone from pchan->protectflag */
							// XXX: careful with quats/axis-angle rotations where we're locking 4d components
							if (pchan->protectflag & OB_LOCK_ROTX) pchan->ikflag |= BONE_IK_NO_XDOF_TEMP;
							if (pchan->protectflag & OB_LOCK_ROTY) pchan->ikflag |= BONE_IK_NO_YDOF_TEMP;
							if (pchan->protectflag & OB_LOCK_ROTZ) pchan->ikflag |= BONE_IK_NO_ZDOF_TEMP;
						}
					}
					
					return 0; 
				}
			}
			
			if ((con->flag & CONSTRAINT_DISABLE)==0 && (con->enforce!=0.0f))
				return 0;
		}
	}

	con = add_pose_constraint(NULL, pchan, "TempConstraint", CONSTRAINT_TYPE_KINEMATIC);
	pchan->constflag |= (PCHAN_HAS_IK|PCHAN_HAS_TARGET);	/* for draw, but also for detecting while pose solving */
	data= con->data;
	if (targetless) { 
		/* if exists, use values from last targetless (but disabled) IK-constraint as base */
		*data = *targetless;
	}
	else
		data->flag= CONSTRAINT_IK_TIP;
	data->flag |= CONSTRAINT_IK_TEMP|CONSTRAINT_IK_AUTO;
	copy_v3_v3(data->grabtarget, pchan->pose_tail);
	data->rootbone= 0; /* watch-it! has to be 0 here, since we're still on the same bone for the first time through the loop [#25885] */
	
	/* we only include bones that are part of a continual connected chain */
	while (pchan) {
		/* here, we set ik-settings for bone from pchan->protectflag */
		// XXX: careful with quats/axis-angle rotations where we're locking 4d components
		if (pchan->protectflag & OB_LOCK_ROTX) pchan->ikflag |= BONE_IK_NO_XDOF_TEMP;
		if (pchan->protectflag & OB_LOCK_ROTY) pchan->ikflag |= BONE_IK_NO_YDOF_TEMP;
		if (pchan->protectflag & OB_LOCK_ROTZ) pchan->ikflag |= BONE_IK_NO_ZDOF_TEMP;
		
		/* now we count this pchan as being included */
		data->rootbone++;
		
		/* continue to parent, but only if we're connected to it */
		if (pchan->bone->flag & BONE_CONNECTED)
			pchan = pchan->parent;
		else
			pchan = NULL;
	}

	/* make a copy of maximum chain-length */
	data->max_rootbone= data->rootbone;

	return 1;
}

/* bone is a candidate to get IK, but we don't do it if it has children connected */
static short pose_grab_with_ik_children(bPose *pose, Bone *bone)
{
	Bone *bonec;
	short wentdeeper=0, added=0;

	/* go deeper if children & children are connected */
	for (bonec= bone->childbase.first; bonec; bonec= bonec->next) {
		if (bonec->flag & BONE_CONNECTED) {
			wentdeeper= 1;
			added+= pose_grab_with_ik_children(pose, bonec);
		}
	}
	if (wentdeeper==0) {
		bPoseChannel *pchan= get_pose_channel(pose, bone->name);
		if (pchan)
			added+= pose_grab_with_ik_add(pchan);
	}

	return added;
}

/* main call which adds temporal IK chains */
static short pose_grab_with_ik(Object *ob)
{
	bArmature *arm;
	bPoseChannel *pchan, *parent;
	Bone *bonec;
	short tot_ik= 0;

	if ((ob==NULL) || (ob->pose==NULL) || (ob->mode & OB_MODE_POSE)==0)
		return 0;

	arm = ob->data;

	/* Rule: allow multiple Bones (but they must be selected, and only one ik-solver per chain should get added) */
	for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
		if (pchan->bone->layer & arm->layer) {
			if (pchan->bone->flag & BONE_SELECTED) {
				/* Rule: no IK for solitatry (unconnected) bones */
				for (bonec=pchan->bone->childbase.first; bonec; bonec=bonec->next) {
					if (bonec->flag & BONE_CONNECTED) {
						break;
					}
				}
				if ((pchan->bone->flag & BONE_CONNECTED)==0 && (bonec == NULL))
					continue;

				/* rule: if selected Bone is not a root bone, it gets a temporal IK */
				if (pchan->parent) {
					/* only adds if there's no IK yet (and no parent bone was selected) */
					for (parent= pchan->parent; parent; parent= parent->parent) {
						if (parent->bone->flag & BONE_SELECTED)
							break;
					}
					if (parent == NULL)
						tot_ik += pose_grab_with_ik_add(pchan);
				}
				else {
					/* rule: go over the children and add IK to the tips */
					tot_ik += pose_grab_with_ik_children(ob->pose, pchan->bone);
				}
			}
		}
	}

	return (tot_ik) ? 1 : 0;
}


/* only called with pose mode active object now */
static void createTransPose(TransInfo *t, Object *ob)
{
	bArmature *arm;
	bPoseChannel *pchan;
	TransData *td;
	TransDataExtension *tdx;
	short ik_on= 0;
	int i;

	t->total= 0;

	/* check validity of state */
	arm= get_armature(ob);
	if ((arm==NULL) || (ob->pose==NULL)) return;

	if (arm->flag & ARM_RESTPOS) {
		if (ELEM(t->mode, TFM_DUMMY, TFM_BONESIZE)==0) {
			// XXX use transform operator reports
			// BKE_report(op->reports, RPT_ERROR, "Can't select linked when sync selection is enabled");
			return;
		}
	}

	/* do we need to add temporal IK chains? */
	if ((arm->flag & ARM_AUTO_IK) && t->mode==TFM_TRANSLATION) {
		ik_on= pose_grab_with_ik(ob);
		if (ik_on) t->flag |= T_AUTOIK;
	}

	/* set flags and count total (warning, can change transform to rotate) */
	t->total = count_set_pose_transflags(&t->mode, t->around, ob);

	if(t->total == 0) return;

	t->flag |= T_POSE;
	t->poseobj= ob;	/* we also allow non-active objects to be transformed, in weightpaint */

	/* init trans data */
	td = t->data = MEM_callocN(t->total*sizeof(TransData), "TransPoseBone");
	tdx = t->ext = MEM_callocN(t->total*sizeof(TransDataExtension), "TransPoseBoneExt");
	for(i=0; i<t->total; i++, td++, tdx++) {
		td->ext= tdx;
		td->val = NULL;
	}

	/* use pose channels to fill trans data */
	td= t->data;
	for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
		if (pchan->bone->flag & BONE_TRANSFORM) {
			add_pose_transdata(t, pchan, ob, td);
			td++;
		}
	}

	if(td != (t->data+t->total)) {
		// XXX use transform operator reports
		// BKE_report(op->reports, RPT_DEBUG, "Bone selection count error");
	}

	/* initialise initial auto=ik chainlen's? */
	if (ik_on) transform_autoik_update(t, 0);
}

/* ********************* armature ************** */

static void createTransArmatureVerts(TransInfo *t)
{
	EditBone *ebo;
	bArmature *arm= t->obedit->data;
	ListBase *edbo = arm->edbo;
	TransData *td;
	float mtx[3][3], smtx[3][3], delta[3], bonemat[3][3];
	
	/* special hack for envelope drawmode and scaling:
	 * 	to allow scaling the size of the envelope around single points,
	 *	mode should become TFM_BONE_ENVELOPE in this case
	 */
	// TODO: maybe we need a separate hotkey for it, but this is consistent with 2.4x for now
	if ((t->mode == TFM_RESIZE) && (arm->drawtype==ARM_ENVELOPE))
		t->mode= TFM_BONE_ENVELOPE;
	
	t->total = 0;
	for (ebo = edbo->first; ebo; ebo = ebo->next)
	{
		if (EBONE_VISIBLE(arm, ebo) && !(ebo->flag & BONE_EDITMODE_LOCKED)) 
		{
			if (t->mode==TFM_BONESIZE)
			{
				if (ebo->flag & BONE_SELECTED)
					t->total++;
			}
			else if (t->mode==TFM_BONE_ROLL)
			{
				if (ebo->flag & BONE_SELECTED)
					t->total++;
			}
			else
			{
				if (ebo->flag & BONE_TIPSEL)
					t->total++;
				if (ebo->flag & BONE_ROOTSEL)
					t->total++;
			}
		}
	}

	if (!t->total) return;

	copy_m3_m4(mtx, t->obedit->obmat);
	invert_m3_m3(smtx, mtx);

	td = t->data = MEM_callocN(t->total*sizeof(TransData), "TransEditBone");

	for (ebo = edbo->first; ebo; ebo = ebo->next)
	{
		ebo->oldlength = ebo->length;	// length==0.0 on extrude, used for scaling radius of bone points

		if (EBONE_VISIBLE(arm, ebo) && !(ebo->flag & BONE_EDITMODE_LOCKED)) 
		{
			if (t->mode==TFM_BONE_ENVELOPE)
			{
				if (ebo->flag & BONE_ROOTSEL)
				{
					td->val= &ebo->rad_head;
					td->ival= *td->val;

					copy_v3_v3(td->center, ebo->head);
					td->flag= TD_SELECTED;

					copy_m3_m3(td->smtx, smtx);
					copy_m3_m3(td->mtx, mtx);

					td->loc = NULL;
					td->ext = NULL;
					td->ob = t->obedit;

					td++;
				}
				if (ebo->flag & BONE_TIPSEL)
				{
					td->val= &ebo->rad_tail;
					td->ival= *td->val;
					copy_v3_v3(td->center, ebo->tail);
					td->flag= TD_SELECTED;

					copy_m3_m3(td->smtx, smtx);
					copy_m3_m3(td->mtx, mtx);

					td->loc = NULL;
					td->ext = NULL;
					td->ob = t->obedit;

					td++;
				}

			}
			else if (t->mode==TFM_BONESIZE)
			{
				if (ebo->flag & BONE_SELECTED) {
					if(arm->drawtype==ARM_ENVELOPE)
					{
						td->loc= NULL;
						td->val= &ebo->dist;
						td->ival= ebo->dist;
					}
					else
					{
						// abusive storage of scale in the loc pointer :)
						td->loc= &ebo->xwidth;
						copy_v3_v3(td->iloc, td->loc);
						td->val= NULL;
					}
					copy_v3_v3(td->center, ebo->head);
					td->flag= TD_SELECTED;

					/* use local bone matrix */
					sub_v3_v3v3(delta, ebo->tail, ebo->head);
					vec_roll_to_mat3(delta, ebo->roll, bonemat);
					mul_m3_m3m3(td->mtx, mtx, bonemat);
					invert_m3_m3(td->smtx, td->mtx);

					copy_m3_m3(td->axismtx, td->mtx);
					normalize_m3(td->axismtx);

					td->ext = NULL;
					td->ob = t->obedit;

					td++;
				}
			}
			else if (t->mode==TFM_BONE_ROLL)
			{
				if (ebo->flag & BONE_SELECTED)
				{
					td->loc= NULL;
					td->val= &(ebo->roll);
					td->ival= ebo->roll;

					copy_v3_v3(td->center, ebo->head);
					td->flag= TD_SELECTED;

					td->ext = NULL;
					td->ob = t->obedit;

					td++;
				}
			}
			else
			{
				if (ebo->flag & BONE_TIPSEL)
				{
					copy_v3_v3(td->iloc, ebo->tail);
					copy_v3_v3(td->center, (t->around==V3D_LOCAL) ? ebo->head : td->iloc);
					td->loc= ebo->tail;
					td->flag= TD_SELECTED;
					if (ebo->flag & BONE_EDITMODE_LOCKED)
						td->protectflag = OB_LOCK_LOC|OB_LOCK_ROT|OB_LOCK_SCALE;

					copy_m3_m3(td->smtx, smtx);
					copy_m3_m3(td->mtx, mtx);

					sub_v3_v3v3(delta, ebo->tail, ebo->head);
					vec_roll_to_mat3(delta, ebo->roll, td->axismtx);

					if ((ebo->flag & BONE_ROOTSEL) == 0)
					{
						td->extra = ebo;
					}

					td->ext = NULL;
					td->val = NULL;
					td->ob = t->obedit;

					td++;
				}
				if (ebo->flag & BONE_ROOTSEL)
				{
					copy_v3_v3(td->iloc, ebo->head);
					copy_v3_v3(td->center, td->iloc);
					td->loc= ebo->head;
					td->flag= TD_SELECTED;
					if (ebo->flag & BONE_EDITMODE_LOCKED)
						td->protectflag = OB_LOCK_LOC|OB_LOCK_ROT|OB_LOCK_SCALE;

					copy_m3_m3(td->smtx, smtx);
					copy_m3_m3(td->mtx, mtx);

					sub_v3_v3v3(delta, ebo->tail, ebo->head);
					vec_roll_to_mat3(delta, ebo->roll, td->axismtx);

					td->extra = ebo; /* to fix roll */

					td->ext = NULL;
					td->val = NULL;
					td->ob = t->obedit;

					td++;
				}
			}
		}
	}
}

/* ********************* meta elements ********* */

static void createTransMBallVerts(TransInfo *t)
{
	MetaBall *mb = (MetaBall*)t->obedit->data;
	 MetaElem *ml;
	TransData *td;
	TransDataExtension *tx;
	float mtx[3][3], smtx[3][3];
	int count=0, countsel=0;
	int propmode = t->flag & T_PROP_EDIT;

	/* count totals */
	for(ml= mb->editelems->first; ml; ml= ml->next) {
		if(ml->flag & SELECT) countsel++;
		if(propmode) count++;
	}

	/* note: in prop mode we need at least 1 selected */
	if (countsel==0) return;

	if(propmode) t->total = count;
	else t->total = countsel;

	td = t->data= MEM_callocN(t->total*sizeof(TransData), "TransObData(MBall EditMode)");
	tx = t->ext = MEM_callocN(t->total*sizeof(TransDataExtension), "MetaElement_TransExtension");

	copy_m3_m4(mtx, t->obedit->obmat);
	invert_m3_m3(smtx, mtx);

	for(ml= mb->editelems->first; ml; ml= ml->next) {
		if(propmode || (ml->flag & SELECT)) {
			td->loc= &ml->x;
			copy_v3_v3(td->iloc, td->loc);
			copy_v3_v3(td->center, td->loc);

			if(ml->flag & SELECT) td->flag= TD_SELECTED | TD_USEQUAT | TD_SINGLESIZE;
			else td->flag= TD_USEQUAT;

			copy_m3_m3(td->smtx, smtx);
			copy_m3_m3(td->mtx, mtx);

			td->ext = tx;

			/* Radius of MetaElem (mass of MetaElem influence) */
			if(ml->flag & MB_SCALE_RAD){
				td->val = &ml->rad;
				td->ival = ml->rad;
			}
			else{
				td->val = &ml->s;
				td->ival = ml->s;
			}

			/* expx/expy/expz determine "shape" of some MetaElem types */
			tx->size = &ml->expx;
			tx->isize[0] = ml->expx;
			tx->isize[1] = ml->expy;
			tx->isize[2] = ml->expz;

			/* quat is used for rotation of MetaElem */
			tx->quat = ml->quat;
			QUATCOPY(tx->iquat, ml->quat);

			tx->rot = NULL;

			td++;
			tx++;
		}
	}
}

/* ********************* curve/surface ********* */

static void calc_distanceCurveVerts(TransData *head, TransData *tail) {
	TransData *td, *td_near = NULL;
	for (td = head; td<=tail; td++) {
		if (td->flag & TD_SELECTED) {
			td_near = td;
			td->dist = 0.0f;
		}
		else if(td_near) {
			float dist;
			dist = len_v3v3(td_near->center, td->center);
			if (dist < (td-1)->dist) {
				td->dist = (td-1)->dist;
			}
			else {
				td->dist = dist;
			}
		}
		else {
			td->dist = MAXFLOAT;
			td->flag |= TD_NOTCONNECTED;
		}
	}
	td_near = NULL;
	for (td = tail; td>=head; td--) {
		if (td->flag & TD_SELECTED) {
			td_near = td;
			td->dist = 0.0f;
		}
		else if(td_near) {
			float dist;
			dist = len_v3v3(td_near->center, td->center);
			if (td->flag & TD_NOTCONNECTED || dist < td->dist || (td+1)->dist < td->dist) {
				td->flag &= ~TD_NOTCONNECTED;
				if (dist < (td+1)->dist) {
					td->dist = (td+1)->dist;
				}
				else {
					td->dist = dist;
				}
			}
		}
	}
}

/* Utility function for getting the handle data from bezier's */
static TransDataCurveHandleFlags *initTransDataCurveHandles(TransData *td, struct BezTriple *bezt) {
	TransDataCurveHandleFlags *hdata;
	td->flag |= TD_BEZTRIPLE;
	hdata = td->hdata = MEM_mallocN(sizeof(TransDataCurveHandleFlags), "CuHandle Data");
	hdata->ih1 = bezt->h1;
	hdata->h1 = &bezt->h1;
	hdata->ih2 = bezt->h2; /* incase the second is not selected */
	hdata->h2 = &bezt->h2;
	return hdata;
}

static void createTransCurveVerts(bContext *C, TransInfo *t)
{
	Object *obedit= CTX_data_edit_object(C);
	Curve *cu= obedit->data;
	TransData *td = NULL;
	  Nurb *nu;
	BezTriple *bezt;
	BPoint *bp;
	float mtx[3][3], smtx[3][3];
	int a;
	int count=0, countsel=0;
	int propmode = t->flag & T_PROP_EDIT;
	short hide_handles = (cu->drawflag & CU_HIDE_HANDLES);
	ListBase *nurbs;

	/* to be sure */
	if(cu->editnurb==NULL) return;

	/* count total of vertices, check identical as in 2nd loop for making transdata! */
	nurbs= curve_editnurbs(cu);
	for(nu= nurbs->first; nu; nu= nu->next) {
		if(nu->type == CU_BEZIER) {
			for(a=0, bezt= nu->bezt; a<nu->pntsu; a++, bezt++) {
				if(bezt->hide==0) {
					if (hide_handles) {
						if(bezt->f2 & SELECT) countsel+=3;
						if(propmode) count+= 3;
					} else {
						if(bezt->f1 & SELECT) countsel++;
						if(bezt->f2 & SELECT) countsel++;
						if(bezt->f3 & SELECT) countsel++;
						if(propmode) count+= 3;
					}
				}
			}
		}
		else {
			for(a= nu->pntsu*nu->pntsv, bp= nu->bp; a>0; a--, bp++) {
				if(bp->hide==0) {
					if(propmode) count++;
					if(bp->f1 & SELECT) countsel++;
				}
			}
		}
	}
	/* note: in prop mode we need at least 1 selected */
	if (countsel==0) return;

	if(propmode) t->total = count;
	else t->total = countsel;
	t->data= MEM_callocN(t->total*sizeof(TransData), "TransObData(Curve EditMode)");

	copy_m3_m4(mtx, t->obedit->obmat);
	invert_m3_m3(smtx, mtx);

	td = t->data;
	for(nu= nurbs->first; nu; nu= nu->next) {
		if(nu->type == CU_BEZIER) {
			TransData *head, *tail;
			head = tail = td;
			for(a=0, bezt= nu->bezt; a<nu->pntsu; a++, bezt++) {
				if(bezt->hide==0) {
					TransDataCurveHandleFlags *hdata = NULL;

					if(		propmode ||
							((bezt->f2 & SELECT) && hide_handles) ||
							((bezt->f1 & SELECT) && hide_handles == 0)
					  ) {
						copy_v3_v3(td->iloc, bezt->vec[0]);
						td->loc= bezt->vec[0];
						copy_v3_v3(td->center, bezt->vec[(hide_handles || bezt->f2 & SELECT) ? 1:0]);
						if (hide_handles) {
							if(bezt->f2 & SELECT) td->flag= TD_SELECTED;
							else td->flag= 0;
						} else {
							if(bezt->f1 & SELECT) td->flag= TD_SELECTED;
							else td->flag= 0;
						}
						td->ext = NULL;
						td->val = NULL;

						hdata = initTransDataCurveHandles(td, bezt);

						copy_m3_m3(td->smtx, smtx);
						copy_m3_m3(td->mtx, mtx);

						td++;
						count++;
						tail++;
					}

					/* This is the Curve Point, the other two are handles */
					if(propmode || (bezt->f2 & SELECT)) {
						copy_v3_v3(td->iloc, bezt->vec[1]);
						td->loc= bezt->vec[1];
						copy_v3_v3(td->center, td->loc);
						if(bezt->f2 & SELECT) td->flag= TD_SELECTED;
						else td->flag= 0;
						td->ext = NULL;

						if (t->mode==TFM_CURVE_SHRINKFATTEN) { /* || t->mode==TFM_RESIZE) {*/ /* TODO - make points scale */
							td->val = &(bezt->radius);
							td->ival = bezt->radius;
						} else if (t->mode==TFM_TILT) {
							td->val = &(bezt->alfa);
							td->ival = bezt->alfa;
						} else {
							td->val = NULL;
						}

						copy_m3_m3(td->smtx, smtx);
						copy_m3_m3(td->mtx, mtx);

						if ((bezt->f1&SELECT)==0 && (bezt->f3&SELECT)==0)
						/* If the middle is selected but the sides arnt, this is needed */
						if (hdata==NULL) { /* if the handle was not saved by the previous handle */
							hdata = initTransDataCurveHandles(td, bezt);
						}

						td++;
						count++;
						tail++;
					}
					if(		propmode ||
							((bezt->f2 & SELECT) && hide_handles) ||
							((bezt->f3 & SELECT) && hide_handles == 0)
					  ) {
						copy_v3_v3(td->iloc, bezt->vec[2]);
						td->loc= bezt->vec[2];
						copy_v3_v3(td->center, bezt->vec[(hide_handles || bezt->f2 & SELECT) ? 1:2]);
						if (hide_handles) {
							if(bezt->f2 & SELECT) td->flag= TD_SELECTED;
							else td->flag= 0;
						} else {
							if(bezt->f3 & SELECT) td->flag= TD_SELECTED;
							else td->flag= 0;
						}
						td->ext = NULL;
						td->val = NULL;

						if (hdata==NULL) { /* if the handle was not saved by the previous handle */
							hdata = initTransDataCurveHandles(td, bezt);
						}

						copy_m3_m3(td->smtx, smtx);
						copy_m3_m3(td->mtx, mtx);

						td++;
						count++;
						tail++;
					}
				}
				else if (propmode && head != tail) {
					calc_distanceCurveVerts(head, tail-1);
					head = tail;
				}
			}
			if (propmode && head != tail)
				calc_distanceCurveVerts(head, tail-1);

			/* TODO - in the case of tilt and radius we can also avoid allocating the initTransDataCurveHandles
			 * but for now just dont change handle types */
			if (ELEM(t->mode, TFM_CURVE_SHRINKFATTEN, TFM_TILT) == 0)
				testhandlesNurb(nu); /* sets the handles based on their selection, do this after the data is copied to the TransData */
		}
		else {
			TransData *head, *tail;
			head = tail = td;
			for(a= nu->pntsu*nu->pntsv, bp= nu->bp; a>0; a--, bp++) {
				if(bp->hide==0) {
					if(propmode || (bp->f1 & SELECT)) {
						copy_v3_v3(td->iloc, bp->vec);
						td->loc= bp->vec;
						copy_v3_v3(td->center, td->loc);
						if(bp->f1 & SELECT) td->flag= TD_SELECTED;
						else td->flag= 0;
						td->ext = NULL;

						if (t->mode==TFM_CURVE_SHRINKFATTEN || t->mode==TFM_RESIZE) {
							td->val = &(bp->radius);
							td->ival = bp->radius;
						} else {
							td->val = &(bp->alfa);
							td->ival = bp->alfa;
						}

						copy_m3_m3(td->smtx, smtx);
						copy_m3_m3(td->mtx, mtx);

						td++;
						count++;
						tail++;
					}
				}
				else if (propmode && head != tail) {
					calc_distanceCurveVerts(head, tail-1);
					head = tail;
				}
			}
			if (propmode && head != tail)
				calc_distanceCurveVerts(head, tail-1);
		}
	}
}

/* ********************* lattice *************** */

static void createTransLatticeVerts(TransInfo *t)
{
	Lattice *latt = ((Lattice*)t->obedit->data)->editlatt->latt;
	TransData *td = NULL;
	BPoint *bp;
	float mtx[3][3], smtx[3][3];
	int a;
	int count=0, countsel=0;
	int propmode = t->flag & T_PROP_EDIT;

	bp = latt->def;
	a  = latt->pntsu * latt->pntsv * latt->pntsw;
	while(a--) {
		if(bp->hide==0) {
			if(bp->f1 & SELECT) countsel++;
			if(propmode) count++;
		}
		bp++;
	}

	 /* note: in prop mode we need at least 1 selected */
	if (countsel==0) return;

	if(propmode) t->total = count;
	else t->total = countsel;
	t->data= MEM_callocN(t->total*sizeof(TransData), "TransObData(Lattice EditMode)");

	copy_m3_m4(mtx, t->obedit->obmat);
	invert_m3_m3(smtx, mtx);

	td = t->data;
	bp = latt->def;
	a  = latt->pntsu * latt->pntsv * latt->pntsw;
	while(a--) {
		if(propmode || (bp->f1 & SELECT)) {
			if(bp->hide==0) {
				copy_v3_v3(td->iloc, bp->vec);
				td->loc= bp->vec;
				copy_v3_v3(td->center, td->loc);
				if(bp->f1 & SELECT) td->flag= TD_SELECTED;
				else td->flag= 0;
				copy_m3_m3(td->smtx, smtx);
				copy_m3_m3(td->mtx, mtx);

				td->ext = NULL;
				td->val = NULL;

				td++;
				count++;
			}
		}
		bp++;
	}
}

/* ******************* particle edit **************** */
static void createTransParticleVerts(bContext *C, TransInfo *t)
{
	TransData *td = NULL;
	TransDataExtension *tx;
	Base *base = CTX_data_active_base(C);
	Object *ob = CTX_data_active_object(C);
	ParticleEditSettings *pset = PE_settings(t->scene);
	PTCacheEdit *edit = PE_get_current(t->scene, ob);
	ParticleSystem *psys = NULL;
	ParticleSystemModifierData *psmd = NULL;
	PTCacheEditPoint *point;
	PTCacheEditKey *key;
	float mat[4][4];
	int i,k, transformparticle;
	int count = 0, hasselected = 0;
	int propmode = t->flag & T_PROP_EDIT;

	if(edit==NULL || t->settings->particle.selectmode==SCE_SELECT_PATH) return;

	psys = edit->psys;

	if(psys)
		psmd = psys_get_modifier(ob,psys);

	base->flag |= BA_HAS_RECALC_DATA;

	for(i=0, point=edit->points; i<edit->totpoint; i++, point++) {
		point->flag &= ~PEP_TRANSFORM;
		transformparticle= 0;

		if((point->flag & PEP_HIDE)==0) {
			for(k=0, key=point->keys; k<point->totkey; k++, key++) {
				if((key->flag&PEK_HIDE)==0) {
					if(key->flag&PEK_SELECT) {
						hasselected= 1;
						transformparticle= 1;
					}
					else if(propmode)
						transformparticle= 1;
				}
			}
		}

		if(transformparticle) {
			count += point->totkey;
			point->flag |= PEP_TRANSFORM;
		}
	}

	 /* note: in prop mode we need at least 1 selected */
	if (hasselected==0) return;

	t->total = count;
	td = t->data = MEM_callocN(t->total * sizeof(TransData), "TransObData(Particle Mode)");

	if(t->mode == TFM_BAKE_TIME)
		tx = t->ext = MEM_callocN(t->total * sizeof(TransDataExtension), "Particle_TransExtension");
	else
		tx = t->ext = NULL;

	unit_m4(mat);

	invert_m4_m4(ob->imat,ob->obmat);

	for(i=0, point=edit->points; i<edit->totpoint; i++, point++) {
		TransData *head, *tail;
		head = tail = td;

		if(!(point->flag & PEP_TRANSFORM)) continue;

		if(psys && !(psys->flag & PSYS_GLOBAL_HAIR))
			psys_mat_hair_to_global(ob, psmd->dm, psys->part->from, psys->particles + i, mat);

		for(k=0, key=point->keys; k<point->totkey; k++, key++) {
			if(key->flag & PEK_USE_WCO) {
				copy_v3_v3(key->world_co, key->co);
				mul_m4_v3(mat, key->world_co);
				td->loc = key->world_co;
			}
			else
				td->loc = key->co;

			copy_v3_v3(td->iloc, td->loc);
			copy_v3_v3(td->center, td->loc);

			if(key->flag & PEK_SELECT)
				td->flag |= TD_SELECTED;
			else if(!propmode)
				td->flag |= TD_SKIP;

			unit_m3(td->mtx);
			unit_m3(td->smtx);

			/* don't allow moving roots */
			if(k==0 && pset->flag & PE_LOCK_FIRST && (!psys || !(psys->flag & PSYS_GLOBAL_HAIR)))
				td->protectflag |= OB_LOCK_LOC;

			td->ob = ob;
			td->ext = tx;
			if(t->mode == TFM_BAKE_TIME) {
				td->val = key->time;
				td->ival = *(key->time);
				/* abuse size and quat for min/max values */
				td->flag |= TD_NO_EXT;
				if(k==0) tx->size = NULL;
				else tx->size = (key - 1)->time;

				if(k == point->totkey - 1) tx->quat = NULL;
				else tx->quat = (key + 1)->time;
			}

			td++;
			if(tx)
				tx++;
			tail++;
		}
		if (propmode && head != tail)
			calc_distanceCurveVerts(head, tail - 1);
	}
}

void flushTransParticles(TransInfo *t)
{
	Scene *scene = t->scene;
	Object *ob = OBACT;
	PTCacheEdit *edit = PE_get_current(scene, ob);
	ParticleSystem *psys = edit->psys;
	ParticleSystemModifierData *psmd = NULL;
	PTCacheEditPoint *point;
	PTCacheEditKey *key;
	TransData *td;
	float mat[4][4], imat[4][4], co[3];
	int i, k, propmode = t->flag & T_PROP_EDIT;

	if(psys)
		psmd = psys_get_modifier(ob, psys);

	/* we do transform in world space, so flush world space position
	 * back to particle local space (only for hair particles) */
	td= t->data;
	for(i=0, point=edit->points; i<edit->totpoint; i++, point++, td++) {
		if(!(point->flag & PEP_TRANSFORM)) continue;

		if(psys && !(psys->flag & PSYS_GLOBAL_HAIR)) {
			psys_mat_hair_to_global(ob, psmd->dm, psys->part->from, psys->particles + i, mat);
			invert_m4_m4(imat,mat);

			for(k=0, key=point->keys; k<point->totkey; k++, key++) {
				copy_v3_v3(co, key->world_co);
				mul_m4_v3(imat, co);


				/* optimization for proportional edit */
				if(!propmode || !compare_v3v3(key->co, co, 0.0001f)) {
					copy_v3_v3(key->co, co);
					point->flag |= PEP_EDIT_RECALC;
				}
			}
		}
		else
			point->flag |= PEP_EDIT_RECALC;
	}

	PE_update_object(scene, OBACT, 1);
}

/* ********************* mesh ****************** */

/* proportional distance based on connectivity  */
#define THRESHOLDFACTOR (1.0f-0.0001f)

static int connectivity_edge(float mtx[][3], EditVert *v1, EditVert *v2)
{
	float edge_vec[3];
	float edge_len;
	int done = 0;

	/* note: hidden verts are not being checked for, this assumes
	 * flushing of hidden faces & edges is working right */
	
	if (v1->f2 + v2->f2 == 4)
		return 0;
	
	sub_v3_v3v3(edge_vec, v1->co, v2->co);
	mul_m3_v3(mtx, edge_vec);

	edge_len = len_v3(edge_vec);

	if (v1->f2) {
		if (v2->f2) {
			if (v2->tmp.fp + edge_len < THRESHOLDFACTOR * v1->tmp.fp) {
				v1->tmp.fp = v2->tmp.fp + edge_len;
				done = 1;
			} else if (v1->tmp.fp + edge_len < THRESHOLDFACTOR * v2->tmp.fp) {
				v2->tmp.fp = v1->tmp.fp + edge_len;
				done = 1;
			}
		}
		else {
			v2->f2 = 1;
			v2->tmp.fp = v1->tmp.fp + edge_len;
			done = 1;
		}
	}
	else if (v2->f2) {
		v1->f2 = 1;
		v1->tmp.fp = v2->tmp.fp + edge_len;
		done = 1;
	}

	return done;
}

static void editmesh_set_connectivity_distance(EditMesh *em, float mtx[][3])
{
	EditVert *eve;
	EditEdge *eed;
	EditFace *efa;
	int done= 1;

	/* f2 flag is used for 'selection' */
	/* tmp.l is offset on scratch array   */
	for(eve= em->verts.first; eve; eve= eve->next) {
		if(eve->h==0) {
			eve->tmp.fp = 0;

			if(eve->f & SELECT) {
				eve->f2= 2;
			}
			else {
				eve->f2 = 0;
			}
		}
	}


	/* Floodfill routine */
	/*
	At worst this is n*n of complexity where n is number of edges
	Best case would be n if the list is ordered perfectly.
	Estimate is n log n in average (so not too bad)
	*/
	while(done) {
		done= 0;

		for(eed= em->edges.first; eed; eed= eed->next) {
			if(eed->h==0) {
				done |= connectivity_edge(mtx, eed->v1, eed->v2);
			}
		}

		/* do internal edges for quads */
		for(efa= em->faces.first; efa; efa= efa->next) {
			if (efa->v4 && efa->h==0) {
				done |= connectivity_edge(mtx, efa->v1, efa->v3);
				done |= connectivity_edge(mtx, efa->v2, efa->v4);
			}
		}
	}
}

/* loop-in-a-loop I know, but we need it! (ton) */
static void get_face_center(float *cent, EditMesh *em, EditVert *eve)
{
	EditFace *efa;

	for(efa= em->faces.first; efa; efa= efa->next)
		if(efa->f & SELECT)
			if(efa->v1==eve || efa->v2==eve || efa->v3==eve || efa->v4==eve)
				break;
	if(efa) {
		copy_v3_v3(cent, efa->cent);
	}
}

//way to overwrite what data is edited with transform
//static void VertsToTransData(TransData *td, EditVert *eve, BakeKey *key)
static void VertsToTransData(TransInfo *t, TransData *td, EditMesh *em, EditVert *eve)
{
	td->flag = 0;
	//if(key)
	//	td->loc = key->co;
	//else
	td->loc = eve->co;

	copy_v3_v3(td->center, td->loc);
	if(t->around==V3D_LOCAL && (em->selectmode & SCE_SELECT_FACE))
		get_face_center(td->center, em, eve);
	copy_v3_v3(td->iloc, td->loc);

	// Setting normals
	copy_v3_v3(td->axismtx[2], eve->no);
	td->axismtx[0][0]		=
		td->axismtx[0][1]	=
		td->axismtx[0][2]	=
		td->axismtx[1][0]	=
		td->axismtx[1][1]	=
		td->axismtx[1][2]	= 0.0f;

	td->ext = NULL;
	td->val = NULL;
	td->extra = NULL;
	if (t->mode == TFM_BWEIGHT) {
		td->val = &(eve->bweight);
		td->ival = eve->bweight;
	}
}

#if 0
static void createTransBMeshVerts(TransInfo *t, BME_Mesh *bm, BME_TransData_Head *td) {
	BME_Vert *v;
	BME_TransData *vtd;
	TransData *tob;
	int i;

	tob = t->data = MEM_callocN(td->len*sizeof(TransData), "TransObData(Bevel tool)");

	for (i=0,v=bm->verts.first;v;v=v->next) {
		if ( (vtd = BME_get_transdata(td,v)) ) {
			tob->loc = vtd->loc;
			tob->val = &vtd->factor;
			copy_v3_v3(tob->iloc,vtd->co);
			copy_v3_v3(tob->center,vtd->org);
			copy_v3_v3(tob->axismtx[0],vtd->vec);
			tob->axismtx[1][0] = vtd->max ? *vtd->max : 0;
			tob++;
			i++;
		}
	}
	/* since td is a memarena, it can hold more transdata than actual elements
	 * (i.e. we can't depend on td->len to determine the number of actual elements) */
	t->total = i;
}
#endif

static void createTransEditVerts(bContext *C, TransInfo *t)
{
	ToolSettings *ts = CTX_data_tool_settings(C);
	TransData *tob = NULL;
	EditMesh *em = ((Mesh *)t->obedit->data)->edit_mesh;
	EditVert *eve;
	EditVert *eve_act = NULL;
	float *mappedcos = NULL, *quats= NULL;
	float mtx[3][3], smtx[3][3], (*defmats)[3][3] = NULL, (*defcos)[3] = NULL;
	int count=0, countsel=0, a, totleft;
	int propmode = (t->flag & T_PROP_EDIT) ? (t->flag & (T_PROP_EDIT | T_PROP_CONNECTED)) : 0;
	int mirror = 0;
	short selectmode = ts->selectmode;

	if (t->flag & T_MIRROR)
	{
		mirror = 1;
	}

	/* edge slide forces edge select */
	if (t->mode == TFM_EDGE_SLIDE) {
		selectmode = SCE_SELECT_EDGE;
	}

	// transform now requires awareness for select mode, so we tag the f1 flags in verts
	if(selectmode & SCE_SELECT_VERTEX) {
		for(eve= em->verts.first; eve; eve= eve->next) {
			if(eve->h==0 && (eve->f & SELECT))
				eve->f1= SELECT;
			else
				eve->f1= 0;
		}
	}
	else if(selectmode & SCE_SELECT_EDGE) {
		EditEdge *eed;
		for(eve= em->verts.first; eve; eve= eve->next) eve->f1= 0;
		for(eed= em->edges.first; eed; eed= eed->next) {
			if(eed->h==0 && (eed->f & SELECT))
				eed->v1->f1= eed->v2->f1= SELECT;
		}
	}
	else {
		EditFace *efa;
		for(eve= em->verts.first; eve; eve= eve->next) eve->f1= 0;
		for(efa= em->faces.first; efa; efa= efa->next) {
			if(efa->h==0 && (efa->f & SELECT)) {
				efa->v1->f1= efa->v2->f1= efa->v3->f1= SELECT;
				if(efa->v4) efa->v4->f1= SELECT;
			}
		}
	}

	/* now we can count */
	for(eve= em->verts.first; eve; eve= eve->next) {
		if(eve->h==0) {
			if(eve->f1) countsel++;
			if(propmode) count++;
		}
	}

	 /* note: in prop mode we need at least 1 selected */
	if (countsel==0) return;

	/* check active */
	if (em->selected.last) {
		EditSelection *ese = em->selected.last;
		if ( ese->type == EDITVERT ) {
			eve_act = (EditVert *)ese->data;
		}
	}


	if(propmode) t->total = count;
	else t->total = countsel;

	tob= t->data= MEM_callocN(t->total*sizeof(TransData), "TransObData(Mesh EditMode)");

	copy_m3_m4(mtx, t->obedit->obmat);
	invert_m3_m3(smtx, mtx);

	if(propmode & T_PROP_CONNECTED) {
		editmesh_set_connectivity_distance(em, mtx);
	}

	/* detect CrazySpace [tm] */
	if(modifiers_getCageIndex(t->scene, t->obedit, NULL, 1)>=0) {
		if(modifiers_isCorrectableDeformed(t->obedit)) {
			/* check if we can use deform matrices for modifier from the
			   start up to stack, they are more accurate than quats */
			totleft= editmesh_get_first_deform_matrices(t->scene, t->obedit, em, &defmats, &defcos);

			/* if we still have more modifiers, also do crazyspace
			   correction with quats, relative to the coordinates after
			   the modifiers that support deform matrices (defcos) */
			if(totleft > 0) {
				mappedcos= crazyspace_get_mapped_editverts(t->scene, t->obedit);
				quats= MEM_mallocN( (t->total)*sizeof(float)*4, "crazy quats");
				crazyspace_set_quats_editmesh(em, (float*)defcos, mappedcos, quats);
				if(mappedcos)
					MEM_freeN(mappedcos);
			}

			if(defcos)
				MEM_freeN(defcos);
		}
	}

	/* find out which half we do */
	if(mirror) {
		for (eve=em->verts.first; eve; eve=eve->next) {
			if(eve->h==0 && eve->f1 && eve->co[0]!=0.0f) {
				if(eve->co[0]<0.0f)
				{
					t->mirror = -1;
					mirror = -1;
				}
				break;
			}
		}
	}

	for (a=0, eve=em->verts.first; eve; eve=eve->next, a++) {
		if(eve->h==0) {
			if(propmode || eve->f1) {
				VertsToTransData(t, tob, em, eve);

				/* selected */
				if(eve->f1) tob->flag |= TD_SELECTED;

				/* active */
				if(eve == eve_act) tob->flag |= TD_ACTIVE;

				if(propmode) {
					if (eve->f2) {
						tob->dist= eve->tmp.fp;
					}
					else {
						tob->flag |= TD_NOTCONNECTED;
						tob->dist = MAXFLOAT;
					}
				}

				/* CrazySpace */
				if(defmats || (quats && eve->tmp.p)) {
					float mat[3][3], imat[3][3], qmat[3][3];

					/* use both or either quat and defmat correction */
					if(quats && eve->tmp.f) {
						quat_to_mat3( qmat,eve->tmp.p);

						if(defmats)
							mul_serie_m3(mat, mtx, qmat, defmats[a],
								NULL, NULL, NULL, NULL, NULL);
						else
							mul_m3_m3m3(mat, mtx, qmat);
					}
					else
						mul_m3_m3m3(mat, mtx, defmats[a]);

					invert_m3_m3(imat, mat);

					copy_m3_m3(tob->smtx, imat);
					copy_m3_m3(tob->mtx, mat);
				}
				else {
					copy_m3_m3(tob->smtx, smtx);
					copy_m3_m3(tob->mtx, mtx);
				}

				/* Mirror? */
				if( (mirror>0 && tob->iloc[0]>0.0f) || (mirror<0 && tob->iloc[0]<0.0f)) {
					EditVert *vmir= editmesh_get_x_mirror_vert(t->obedit, em, eve, tob->iloc, a);	/* initializes octree on first call */
					if(vmir != eve) {
						tob->extra = vmir;
					}
				}
				tob++;
			}
		}
	}
	
	if (mirror != 0)
	{
		tob = t->data;
		for( a = 0; a < t->total; a++, tob++ )
		{
			if (ABS(tob->loc[0]) <= 0.00001f)
			{
				tob->flag |= TD_MIRROR_EDGE;
			}
		}
	}
	
	/* crazy space free */
	if(quats)
		MEM_freeN(quats);
	if(defmats)
		MEM_freeN(defmats);
}

/* *** NODE EDITOR *** */
void flushTransNodes(TransInfo *t)
{
	int a;
	TransData2D *td;

	/* flush to 2d vector from internally used 3d vector */
	for(a=0, td= t->data2d; a<t->total; a++, td++) {
		td->loc2d[0]= td->loc[0];
		td->loc2d[1]= td->loc[1];
	}
	
	/* handle intersection with noodles */
	if(t->total==1) {
		ED_node_link_intersect_test(t->sa, 1);
	}
	
}

/* *** SEQUENCE EDITOR *** */

/* commented _only_ because the meta may have animaion data which
 * needs moving too [#28158] */

#define SEQ_TX_NESTED_METAS

void flushTransSeq(TransInfo *t)
{
	ListBase *seqbasep= seq_give_editing(t->scene, FALSE)->seqbasep; /* Editing null check already done */
	int a, new_frame, old_start;
	TransData *td= NULL;
	TransData2D *td2d= NULL;
	TransDataSeq *tdsq= NULL;
	Sequence *seq;



	/* prevent updating the same seq twice
	 * if the transdata order is changed this will mess up
	 * but so will TransDataSeq */
	Sequence *seq_prev= NULL;

	/* flush to 2d vector from internally used 3d vector */
	for(a=0, td= t->data, td2d= t->data2d; a<t->total; a++, td++, td2d++) {
		tdsq= (TransDataSeq *)td->extra;
		seq= tdsq->seq;
		old_start = seq->start;
		new_frame= (int)floor(td2d->loc[0] + 0.5f);

		switch (tdsq->sel_flag) {
		case SELECT:
#ifdef SEQ_TX_NESTED_METAS
			if ((seq->depth != 0 || seq_tx_test(seq))) /* for meta's, their children move */
				seq->start= new_frame - tdsq->start_offset;
#else
			if (seq->type != SEQ_META && (seq->depth != 0 || seq_tx_test(seq))) /* for meta's, their children move */
				seq->start= new_frame - tdsq->start_offset;
#endif
			if (seq->depth==0) {
				seq->machine= (int)floor(td2d->loc[1] + 0.5f);
				CLAMP(seq->machine, 1, MAXSEQ);
			}
			break;
		case SEQ_LEFTSEL: /* no vertical transform  */
			seq_tx_set_final_left(seq, new_frame);
			seq_tx_handle_xlimits(seq, tdsq->flag&SEQ_LEFTSEL, tdsq->flag&SEQ_RIGHTSEL);
			seq_single_fix(seq); /* todo - move this into aftertrans update? - old seq tx needed it anyway */
			break;
		case SEQ_RIGHTSEL: /* no vertical transform  */
			seq_tx_set_final_right(seq, new_frame);
			seq_tx_handle_xlimits(seq, tdsq->flag&SEQ_LEFTSEL, tdsq->flag&SEQ_RIGHTSEL);
			seq_single_fix(seq); /* todo - move this into aftertrans update? - old seq tx needed it anyway */
			break;
		}

		if (seq != seq_prev) {
			if(seq->depth==0) {
				/* Calculate this strip and all nested strips
				 * children are ALWAYS transformed first
				 * so we dont need to do this in another loop. */
				calc_sequence(t->scene, seq);
			}
			else {
				calc_sequence_disp(t->scene, seq);
			}

			if(tdsq->sel_flag == SELECT)
				seq_offset_animdata(t->scene, seq, seq->start - old_start);
		}
		seq_prev= seq;
	}

	/* need to do the overlap check in a new loop otherwise adjacent strips
	 * will not be updated and we'll get false positives */
	seq_prev= NULL;
	for(a=0, td= t->data, td2d= t->data2d; a<t->total; a++, td++, td2d++) {

		tdsq= (TransDataSeq *)td->extra;
		seq= tdsq->seq;

		if (seq != seq_prev) {
			if(seq->depth==0) {
				/* test overlap, displayes red outline */
				seq->flag &= ~SEQ_OVERLAP;
				if( seq_test_overlap(seqbasep, seq) ) {
					seq->flag |= SEQ_OVERLAP;
				}
			}
		}
		seq_prev= seq;
	}

	if (t->mode == TFM_SEQ_SLIDE) { /* originally TFM_TIME_EXTEND, transform changes */
		/* Special annoying case here, need to calc metas with TFM_TIME_EXTEND only */
		seq= seqbasep->first;

		while(seq) {
			if (seq->type == SEQ_META && seq->flag & SELECT)
				calc_sequence(t->scene, seq);
			seq= seq->next;
		}
	}
}

/* ********************* UV ****************** */

static void UVsToTransData(SpaceImage *sima, TransData *td, TransData2D *td2d, float *uv, int selected)
{
	float aspx, aspy;

	ED_space_image_uv_aspect(sima, &aspx, &aspy);

	/* uv coords are scaled by aspects. this is needed for rotations and
	   proportional editing to be consistent with the stretchted uv coords
	   that are displayed. this also means that for display and numinput,
	   and when the the uv coords are flushed, these are converted each time */
	td2d->loc[0] = uv[0]*aspx;
	td2d->loc[1] = uv[1]*aspy;
	td2d->loc[2] = 0.0f;
	td2d->loc2d = uv;

	td->flag = 0;
	td->loc = td2d->loc;
	copy_v3_v3(td->center, td->loc);
	copy_v3_v3(td->iloc, td->loc);

	memset(td->axismtx, 0, sizeof(td->axismtx));
	td->axismtx[2][2] = 1.0f;

	td->ext= NULL; td->val= NULL;

	if(selected) {
		td->flag |= TD_SELECTED;
		td->dist= 0.0;
	}
	else {
		td->dist= MAXFLOAT;
	}
	unit_m3(td->mtx);
	unit_m3(td->smtx);
}

static void createTransUVs(bContext *C, TransInfo *t)
{
	SpaceImage *sima = CTX_wm_space_image(C);
	Image *ima = CTX_data_edit_image(C);
	Scene *scene = t->scene;
	TransData *td = NULL;
	TransData2D *td2d = NULL;
	MTFace *tf;
	int count=0, countsel=0;
	int propmode = t->flag & T_PROP_EDIT;

	EditMesh *em = ((Mesh *)t->obedit->data)->edit_mesh;
	EditFace *efa;

	if(!ED_space_image_show_uvedit(sima, t->obedit)) return;

	/* count */
	for (efa= em->faces.first; efa; efa= efa->next) {
		tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);

		if(uvedit_face_visible(scene, ima, efa, tf)) {
			efa->tmp.p = tf;

			if (uvedit_uv_selected(scene, efa, tf, 0)) countsel++;
			if (uvedit_uv_selected(scene, efa, tf, 1)) countsel++;
			if (uvedit_uv_selected(scene, efa, tf, 2)) countsel++;
			if (efa->v4 && uvedit_uv_selected(scene, efa, tf, 3)) countsel++;
			if(propmode)
				count += (efa->v4)? 4: 3;
		} else {
			efa->tmp.p = NULL;
		}
	}

	 /* note: in prop mode we need at least 1 selected */
	if (countsel==0) return;

	t->total= (propmode)? count: countsel;
	t->data= MEM_callocN(t->total*sizeof(TransData), "TransObData(UV Editing)");
	/* for each 2d uv coord a 3d vector is allocated, so that they can be
	   treated just as if they were 3d verts */
	t->data2d= MEM_callocN(t->total*sizeof(TransData2D), "TransObData2D(UV Editing)");

	if(sima->flag & SI_CLIP_UV)
		t->flag |= T_CLIP_UV;

	td= t->data;
	td2d= t->data2d;

	for (efa= em->faces.first; efa; efa= efa->next) {
		if ((tf=(MTFace *)efa->tmp.p)) {
			if (propmode) {
				UVsToTransData(sima, td++, td2d++, tf->uv[0], uvedit_uv_selected(scene, efa, tf, 0));
				UVsToTransData(sima, td++, td2d++, tf->uv[1], uvedit_uv_selected(scene, efa, tf, 1));
				UVsToTransData(sima, td++, td2d++, tf->uv[2], uvedit_uv_selected(scene, efa, tf, 2));
				if(efa->v4)
					UVsToTransData(sima, td++, td2d++, tf->uv[3], uvedit_uv_selected(scene, efa, tf, 3));
			} else {
				if(uvedit_uv_selected(scene, efa, tf, 0))				UVsToTransData(sima, td++, td2d++, tf->uv[0], 1);
				if(uvedit_uv_selected(scene, efa, tf, 1))				UVsToTransData(sima, td++, td2d++, tf->uv[1], 1);
				if(uvedit_uv_selected(scene, efa, tf, 2))				UVsToTransData(sima, td++, td2d++, tf->uv[2], 1);
				if(efa->v4 && uvedit_uv_selected(scene, efa, tf, 3))	UVsToTransData(sima, td++, td2d++, tf->uv[3], 1);
			}
		}
	}

	if (sima->flag & SI_LIVE_UNWRAP)
		ED_uvedit_live_unwrap_begin(t->scene, t->obedit);
}

void flushTransUVs(TransInfo *t)
{
	SpaceImage *sima = t->sa->spacedata.first;
	TransData2D *td;
	int a, width, height;
	float aspx, aspy, invx, invy;

	ED_space_image_uv_aspect(sima, &aspx, &aspy);
	ED_space_image_size(sima, &width, &height);
	invx= 1.0f/aspx;
	invy= 1.0f/aspy;

	/* flush to 2d vector from internally used 3d vector */
	for(a=0, td= t->data2d; a<t->total; a++, td++) {
		td->loc2d[0]= td->loc[0]*invx;
		td->loc2d[1]= td->loc[1]*invy;

		if((sima->flag & SI_PIXELSNAP) && (t->state != TRANS_CANCEL)) {
			td->loc2d[0]= (float)floor(width*td->loc2d[0] + 0.5f)/width;
			td->loc2d[1]= (float)floor(height*td->loc2d[1] + 0.5f)/height;
		}
	}
}

int clipUVTransform(TransInfo *t, float *vec, int resize)
{
	TransData *td;
	int a, clipx=1, clipy=1;
	float aspx, aspy, min[2], max[2];

	ED_space_image_uv_aspect(t->sa->spacedata.first, &aspx, &aspy);
	min[0]= min[1]= 0.0f;
	max[0]= aspx; max[1]= aspy;

	for(a=0, td= t->data; a<t->total; a++, td++) {
		DO_MINMAX2(td->loc, min, max);
	}

	if(resize) {
		if(min[0] < 0.0f && t->center[0] > 0.0f && t->center[0] < aspx*0.5f)
			vec[0] *= t->center[0]/(t->center[0] - min[0]);
		else if(max[0] > aspx && t->center[0] < aspx)
			vec[0] *= (t->center[0] - aspx)/(t->center[0] - max[0]);
		else
			clipx= 0;

		if(min[1] < 0.0f && t->center[1] > 0.0f && t->center[1] < aspy*0.5f)
			vec[1] *= t->center[1]/(t->center[1] - min[1]);
		else if(max[1] > aspy && t->center[1] < aspy)
			vec[1] *= (t->center[1] - aspy)/(t->center[1] - max[1]);
		else
			clipy= 0;
	}
	else {
		if(min[0] < 0.0f)
			vec[0] -= min[0];
		else if(max[0] > aspx)
			vec[0] -= max[0]-aspx;
		else
			clipx= 0;

		if(min[1] < 0.0f)
			vec[1] -= min[1];
		else if(max[1] > aspy)
			vec[1] -= max[1]-aspy;
		else
			clipy= 0;
	}

	return (clipx || clipy);
}

/* ********************* ANIMATION EDITORS (GENERAL) ************************* */

/* This function tests if a point is on the "mouse" side of the cursor/frame-marking */
static short FrameOnMouseSide(char side, float frame, float cframe)
{
	/* both sides, so it doesn't matter */
	if (side == 'B') return 1;

	/* only on the named side */
	if (side == 'R')
		return (frame >= cframe) ? 1 : 0;
	else
		return (frame <= cframe) ? 1 : 0;
}

/* ********************* NLA EDITOR ************************* */

static void createTransNlaData(bContext *C, TransInfo *t)
{
	Scene *scene= t->scene;
	SpaceNla *snla = NULL;
	TransData *td = NULL;
	TransDataNla *tdn = NULL;
	
	bAnimContext ac;
	ListBase anim_data = {NULL, NULL};
	bAnimListElem *ale;
	int filter;
	
	int count=0;
	
	/* determine what type of data we are operating on */
	if (ANIM_animdata_get_context(C, &ac) == 0)
		return;
	snla = (SpaceNla *)ac.sl;
	
	/* filter data */
	filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_FOREDIT);
	ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
	
	/* which side of the current frame should be allowed */
	if (t->mode == TFM_TIME_EXTEND) {
		/* only side on which mouse is gets transformed */
		float xmouse, ymouse;
		
		UI_view2d_region_to_view(&ac.ar->v2d, t->imval[0], t->imval[1], &xmouse, &ymouse);
		t->frame_side= (xmouse > CFRA) ? 'R' : 'L';
	}
	else {
		/* normal transform - both sides of current frame are considered */
		t->frame_side = 'B';
	}
	
	/* loop 1: count how many strips are selected (consider each strip as 2 points) */
	for (ale= anim_data.first; ale; ale= ale->next) {
		NlaTrack *nlt= (NlaTrack *)ale->data;
		NlaStrip *strip;
		
		/* make some meta-strips for chains of selected strips */
		BKE_nlastrips_make_metas(&nlt->strips, 1);
		
		/* only consider selected strips */
		for (strip= nlt->strips.first; strip; strip= strip->next) {
			// TODO: we can make strips have handles later on...
			/* transition strips can't get directly transformed */
			if (strip->type != NLASTRIP_TYPE_TRANSITION) {
				if (strip->flag & NLASTRIP_FLAG_SELECT) {
					if (FrameOnMouseSide(t->frame_side, strip->start, (float)CFRA)) count++;
					if (FrameOnMouseSide(t->frame_side, strip->end, (float)CFRA)) count++;
				}
			}
		}
	}
	
	/* stop if trying to build list if nothing selected */
	if (count == 0) {
		/* cleanup temp list */
		BLI_freelistN(&anim_data);
		return;
	}
	
	/* allocate memory for data */
	t->total= count;
	
	t->data= MEM_callocN(t->total*sizeof(TransData), "TransData(NLA Editor)");
	td= t->data;
	t->customData= MEM_callocN(t->total*sizeof(TransDataNla), "TransDataNla (NLA Editor)");
	tdn= t->customData;
	
	/* loop 2: build transdata array */
	for (ale= anim_data.first; ale; ale= ale->next) {
		/* only if a real NLA-track */
		if (ale->type == ANIMTYPE_NLATRACK) {
			AnimData *adt = ale->adt;
			NlaTrack *nlt= (NlaTrack *)ale->data;
			NlaStrip *strip;
			
			/* only consider selected strips */
			for (strip= nlt->strips.first; strip; strip= strip->next) {
				// TODO: we can make strips have handles later on...
				/* transition strips can't get directly transformed */
				if (strip->type != NLASTRIP_TYPE_TRANSITION) {
					if (strip->flag & NLASTRIP_FLAG_SELECT) {
						/* our transform data is constructed as follows:
						 *	- only the handles on the right side of the current-frame get included
						 *	- td structs are transform-elements operated on by the transform system
						 *	  and represent a single handle. The storage/pointer used (val or loc) depends on
						 *	  whether we're scaling or transforming. Ultimately though, the handles
						 * 	  the td writes to will simply be a dummy in tdn
						 *	- for each strip being transformed, a single tdn struct is used, so in some
						 *	  cases, there will need to be 1 of these tdn elements in the array skipped...
						 */
						float center[3], yval;
						
						/* firstly, init tdn settings */
						tdn->id= ale->id;
						tdn->oldTrack= tdn->nlt= nlt;
						tdn->strip= strip;
						tdn->trackIndex= BLI_findindex(&adt->nla_tracks, nlt);
						
						yval= (float)(tdn->trackIndex * NLACHANNEL_STEP(snla));
						
						tdn->h1[0]= strip->start;
						tdn->h1[1]= yval;
						tdn->h2[0]= strip->end;
						tdn->h2[1]= yval;
						
						center[0]= (float)CFRA;
						center[1]= yval;
						center[2]= 0.0f;
						
						/* set td's based on which handles are applicable */
						if (FrameOnMouseSide(t->frame_side, strip->start, (float)CFRA))
						{
							/* just set tdn to assume that it only has one handle for now */
							tdn->handle= -1;
							
							/* now, link the transform data up to this data */
							if (ELEM(t->mode, TFM_TRANSLATION, TFM_TIME_EXTEND)) {
								td->loc= tdn->h1;
								copy_v3_v3(td->iloc, tdn->h1);
								
								/* store all the other gunk that is required by transform */
								copy_v3_v3(td->center, center);
								memset(td->axismtx, 0, sizeof(td->axismtx));
								td->axismtx[2][2] = 1.0f;
								
								td->ext= NULL; td->val= NULL;
								
								td->flag |= TD_SELECTED;
								td->dist= 0.0f;
								
								unit_m3(td->mtx);
								unit_m3(td->smtx);
							}
							else {
								/* time scaling only needs single value */
								td->val= &tdn->h1[0];
								td->ival= tdn->h1[0];
							}
							
							td->extra= tdn;
							td++;
						}
						if (FrameOnMouseSide(t->frame_side, strip->end, (float)CFRA))
						{
							/* if tdn is already holding the start handle, then we're doing both, otherwise, only end */
							tdn->handle= (tdn->handle) ? 2 : 1;
							
							/* now, link the transform data up to this data */
							if (ELEM(t->mode, TFM_TRANSLATION, TFM_TIME_EXTEND)) {
								td->loc= tdn->h2;
								copy_v3_v3(td->iloc, tdn->h2);
								
								/* store all the other gunk that is required by transform */
								copy_v3_v3(td->center, center);
								memset(td->axismtx, 0, sizeof(td->axismtx));
								td->axismtx[2][2] = 1.0f;
								
								td->ext= NULL; td->val= NULL;
								
								td->flag |= TD_SELECTED;
								td->dist= 0.0f;
								
								unit_m3(td->mtx);
								unit_m3(td->smtx);
							}
							else {
								/* time scaling only needs single value */
								td->val= &tdn->h2[0];
								td->ival= tdn->h2[0];
							}
							
							td->extra= tdn;
							td++;
						}
						
						/* if both handles were used, skip the next tdn (i.e. leave it blank) since the counting code is dumb...
						 * otherwise, just advance to the next one...
						 */
						if (tdn->handle == 2)
							tdn += 2;
						else
							tdn++;
					}
				}
			}
		}
	}
	
	/* cleanup temp list */
	BLI_freelistN(&anim_data);
}

/* ********************* ACTION EDITOR ****************** */

/* Called by special_aftertrans_update to make sure selected gp-frames replace
 * any other gp-frames which may reside on that frame (that are not selected).
 * It also makes sure gp-frames are still stored in chronological order after
 * transform.
 */
static void posttrans_gpd_clean (bGPdata *gpd)
{
	bGPDlayer *gpl;
	
	for (gpl= gpd->layers.first; gpl; gpl= gpl->next) {
		ListBase sel_buffer = {NULL, NULL};
		bGPDframe *gpf, *gpfn;
		bGPDframe *gfs, *gfsn;
		
		/* loop 1: loop through and isolate selected gp-frames to buffer
		 * (these need to be sorted as they are isolated)
		 */
		for (gpf= gpl->frames.first; gpf; gpf= gpfn) {
			short added= 0;
			gpfn= gpf->next;
			
			if (gpf->flag & GP_FRAME_SELECT) {
				BLI_remlink(&gpl->frames, gpf);
				
				/* find place to add them in buffer
				 * - go backwards as most frames will still be in order,
				 *   so doing it this way will be faster
				 */
				for (gfs= sel_buffer.last; gfs; gfs= gfs->prev) {
					/* if current (gpf) occurs after this one in buffer, add! */
					if (gfs->framenum < gpf->framenum) {
						BLI_insertlinkafter(&sel_buffer, gfs, gpf);
						added= 1;
						break;
					}
				}
				if (added == 0)
					BLI_addhead(&sel_buffer, gpf);
			}
		}
		
		/* error checking: it is unlikely, but may be possible to have none selected */
		if (sel_buffer.first == NULL)
			continue;
		
		/* if all were selected (i.e. gpl->frames is empty), then just transfer sel-buf over */
		if (gpl->frames.first == NULL) {
			gpl->frames.first= sel_buffer.first;
			gpl->frames.last= sel_buffer.last;
			
			continue;
		}
		
		/* loop 2: remove duplicates of frames in buffers */
		for (gpf= gpl->frames.first; gpf && sel_buffer.first; gpf= gpfn) {
			gpfn= gpf->next;
			
			/* loop through sel_buffer, emptying stuff from front of buffer if ok */
			for (gfs= sel_buffer.first; gfs && gpf; gfs= gfsn) {
				gfsn= gfs->next;
				
				/* if this buffer frame needs to go before current, add it! */
				if (gfs->framenum < gpf->framenum) {
					/* transfer buffer frame to frames list (before current) */
					BLI_remlink(&sel_buffer, gfs);
					BLI_insertlinkbefore(&gpl->frames, gpf, gfs);
				}
				/* if this buffer frame is on same frame, replace current with it and stop */
				else if (gfs->framenum == gpf->framenum) {
					/* transfer buffer frame to frames list (before current) */
					BLI_remlink(&sel_buffer, gfs);
					BLI_insertlinkbefore(&gpl->frames, gpf, gfs);
					
					/* get rid of current frame */
					gpencil_layer_delframe(gpl, gpf);
				}
			}
		}
		
		/* if anything is still in buffer, append to end */
		for (gfs= sel_buffer.first; gfs; gfs= gfsn) {
			gfsn= gfs->next;
			
			BLI_remlink(&sel_buffer, gfs);
			BLI_addtail(&gpl->frames, gfs);
		}
	}
}

/* Called during special_aftertrans_update to make sure selected keyframes replace
 * any other keyframes which may reside on that frame (that is not selected).
 */
static void posttrans_fcurve_clean (FCurve *fcu, const short use_handle)
{
	float *selcache;	/* cache for frame numbers of selected frames (fcu->totvert*sizeof(float)) */
	int len, index, i;	/* number of frames in cache, item index */

	/* allocate memory for the cache */
	// TODO: investigate using BezTriple columns instead?
	if (fcu->totvert == 0 || fcu->bezt==NULL)
		return;
	selcache= MEM_callocN(sizeof(float)*fcu->totvert, "FCurveSelFrameNums");
	len= 0;
	index= 0;

	/* We do 2 loops, 1 for marking keyframes for deletion, one for deleting
	 * as there is no guarantee what order the keyframes are exactly, even though
	 * they have been sorted by time.
	 */

	/*	Loop 1: find selected keyframes   */
	for (i = 0; i < fcu->totvert; i++) {
		BezTriple *bezt= &fcu->bezt[i];
		
		if (BEZSELECTED(bezt)) {
			selcache[index]= bezt->vec[1][0];
			index++;
			len++;
		}
	}

	/* Loop 2: delete unselected keyframes on the same frames 
	 * (if any keyframes were found, or the whole curve wasn't affected) 
	 */
	if ((len) && (len != fcu->totvert)) {
		for (i= fcu->totvert-1; i >= 0; i--) {
			BezTriple *bezt= &fcu->bezt[i];
			
			if (BEZSELECTED(bezt) == 0) {
				/* check beztriple should be removed according to cache */
				for (index= 0; index < len; index++) {
					if (IS_EQF(bezt->vec[1][0], selcache[index])) {
						delete_fcurve_key(fcu, i, 0);
						break;
					}
					else if (bezt->vec[1][0] < selcache[index])
						break;
				}
			}
		}
		
		testhandles_fcurve(fcu, use_handle);
	}

	/* free cache */
	MEM_freeN(selcache);
}



/* Called by special_aftertrans_update to make sure selected keyframes replace
 * any other keyframes which may reside on that frame (that is not selected).
 * remake_action_ipos should have already been called
 */
static void posttrans_action_clean (bAnimContext *ac, bAction *act)
{
	ListBase anim_data = {NULL, NULL};
	bAnimListElem *ale;
	int filter;

	/* filter data */
	filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_FOREDIT /*| ANIMFILTER_CURVESONLY*/);
	ANIM_animdata_filter(ac, &anim_data, filter, act, ANIMCONT_ACTION);

	/* loop through relevant data, removing keyframes as appropriate
	 *  	- all keyframes are converted in/out of global time
	 */
	for (ale= anim_data.first; ale; ale= ale->next) {
		AnimData *adt= ANIM_nla_mapping_get(ac, ale);
		
		if (adt) {
			ANIM_nla_mapping_apply_fcurve(adt, ale->key_data, 0, 1);
			posttrans_fcurve_clean(ale->key_data, FALSE); /* only use handles in graph editor */
			ANIM_nla_mapping_apply_fcurve(adt, ale->key_data, 1, 1);
		}
		else
			posttrans_fcurve_clean(ale->key_data, FALSE); /* only use handles in graph editor */
	}

	/* free temp data */
	BLI_freelistN(&anim_data);
}

/* ----------------------------- */

/* fully select selected beztriples, but only include if it's on the right side of cfra */
static int count_fcurve_keys(FCurve *fcu, char side, float cfra)
{
	BezTriple *bezt;
	int i, count = 0;

	if (ELEM(NULL, fcu, fcu->bezt))
		return count;

	/* only include points that occur on the right side of cfra */
	for (i=0, bezt=fcu->bezt; i < fcu->totvert; i++, bezt++) {
		if (bezt->f2 & SELECT) {
			/* no need to adjust the handle selection since they are assumed
			 * selected (like graph editor with SIPO_NOHANDLES) */
			if (FrameOnMouseSide(side, bezt->vec[1][0], cfra)) {
				count += 1;
			}
		}
	}

	return count;
}

/* fully select selected beztriples, but only include if it's on the right side of cfra */
static int count_gplayer_frames(bGPDlayer *gpl, char side, float cfra)
{
	bGPDframe *gpf;
	int count = 0;
	
	if (gpl == NULL)
		return count;
	
	/* only include points that occur on the right side of cfra */
	for (gpf= gpl->frames.first; gpf; gpf= gpf->next) {
		if (gpf->flag & GP_FRAME_SELECT) {
			if (FrameOnMouseSide(side, (float)gpf->framenum, cfra))
				count++;
		}
	}
	
	return count;
}

/* This function assigns the information to transdata */
static void TimeToTransData(TransData *td, float *time, AnimData *adt)
{
	/* memory is calloc'ed, so that should zero everything nicely for us */
	td->val = time;
	td->ival = *(time);

	/* store the AnimData where this keyframe exists as a keyframe of the
	 * active action as td->extra.
	 */
	td->extra= adt;
}

/* This function advances the address to which td points to, so it must return
 * the new address so that the next time new transform data is added, it doesn't
 * overwrite the existing ones...  i.e.   td = IcuToTransData(td, icu, ob, side, cfra);
 *
 * The 'side' argument is needed for the extend mode. 'B' = both sides, 'R'/'L' mean only data
 * on the named side are used.
 */
static TransData *ActionFCurveToTransData(TransData *td, TransData2D **td2dv, FCurve *fcu, AnimData *adt, char side, float cfra)
{
	BezTriple *bezt;
	TransData2D *td2d = *td2dv;
	int i;

	if (ELEM(NULL, fcu, fcu->bezt))
		return td;

	for (i=0, bezt=fcu->bezt; i < fcu->totvert; i++, bezt++) {
		/* only add selected keyframes (for now, proportional edit is not enabled) */
		if (bezt->f2 & SELECT) { /* note this MUST match count_fcurve_keys(), so can't use BEZSELECTED() macro */
			/* only add if on the right 'side' of the current frame */
			if (FrameOnMouseSide(side, bezt->vec[1][0], cfra)) {
				TimeToTransData(td, bezt->vec[1], adt);
				
				/*set flags to move handles as necassary*/
				td->flag |= TD_MOVEHANDLE1|TD_MOVEHANDLE2;
				td2d->h1 = bezt->vec[0];
				td2d->h2 = bezt->vec[2];
				
				VECCOPY2D(td2d->ih1, td2d->h1);
				VECCOPY2D(td2d->ih2, td2d->h2);
				
				td++;
				td2d++;
			}
		}
	}
	
	*td2dv = td2d;
	
	return td;
}

/* helper struct for gp-frame transforms (only used here) */
typedef struct tGPFtransdata {
	float val;			/* where transdata writes transform */
	int *sdata;			/* pointer to gpf->framenum */
} tGPFtransdata;

/* This function helps flush transdata written to tempdata into the gp-frames  */
void flushTransGPactionData (TransInfo *t)
{
	tGPFtransdata *tfd;
	int i;

	/* find the first one to start from */
	if (t->mode == TFM_TIME_SLIDE)
		tfd= (tGPFtransdata *)( (float *)(t->customData) + 2 );
	else
		tfd= (tGPFtransdata *)(t->customData);

	/* flush data! */
	for (i = 0; i < t->total; i++, tfd++) {
		*(tfd->sdata)= (int)floor(tfd->val + 0.5f);
	}
}

/* This function advances the address to which td points to, so it must return
 * the new address so that the next time new transform data is added, it doesn't
 * overwrite the existing ones...  i.e.   td = GPLayerToTransData(td, ipo, ob, side, cfra);
 *
 * The 'side' argument is needed for the extend mode. 'B' = both sides, 'R'/'L' mean only data
 * on the named side are used.
 */
static int GPLayerToTransData (TransData *td, tGPFtransdata *tfd, bGPDlayer *gpl, char side, float cfra)
{
	bGPDframe *gpf;
	int count= 0;
	
	/* check for select frames on right side of current frame */
	for (gpf= gpl->frames.first; gpf; gpf= gpf->next) {
		if (gpf->flag & GP_FRAME_SELECT) {
			if (FrameOnMouseSide(side, (float)gpf->framenum, cfra)) {
				/* memory is calloc'ed, so that should zero everything nicely for us */
				td->val= &tfd->val;
				td->ival= (float)gpf->framenum;
				
				tfd->val= (float)gpf->framenum;
				tfd->sdata= &gpf->framenum;
				
				/* advance td now */
				td++;
				tfd++;
				count++;
			}
		}
	}
	
	return count;
}

static void createTransActionData(bContext *C, TransInfo *t)
{
	Scene *scene= t->scene;
	TransData *td = NULL;
	TransData2D *td2d = NULL;
	tGPFtransdata *tfd = NULL;
	
	bAnimContext ac;
	ListBase anim_data = {NULL, NULL};
	bAnimListElem *ale;
	int filter;
	
	int count=0;
	float cfra;
	
	/* determine what type of data we are operating on */
	if (ANIM_animdata_get_context(C, &ac) == 0)
		return;
	
	/* filter data */
	if (ac.datatype == ANIMCONT_GPENCIL)
		filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_FOREDIT);
	else
		filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_FOREDIT /*| ANIMFILTER_CURVESONLY*/);
	ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
	
	/* which side of the current frame should be allowed */
	if (t->mode == TFM_TIME_EXTEND) {
		/* only side on which mouse is gets transformed */
		float xmouse, ymouse;
		
		UI_view2d_region_to_view(&ac.ar->v2d, t->imval[0], t->imval[1], &xmouse, &ymouse);
		t->frame_side = (xmouse > CFRA) ? 'R' : 'L'; // XXX use t->frame_side
	}
	else {
		/* normal transform - both sides of current frame are considered */
		t->frame_side = 'B';
	}
	
	/* loop 1: fully select ipo-keys and count how many BezTriples are selected */
	for (ale= anim_data.first; ale; ale= ale->next) {
		AnimData *adt= ANIM_nla_mapping_get(&ac, ale);
		
		/* convert current-frame to action-time (slightly less accurate, espcially under
		 * higher scaling ratios, but is faster than converting all points)
		 */
		if (adt)
			cfra = BKE_nla_tweakedit_remap(adt, (float)CFRA, NLATIME_CONVERT_UNMAP);
		else
			cfra = (float)CFRA;
		
		if (ale->type == ANIMTYPE_FCURVE)
			count += count_fcurve_keys(ale->key_data, t->frame_side, cfra);
		else
			count += count_gplayer_frames(ale->data, t->frame_side, cfra);
	}
	
	/* stop if trying to build list if nothing selected */
	if (count == 0) {
		/* cleanup temp list */
		BLI_freelistN(&anim_data);
		return;
	}
	
	/* allocate memory for data */
	t->total= count;
	
	t->data= MEM_callocN(t->total*sizeof(TransData), "TransData(Action Editor)");
	t->data2d= MEM_callocN(t->total*sizeof(TransData2D), "transdata2d");
	td= t->data;
	td2d = t->data2d;
	
	if (ac.datatype == ANIMCONT_GPENCIL) {
		if (t->mode == TFM_TIME_SLIDE) {
			t->customData= MEM_callocN((sizeof(float)*2)+(sizeof(tGPFtransdata)*count), "TimeSlide + tGPFtransdata");
			tfd= (tGPFtransdata *)( (float *)(t->customData) + 2 );
		}
		else {
			t->customData= MEM_callocN(sizeof(tGPFtransdata)*count, "tGPFtransdata");
			tfd= (tGPFtransdata *)(t->customData);
		}
	}
	else if (t->mode == TFM_TIME_SLIDE)
		t->customData= MEM_callocN(sizeof(float)*2, "TimeSlide Min/Max");
	
	/* loop 2: build transdata array */
	for (ale= anim_data.first; ale; ale= ale->next) {
		if (ale->type == ANIMTYPE_GPLAYER) {
			bGPDlayer *gpl= (bGPDlayer *)ale->data;
			int i;
			
			i = GPLayerToTransData(td, tfd, gpl, t->frame_side, cfra);
			td += i;
			tfd += i;
		}
		else {
			AnimData *adt= ANIM_nla_mapping_get(&ac, ale);
			FCurve *fcu= (FCurve *)ale->key_data;
			
			/* convert current-frame to action-time (slightly less accurate, espcially under
			 * higher scaling ratios, but is faster than converting all points)
			 */
			if (adt)
				cfra = BKE_nla_tweakedit_remap(adt, (float)CFRA, NLATIME_CONVERT_UNMAP);
			else
				cfra = (float)CFRA;
			
			td= ActionFCurveToTransData(td, &td2d, fcu, adt, t->frame_side, cfra);
		}
	}
	
	/* check if we're supposed to be setting minx/maxx for TimeSlide */
	if (t->mode == TFM_TIME_SLIDE) {
		float min=999999999.0f, max=-999999999.0f;
		int i;
		
		td= t->data;
		for (i=0; i < count; i++, td++) {
			if (min > *(td->val)) min= *(td->val);
			if (max < *(td->val)) max= *(td->val);
		}
		
		if (min == max) {
			/* just use the current frame ranges */
			min = (float)PSFRA;
			max = (float)PEFRA;
		}
		
		/* minx/maxx values used by TimeSlide are stored as a
		 * calloced 2-float array in t->customData. This gets freed
		 * in postTrans (T_FREE_CUSTOMDATA).
		 */
		*((float *)(t->customData)) = min;
		*((float *)(t->customData) + 1) = max;
	}

	/* cleanup temp list */
	BLI_freelistN(&anim_data);
}

/* ********************* GRAPH EDITOR ************************* */

/* Helper function for createTransGraphEditData, which is reponsible for associating
 * source data with transform data
 */
static void bezt_to_transdata (TransData *td, TransData2D *td2d, AnimData *adt, BezTriple *bezt, 
				int bi, short selected, short ishandle, short intvals, 
				float mtx[3][3], float smtx[3][3])
{
	float *loc = bezt->vec[bi];
	float *cent = bezt->vec[1];

	/* New location from td gets dumped onto the old-location of td2d, which then
	 * gets copied to the actual data at td2d->loc2d (bezt->vec[n])
	 *
	 * Due to NLA mapping, we apply NLA mapping to some of the verts here,
	 * and then that mapping will be undone after transform is done.
	 */
	
	if (adt) {
		td2d->loc[0] = BKE_nla_tweakedit_remap(adt, loc[0], NLATIME_CONVERT_MAP);
		td2d->loc[1] = loc[1];
		td2d->loc[2] = 0.0f;
		td2d->loc2d = loc;
		
		td->loc = td2d->loc;
		td->center[0] = BKE_nla_tweakedit_remap(adt, cent[0], NLATIME_CONVERT_MAP);
		td->center[1] = cent[1];
		td->center[2] = 0.0f;
		
		copy_v3_v3(td->iloc, td->loc);
	}
	else {
		td2d->loc[0] = loc[0];
		td2d->loc[1] = loc[1];
		td2d->loc[2] = 0.0f;
		td2d->loc2d = loc;
		
		td->loc = td2d->loc;
		copy_v3_v3(td->center, cent);
		copy_v3_v3(td->iloc, td->loc);
	}

	if (td->flag & TD_MOVEHANDLE1) {
		td2d->h1 = bezt->vec[0];
		VECCOPY2D(td2d->ih1, td2d->h1);
	} 
	else 	
		td2d->h1 = NULL;

	if (td->flag & TD_MOVEHANDLE2) {
		td2d->h2 = bezt->vec[2];
		VECCOPY2D(td2d->ih2, td2d->h2);
	} 
	else 
		td2d->h2 = NULL;

	memset(td->axismtx, 0, sizeof(td->axismtx));
	td->axismtx[2][2] = 1.0f;
	
	td->ext= NULL; td->val= NULL;
	
	/* store AnimData info in td->extra, for applying mapping when flushing */
	td->extra= adt;
	
	if (selected) {
		td->flag |= TD_SELECTED;
		td->dist= 0.0f;
	}
	else
		td->dist= MAXFLOAT;
	
	if (ishandle)
		td->flag |= TD_NOTIMESNAP;
	if (intvals)
		td->flag |= TD_INTVALUES;

	/* copy space-conversion matrices for dealing with non-uniform scales */
	copy_m3_m3(td->mtx, mtx);
	copy_m3_m3(td->smtx, smtx);
}

static void createTransGraphEditData(bContext *C, TransInfo *t)
{
	SpaceIpo *sipo = (SpaceIpo *)t->sa->spacedata.first;
	Scene *scene= t->scene;
	ARegion *ar= t->ar;
	View2D *v2d= &ar->v2d;
	
	TransData *td = NULL;
	TransData2D *td2d = NULL;
	
	bAnimContext ac;
	ListBase anim_data = {NULL, NULL};
	bAnimListElem *ale;
	int filter;
	
	BezTriple *bezt;
	int count=0, i;
	float cfra;
	float mtx[3][3], smtx[3][3];
	const short use_handle = !(sipo->flag & SIPO_NOHANDLES);
	
	/* determine what type of data we are operating on */
	if (ANIM_animdata_get_context(C, &ac) == 0)
		return;
	
	/* filter data */
	filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_CURVE_VISIBLE);
	ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
	
	/* which side of the current frame should be allowed */
		// XXX we still want this mode, but how to get this using standard transform too?
	if (t->mode == TFM_TIME_EXTEND) {
		/* only side on which mouse is gets transformed */
		float xmouse, ymouse;
		
		UI_view2d_region_to_view(v2d, t->imval[0], t->imval[1], &xmouse, &ymouse);
		t->frame_side = (xmouse > CFRA) ? 'R' : 'L'; // XXX use t->frame_side
	}
	else {
		/* normal transform - both sides of current frame are considered */
		t->frame_side = 'B';
	}
	
	/* loop 1: count how many BezTriples (specifically their verts) are selected (or should be edited) */
	for (ale= anim_data.first; ale; ale= ale->next) {
		AnimData *adt= ANIM_nla_mapping_get(&ac, ale);
		FCurve *fcu= (FCurve *)ale->key_data;
		
		/* convert current-frame to action-time (slightly less accurate, espcially under
		 * higher scaling ratios, but is faster than converting all points)
		 */
		if (adt)
			cfra = BKE_nla_tweakedit_remap(adt, (float)CFRA, NLATIME_CONVERT_UNMAP);
		else
			cfra = (float)CFRA;
		
		/* F-Curve may not have any keyframes */
		if (fcu->bezt == NULL)
			continue;
		
		/* only include BezTriples whose 'keyframe' occurs on the same side of the current frame as mouse */
		for (i=0, bezt=fcu->bezt; i < fcu->totvert; i++, bezt++) {
			if (FrameOnMouseSide(t->frame_side, bezt->vec[1][0], cfra)) {
				const char sel2= bezt->f2 & SELECT;
				const char sel1= use_handle ? bezt->f1 & SELECT : sel2;
				const char sel3= use_handle ? bezt->f3 & SELECT : sel2;

				if (ELEM3(t->mode, TFM_TRANSLATION, TFM_TIME_TRANSLATE, TFM_TIME_SLIDE)) {
					/* for 'normal' pivots - just include anything that is selected.
					   this works a bit differently in translation modes */
					if (sel2) count++;
					else {
						if (sel1) count++;
						if (sel3) count++;
					}
				} 
				else if (sipo->around == V3D_LOCAL) {
					/* for local-pivot we only need to count the number of selected handles only, so that centerpoints don't
					 * don't get moved wrong
					 */
					if (bezt->ipo == BEZT_IPO_BEZ) {
						if (sel1) count++;
						if (sel3) count++;
					}
					/* else if (sel2) count++; // TODO: could this cause problems? */
					/* - yes this causes problems, because no td is created for the center point */
				}
				else {
					/* for 'normal' pivots - just include anything that is selected */
					if (sel1) count++;
					if (sel2) count++;
					if (sel3) count++;
				}
			}
		}
	}
	
	/* stop if trying to build list if nothing selected */
	if (count == 0) {
		/* cleanup temp list */
		BLI_freelistN(&anim_data);
		return;
	}
	
	/* allocate memory for data */
	t->total= count;
	
	t->data= MEM_callocN(t->total*sizeof(TransData), "TransData (Graph Editor)");
		/* for each 2d vert a 3d vector is allocated, so that they can be treated just as if they were 3d verts */
	t->data2d= MEM_callocN(t->total*sizeof(TransData2D), "TransData2D (Graph Editor)");
	
	td= t->data;
	td2d= t->data2d;
	
	/* precompute space-conversion matrices for dealing with non-uniform scaling of Graph Editor */
	unit_m3(mtx);
	unit_m3(smtx);
	
	if (ELEM(t->mode, TFM_ROTATION, TFM_RESIZE)) {
		float xscale, yscale;
		
		/* apply scale factors to x and y axes of space-conversion matrices */
		UI_view2d_getscale(v2d, &xscale, &yscale);
		
		/* mtx is data to global (i.e. view) conversion */
		mul_v3_fl(mtx[0], xscale);
		mul_v3_fl(mtx[1], yscale);
		
		/* smtx is global (i.e. view) to data conversion */
		if (IS_EQF(xscale, 0.0f) == 0) mul_v3_fl(smtx[0], 1.0f/xscale);
		if (IS_EQF(yscale, 0.0f) == 0) mul_v3_fl(smtx[1], 1.0f/yscale);
	}
	
	/* loop 2: build transdata arrays */
	for (ale= anim_data.first; ale; ale= ale->next) {
		AnimData *adt= ANIM_nla_mapping_get(&ac, ale);
		FCurve *fcu= (FCurve *)ale->key_data;
		short intvals= (fcu->flag & FCURVE_INT_VALUES);
		
		/* convert current-frame to action-time (slightly less accurate, espcially under
		 * higher scaling ratios, but is faster than converting all points)
		 */
		if (adt)
			cfra = BKE_nla_tweakedit_remap(adt, (float)CFRA, NLATIME_CONVERT_UNMAP);
		else
			cfra = (float)CFRA;
			
		/* F-Curve may not have any keyframes */
		if (fcu->bezt == NULL)
			continue;
		
		ANIM_unit_mapping_apply_fcurve(ac.scene, ale->id, ale->key_data, ANIM_UNITCONV_ONLYSEL|ANIM_UNITCONV_SELVERTS);
		
		/* only include BezTriples whose 'keyframe' occurs on the same side of the current frame as mouse (if applicable) */
		for (i=0, bezt= fcu->bezt; i < fcu->totvert; i++, bezt++) {
			if (FrameOnMouseSide(t->frame_side, bezt->vec[1][0], cfra)) {
				const char sel2= bezt->f2 & SELECT;
				const char sel1= use_handle ? bezt->f1 & SELECT : sel2;
				const char sel3= use_handle ? bezt->f3 & SELECT : sel2;

				TransDataCurveHandleFlags *hdata = NULL;
				/* short h1=1, h2=1; */ /* UNUSED */
				
				/* only include handles if selected, irrespective of the interpolation modes.
				 * also, only treat handles specially if the center point isn't selected. 
				 */
				if (!ELEM3(t->mode, TFM_TRANSLATION, TFM_TIME_TRANSLATE, TFM_TIME_SLIDE) || !(sel2)) {
					if (sel1) {
						hdata = initTransDataCurveHandles(td, bezt);
						bezt_to_transdata(td++, td2d++, adt, bezt, 0, 1, 1, intvals, mtx, smtx);
					} 
					else {
						/* h1= 0; */ /* UNUSED */
					}
					
					if (sel3) {
						if (hdata==NULL)
							hdata = initTransDataCurveHandles(td, bezt);
						bezt_to_transdata(td++, td2d++, adt, bezt, 2, 1, 1, intvals, mtx, smtx);
					} 
					else {
						/* h2= 0; */ /* UNUSED */
					}
				}
				
				/* only include main vert if selected */
				if (sel2 && (sipo->around != V3D_LOCAL || ELEM3(t->mode, TFM_TRANSLATION, TFM_TIME_TRANSLATE, TFM_TIME_SLIDE))) {

					/* move handles relative to center */
					if (ELEM3(t->mode, TFM_TRANSLATION, TFM_TIME_TRANSLATE, TFM_TIME_SLIDE)) {
						if (sel1) td->flag |= TD_MOVEHANDLE1;
						if (sel3) td->flag |= TD_MOVEHANDLE2;
					}
					
					/* if handles were not selected, store their selection status */
					if (!(sel1) && !(sel3)) {
						if (hdata == NULL)
							hdata = initTransDataCurveHandles(td, bezt);
					}
				
					bezt_to_transdata(td++, td2d++, adt, bezt, 1, 1, 0, intvals, mtx, smtx);
					
				}
				/* special hack (must be done after initTransDataCurveHandles(), as that stores handle settings to restore...):
				 *	- Check if we've got entire BezTriple selected and we're scaling/rotating that point,
				 *	  then check if we're using auto-handles.
				 *	- If so, change them auto-handles to aligned handles so that handles get affected too
				 */
				if (ELEM(bezt->h1, HD_AUTO, HD_AUTO_ANIM) && ELEM(bezt->h2, HD_AUTO, HD_AUTO_ANIM) && ELEM(t->mode, TFM_ROTATION, TFM_RESIZE)) {
					if (hdata && (sel1) && (sel3)) {
						bezt->h1= HD_ALIGN;
						bezt->h2= HD_ALIGN;
					}
				}
			}
		}
		
		/* Sets handles based on the selection */
		testhandles_fcurve(fcu, use_handle);
	}
	
	/* cleanup temp list */
	BLI_freelistN(&anim_data);
}


/* ------------------------ */

/* struct for use in re-sorting BezTriples during Graph Editor transform */
typedef struct BeztMap {
	BezTriple *bezt;
	unsigned int oldIndex; 		/* index of bezt in fcu->bezt array before sorting */
	unsigned int newIndex;		/* index of bezt in fcu->bezt array after sorting */
	short swapHs; 				/* swap order of handles (-1=clear; 0=not checked, 1=swap) */
	char pipo, cipo;			/* interpolation of current and next segments */
} BeztMap;


/* This function converts an FCurve's BezTriple array to a BeztMap array
 * NOTE: this allocates memory that will need to get freed later
 */
static BeztMap *bezt_to_beztmaps (BezTriple *bezts, int totvert, const short UNUSED(use_handle))
{
	BezTriple *bezt= bezts;
	BezTriple *prevbezt= NULL;
	BeztMap *bezm, *bezms;
	int i;
	
	/* allocate memory for this array */
	if (totvert==0 || bezts==NULL)
		return NULL;
	bezm= bezms= MEM_callocN(sizeof(BeztMap)*totvert, "BeztMaps");
	
	/* assign beztriples to beztmaps */
	for (i=0; i < totvert; i++, bezm++, prevbezt=bezt, bezt++) {
		bezm->bezt= bezt;
		
		bezm->oldIndex= i;
		bezm->newIndex= i;
		
		bezm->pipo= (prevbezt) ? prevbezt->ipo : bezt->ipo;
		bezm->cipo= bezt->ipo;
	}

	return bezms;
}

/* This function copies the code of sort_time_ipocurve, but acts on BeztMap structs instead */
static void sort_time_beztmaps (BeztMap *bezms, int totvert, const short UNUSED(use_handle))
{
	BeztMap *bezm;
	int i, ok= 1;
	
	/* keep repeating the process until nothing is out of place anymore */
	while (ok) {
		ok= 0;
		
		bezm= bezms;
		i= totvert;
		while (i--) {
			/* is current bezm out of order (i.e. occurs later than next)? */
			if (i > 0) {
				if (bezm->bezt->vec[1][0] > (bezm+1)->bezt->vec[1][0]) {
					bezm->newIndex++;
					(bezm+1)->newIndex--;
					
					SWAP(BeztMap, *bezm, *(bezm+1));
					
					ok= 1;
				}
			}
			
			/* do we need to check if the handles need to be swapped?
			 * optimisation: this only needs to be performed in the first loop
			 */
			if (bezm->swapHs == 0) {
				if ( (bezm->bezt->vec[0][0] > bezm->bezt->vec[1][0]) &&
					 (bezm->bezt->vec[2][0] < bezm->bezt->vec[1][0]) )
				{
					/* handles need to be swapped */
					bezm->swapHs = 1;
				}
				else {
					/* handles need to be cleared */
					bezm->swapHs = -1;
				}
			}
			
			bezm++;
		}
	}
}

/* This function firstly adjusts the pointers that the transdata has to each BezTriple */
static void beztmap_to_data (TransInfo *t, FCurve *fcu, BeztMap *bezms, int totvert, const short UNUSED(use_handle))
{
	BezTriple *bezts = fcu->bezt;
	BeztMap *bezm;
	TransData2D *td2d;
	TransData *td;
	int i, j;
	char *adjusted;
	
	/* dynamically allocate an array of chars to mark whether an TransData's
	 * pointers have been fixed already, so that we don't override ones that are
	 * already done
	  */
	adjusted= MEM_callocN(t->total, "beztmap_adjusted_map");
	
	/* for each beztmap item, find if it is used anywhere */
	bezm= bezms;
	for (i= 0; i < totvert; i++, bezm++) {
		/* loop through transdata, testing if we have a hit
		 * for the handles (vec[0]/vec[2]), we must also check if they need to be swapped...
		 */
		td2d= t->data2d;
		td= t->data;
		for (j= 0; j < t->total; j++, td2d++, td++) {
			/* skip item if already marked */
			if (adjusted[j] != 0) continue;
			
			/* update all transdata pointers, no need to check for selections etc,
			 * since only points that are really needed were created as transdata
			 */
			if (td2d->loc2d == bezm->bezt->vec[0]) {
				if (bezm->swapHs == 1)
					td2d->loc2d= (bezts + bezm->newIndex)->vec[2];
				else
					td2d->loc2d= (bezts + bezm->newIndex)->vec[0];
				adjusted[j] = 1;
			}
			else if (td2d->loc2d == bezm->bezt->vec[2]) {
				if (bezm->swapHs == 1)
					td2d->loc2d= (bezts + bezm->newIndex)->vec[0];
				else
					td2d->loc2d= (bezts + bezm->newIndex)->vec[2];
				adjusted[j] = 1;
			}
			else if (td2d->loc2d == bezm->bezt->vec[1]) {
				td2d->loc2d= (bezts + bezm->newIndex)->vec[1];
					
				/* if only control point is selected, the handle pointers need to be updated as well */
				if(td2d->h1)
					td2d->h1= (bezts + bezm->newIndex)->vec[0];
				if(td2d->h2)
					td2d->h2= (bezts + bezm->newIndex)->vec[2];
					
				adjusted[j] = 1;
			}

			/* the handle type pointer has to be updated too */
			if (adjusted[j] && td->flag & TD_BEZTRIPLE && td->hdata) {
				if(bezm->swapHs == 1) {
					td->hdata->h1 = &(bezts + bezm->newIndex)->h2;
					td->hdata->h2 = &(bezts + bezm->newIndex)->h1;
				}
				else {
					td->hdata->h1 = &(bezts + bezm->newIndex)->h1;
					td->hdata->h2 = &(bezts + bezm->newIndex)->h2;
				}
			}
		}
		
	}
	
	/* free temp memory used for 'adjusted' array */
	MEM_freeN(adjusted);
}

/* This function is called by recalcData during the Transform loop to recalculate
 * the handles of curves and sort the keyframes so that the curves draw correctly.
 * It is only called if some keyframes have moved out of order.
 *
 * anim_data is the list of channels (F-Curves) retrieved already containing the
 * channels to work on. It should not be freed here as it may still need to be used.
 */
void remake_graph_transdata (TransInfo *t, ListBase *anim_data)
{
	SpaceIpo *sipo = (SpaceIpo *)t->sa->spacedata.first;
	bAnimListElem *ale;
	const short use_handle = !(sipo->flag & SIPO_NOHANDLES);
	
	/* sort and reassign verts */
	for (ale= anim_data->first; ale; ale= ale->next) {
		FCurve *fcu= (FCurve *)ale->key_data;
		
		if (fcu->bezt) {
			BeztMap *bezm;
			
			/* adjust transform-data pointers */
			/* note, none of these functions use 'use_handle', it could be removed */
			bezm= bezt_to_beztmaps(fcu->bezt, fcu->totvert, use_handle);
			sort_time_beztmaps(bezm, fcu->totvert, use_handle);
			beztmap_to_data(t, fcu, bezm, fcu->totvert, use_handle);
			
			/* free mapping stuff */
			MEM_freeN(bezm);
			
			/* re-sort actual beztriples (perhaps this could be done using the beztmaps to save time?) */
			sort_time_fcurve(fcu);
			
			/* make sure handles are all set correctly */
			testhandles_fcurve(fcu, use_handle);
		}
	}
}

/* this function is called on recalcData to apply the transforms applied
 * to the transdata on to the actual keyframe data
 */
void flushTransGraphData(TransInfo *t)
{
	SpaceIpo *sipo = (SpaceIpo *)t->sa->spacedata.first;
	TransData *td;
	TransData2D *td2d;
	Scene *scene= t->scene;
	double secf= FPS;
	int a;

	/* flush to 2d vector from internally used 3d vector */
	for (a=0, td= t->data, td2d=t->data2d; a<t->total; a++, td++, td2d++) {
		AnimData *adt= (AnimData *)td->extra; /* pointers to relevant AnimData blocks are stored in the td->extra pointers */
		
		/* handle snapping for time values
		 *	- we should still be in NLA-mapping timespace
		 *	- only apply to keyframes (but never to handles)
		 */
		if ((td->flag & TD_NOTIMESNAP)==0) {
			switch (sipo->autosnap) {
				case SACTSNAP_FRAME: /* snap to nearest frame (or second if drawing seconds) */
					if (sipo->flag & SIPO_DRAWTIME)
						td2d->loc[0]= (float)( floor((td2d->loc[0]/secf) + 0.5f) * secf );
					else
						td2d->loc[0]= (float)( floor(td2d->loc[0]+0.5f) );
					break;
				
				case SACTSNAP_MARKER: /* snap to nearest marker */
					td2d->loc[0]= (float)ED_markers_find_nearest_marker_time(&t->scene->markers, td2d->loc[0]);
					break;
			}
		}
		
		/* we need to unapply the nla-mapping from the time in some situations */
		if (adt)
			td2d->loc2d[0]= BKE_nla_tweakedit_remap(adt, td2d->loc[0], NLATIME_CONVERT_UNMAP);
		else
			td2d->loc2d[0]= td2d->loc[0];
		
		/* if int-values only, truncate to integers */
		if (td->flag & TD_INTVALUES)
			td2d->loc2d[1]= floorf(td2d->loc[1] + 0.5f);
		else
			td2d->loc2d[1]= td2d->loc[1];
		
		if ((td->flag & TD_MOVEHANDLE1) && td2d->h1) {
			td2d->h1[0] = td2d->ih1[0] + td->loc[0] - td->iloc[0];
			td2d->h1[1] = td2d->ih1[1] + td->loc[1] - td->iloc[1];
		}
		
		if ((td->flag & TD_MOVEHANDLE2) && td2d->h2) {
			td2d->h2[0] = td2d->ih2[0] + td->loc[0] - td->iloc[0];
			td2d->h2[1] = td2d->ih2[1] + td->loc[1] - td->iloc[1];
		}
	}
}

/* ******************* Sequencer Transform data ******************* */

/* This function applies the rules for transforming a strip so duplicate
 * checks dont need to be added in multiple places.
 *
 * recursive, count and flag MUST be set.
 *
 * seq->depth must be set before running this function so we know if the strips
 * are root level or not
 */
static void SeqTransInfo(TransInfo *t, Sequence *seq, int *recursive, int *count, int *flag)
{
	/* for extend we need to do some tricks */
	if (t->mode == TFM_TIME_EXTEND) {

		/* *** Extend Transform *** */

		Scene * scene= t->scene;
		int cfra= CFRA;
		int left= seq_tx_get_final_left(seq, 0);
		int right= seq_tx_get_final_right(seq, 0);

		if (seq->depth == 0 && ((seq->flag & SELECT) == 0 || (seq->flag & SEQ_LOCK))) {
			*recursive= 0;
			*count= 0;
			*flag= 0;
		}
		else if (seq->type ==SEQ_META) {

			/* for meta's we only ever need to extend their children, no matter what depth
			 * just check the meta's are in the bounds */
			if (t->frame_side=='R' && right <= cfra)		*recursive= 0;
			else if (t->frame_side=='L' && left >= cfra)	*recursive= 0;
			else											*recursive= 1;

			*count= 0;
			*flag= 0;
		}
		else {

			*recursive= 0;	/* not a meta, so no thinking here */
			*count= 1;		/* unless its set to 0, extend will never set 2 handles at once */
			*flag= (seq->flag | SELECT) & ~(SEQ_LEFTSEL|SEQ_RIGHTSEL);

			if (t->frame_side=='R') {
				if (right <= cfra)		*count= *flag= 0;	/* ignore */
				else if (left > cfra)	;	/* keep the selection */
				else					*flag |= SEQ_RIGHTSEL;
			}
			else {
				if (left >= cfra)		*count= *flag= 0;	/* ignore */
				else if (right < cfra)	;	/* keep the selection */
				else					*flag |= SEQ_LEFTSEL;
			}
		}
	} else {

		t->frame_side= 'B';

		/* *** Normal Transform *** */

		if (seq->depth == 0) {

			/* Count */

			/* Non nested strips (resect selection and handles) */
			if ((seq->flag & SELECT) == 0 || (seq->flag & SEQ_LOCK)) {
				*recursive= 0;
				*count= 0;
				*flag= 0;
			}
			else {
				if ((seq->flag & (SEQ_LEFTSEL|SEQ_RIGHTSEL)) == (SEQ_LEFTSEL|SEQ_RIGHTSEL)) {
					*flag= seq->flag;
					*count= 2; /* we need 2 transdata's */
				} else {
					*flag= seq->flag;
					*count= 1; /* selected or with a handle selected */
				}

				/* Recursive */

				if ((seq->type == SEQ_META) && ((seq->flag & (SEQ_LEFTSEL|SEQ_RIGHTSEL)) == 0)) {
					/* if any handles are selected, dont recurse */
					*recursive = 1;
				}
				else {
					*recursive = 0;
				}
			}
		}
		else {
			/* Nested, different rules apply */

#ifdef SEQ_TX_NESTED_METAS
			*flag= (seq->flag | SELECT) & ~(SEQ_LEFTSEL|SEQ_RIGHTSEL);
			*count= 1; /* ignore the selection for nested */
			*recursive = (seq->type == SEQ_META	);
#else
			if (seq->type == SEQ_META) {
				/* Meta's can only directly be moved between channels since they
				 * dont have their start and length set directly (children affect that)
				 * since this Meta is nested we dont need any of its data infact.
				 * calc_sequence() will update its settings when run on the toplevel meta */
				*flag= 0;
				*count= 0;
				*recursive = 1;
			}
			else {
				*flag= (seq->flag | SELECT) & ~(SEQ_LEFTSEL|SEQ_RIGHTSEL);
				*count= 1; /* ignore the selection for nested */
				*recursive = 0;
			}
#endif
		}
	}
}



static int SeqTransCount(TransInfo *t, ListBase *seqbase, int depth)
{
	Sequence *seq;
	int tot= 0, recursive, count, flag;

	for (seq= seqbase->first; seq; seq= seq->next) {
		seq->depth= depth;

		SeqTransInfo(t, seq, &recursive, &count, &flag); /* ignore the flag */
		tot += count;

		if (recursive) {
			tot += SeqTransCount(t, &seq->seqbase, depth+1);
		}
	}

	return tot;
}


static TransData *SeqToTransData(TransData *td, TransData2D *td2d, TransDataSeq *tdsq, Sequence *seq, int flag, int sel_flag)
{
	int start_left;

	switch(sel_flag) {
	case SELECT:
		/* Use seq_tx_get_final_left() and an offset here
		 * so transform has the left hand location of the strip.
		 * tdsq->start_offset is used when flushing the tx data back */
		start_left= seq_tx_get_final_left(seq, 0);
		td2d->loc[0]= start_left;
		tdsq->start_offset= start_left - seq->start; /* use to apply the original location */
		break;
	case SEQ_LEFTSEL:
		start_left= seq_tx_get_final_left(seq, 0);
		td2d->loc[0] = start_left;
		break;
	case SEQ_RIGHTSEL:
		td2d->loc[0] = seq_tx_get_final_right(seq, 0);
		break;
	}

	td2d->loc[1] = seq->machine; /* channel - Y location */
	td2d->loc[2] = 0.0f;
	td2d->loc2d = NULL;


	tdsq->seq= seq;

	/* Use instead of seq->flag for nested strips and other
	 * cases where the selection may need to be modified */
	tdsq->flag= flag;
	tdsq->sel_flag= sel_flag;


	td->extra= (void *)tdsq; /* allow us to update the strip from here */

	td->flag = 0;
	td->loc = td2d->loc;
	copy_v3_v3(td->center, td->loc);
	copy_v3_v3(td->iloc, td->loc);

	memset(td->axismtx, 0, sizeof(td->axismtx));
	td->axismtx[2][2] = 1.0f;

	td->ext= NULL; td->val= NULL;

	td->flag |= TD_SELECTED;
	td->dist= 0.0;

	unit_m3(td->mtx);
	unit_m3(td->smtx);

	/* Time Transform (extend) */
	td->val= td2d->loc;
	td->ival= td2d->loc[0];

	return td;
}

static int SeqToTransData_Recursive(TransInfo *t, ListBase *seqbase, TransData *td, TransData2D *td2d, TransDataSeq *tdsq)
{
	Sequence *seq;
	int recursive, count, flag;
	int tot= 0;

	for (seq= seqbase->first; seq; seq= seq->next) {

		SeqTransInfo(t, seq, &recursive, &count, &flag);

		/* add children first so recalculating metastrips does nested strips first */
		if (recursive) {
			int tot_children= SeqToTransData_Recursive(t, &seq->seqbase, td, td2d, tdsq);

			td=		td +	tot_children;
			td2d=	td2d +	tot_children;
			tdsq=	tdsq +	tot_children;

			tot += tot_children;
		}

		/* use 'flag' which is derived from seq->flag but modified for special cases */
		if (flag & SELECT) {
			if (flag & (SEQ_LEFTSEL|SEQ_RIGHTSEL)) {
				if (flag & SEQ_LEFTSEL) {
					SeqToTransData(td++, td2d++, tdsq++, seq, flag, SEQ_LEFTSEL);
					tot++;
				}
				if (flag & SEQ_RIGHTSEL) {
					SeqToTransData(td++, td2d++, tdsq++, seq, flag, SEQ_RIGHTSEL);
					tot++;
				}
			}
			else {
				SeqToTransData(td++, td2d++, tdsq++, seq, flag, SELECT);
				tot++;
			}
		}
	}

	return tot;
}

static void freeSeqData(TransInfo *t)
{
	Editing *ed= seq_give_editing(t->scene, FALSE);

	if(ed != NULL) {
		ListBase *seqbasep= ed->seqbasep;
		TransData *td= t->data;
		int a;

		/* prevent updating the same seq twice
		 * if the transdata order is changed this will mess up
		 * but so will TransDataSeq */
		Sequence *seq_prev= NULL;
		Sequence *seq;


		if (!(t->state == TRANS_CANCEL)) {

#if 0		// default 2.4 behavior

			/* flush to 2d vector from internally used 3d vector */
			for(a=0; a<t->total; a++, td++) {
				if ((seq != seq_prev) && (seq->depth==0) && (seq->flag & SEQ_OVERLAP)) {
				seq= ((TransDataSeq *)td->extra)->seq;
					shuffle_seq(seqbasep, seq);
				}

				seq_prev= seq;
			}

#else		// durian hack
			{
				int overlap= 0;

				for(a=0; a<t->total; a++, td++) {
					seq_prev= NULL;
					seq= ((TransDataSeq *)td->extra)->seq;
					if ((seq != seq_prev) && (seq->depth==0) && (seq->flag & SEQ_OVERLAP)) {
						overlap= 1;
						break;
					}
					seq_prev= seq;
				}

				if(overlap) {
					int has_effect= 0;
					for(seq= seqbasep->first; seq; seq= seq->next)
						seq->tmp= NULL;

					td= t->data;
					seq_prev= NULL;
					for(a=0; a<t->total; a++, td++) {
						seq= ((TransDataSeq *)td->extra)->seq;
						if ((seq != seq_prev)) {
							/* check effects strips, we cant change their time */
							if((seq->type & SEQ_EFFECT) && seq->seq1) {
								has_effect= TRUE;
							}
							else {
								/* Tag seq with a non zero value, used by shuffle_seq_time to identify the ones to shuffle */
								seq->tmp= (void*)1;
							}
						}
					}

					shuffle_seq_time(seqbasep, t->scene);

					if(has_effect) {
						/* update effects strips based on strips just moved in time */
						td= t->data;
						seq_prev= NULL;
						for(a=0; a<t->total; a++, td++) {
							seq= ((TransDataSeq *)td->extra)->seq;
							if ((seq != seq_prev)) {
								if((seq->type & SEQ_EFFECT) && seq->seq1) {
									calc_sequence(t->scene, seq);
								}
							}
						}

						/* now if any effects _still_ overlap, we need to move them up */
						td= t->data;
						seq_prev= NULL;
						for(a=0; a<t->total; a++, td++) {
							seq= ((TransDataSeq *)td->extra)->seq;
							if ((seq != seq_prev)) {
								if((seq->type & SEQ_EFFECT) && seq->seq1) {
									if(seq_test_overlap(seqbasep, seq)) {
										shuffle_seq(seqbasep, seq, t->scene);
									}
								}
							}
						}
						/* done with effects */
					}
				}
			}
#endif

			for(seq= seqbasep->first; seq; seq= seq->next) {
				/* We might want to build a list of effects that need to be updated during transform */
				if(seq->type & SEQ_EFFECT) {
					if		(seq->seq1 && seq->seq1->flag & SELECT) calc_sequence(t->scene, seq);
					else if	(seq->seq2 && seq->seq2->flag & SELECT) calc_sequence(t->scene, seq);
					else if	(seq->seq3 && seq->seq3->flag & SELECT) calc_sequence(t->scene, seq);
				}
			}

			sort_seq(t->scene);
		}
		else {
			/* Cancelled, need to update the strips display */
			for(a=0; a<t->total; a++, td++) {
				seq= ((TransDataSeq *)td->extra)->seq;
				if ((seq != seq_prev) && (seq->depth==0)) {
					calc_sequence_disp(t->scene, seq);
				}
				seq_prev= seq;
			}
		}
	}

	if (t->customData) {
		MEM_freeN(t->customData);
		t->customData= NULL;
	}
	if (t->data) {
		MEM_freeN(t->data); // XXX postTrans usually does this
		t->data= NULL;
	}
}

static void createTransSeqData(bContext *C, TransInfo *t)
{
#define XXX_DURIAN_ANIM_TX_HACK

	View2D *v2d= UI_view2d_fromcontext(C);
	Scene *scene= t->scene;
	Editing *ed= seq_give_editing(t->scene, FALSE);
	TransData *td = NULL;
	TransData2D *td2d= NULL;
	TransDataSeq *tdsq= NULL;

	int count=0;

	if (ed==NULL) {
		t->total= 0;
		return;
	}

	t->customFree= freeSeqData;

	/* which side of the current frame should be allowed */
	if (t->mode == TFM_TIME_EXTEND) {
		/* only side on which mouse is gets transformed */
		float xmouse, ymouse;

		UI_view2d_region_to_view(v2d, t->imval[0], t->imval[1], &xmouse, &ymouse);
		t->frame_side = (xmouse > CFRA) ? 'R' : 'L';
	}
	else {
		/* normal transform - both sides of current frame are considered */
		t->frame_side = 'B';
	}

#ifdef XXX_DURIAN_ANIM_TX_HACK
	{
		Sequence *seq;
		for(seq= ed->seqbasep->first; seq; seq= seq->next) {
			/* hack */
			if((seq->flag & SELECT)==0 && seq->type & SEQ_EFFECT) {
				Sequence *seq_user;
				int i;
				for(i=0; i<3; i++) {
					seq_user= *((&seq->seq1) + i);
					if (seq_user && (seq_user->flag & SELECT) && !(seq_user->flag & SEQ_LOCK) && !(seq_user->flag & (SEQ_LEFTSEL|SEQ_RIGHTSEL))) {
						seq->flag |= SELECT;
					}
				}
			}
		}
	}
#endif

	count = SeqTransCount(t, ed->seqbasep, 0);

	/* allocate memory for data */
	t->total= count;

	/* stop if trying to build list if nothing selected */
	if (count == 0) {
		return;
	}

	td = t->data = MEM_callocN(t->total*sizeof(TransData), "TransSeq TransData");
	td2d = t->data2d = MEM_callocN(t->total*sizeof(TransData2D), "TransSeq TransData2D");
	tdsq = t->customData= MEM_callocN(t->total*sizeof(TransDataSeq), "TransSeq TransDataSeq");



	/* loop 2: build transdata array */
	SeqToTransData_Recursive(t, ed->seqbasep, td, td2d, tdsq);

#undef XXX_DURIAN_ANIM_TX_HACK
}


/* *********************** Object Transform data ******************* */

/* Little helper function for ObjectToTransData used to give certain
 * constraints (ChildOf, FollowPath, and others that may be added)
 * inverse corrections for transform, so that they aren't in CrazySpace.
 * These particular constraints benefit from this, but others don't, hence
 * this semi-hack ;-)    - Aligorith
 */
static short constraints_list_needinv(TransInfo *t, ListBase *list)
{
	bConstraint *con;

	/* loop through constraints, checking if there's one of the mentioned
	 * constraints needing special crazyspace corrections
	 */
	if (list) {
		for (con= list->first; con; con=con->next) {
			/* only consider constraint if it is enabled, and has influence on result */
			if ((con->flag & CONSTRAINT_DISABLE)==0 && (con->enforce!=0.0f)) {
				/* (affirmative) returns for specific constraints here... */
					/* constraints that require this regardless  */
				if (con->type == CONSTRAINT_TYPE_CHILDOF) return 1;
				if (con->type == CONSTRAINT_TYPE_FOLLOWPATH) return 1;
				if (con->type == CONSTRAINT_TYPE_CLAMPTO) return 1;
				
					/* constraints that require this only under special conditions */
				if (con->type == CONSTRAINT_TYPE_ROTLIKE) {
					/* CopyRot constraint only does this when rotating, and offset is on */
					bRotateLikeConstraint *data = (bRotateLikeConstraint *)con->data;
					
					if ((data->flag & ROTLIKE_OFFSET) && (t->mode == TFM_ROTATION))
						return 1;
				}
			}
		}
	}

	/* no appropriate candidates found */
	return 0;
}

/* transcribe given object into TransData for Transforming */
static void ObjectToTransData(TransInfo *t, TransData *td, Object *ob)
{
	Scene *scene = t->scene;
	float obmtx[3][3];
	short constinv;
	short skip_invert = 0;

	/* axismtx has the real orientation */
	copy_m3_m4(td->axismtx, ob->obmat);
	normalize_m3(td->axismtx);

	td->con= ob->constraints.first;

	/* hack: tempolarily disable tracking and/or constraints when getting
	 *		object matrix, if tracking is on, or if constraints don't need
	 * 		inverse correction to stop it from screwing up space conversion
	 *		matrix later
	 */
	constinv = constraints_list_needinv(t, &ob->constraints);

	/* disable constraints inversion for dummy pass */
	if (t->mode == TFM_DUMMY)
		skip_invert = 1;

	if (skip_invert == 0 && constinv == 0) {
		if (constinv == 0)
			ob->transflag |= OB_NO_CONSTRAINTS; /* where_is_object_time checks this */
		
		where_is_object(t->scene, ob);
		
		if (constinv == 0)
			ob->transflag &= ~OB_NO_CONSTRAINTS;
	}
	else
		where_is_object(t->scene, ob);

	td->ob = ob;

	td->loc = ob->loc;
	copy_v3_v3(td->iloc, td->loc);
	
	if (ob->rotmode > 0) {
		td->ext->rot= ob->rot;
		td->ext->rotAxis= NULL;
		td->ext->rotAngle= NULL;
		td->ext->quat= NULL;
		
		copy_v3_v3(td->ext->irot, ob->rot);
		copy_v3_v3(td->ext->drot, ob->drot);
	}
	else if (ob->rotmode == ROT_MODE_AXISANGLE) {
		td->ext->rot= NULL;
		td->ext->rotAxis= ob->rotAxis;
		td->ext->rotAngle= &ob->rotAngle;
		td->ext->quat= NULL;
		
		td->ext->irotAngle= ob->rotAngle;
		copy_v3_v3(td->ext->irotAxis, ob->rotAxis);
		// td->ext->drotAngle= ob->drotAngle;			// XXX, not implimented
		// copy_v3_v3(td->ext->drotAxis, ob->drotAxis);	// XXX, not implimented
	}
	else {
		td->ext->rot= NULL;
		td->ext->rotAxis= NULL;
		td->ext->rotAngle= NULL;
		td->ext->quat= ob->quat;
		
		QUATCOPY(td->ext->iquat, ob->quat);
		QUATCOPY(td->ext->dquat, ob->dquat);
	}
	td->ext->rotOrder=ob->rotmode;

	td->ext->size = ob->size;
	copy_v3_v3(td->ext->isize, ob->size);
	copy_v3_v3(td->ext->dsize, ob->dsize);

	copy_v3_v3(td->center, ob->obmat[3]);

	copy_m4_m4(td->ext->obmat, ob->obmat);

	/* is there a need to set the global<->data space conversion matrices? */
	if (ob->parent || constinv) {
		float totmat[3][3], obinv[3][3];

		/* Get the effect of parenting, and/or certain constraints.
		 * NOTE: some Constraints, and also Tracking should never get this
		 *		done, as it doesn't work well.
		 */
		object_to_mat3(ob, obmtx);
		copy_m3_m4(totmat, ob->obmat);
		invert_m3_m3(obinv, totmat);
		mul_m3_m3m3(td->smtx, obmtx, obinv);
		invert_m3_m3(td->mtx, td->smtx);
	}
	else {
		/* no conversion to/from dataspace */
		unit_m3(td->smtx);
		unit_m3(td->mtx);
	}

	/* set active flag */
	if (ob == OBACT)
	{
		td->flag |= TD_ACTIVE;
	}
}


/* sets flags in Bases to define whether they take part in transform */
/* it deselects Bases, so we have to call the clear function always after */
static void set_trans_object_base_flags(TransInfo *t)
{
	Scene *scene = t->scene;
	View3D *v3d = t->view;

	/*
	 if Base selected and has parent selected:
	 base->flag= BA_WAS_SEL
	 */
	Base *base;

	/* don't do it if we're not actually going to recalculate anything */
	if(t->mode == TFM_DUMMY)
		return;

	/* makes sure base flags and object flags are identical */
	copy_baseflags(t->scene);

	/* handle pending update events, otherwise they got copied below */
	for (base= scene->base.first; base; base= base->next) {
		if(base->object->recalc)
			object_handle_update(t->scene, base->object);
	}

	for (base= scene->base.first; base; base= base->next) {
		base->flag &= ~BA_WAS_SEL;

		if(TESTBASELIB_BGMODE(v3d, scene, base)) {
			Object *ob= base->object;
			Object *parsel= ob->parent;

			/* if parent selected, deselect */
			while(parsel) {
				if(parsel->flag & SELECT) {
					Base *parbase = object_in_scene(parsel, scene);
					if(parbase) { /* in rare cases this can fail */
						if TESTBASELIB_BGMODE(v3d, scene, parbase) {
							break;
						}
					}
				}
				parsel= parsel->parent;
			}

			if(parsel)
			{
				/* rotation around local centers are allowed to propagate */
				if ((t->mode == TFM_ROTATION || t->mode == TFM_TRACKBALL)  && t->around == V3D_LOCAL) {
					base->flag |= BA_TRANSFORM_CHILD;
				} else {
					base->flag &= ~SELECT;
					base->flag |= BA_WAS_SEL;
				}
			}
			/* used for flush, depgraph will change recalcs if needed :) */
			ob->recalc |= OB_RECALC_OB;
		}
	}

	/* all recalc flags get flushed to all layers, so a layer flip later on works fine */
	DAG_scene_flush_update(G.main, t->scene, -1, 0);

	/* and we store them temporal in base (only used for transform code) */
	/* this because after doing updates, the object->recalc is cleared */
	for (base= scene->base.first; base; base= base->next) {
		if(base->object->recalc & OB_RECALC_OB)
			base->flag |= BA_HAS_RECALC_OB;
		if(base->object->recalc & OB_RECALC_DATA)
			base->flag |= BA_HAS_RECALC_DATA;
	}
}

static int mark_children(Object *ob)
{
	if (ob->flag & (SELECT|BA_TRANSFORM_CHILD))
		return 1;

	if (ob->parent)
	{
		if (mark_children(ob->parent))
		{
			ob->flag |= BA_TRANSFORM_CHILD;
			return 1;
		}
	}
	
	return 0;
}

static int count_proportional_objects(TransInfo *t)
{
	int total = 0;
	Scene *scene = t->scene;
	View3D *v3d = t->view;
	Base *base;

	/* rotations around local centers are allowed to propagate, so we take all objects */
	if (!((t->mode == TFM_ROTATION || t->mode == TFM_TRACKBALL)  && t->around == V3D_LOCAL))
	{
		/* mark all parents */
		for (base= scene->base.first; base; base= base->next) {
			if(TESTBASELIB_BGMODE(v3d, scene, base)) {
				Object *parent = base->object->parent;
	
				/* flag all parents */
				while(parent) {
					parent->flag |= BA_TRANSFORM_PARENT;
					parent = parent->parent;
				}
			}
		}

		/* mark all children */
		for (base= scene->base.first; base; base= base->next) {
			/* all base not already selected or marked that is editable */
			if ((base->object->flag & (SELECT|BA_TRANSFORM_CHILD|BA_TRANSFORM_PARENT)) == 0 && BASE_EDITABLE_BGMODE(v3d, scene, base))
			{
				mark_children(base->object);
			}
		}
	}
	
	for (base= scene->base.first; base; base= base->next) {
		Object *ob= base->object;

		/* if base is not selected, not a parent of selection or not a child of selection and it is editable */
		if ((ob->flag & (SELECT|BA_TRANSFORM_CHILD|BA_TRANSFORM_PARENT)) == 0 && BASE_EDITABLE_BGMODE(v3d, scene, base))
		{

			/* used for flush, depgraph will change recalcs if needed :) */
			ob->recalc |= OB_RECALC_OB;

			total += 1;
		}
	}
	

	/* all recalc flags get flushed to all layers, so a layer flip later on works fine */
	DAG_scene_flush_update(G.main, t->scene, -1, 0);

	/* and we store them temporal in base (only used for transform code) */
	/* this because after doing updates, the object->recalc is cleared */
	for (base= scene->base.first; base; base= base->next) {
		if(base->object->recalc & OB_RECALC_OB)
			base->flag |= BA_HAS_RECALC_OB;
		if(base->object->recalc & OB_RECALC_DATA)
			base->flag |= BA_HAS_RECALC_DATA;
	}

	return total;
}

static void clear_trans_object_base_flags(TransInfo *t)
{
	Scene *sce = t->scene;
	Base *base;

	for (base= sce->base.first; base; base = base->next)
	{
		if(base->flag & BA_WAS_SEL)
			base->flag |= SELECT;

		base->flag &= ~(BA_WAS_SEL|BA_HAS_RECALC_OB|BA_HAS_RECALC_DATA|BA_TEMP_TAG|BA_TRANSFORM_CHILD|BA_TRANSFORM_PARENT);
	}
}

/* auto-keyframing feature - for objects
 * 	tmode: should be a transform mode
 */
// NOTE: context may not always be available, so must check before using it as it's a luxury for a few cases
void autokeyframe_ob_cb_func(bContext *C, Scene *scene, View3D *v3d, Object *ob, int tmode)
{
	ID *id= &ob->id;
	FCurve *fcu;
	
	// TODO: this should probably be done per channel instead...
	if (autokeyframe_cfra_can_key(scene, id)) {
		ReportList *reports = CTX_wm_reports(C);
		KeyingSet *active_ks = ANIM_scene_get_active_keyingset(scene);
		ListBase dsources = {NULL, NULL};
		float cfra= (float)CFRA; // xxx this will do for now
		short flag = 0;
		
		/* get flags used for inserting keyframes */
		flag = ANIM_get_keyframing_flags(scene, 1);
		
		/* add datasource override for the camera object */
		ANIM_relative_keyingset_add_source(&dsources, id, NULL, NULL); 
		
		if (IS_AUTOKEY_FLAG(scene, ONLYKEYINGSET) && (active_ks)) {
			/* only insert into active keyingset 
			 * NOTE: we assume here that the active Keying Set does not need to have its iterator overridden spe
			 */
			ANIM_apply_keyingset(C, &dsources, NULL, active_ks, MODIFYKEY_MODE_INSERT, cfra);
		}
		else if (IS_AUTOKEY_FLAG(scene, INSERTAVAIL)) {
			AnimData *adt= ob->adt;
			
			/* only key on available channels */
			if (adt && adt->action) {
				for (fcu= adt->action->curves.first; fcu; fcu= fcu->next) {
					fcu->flag &= ~FCURVE_SELECTED;
					insert_keyframe(reports, id, adt->action, ((fcu->grp)?(fcu->grp->name):(NULL)), fcu->rna_path, fcu->array_index, cfra, flag);
				}
			}
		}
		else if (IS_AUTOKEY_FLAG(scene, INSERTNEEDED)) {
			short doLoc=0, doRot=0, doScale=0;
			
			/* filter the conditions when this happens (assume that curarea->spacetype==SPACE_VIE3D) */
			if (tmode == TFM_TRANSLATION) {
				doLoc = 1;
			}
			else if (tmode == TFM_ROTATION) {
				if (v3d->around == V3D_ACTIVE) {
					if (ob != OBACT)
						doLoc = 1;
				}
				else if (v3d->around == V3D_CURSOR)
					doLoc = 1;
				
				if ((v3d->flag & V3D_ALIGN)==0)
					doRot = 1;
			}
			else if (tmode == TFM_RESIZE) {
				if (v3d->around == V3D_ACTIVE) {
					if (ob != OBACT)
						doLoc = 1;
				}
				else if (v3d->around == V3D_CURSOR)
					doLoc = 1;
				
				if ((v3d->flag & V3D_ALIGN)==0)
					doScale = 1;
			}
			
			/* insert keyframes for the affected sets of channels using the builtin KeyingSets found */
			if (doLoc) {
				KeyingSet *ks= ANIM_builtin_keyingset_get_named(NULL, "Location");
				ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, cfra);
			}
			if (doRot) {
				KeyingSet *ks= ANIM_builtin_keyingset_get_named(NULL, "Rotation");
				ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, cfra);
			}
			if (doScale) {
				KeyingSet *ks= ANIM_builtin_keyingset_get_named(NULL, "Scaling");
				ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, cfra);
			}
		}
		/* insert keyframe in all (transform) channels */
		else {
			KeyingSet *ks= ANIM_builtin_keyingset_get_named(NULL, "LocRotScale");
			ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, cfra);
		}
		
		/* free temp info */
		BLI_freelistN(&dsources);
	}
}

/* auto-keyframing feature - for poses/pose-channels
 * 	tmode: should be a transform mode
 *	targetless_ik: has targetless ik been done on any channels?
 */
// NOTE: context may not always be available, so must check before using it as it's a luxury for a few cases
void autokeyframe_pose_cb_func(bContext *C, Scene *scene, View3D *v3d, Object *ob, int tmode, short targetless_ik)
{
	ID *id= &ob->id;
	AnimData *adt= ob->adt;
	bAction	*act= (adt) ? adt->action : NULL;
	bPose	*pose= ob->pose;
	bPoseChannel *pchan;
	FCurve *fcu;
	
	// TODO: this should probably be done per channel instead...
	if (autokeyframe_cfra_can_key(scene, id)) {
		ReportList *reports = CTX_wm_reports(C);
		KeyingSet *active_ks = ANIM_scene_get_active_keyingset(scene);
		float cfra= (float)CFRA;
		short flag= 0;
		
		/* flag is initialised from UserPref keyframing settings
		 *	- special exception for targetless IK - INSERTKEY_MATRIX keyframes should get
		 * 	  visual keyframes even if flag not set, as it's not that useful otherwise
		 *	  (for quick animation recording)
		 */
		flag = ANIM_get_keyframing_flags(scene, 1);
		
		if (targetless_ik) 
			flag |= INSERTKEY_MATRIX;
		
		for (pchan=pose->chanbase.first; pchan; pchan=pchan->next) {
			if (pchan->bone->flag & BONE_TRANSFORM) {
				ListBase dsources = {NULL, NULL};
				
				/* clear any 'unkeyed' flag it may have */
				pchan->bone->flag &= ~BONE_UNKEYED;
				
				/* add datasource override for the camera object */
				ANIM_relative_keyingset_add_source(&dsources, id, &RNA_PoseBone, pchan); 
				
				/* only insert into active keyingset? */
				if (IS_AUTOKEY_FLAG(scene, ONLYKEYINGSET) && (active_ks)) {
					/* run the active Keying Set on the current datasource */
					ANIM_apply_keyingset(C, &dsources, NULL, active_ks, MODIFYKEY_MODE_INSERT, cfra);
				}
				/* only insert into available channels? */
				else if (IS_AUTOKEY_FLAG(scene, INSERTAVAIL)) {
					if (act) {
						for (fcu= act->curves.first; fcu; fcu= fcu->next) {
							/* only insert keyframes for this F-Curve if it affects the current bone */
							if (strstr(fcu->rna_path, "bones")) {
								char *pchanName= BLI_getQuotedStr(fcu->rna_path, "bones[");
								
								/* only if bone name matches too... 
								 * NOTE: this will do constraints too, but those are ok to do here too?
								 */
								if (pchanName && strcmp(pchanName, pchan->name) == 0) 
									insert_keyframe(reports, id, act, ((fcu->grp)?(fcu->grp->name):(NULL)), fcu->rna_path, fcu->array_index, cfra, flag);
									
								if (pchanName) MEM_freeN(pchanName);
							}
						}
					}
				}
				/* only insert keyframe if needed? */
				else if (IS_AUTOKEY_FLAG(scene, INSERTNEEDED)) {
					short doLoc=0, doRot=0, doScale=0;
					
					/* filter the conditions when this happens (assume that curarea->spacetype==SPACE_VIE3D) */
					if (tmode == TFM_TRANSLATION) {
						if (targetless_ik)
							doRot= 1;
						else
							doLoc = 1;
					}
					else if (tmode == TFM_ROTATION) {
						if (ELEM(v3d->around, V3D_CURSOR, V3D_ACTIVE))
							doLoc = 1;
							
						if ((v3d->flag & V3D_ALIGN)==0)
							doRot = 1;
					}
					else if (tmode == TFM_RESIZE) {
						if (ELEM(v3d->around, V3D_CURSOR, V3D_ACTIVE))
							doLoc = 1;
							
						if ((v3d->flag & V3D_ALIGN)==0)
							doScale = 1;
					}
					
					if (doLoc) {
						KeyingSet *ks= ANIM_builtin_keyingset_get_named(NULL, "Location");
						ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, cfra);
					}
					if (doRot) {
						KeyingSet *ks= ANIM_builtin_keyingset_get_named(NULL, "Rotation");
						ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, cfra);
					}
					if (doScale) {
						KeyingSet *ks= ANIM_builtin_keyingset_get_named(NULL, "Scaling");
						ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, cfra);
					}
				}
				/* insert keyframe in all (transform) channels */
				else {
					KeyingSet *ks= ANIM_builtin_keyingset_get_named(NULL, "LocRotScale");
					ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, cfra);
				}
				
				/* free temp info */
				BLI_freelistN(&dsources);
			}
		}
		
		/* do the bone paths 
		 * 	- only do this when there is context info, since we need that to resolve
		 *	  how to do the updates and so on...
		 *	- do not calculate unless there are paths already to update...
		 */
		if (C && (ob->pose->avs.path_bakeflag & MOTIONPATH_BAKE_HAS_PATHS)) {
			//ED_pose_clear_paths(C, ob); // XXX for now, don't need to clear
			ED_pose_recalculate_paths(scene, ob);
		}
	}
	else {
		/* tag channels that should have unkeyed data */
		for (pchan=pose->chanbase.first; pchan; pchan=pchan->next) {
			if (pchan->bone->flag & BONE_TRANSFORM) {
				/* tag this channel */
				pchan->bone->flag |= BONE_UNKEYED;
			}
		}
	}
}


/* inserting keys, pointcache, redraw events... */
/* 
 * note: sequencer freeing has its own function now because of a conflict with transform's order of freeing (campbell)
 * 		 Order changed, the sequencer stuff should go back in here
 * */
void special_aftertrans_update(bContext *C, TransInfo *t)
{
	Object *ob;
//	short redrawipo=0, resetslowpar=1;
	int cancelled= (t->state == TRANS_CANCEL);
	short duplicate= (t->mode == TFM_TIME_DUPLICATE);
	
	/* early out when nothing happened */
	if (t->total == 0 || t->mode == TFM_DUMMY)
		return;
	
	if (t->spacetype==SPACE_VIEW3D) {
		if (t->obedit) {
			if (cancelled==0) {
				EM_automerge(t->scene, t->obedit, 1);
			}
		}
	}
	
	if (t->spacetype == SPACE_SEQ) {
		/* freeSeqData in transform_conversions.c does this
		 * keep here so the else at the end wont run... */

		SpaceSeq *sseq= (SpaceSeq *)t->sa->spacedata.first;

		/* marker transform, not especially nice but we may want to move markers
		 * at the same time as keyframes in the dope sheet. */
		if ((sseq->flag & SEQ_MARKER_TRANS) && (cancelled == 0)) {
			/* cant use , TFM_TIME_EXTEND
			 * for some reason EXTEND is changed into TRANSLATE, so use frame_side instead */

			if(t->mode == TFM_SEQ_SLIDE) {
				if(t->frame_side == 'B')
					ED_markers_post_apply_transform(&t->scene->markers, t->scene, TFM_TIME_TRANSLATE, t->values[0], t->frame_side);
			}
			else if (ELEM(t->frame_side, 'L', 'R')) {
				ED_markers_post_apply_transform(&t->scene->markers, t->scene, TFM_TIME_EXTEND, t->values[0], t->frame_side);
			}
		}

	}
	else if (t->spacetype == SPACE_NODE) {
		SpaceNode *snode= (SpaceNode *)t->sa->spacedata.first;
		ED_node_update_hierarchy(C, snode->edittree);
		
		if(cancelled == 0)
			ED_node_link_insert(t->sa);
		
		/* clear link line */
		ED_node_link_intersect_test(t->sa, 0);
	}
	else if (t->spacetype == SPACE_ACTION) {
		SpaceAction *saction= (SpaceAction *)t->sa->spacedata.first;
		bAnimContext ac;
		
		/* initialise relevant anim-context 'context' data */
		if (ANIM_animdata_get_context(C, &ac) == 0)
			return;
			
		ob = ac.obact;
		
		if (ELEM(ac.datatype, ANIMCONT_DOPESHEET, ANIMCONT_SHAPEKEY)) {
			ListBase anim_data = {NULL, NULL};
			bAnimListElem *ale;
			short filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_FOREDIT /*| ANIMFILTER_CURVESONLY*/);
			
			/* get channels to work on */
			ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
			
			/* these should all be F-Curves */
			for (ale= anim_data.first; ale; ale= ale->next) {
				AnimData *adt= ANIM_nla_mapping_get(&ac, ale);
				FCurve *fcu= (FCurve *)ale->key_data;
				
				/* 3 cases here for curve cleanups:
				 * 1) NOTRANSKEYCULL on     -> cleanup of duplicates shouldn't be done
				 * 2) cancelled == 0        -> user confirmed the transform, so duplicates should be removed
				 * 3) cancelled + duplicate -> user cancelled the transform, but we made duplicates, so get rid of these
				 */
				if ( (saction->flag & SACTION_NOTRANSKEYCULL)==0 &&
					 ((cancelled == 0) || (duplicate)) )
				{
					if (adt) {
						ANIM_nla_mapping_apply_fcurve(adt, fcu, 0, 1);
						posttrans_fcurve_clean(fcu, FALSE); /* only use handles in graph editor */
						ANIM_nla_mapping_apply_fcurve(adt, fcu, 1, 1);
					}
					else
						posttrans_fcurve_clean(fcu, FALSE); /* only use handles in graph editor */
				}
			}
			
			/* free temp memory */
			BLI_freelistN(&anim_data);
		}
		else if (ac.datatype == ANIMCONT_ACTION) { // TODO: just integrate into the above...
			/* Depending on the lock status, draw necessary views */
			// fixme... some of this stuff is not good
			if (ob) {
				if (ob->pose || ob_get_key(ob))
					DAG_id_tag_update(&ob->id, OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME);
				else
					DAG_id_tag_update(&ob->id, OB_RECALC_OB);
			}
			
			/* 3 cases here for curve cleanups:
			 * 1) NOTRANSKEYCULL on     -> cleanup of duplicates shouldn't be done
			 * 2) cancelled == 0        -> user confirmed the transform, so duplicates should be removed
			 * 3) cancelled + duplicate -> user cancelled the transform, but we made duplicates, so get rid of these
			 */
			if ( (saction->flag & SACTION_NOTRANSKEYCULL)==0 &&
				 ((cancelled == 0) || (duplicate)) )
			{
				posttrans_action_clean(&ac, (bAction *)ac.data);
			}
		}
		else if (ac.datatype == ANIMCONT_GPENCIL) {
			/* remove duplicate frames and also make sure points are in order! */
				/* 3 cases here for curve cleanups:
				 * 1) NOTRANSKEYCULL on     -> cleanup of duplicates shouldn't be done
				 * 2) cancelled == 0        -> user confirmed the transform, so duplicates should be removed
				 * 3) cancelled + duplicate -> user cancelled the transform, but we made duplicates, so get rid of these
				 */
			if ( (saction->flag & SACTION_NOTRANSKEYCULL)==0 &&
				 ((cancelled == 0) || (duplicate)) )
			{
				bGPdata *gpd;
				
				// XXX: BAD! this get gpencil datablocks directly from main db...
				// but that's how this currently works :/
				for (gpd = G.main->gpencil.first; gpd; gpd = gpd->id.next) {
					if (ID_REAL_USERS(gpd))
						posttrans_gpd_clean(gpd);
				}
			}
		}
		
		/* marker transform, not especially nice but we may want to move markers
		 * at the same time as keyframes in the dope sheet. 
		 */
		if ((saction->flag & SACTION_MARKERS_MOVE) && (cancelled == 0)) {
			if (t->mode == TFM_TIME_TRANSLATE) {
#if 0
				if (ELEM(t->frame_side, 'L', 'R')) { /* TFM_TIME_EXTEND */
					/* same as below */
					ED_markers_post_apply_transform(ED_context_get_markers(C), t->scene, t->mode, t->values[0], t->frame_side);
				}
				else /* TFM_TIME_TRANSLATE */
#endif
				{
					ED_markers_post_apply_transform(ED_context_get_markers(C), t->scene, t->mode, t->values[0], t->frame_side);
				}
			}
			else if (t->mode == TFM_TIME_SCALE) {
				ED_markers_post_apply_transform(ED_context_get_markers(C), t->scene, t->mode, t->values[0], t->frame_side);
			}
		}
		
		/* make sure all F-Curves are set correctly */
		if (ac.datatype != ANIMCONT_GPENCIL)
			ANIM_editkeyframes_refresh(&ac);
		
		/* clear flag that was set for time-slide drawing */
		saction->flag &= ~SACTION_MOVING;
	}
	else if (t->spacetype == SPACE_IPO) {
		SpaceIpo *sipo= (SpaceIpo *)t->sa->spacedata.first;
		bAnimContext ac;
		const short use_handle = !(sipo->flag & SIPO_NOHANDLES);
		
		/* initialise relevant anim-context 'context' data */
		if (ANIM_animdata_get_context(C, &ac) == 0)
			return;
		
		if (ac.datatype)
		{
			ListBase anim_data = {NULL, NULL};
			bAnimListElem *ale;
			short filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_CURVE_VISIBLE);
			
			/* get channels to work on */
			ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
			
			for (ale= anim_data.first; ale; ale= ale->next) {
				AnimData *adt= ANIM_nla_mapping_get(&ac, ale);
				FCurve *fcu= (FCurve *)ale->key_data;
				
				/* 3 cases here for curve cleanups:
				 * 1) NOTRANSKEYCULL on     -> cleanup of duplicates shouldn't be done
				 * 2) cancelled == 0        -> user confirmed the transform, so duplicates should be removed
				 * 3) cancelled + duplicate -> user cancelled the transform, but we made duplicates, so get rid of these
				 */
				if ( (sipo->flag & SIPO_NOTRANSKEYCULL)==0 &&
					 ((cancelled == 0) || (duplicate)) )
				{
					if (adt) {
						ANIM_nla_mapping_apply_fcurve(adt, fcu, 0, 0);
						posttrans_fcurve_clean(fcu, use_handle);
						ANIM_nla_mapping_apply_fcurve(adt, fcu, 1, 0);
					}
					else
						posttrans_fcurve_clean(fcu, use_handle);
				}
			}
			
			/* free temp memory */
			BLI_freelistN(&anim_data);
		}
		
		/* Make sure all F-Curves are set correctly, but not if transform was
		 * canceled, since then curves were already restored to initial state.
		 * Note: if the refresh is really needed after cancel then some way
		 *       has to be added to not update handle types (see bug 22289).
		 */
		if(!cancelled)
			ANIM_editkeyframes_refresh(&ac);
	}
	else if (t->spacetype == SPACE_NLA) {
		bAnimContext ac;
		
		/* initialise relevant anim-context 'context' data */
		if (ANIM_animdata_get_context(C, &ac) == 0)
			return;
			
		if (ac.datatype)
		{
			ListBase anim_data = {NULL, NULL};
			bAnimListElem *ale;
			short filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_FOREDIT);
			
			/* get channels to work on */
			ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
			
			for (ale= anim_data.first; ale; ale= ale->next) {
				NlaTrack *nlt= (NlaTrack *)ale->data;
				
				/* make sure strips are in order again */
				BKE_nlatrack_sort_strips(nlt);
				
				/* remove the temp metas */
				BKE_nlastrips_clear_metas(&nlt->strips, 0, 1);
			}
			
			/* free temp memory */
			BLI_freelistN(&anim_data);
			
			/* perform after-transfrom validation */
			ED_nla_postop_refresh(&ac);
		}
	}
	else if (t->obedit) {
		if (t->obedit->type == OB_MESH)
		{
			EditMesh *em = ((Mesh *)t->obedit->data)->edit_mesh;
			/* table needs to be created for each edit command, since vertices can move etc */
			mesh_octree_table(t->obedit, em, NULL, 'e');
		}
	}
	else if ((t->flag & T_POSE) && (t->poseobj)) {
		bArmature *arm;
		bPoseChannel *pchan;
		short targetless_ik= 0;

		ob= t->poseobj;
		arm= ob->data;

		if((t->flag & T_AUTOIK) && (t->options & CTX_AUTOCONFIRM)) {
			/* when running transform non-interactively (operator exec),
			 * we need to update the pose otherwise no updates get called during
			 * transform and the auto-ik is not applied. see [#26164] */
			struct Object *pose_ob=t->poseobj;
			where_is_pose(t->scene, pose_ob);
		}

		/* set BONE_TRANSFORM flags for autokey, manipulator draw might have changed them */
		if (!cancelled && (t->mode != TFM_DUMMY))
			count_set_pose_transflags(&t->mode, t->around, ob);

		/* if target-less IK grabbing, we calculate the pchan transforms and clear flag */
		if (!cancelled && t->mode==TFM_TRANSLATION)
			targetless_ik= apply_targetless_ik(ob);
		else {
			/* not forget to clear the auto flag */
			for (pchan=ob->pose->chanbase.first; pchan; pchan=pchan->next) {
				bKinematicConstraint *data= has_targetless_ik(pchan);
				if(data) data->flag &= ~CONSTRAINT_IK_AUTO;
			}
		}

		if (t->mode==TFM_TRANSLATION)
			pose_grab_with_ik_clear(ob);

		/* automatic inserting of keys and unkeyed tagging - only if transform wasn't cancelled (or TFM_DUMMY) */
		if (!cancelled && (t->mode != TFM_DUMMY)) {
			autokeyframe_pose_cb_func(C, t->scene, (View3D *)t->view, ob, t->mode, targetless_ik);
			DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
		}
		else if (arm->flag & ARM_DELAYDEFORM) {
			/* old optimize trick... this enforces to bypass the depgraph */
			DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
			ob->recalc= 0;	// is set on OK position already by recalcData()
		}
		else
			DAG_id_tag_update(&ob->id, OB_RECALC_DATA);

	}
	else if(t->scene->basact && (ob = t->scene->basact->object) && (ob->mode & OB_MODE_PARTICLE_EDIT) && PE_get_current(t->scene, ob)) {
		;
	}
	else { /* Objects */
		int i, recalcObPaths=0;

		for (i = 0; i < t->total; i++) {
			TransData *td = t->data + i;
			ListBase pidlist;
			PTCacheID *pid;
			ob = td->ob;

			if (td->flag & TD_NOACTION)
				break;
			
			if (td->flag & TD_SKIP)
				continue;

			/* flag object caches as outdated */
			BKE_ptcache_ids_from_object(&pidlist, ob, t->scene, MAX_DUPLI_RECUR);
			for(pid=pidlist.first; pid; pid=pid->next) {
				if(pid->type != PTCACHE_TYPE_PARTICLES) /* particles don't need reset on geometry change */
					pid->cache->flag |= PTCACHE_OUTDATED;
			}
			BLI_freelistN(&pidlist);

			/* pointcache refresh */
			if (BKE_ptcache_object_reset(t->scene, ob, PTCACHE_RESET_OUTDATED))
				ob->recalc |= OB_RECALC_DATA;

			/* Needed for proper updating of "quick cached" dynamics. */
			/* Creates troubles for moving animated objects without */
			/* autokey though, probably needed is an anim sys override? */
			/* Please remove if some other solution is found. -jahka */
			DAG_id_tag_update(&ob->id, OB_RECALC_OB);

			/* Set autokey if necessary */
			if (!cancelled) {
				autokeyframe_ob_cb_func(C, t->scene, (View3D *)t->view, ob, t->mode);
				
				/* only calculate paths if there are paths to be recalculated */
				if (ob->avs.path_bakeflag & MOTIONPATH_BAKE_HAS_PATHS)
					recalcObPaths= 1;
			}
		}
		
		/* recalculate motion paths for objects (if necessary) 
		 * NOTE: only do this when there is context info
		 */
		if (C && recalcObPaths) {
			//ED_objects_clear_paths(C); // XXX for now, don't need to clear
			ED_objects_recalculate_paths(C, t->scene);

			/* recalculating the frame positions means we loose our original transform if its not auto-keyed [#24451]
			 * this hack re-applies it, which is annoying, only alternatives are...
			 * - dont recalc paths.
			 * - have an object_handle_update() which gives is the new transform without touching the objects.
			 * - only recalc paths on auto-keying.
			 * - ED_objects_recalculate_paths could backup/restore transforms.
			 * - re-apply the transform which is simplest in this case. (2 lines below)
			 */
			t->redraw |= TREDRAW_HARD;
			transformApply(C, t);
		}
	}

	clear_trans_object_base_flags(t);


#if 0 // TRANSFORM_FIX_ME
	if(resetslowpar)
		reset_slowparents();
#endif
}

static void createTransObject(bContext *C, TransInfo *t)
{
	TransData *td = NULL;
	TransDataExtension *tx;
	int propmode = t->flag & T_PROP_EDIT;

	set_trans_object_base_flags(t);

	/* count */
	t->total= CTX_DATA_COUNT(C, selected_objects);
	
	if(!t->total) {
		/* clear here, main transform function escapes too */
		clear_trans_object_base_flags(t);
		return;
	}
	
	if (propmode)
	{
		t->total += count_proportional_objects(t);
	}

	td = t->data = MEM_callocN(t->total*sizeof(TransData), "TransOb");
	tx = t->ext = MEM_callocN(t->total*sizeof(TransDataExtension), "TransObExtension");

	CTX_DATA_BEGIN(C, Base*, base, selected_bases)
	{
		Object *ob= base->object;
		
		td->flag = TD_SELECTED;
		td->protectflag= ob->protectflag;
		td->ext = tx;
		td->ext->rotOrder= ob->rotmode;
		
		if (base->flag & BA_TRANSFORM_CHILD)
		{
			td->flag |= TD_NOCENTER;
			td->flag |= TD_NO_LOC;
		}
		
		/* select linked objects, but skip them later */
		if (ob->id.lib != NULL) {
			td->flag |= TD_SKIP;
		}
		
		ObjectToTransData(t, td, ob);
		td->val = NULL;
		td++;
		tx++;
	}
	CTX_DATA_END;
	
	if (propmode)
	{
		Scene *scene = t->scene;
		View3D *v3d = t->view;
		Base *base;

		for (base= scene->base.first; base; base= base->next) {
			Object *ob= base->object;

			/* if base is not selected, not a parent of selection or not a child of selection and it is editable */
			if ((ob->flag & (SELECT|BA_TRANSFORM_CHILD|BA_TRANSFORM_PARENT)) == 0 && BASE_EDITABLE_BGMODE(v3d, scene, base))
			{
				td->protectflag= ob->protectflag;
				td->ext = tx;
				td->ext->rotOrder= ob->rotmode;
				
				ObjectToTransData(t, td, ob);
				td->val = NULL;
				td++;
				tx++;
			}
		}
	}
}

/* transcribe given node into TransData2D for Transforming */
static void NodeToTransData(TransData *td, TransData2D *td2d, bNode *node)
// static void NodeToTransData(bContext *C, TransInfo *t, TransData2D *td, bNode *node)
{
	td2d->loc[0] = node->locx; /* hold original location */
	td2d->loc[1] = node->locy;
	td2d->loc[2] = 0.0f;
	td2d->loc2d = &node->locx; /* current location */

	td->flag = 0;
	/* exclude nodes whose parent is also transformed */
	if (node->parent && (node->parent->flag & NODE_TRANSFORM)) {
		td->flag |= TD_SKIP;
	}

	td->loc = td2d->loc;
	copy_v3_v3(td->center, td->loc);
	copy_v3_v3(td->iloc, td->loc);

	memset(td->axismtx, 0, sizeof(td->axismtx));
	td->axismtx[2][2] = 1.0f;

	td->ext= NULL; td->val= NULL;

	td->flag |= TD_SELECTED;
	td->dist= 0.0;

	unit_m3(td->mtx);
	unit_m3(td->smtx);
}

static void createTransNodeData(bContext *C, TransInfo *t)
{
	TransData *td;
	TransData2D *td2d;
	SpaceNode *snode= t->sa->spacedata.first;
	bNode *node;

	if(!snode->edittree) {
		t->total= 0;
		return;
	}

	/* set transform flags on nodes */
	for (node=snode->edittree->nodes.first; node; node=node->next) {
		if ((node->flag & NODE_SELECT) || (node->parent && (node->parent->flag & NODE_TRANSFORM)))
			node->flag |= NODE_TRANSFORM;
		else
			node->flag &= ~NODE_TRANSFORM;
	}

	t->total= CTX_DATA_COUNT(C, selected_nodes);

	td = t->data = MEM_callocN(t->total*sizeof(TransData), "TransNode TransData");
	td2d = t->data2d = MEM_callocN(t->total*sizeof(TransData2D), "TransNode TransData2D");

	CTX_DATA_BEGIN(C, bNode *, selnode, selected_nodes)
		NodeToTransData(td++, td2d++, selnode);
	CTX_DATA_END
}

void createTransData(bContext *C, TransInfo *t)
{
	Scene *scene = t->scene;
	Object *ob = OBACT;

	if (t->options & CTX_TEXTURE) {
		t->flag |= T_TEXTURE;
		createTransTexspace(t);
	}
	else if (t->options & CTX_EDGE) {
		t->ext = NULL;
		t->flag |= T_EDIT;
		createTransEdge(t);
		if(t->data && t->flag & T_PROP_EDIT) {
			sort_trans_data(t);	// makes selected become first in array
			set_prop_dist(t, 1);
			sort_trans_data_dist(t);
		}
	}
	else if (t->options == CTX_BMESH) {
		// TRANSFORM_FIX_ME
		//createTransBMeshVerts(t, G.editBMesh->bm, G.editBMesh->td);
	}
	else if (t->spacetype == SPACE_IMAGE) {
		t->flag |= T_POINTS|T_2D_EDIT;
		createTransUVs(C, t);
		if(t->data && (t->flag & T_PROP_EDIT)) {
			sort_trans_data(t);	// makes selected become first in array
			set_prop_dist(t, 1);
			sort_trans_data_dist(t);
		}
	}
	else if (t->spacetype == SPACE_ACTION) {
		t->flag |= T_POINTS|T_2D_EDIT;
		createTransActionData(C, t);
	}
	else if (t->spacetype == SPACE_NLA) {
		t->flag |= T_POINTS|T_2D_EDIT;
		createTransNlaData(C, t);
	}
	else if (t->spacetype == SPACE_SEQ) {
		t->flag |= T_POINTS|T_2D_EDIT;
		t->num.flag |= NUM_NO_FRACTION; /* sequencer has no use for floating point transformations */
		createTransSeqData(C, t);
	}
	else if (t->spacetype == SPACE_IPO) {
		t->flag |= T_POINTS|T_2D_EDIT;
		createTransGraphEditData(C, t);
#if 0
		if (t->data && (t->flag & T_PROP_EDIT)) {
			sort_trans_data(t);	// makes selected become first in array
			set_prop_dist(t, 1);
			sort_trans_data_dist(t);
		}
#endif
	}
	else if(t->spacetype == SPACE_NODE) {
		t->flag |= T_2D_EDIT|T_POINTS;
		createTransNodeData(C, t);
		if (t->data && (t->flag & T_PROP_EDIT)) {
			sort_trans_data(t);	// makes selected become first in array
			set_prop_dist(t, 1);
			sort_trans_data_dist(t);
		}
	}
	else if (t->obedit) {
		t->ext = NULL;
		if (t->obedit->type == OB_MESH) {
			createTransEditVerts(C, t);
		}
		else if ELEM(t->obedit->type, OB_CURVE, OB_SURF) {
			createTransCurveVerts(C, t);
		}
		else if (t->obedit->type==OB_LATTICE) {
			createTransLatticeVerts(t);
		}
		else if (t->obedit->type==OB_MBALL) {
			createTransMBallVerts(t);
		}
		else if (t->obedit->type==OB_ARMATURE) {
			t->flag &= ~T_PROP_EDIT;
			createTransArmatureVerts(t);
		}
		else {
			printf("edit type not implemented!\n");
		}

		t->flag |= T_EDIT|T_POINTS;

		if(t->data && t->flag & T_PROP_EDIT) {
			if (ELEM(t->obedit->type, OB_CURVE, OB_MESH)) {
				sort_trans_data(t);	// makes selected become first in array
				set_prop_dist(t, 0);
				sort_trans_data_dist(t);
			}
			else {
				sort_trans_data(t);	// makes selected become first in array
				set_prop_dist(t, 1);
				sort_trans_data_dist(t);
			}
		}

		/* exception... hackish, we want bonesize to use bone orientation matrix (ton) */
		if(t->mode==TFM_BONESIZE) {
			t->flag &= ~(T_EDIT|T_POINTS);
			t->flag |= T_POSE;
			t->poseobj = ob;	/* <- tsk tsk, this is going to give issues one day */
		}
	}
	else if (ob && (ob->mode & OB_MODE_POSE)) {
		// XXX this is currently limited to active armature only...
		// XXX active-layer checking isn't done as that should probably be checked through context instead
		createTransPose(t, ob);
	}
	else if (ob && (ob->mode & OB_MODE_WEIGHT_PAINT)) {
		/* important that ob_armature can be set even when its not selected [#23412]
		 * lines below just check is also visible */
		Object *ob_armature= modifiers_isDeformedByArmature(ob);
		if(ob_armature && ob_armature->mode & OB_MODE_POSE) {
			Base *base_arm= object_in_scene(ob_armature, t->scene);
			if(base_arm) {
				View3D *v3d = t->view;
				if(BASE_VISIBLE(v3d, base_arm)) {
					createTransPose(t, ob_armature);
				}
			}
			
		}
	}
	else if (ob && (ob->mode & OB_MODE_PARTICLE_EDIT) 
		&& PE_start_edit(PE_get_current(scene, ob))) {
		createTransParticleVerts(C, t);
		t->flag |= T_POINTS;

		if(t->data && t->flag & T_PROP_EDIT) {
			sort_trans_data(t);	// makes selected become first in array
			set_prop_dist(t, 1);
			sort_trans_data_dist(t);
		}
	}
	else if (ob && (ob->mode & (OB_MODE_SCULPT|OB_MODE_TEXTURE_PAINT))) {
		/* sculpt mode and project paint have own undo stack
		 * transform ops redo clears sculpt/project undo stack.
		 *
		 * Could use 'OB_MODE_ALL_PAINT' since there are key conflicts,
		 * transform + paint isnt well supported. */
	}
	else {
		createTransObject(C, t);
		t->flag |= T_OBJECT;

		if(t->data && t->flag & T_PROP_EDIT) {
			// selected objects are already first, no need to presort
			set_prop_dist(t, 1);
			sort_trans_data_dist(t);
		}

		if ((t->spacetype == SPACE_VIEW3D) && (t->ar->regiontype == RGN_TYPE_WINDOW))
		{
			View3D *v3d = t->view;
			RegionView3D *rv3d = CTX_wm_region_view3d(C);
			if(rv3d && (t->flag & T_OBJECT) && v3d->camera == OBACT && rv3d->persp==RV3D_CAMOB)
			{
				t->flag |= T_CAMERA;
			}
		}
	}

// TRANSFORM_FIX_ME
//	/* temporal...? */
//	t->scene->recalc |= SCE_PRV_CHANGED;	/* test for 3d preview */
}