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
|
/*
* 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.
*/
/** \file
* \ingroup collada
*/
#include <stddef.h>
/* COLLADABU_ASSERT, may be able to remove later */
#include "COLLADABUPlatform.h"
#include "DNA_armature_types.h"
#include "ED_keyframing.h"
#include "BLI_listbase.h"
#include "BLI_math.h"
#include "BLI_string.h"
#include "BLI_string_utils.h"
#include "BLT_translation.h"
#include "BKE_action.h"
#include "BKE_armature.h"
#include "BKE_fcurve.h"
#include "BKE_object.h"
#include "MEM_guardedalloc.h"
#include "collada_utils.h"
#include "AnimationImporter.h"
#include "ArmatureImporter.h"
#include "MaterialExporter.h"
#include <algorithm>
/* first try node name, if not available (since is optional), fall back to original id */
template<class T> static const char *bc_get_joint_name(T *node)
{
const std::string &id = node->getName();
return id.size() ? id.c_str() : node->getOriginalId().c_str();
}
FCurve *AnimationImporter::create_fcurve(int array_index, const char *rna_path)
{
FCurve *fcu = (FCurve *)MEM_callocN(sizeof(FCurve), "FCurve");
fcu->flag = (FCURVE_VISIBLE | FCURVE_AUTO_HANDLES | FCURVE_SELECTED);
fcu->rna_path = BLI_strdupn(rna_path, strlen(rna_path));
fcu->array_index = array_index;
return fcu;
}
void AnimationImporter::add_bezt(FCurve *fcu,
float frame,
float value,
eBezTriple_Interpolation ipo)
{
// float fps = (float)FPS;
BezTriple bez;
memset(&bez, 0, sizeof(BezTriple));
bez.vec[1][0] = frame;
bez.vec[1][1] = value;
bez.ipo = ipo; /* use default interpolation mode here... */
bez.f1 = bez.f2 = bez.f3 = SELECT;
bez.h1 = bez.h2 = HD_AUTO;
insert_bezt_fcurve(fcu, &bez, INSERTKEY_NOFLAGS);
calchandles_fcurve(fcu);
}
/* create one or several fcurves depending on the number of parameters being animated */
void AnimationImporter::animation_to_fcurves(COLLADAFW::AnimationCurve *curve)
{
COLLADAFW::FloatOrDoubleArray &input = curve->getInputValues();
COLLADAFW::FloatOrDoubleArray &output = curve->getOutputValues();
float fps = (float)FPS;
size_t dim = curve->getOutDimension();
unsigned int i;
std::vector<FCurve *> &fcurves = curve_map[curve->getUniqueId()];
switch (dim) {
case 1: /* X, Y, Z or angle */
case 3: /* XYZ */
case 4:
case 16: /* matrix */
{
for (i = 0; i < dim; i++) {
FCurve *fcu = (FCurve *)MEM_callocN(sizeof(FCurve), "FCurve");
fcu->flag = (FCURVE_VISIBLE | FCURVE_AUTO_HANDLES | FCURVE_SELECTED);
fcu->array_index = 0;
fcu->auto_smoothing = U.auto_smoothing_new;
for (unsigned int j = 0; j < curve->getKeyCount(); j++) {
BezTriple bez;
memset(&bez, 0, sizeof(BezTriple));
/* input, output */
bez.vec[1][0] = bc_get_float_value(input, j) * fps;
bez.vec[1][1] = bc_get_float_value(output, j * dim + i);
bez.h1 = bez.h2 = HD_AUTO;
if (curve->getInterpolationType() == COLLADAFW::AnimationCurve::INTERPOLATION_BEZIER ||
curve->getInterpolationType() == COLLADAFW::AnimationCurve::INTERPOLATION_STEP) {
COLLADAFW::FloatOrDoubleArray &intan = curve->getInTangentValues();
COLLADAFW::FloatOrDoubleArray &outtan = curve->getOutTangentValues();
/* intangent */
unsigned int index = 2 * (j * dim + i);
bez.vec[0][0] = bc_get_float_value(intan, index) * fps;
bez.vec[0][1] = bc_get_float_value(intan, index + 1);
/* outtangent */
bez.vec[2][0] = bc_get_float_value(outtan, index) * fps;
bez.vec[2][1] = bc_get_float_value(outtan, index + 1);
if (curve->getInterpolationType() == COLLADAFW::AnimationCurve::INTERPOLATION_BEZIER) {
bez.ipo = BEZT_IPO_BEZ;
bez.h1 = bez.h2 = HD_AUTO_ANIM;
}
else {
bez.ipo = BEZT_IPO_CONST;
}
}
else {
bez.ipo = BEZT_IPO_LIN;
}
#if 0
bez.ipo = U.ipo_new; /* use default interpolation mode here... */
#endif
bez.f1 = bez.f2 = bez.f3 = SELECT;
insert_bezt_fcurve(fcu, &bez, INSERTKEY_NOFLAGS);
}
calchandles_fcurve(fcu);
fcurves.push_back(fcu);
unused_curves.push_back(fcu);
}
} break;
default:
fprintf(stderr,
"Output dimension of %d is not yet supported (animation id = %s)\n",
(int)dim,
curve->getOriginalId().c_str());
}
}
void AnimationImporter::fcurve_deg_to_rad(FCurve *cu)
{
for (unsigned int i = 0; i < cu->totvert; i++) {
/* TODO convert handles too */
cu->bezt[i].vec[1][1] *= DEG2RADF(1.0f);
cu->bezt[i].vec[0][1] *= DEG2RADF(1.0f);
cu->bezt[i].vec[2][1] *= DEG2RADF(1.0f);
}
}
void AnimationImporter::fcurve_scale(FCurve *cu, int scale)
{
for (unsigned int i = 0; i < cu->totvert; i++) {
/* TODO convert handles too */
cu->bezt[i].vec[1][1] *= scale;
cu->bezt[i].vec[0][1] *= scale;
cu->bezt[i].vec[2][1] *= scale;
}
}
void AnimationImporter::fcurve_is_used(FCurve *fcu)
{
unused_curves.erase(std::remove(unused_curves.begin(), unused_curves.end(), fcu),
unused_curves.end());
}
void AnimationImporter::add_fcurves_to_object(Main *bmain,
Object *ob,
std::vector<FCurve *> &curves,
char *rna_path,
int array_index,
Animation *animated)
{
bAction *act;
if (!ob->adt || !ob->adt->action) {
act = verify_adt_action(bmain, (ID *)&ob->id, 1);
}
else {
act = ob->adt->action;
}
std::vector<FCurve *>::iterator it;
int i;
#if 0
char *p = strstr(rna_path, "rotation_euler");
bool is_rotation = p && *(p + strlen("rotation_euler")) == '\0';
/* convert degrees to radians for rotation */
if (is_rotation) {
fcurve_deg_to_rad(fcu);
}
#endif
for (it = curves.begin(), i = 0; it != curves.end(); it++, i++) {
FCurve *fcu = *it;
fcu->rna_path = BLI_strdupn(rna_path, strlen(rna_path));
if (array_index == -1) {
fcu->array_index = i;
}
else {
fcu->array_index = array_index;
}
if (ob->type == OB_ARMATURE) {
bActionGroup *grp = NULL;
const char *bone_name = bc_get_joint_name(animated->node);
if (bone_name) {
/* try to find group */
grp = BKE_action_group_find_name(act, bone_name);
/* no matching groups, so add one */
if (grp == NULL) {
/* Add a new group, and make it active */
grp = (bActionGroup *)MEM_callocN(sizeof(bActionGroup), "bActionGroup");
grp->flag = AGRP_SELECTED;
BLI_strncpy(grp->name, bone_name, sizeof(grp->name));
BLI_addtail(&act->groups, grp);
BLI_uniquename(&act->groups,
grp,
CTX_DATA_(BLT_I18NCONTEXT_ID_ACTION, "Group"),
'.',
offsetof(bActionGroup, name),
64);
}
/* add F-Curve to group */
action_groups_add_channel(act, grp, fcu);
fcurve_is_used(fcu);
}
#if 0
if (is_rotation) {
fcurves_actionGroup_map[grp].push_back(fcu);
}
#endif
}
else {
BLI_addtail(&act->curves, fcu);
fcurve_is_used(fcu);
}
}
}
AnimationImporter::~AnimationImporter()
{
/* free unused FCurves */
for (std::vector<FCurve *>::iterator it = unused_curves.begin(); it != unused_curves.end();
it++) {
free_fcurve(*it);
}
if (unused_curves.size()) {
fprintf(stderr, "removed %d unused curves\n", (int)unused_curves.size());
}
}
bool AnimationImporter::write_animation(const COLLADAFW::Animation *anim)
{
if (anim->getAnimationType() == COLLADAFW::Animation::ANIMATION_CURVE) {
COLLADAFW::AnimationCurve *curve = (COLLADAFW::AnimationCurve *)anim;
/* XXX Don't know if it's necessary
* Should we check outPhysicalDimension? */
if (curve->getInPhysicalDimension() != COLLADAFW::PHYSICAL_DIMENSION_TIME) {
fprintf(stderr, "Inputs physical dimension is not time.\n");
return true;
}
/* a curve can have mixed interpolation type,
* in this case curve->getInterpolationTypes returns a list of interpolation types per key */
COLLADAFW::AnimationCurve::InterpolationType interp = curve->getInterpolationType();
if (interp != COLLADAFW::AnimationCurve::INTERPOLATION_MIXED) {
switch (interp) {
case COLLADAFW::AnimationCurve::INTERPOLATION_LINEAR:
case COLLADAFW::AnimationCurve::INTERPOLATION_BEZIER:
case COLLADAFW::AnimationCurve::INTERPOLATION_STEP:
animation_to_fcurves(curve);
break;
default:
/* TODO there're also CARDINAL, HERMITE, BSPLINE and STEP types */
fprintf(stderr,
"CARDINAL, HERMITE and BSPLINE anim interpolation types not supported yet.\n");
break;
}
}
else {
/* not supported yet */
fprintf(stderr, "MIXED anim interpolation type is not supported yet.\n");
}
}
else {
fprintf(stderr, "FORMULA animation type is not supported yet.\n");
}
return true;
}
/* called on post-process stage after writeVisualScenes */
bool AnimationImporter::write_animation_list(const COLLADAFW::AnimationList *animlist)
{
const COLLADAFW::UniqueId &animlist_id = animlist->getUniqueId();
animlist_map[animlist_id] = animlist;
#if 0
/* should not happen */
if (uid_animated_map.find(animlist_id) == uid_animated_map.end()) {
return true;
}
/* for bones rna_path is like: pose.bones["bone-name"].rotation */
#endif
return true;
}
/* \todo refactor read_node_transform to not automatically apply anything,
* but rather return the transform matrix, so caller can do with it what is
* necessary. Same for \ref get_node_mat */
void AnimationImporter::read_node_transform(COLLADAFW::Node *node, Object *ob)
{
float mat[4][4];
TransformReader::get_node_mat(mat, node, &uid_animated_map, ob);
if (ob) {
copy_m4_m4(ob->obmat, mat);
BKE_object_apply_mat4(ob, ob->obmat, 0, 0);
}
}
#if 0
virtual void AnimationImporter::change_eul_to_quat(Object *ob, bAction *act)
{
bActionGroup *grp;
int i;
for (grp = (bActionGroup *)act->groups.first; grp; grp = grp->next) {
FCurve *eulcu[3] = {NULL, NULL, NULL};
if (fcurves_actionGroup_map.find(grp) == fcurves_actionGroup_map.end()) {
continue;
}
std::vector<FCurve *> &rot_fcurves = fcurves_actionGroup_map[grp];
if (rot_fcurves.size() > 3) {
continue;
}
for (i = 0; i < rot_fcurves.size(); i++) {
eulcu[rot_fcurves[i]->array_index] = rot_fcurves[i];
}
char joint_path[100];
char rna_path[100];
BLI_snprintf(joint_path, sizeof(joint_path), "pose.bones[\"%s\"]", grp->name);
BLI_snprintf(rna_path, sizeof(rna_path), "%s.rotation_quaternion", joint_path);
FCurve *quatcu[4] = {
create_fcurve(0, rna_path),
create_fcurve(1, rna_path),
create_fcurve(2, rna_path),
create_fcurve(3, rna_path),
};
bPoseChannel *chan = BKE_pose_channel_find_name(ob->pose, grp->name);
float m4[4][4], irest[3][3];
invert_m4_m4(m4, chan->bone->arm_mat);
copy_m3_m4(irest, m4);
for (i = 0; i < 3; i++) {
FCurve *cu = eulcu[i];
if (!cu) {
continue;
}
for (int j = 0; j < cu->totvert; j++) {
float frame = cu->bezt[j].vec[1][0];
float eul[3] = {
eulcu[0] ? evaluate_fcurve(eulcu[0], frame) : 0.0f,
eulcu[1] ? evaluate_fcurve(eulcu[1], frame) : 0.0f,
eulcu[2] ? evaluate_fcurve(eulcu[2], frame) : 0.0f,
};
/* make eul relative to bone rest pose */
float rot[3][3], rel[3][3], quat[4];
# if 0
eul_to_mat3(rot, eul);
mul_m3_m3m3(rel, irest, rot);
mat3_to_quat(quat, rel);
# endif
eul_to_quat(quat, eul);
for (int k = 0; k < 4; k++) {
create_bezt(quatcu[k], frame, quat[k], U.ipo_new);
}
}
}
/* now replace old Euler curves */
for (i = 0; i < 3; i++) {
if (!eulcu[i]) {
continue;
}
action_groups_remove_channel(act, eulcu[i]);
free_fcurve(eulcu[i]);
}
chan->rotmode = ROT_MODE_QUAT;
for (i = 0; i < 4; i++) {
action_groups_add_channel(act, grp, quatcu[i]);
}
}
bPoseChannel *pchan;
for (pchan = (bPoseChannel *)ob->pose->chanbase.first; pchan; pchan = pchan->next) {
pchan->rotmode = ROT_MODE_QUAT;
}
}
#endif
/* sets the rna_path and array index to curve */
void AnimationImporter::modify_fcurve(std::vector<FCurve *> *curves,
const char *rna_path,
int array_index,
int scale)
{
std::vector<FCurve *>::iterator it;
int i;
for (it = curves->begin(), i = 0; it != curves->end(); it++, i++) {
FCurve *fcu = *it;
fcu->rna_path = BLI_strdup(rna_path);
if (array_index == -1) {
fcu->array_index = i;
}
else {
fcu->array_index = array_index;
}
if (scale != 1) {
fcurve_scale(fcu, scale);
}
fcurve_is_used(fcu);
}
}
void AnimationImporter::unused_fcurve(std::vector<FCurve *> *curves)
{
/* when an error happens and we can't actually use curve remove it from unused_curves */
std::vector<FCurve *>::iterator it;
for (it = curves->begin(); it != curves->end(); it++) {
FCurve *fcu = *it;
fcurve_is_used(fcu);
}
}
void AnimationImporter::find_frames(std::vector<float> *frames, std::vector<FCurve *> *curves)
{
std::vector<FCurve *>::iterator iter;
for (iter = curves->begin(); iter != curves->end(); iter++) {
FCurve *fcu = *iter;
for (unsigned int k = 0; k < fcu->totvert; k++) {
/* get frame value from bezTriple */
float fra = fcu->bezt[k].vec[1][0];
/* if frame already not added add frame to frames */
if (std::find(frames->begin(), frames->end(), fra) == frames->end()) {
frames->push_back(fra);
}
}
}
}
static int get_animation_axis_index(const COLLADABU::Math::Vector3 &axis)
{
int index;
if (COLLADABU::Math::Vector3::UNIT_X == axis) {
index = 0;
}
else if (COLLADABU::Math::Vector3::UNIT_Y == axis) {
index = 1;
}
else if (COLLADABU::Math::Vector3::UNIT_Z == axis) {
index = 2;
}
else {
index = -1;
}
return index;
}
/* creates the rna_paths and array indices of fcurves from animations using transformation and
* bound animation class of each animation. */
void AnimationImporter::Assign_transform_animations(
COLLADAFW::Transformation *transform,
const COLLADAFW::AnimationList::AnimationBinding *binding,
std::vector<FCurve *> *curves,
bool is_joint,
char *joint_path)
{
COLLADAFW::Transformation::TransformationType tm_type = transform->getTransformationType();
bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE;
/* to check if the no of curves are valid */
bool xyz = ((tm_type == COLLADAFW::Transformation::TRANSLATE ||
tm_type == COLLADAFW::Transformation::SCALE) &&
binding->animationClass == COLLADAFW::AnimationList::POSITION_XYZ);
if (!((!xyz && curves->size() == 1) || (xyz && curves->size() == 3) || is_matrix)) {
fprintf(stderr, "expected %d curves, got %d\n", xyz ? 3 : 1, (int)curves->size());
return;
}
char rna_path[100];
switch (tm_type) {
case COLLADAFW::Transformation::TRANSLATE:
case COLLADAFW::Transformation::SCALE: {
bool loc = tm_type == COLLADAFW::Transformation::TRANSLATE;
if (is_joint) {
BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, loc ? "location" : "scale");
}
else {
BLI_strncpy(rna_path, loc ? "location" : "scale", sizeof(rna_path));
}
switch (binding->animationClass) {
case COLLADAFW::AnimationList::POSITION_X:
modify_fcurve(curves, rna_path, 0);
break;
case COLLADAFW::AnimationList::POSITION_Y:
modify_fcurve(curves, rna_path, 1);
break;
case COLLADAFW::AnimationList::POSITION_Z:
modify_fcurve(curves, rna_path, 2);
break;
case COLLADAFW::AnimationList::POSITION_XYZ:
modify_fcurve(curves, rna_path, -1);
break;
default:
unused_fcurve(curves);
fprintf(stderr,
"AnimationClass %d is not supported for %s.\n",
binding->animationClass,
loc ? "TRANSLATE" : "SCALE");
}
break;
}
case COLLADAFW::Transformation::ROTATE: {
if (is_joint) {
BLI_snprintf(rna_path, sizeof(rna_path), "%s.rotation_euler", joint_path);
}
else {
BLI_strncpy(rna_path, "rotation_euler", sizeof(rna_path));
}
std::vector<FCurve *>::iterator iter;
for (iter = curves->begin(); iter != curves->end(); iter++) {
FCurve *fcu = *iter;
/* if transform is rotation the fcurves values must be turned in to radian. */
if (is_rotation) {
fcurve_deg_to_rad(fcu);
}
}
COLLADAFW::Rotate *rot = (COLLADAFW::Rotate *)transform;
COLLADABU::Math::Vector3 &axis = rot->getRotationAxis();
switch (binding->animationClass) {
case COLLADAFW::AnimationList::ANGLE: {
int axis_index = get_animation_axis_index(axis);
if (axis_index >= 0) {
modify_fcurve(curves, rna_path, axis_index);
}
else {
unused_fcurve(curves);
}
} break;
case COLLADAFW::AnimationList::AXISANGLE:
/* TODO convert axis-angle to quat? or XYZ? */
default:
unused_fcurve(curves);
fprintf(stderr,
"AnimationClass %d is not supported for ROTATE transformation.\n",
binding->animationClass);
}
break;
}
case COLLADAFW::Transformation::MATRIX:
#if 0
{
COLLADAFW::Matrix *mat = (COLLADAFW::Matrix *)transform;
COLLADABU::Math::Matrix4 mat4 = mat->getMatrix();
switch (binding->animationClass) {
case COLLADAFW::AnimationList::TRANSFORM:
}
}
#endif
unused_fcurve(curves);
break;
case COLLADAFW::Transformation::SKEW:
case COLLADAFW::Transformation::LOOKAT:
unused_fcurve(curves);
fprintf(stderr, "Animation of SKEW and LOOKAT transformations is not supported yet.\n");
break;
}
}
/* creates the rna_paths and array indices of fcurves from animations using color and bound
* animation class of each animation. */
void AnimationImporter::Assign_color_animations(const COLLADAFW::UniqueId &listid,
ListBase *AnimCurves,
const char *anim_type)
{
char rna_path[100];
BLI_strncpy(rna_path, anim_type, sizeof(rna_path));
const COLLADAFW::AnimationList *animlist = animlist_map[listid];
if (animlist == NULL) {
fprintf(stderr,
"Collada: No animlist found for ID: %s of type %s\n",
listid.toAscii().c_str(),
anim_type);
return;
}
const COLLADAFW::AnimationList::AnimationBindings &bindings = animlist->getAnimationBindings();
/* all the curves belonging to the current binding */
std::vector<FCurve *> animcurves;
for (unsigned int j = 0; j < bindings.getCount(); j++) {
animcurves = curve_map[bindings[j].animation];
switch (bindings[j].animationClass) {
case COLLADAFW::AnimationList::COLOR_R:
modify_fcurve(&animcurves, rna_path, 0);
break;
case COLLADAFW::AnimationList::COLOR_G:
modify_fcurve(&animcurves, rna_path, 1);
break;
case COLLADAFW::AnimationList::COLOR_B:
modify_fcurve(&animcurves, rna_path, 2);
break;
case COLLADAFW::AnimationList::COLOR_RGB:
case COLLADAFW::AnimationList::COLOR_RGBA: /* to do-> set intensity */
modify_fcurve(&animcurves, rna_path, -1);
break;
default:
unused_fcurve(&animcurves);
fprintf(stderr,
"AnimationClass %d is not supported for %s.\n",
bindings[j].animationClass,
"COLOR");
}
std::vector<FCurve *>::iterator iter;
/* Add the curves of the current animation to the object */
for (iter = animcurves.begin(); iter != animcurves.end(); iter++) {
FCurve *fcu = *iter;
BLI_addtail(AnimCurves, fcu);
fcurve_is_used(fcu);
}
}
}
void AnimationImporter::Assign_float_animations(const COLLADAFW::UniqueId &listid,
ListBase *AnimCurves,
const char *anim_type)
{
char rna_path[100];
if (animlist_map.find(listid) == animlist_map.end()) {
return;
}
else {
/* anim_type has animations */
const COLLADAFW::AnimationList *animlist = animlist_map[listid];
const COLLADAFW::AnimationList::AnimationBindings &bindings = animlist->getAnimationBindings();
/* all the curves belonging to the current binding */
std::vector<FCurve *> animcurves;
for (unsigned int j = 0; j < bindings.getCount(); j++) {
animcurves = curve_map[bindings[j].animation];
BLI_strncpy(rna_path, anim_type, sizeof(rna_path));
modify_fcurve(&animcurves, rna_path, 0);
std::vector<FCurve *>::iterator iter;
/* Add the curves of the current animation to the object */
for (iter = animcurves.begin(); iter != animcurves.end(); iter++) {
FCurve *fcu = *iter;
/* All anim_types whose values are to be converted from Degree to Radians can be ORed here
*/
if (STREQ("spot_size", anim_type)) {
/* NOTE: Do NOT convert if imported file was made by blender <= 2.69.10
* Reason: old blender versions stored spot_size in radians (was a bug)
*/
if (this->import_from_version == "" ||
BLI_strcasecmp_natural(this->import_from_version.c_str(), "2.69.10") != -1) {
fcurve_deg_to_rad(fcu);
}
}
/** XXX What About animtype "rotation" ? */
BLI_addtail(AnimCurves, fcu);
fcurve_is_used(fcu);
}
}
}
}
float AnimationImporter::convert_to_focal_length(float in_xfov,
int fov_type,
float aspect,
float sensorx)
{
/* NOTE: Needs more testing (As we currently have no official test data for this) */
float xfov = (fov_type == CAMERA_YFOV) ?
(2.0f * atanf(aspect * tanf(DEG2RADF(in_xfov) * 0.5f))) :
DEG2RADF(in_xfov);
return fov_to_focallength(xfov, sensorx);
}
/*
* Lens animations must be stored in COLLADA by using FOV,
* while blender internally uses focal length.
* The imported animation curves must be converted appropriately.
*/
void AnimationImporter::Assign_lens_animations(const COLLADAFW::UniqueId &listid,
ListBase *AnimCurves,
const double aspect,
Camera *cam,
const char *anim_type,
int fov_type)
{
char rna_path[100];
if (animlist_map.find(listid) == animlist_map.end()) {
return;
}
else {
/* anim_type has animations */
const COLLADAFW::AnimationList *animlist = animlist_map[listid];
const COLLADAFW::AnimationList::AnimationBindings &bindings = animlist->getAnimationBindings();
/* all the curves belonging to the current binding */
std::vector<FCurve *> animcurves;
for (unsigned int j = 0; j < bindings.getCount(); j++) {
animcurves = curve_map[bindings[j].animation];
BLI_strncpy(rna_path, anim_type, sizeof(rna_path));
modify_fcurve(&animcurves, rna_path, 0);
std::vector<FCurve *>::iterator iter;
/* Add the curves of the current animation to the object */
for (iter = animcurves.begin(); iter != animcurves.end(); iter++) {
FCurve *fcu = *iter;
for (unsigned int i = 0; i < fcu->totvert; i++) {
fcu->bezt[i].vec[0][1] = convert_to_focal_length(
fcu->bezt[i].vec[0][1], fov_type, aspect, cam->sensor_x);
fcu->bezt[i].vec[1][1] = convert_to_focal_length(
fcu->bezt[i].vec[1][1], fov_type, aspect, cam->sensor_x);
fcu->bezt[i].vec[2][1] = convert_to_focal_length(
fcu->bezt[i].vec[2][1], fov_type, aspect, cam->sensor_x);
}
BLI_addtail(AnimCurves, fcu);
fcurve_is_used(fcu);
}
}
}
}
void AnimationImporter::apply_matrix_curves(Object *ob,
std::vector<FCurve *> &animcurves,
COLLADAFW::Node *root,
COLLADAFW::Node *node,
COLLADAFW::Transformation *tm)
{
bool is_joint = node->getType() == COLLADAFW::Node::JOINT;
const char *bone_name = is_joint ? bc_get_joint_name(node) : NULL;
char joint_path[200];
if (is_joint) {
armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path));
}
std::vector<float> frames;
find_frames(&frames, &animcurves);
float irest_dae[4][4];
float rest[4][4], irest[4][4];
if (is_joint) {
get_joint_rest_mat(irest_dae, root, node);
invert_m4(irest_dae);
Bone *bone = BKE_armature_find_bone_name((bArmature *)ob->data, bone_name);
if (!bone) {
fprintf(stderr, "cannot find bone \"%s\"\n", bone_name);
return;
}
unit_m4(rest);
copy_m4_m4(rest, bone->arm_mat);
invert_m4_m4(irest, rest);
}
/* new curves to assign matrix transform animation */
FCurve *newcu[10]; /* if tm_type is matrix, then create 10 curves: 4 rot, 3 loc, 3 scale */
unsigned int totcu = 10;
const char *tm_str = NULL;
char rna_path[200];
for (int i = 0; i < totcu; i++) {
int axis = i;
if (i < 4) {
tm_str = "rotation_quaternion";
axis = i;
}
else if (i < 7) {
tm_str = "location";
axis = i - 4;
}
else {
tm_str = "scale";
axis = i - 7;
}
if (is_joint) {
BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, tm_str);
}
else {
BLI_strncpy(rna_path, tm_str, sizeof(rna_path));
}
newcu[i] = create_fcurve(axis, rna_path);
newcu[i]->totvert = frames.size();
}
if (frames.size() == 0) {
return;
}
std::sort(frames.begin(), frames.end());
std::vector<float>::iterator it;
#if 0
float qref[4];
unit_qt(qref);
#endif
/* sample values at each frame */
for (it = frames.begin(); it != frames.end(); it++) {
float fra = *it;
float mat[4][4];
float matfra[4][4];
unit_m4(matfra);
/* calc object-space mat */
evaluate_transform_at_frame(matfra, node, fra);
/* for joints, we need a special matrix */
if (is_joint) {
/* special matrix: iR * M * iR_dae * R
* where R, iR are bone rest and inverse rest mats in world space (Blender bones),
* iR_dae is joint inverse rest matrix (DAE)
* and M is an evaluated joint world-space matrix (DAE) */
float temp[4][4], par[4][4];
/* calc M */
calc_joint_parent_mat_rest(par, NULL, root, node);
mul_m4_m4m4(temp, par, matfra);
#if 0
evaluate_joint_world_transform_at_frame(temp, NULL, node, fra);
#endif
/* calc special matrix */
mul_m4_series(mat, irest, temp, irest_dae, rest);
}
else {
copy_m4_m4(mat, matfra);
}
float rot[4], loc[3], scale[3];
mat4_decompose(loc, rot, scale, mat);
/* add keys */
for (int i = 0; i < totcu; i++) {
if (i < 4) {
add_bezt(newcu[i], fra, rot[i]);
}
else if (i < 7) {
add_bezt(newcu[i], fra, loc[i - 4]);
}
else {
add_bezt(newcu[i], fra, scale[i - 7]);
}
}
}
Main *bmain = CTX_data_main(mContext);
verify_adt_action(bmain, (ID *)&ob->id, 1);
ListBase *curves = &ob->adt->action->curves;
/* add curves */
for (int i = 0; i < totcu; i++) {
if (is_joint) {
add_bone_fcurve(ob, node, newcu[i]);
}
else {
BLI_addtail(curves, newcu[i]);
}
#if 0
fcurve_is_used(newcu[i]); /* never added to unused */
#endif
}
if (is_joint) {
bPoseChannel *chan = BKE_pose_channel_find_name(ob->pose, bone_name);
chan->rotmode = ROT_MODE_QUAT;
}
else {
ob->rotmode = ROT_MODE_QUAT;
}
return;
}
/*
* This function returns the aspect ration from the Collada camera.
*
* Note:COLLADA allows to specify either XFov, or YFov alone.
* In that case the aspect ratio can be determined from
* the viewport aspect ratio (which is 1:1 ?)
* XXX: check this: its probably wrong!
* If both values are specified, then the aspect ration is simply xfov/yfov
* and if aspect ratio is efined, then .. well then its that one.
*/
static const double get_aspect_ratio(const COLLADAFW::Camera *camera)
{
double aspect = camera->getAspectRatio().getValue();
if (aspect == 0) {
const double yfov = camera->getYFov().getValue();
if (yfov == 0) {
aspect = 1; /* assume yfov and xfov are equal */
}
else {
const double xfov = camera->getXFov().getValue();
if (xfov == 0) {
aspect = 1;
}
else {
aspect = xfov / yfov;
}
}
}
return aspect;
}
static ListBase &get_animation_curves(Main *bmain, Material *ma)
{
bAction *act;
if (!ma->adt || !ma->adt->action) {
act = verify_adt_action(bmain, (ID *)&ma->id, 1);
}
else {
act = ma->adt->action;
}
return act->curves;
}
void AnimationImporter::translate_Animations(
COLLADAFW::Node *node,
std::map<COLLADAFW::UniqueId, COLLADAFW::Node *> &root_map,
std::multimap<COLLADAFW::UniqueId, Object *> &object_map,
std::map<COLLADAFW::UniqueId, const COLLADAFW::Object *> FW_object_map,
std::map<COLLADAFW::UniqueId, Material *> uid_material_map)
{
bool is_joint = node->getType() == COLLADAFW::Node::JOINT;
COLLADAFW::UniqueId uid = node->getUniqueId();
COLLADAFW::Node *root = root_map.find(uid) == root_map.end() ? node : root_map[uid];
Object *ob;
if (is_joint) {
ob = armature_importer->get_armature_for_joint(root);
}
else {
ob = object_map.find(uid) == object_map.end() ? NULL : object_map.find(uid)->second;
}
if (!ob) {
fprintf(stderr, "cannot find Object for Node with id=\"%s\"\n", node->getOriginalId().c_str());
return;
}
AnimationImporter::AnimMix *animType = get_animation_type(node, FW_object_map);
bAction *act;
Main *bmain = CTX_data_main(mContext);
if ((animType->transform) != 0) {
/* const char *bone_name = is_joint ? bc_get_joint_name(node) : NULL; */ /* UNUSED */
char joint_path[200];
if (is_joint) {
armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path));
}
if (!ob->adt || !ob->adt->action) {
act = verify_adt_action(bmain, (ID *)&ob->id, 1);
}
else {
act = ob->adt->action;
}
/* Get the list of animation curves of the object */
ListBase *AnimCurves = &(act->curves);
const COLLADAFW::TransformationPointerArray &nodeTransforms = node->getTransformations();
/* for each transformation in node */
for (unsigned int i = 0; i < nodeTransforms.getCount(); i++) {
COLLADAFW::Transformation *transform = nodeTransforms[i];
COLLADAFW::Transformation::TransformationType tm_type = transform->getTransformationType();
bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE;
bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
const COLLADAFW::UniqueId &listid = transform->getAnimationList();
/* check if transformation has animations */
if (animlist_map.find(listid) == animlist_map.end()) {
continue;
}
else {
/* transformation has animations */
const COLLADAFW::AnimationList *animlist = animlist_map[listid];
const COLLADAFW::AnimationList::AnimationBindings &bindings =
animlist->getAnimationBindings();
/* all the curves belonging to the current binding */
std::vector<FCurve *> animcurves;
for (unsigned int j = 0; j < bindings.getCount(); j++) {
animcurves = curve_map[bindings[j].animation];
if (is_matrix) {
apply_matrix_curves(ob, animcurves, root, node, transform);
}
else {
/* calculate rnapaths and array index of fcurves according to transformation and
* animation class */
Assign_transform_animations(
transform, &bindings[j], &animcurves, is_joint, joint_path);
std::vector<FCurve *>::iterator iter;
/* Add the curves of the current animation to the object */
for (iter = animcurves.begin(); iter != animcurves.end(); iter++) {
FCurve *fcu = *iter;
BLI_addtail(AnimCurves, fcu);
fcurve_is_used(fcu);
}
}
}
}
if (is_rotation && !(is_joint || is_matrix)) {
ob->rotmode = ROT_MODE_EUL;
}
}
}
if ((animType->light) != 0) {
Light *lamp = (Light *)ob->data;
if (!lamp->adt || !lamp->adt->action) {
act = verify_adt_action(bmain, (ID *)&lamp->id, 1);
}
else {
act = lamp->adt->action;
}
ListBase *AnimCurves = &(act->curves);
const COLLADAFW::InstanceLightPointerArray &nodeLights = node->getInstanceLights();
for (unsigned int i = 0; i < nodeLights.getCount(); i++) {
const COLLADAFW::Light *light = (COLLADAFW::Light *)
FW_object_map[nodeLights[i]->getInstanciatedObjectId()];
if ((animType->light & LIGHT_COLOR) != 0) {
const COLLADAFW::Color *col = &(light->getColor());
const COLLADAFW::UniqueId &listid = col->getAnimationList();
Assign_color_animations(listid, AnimCurves, "color");
}
if ((animType->light & LIGHT_FOA) != 0) {
const COLLADAFW::AnimatableFloat *foa = &(light->getFallOffAngle());
const COLLADAFW::UniqueId &listid = foa->getAnimationList();
Assign_float_animations(listid, AnimCurves, "spot_size");
}
if ((animType->light & LIGHT_FOE) != 0) {
const COLLADAFW::AnimatableFloat *foe = &(light->getFallOffExponent());
const COLLADAFW::UniqueId &listid = foe->getAnimationList();
Assign_float_animations(listid, AnimCurves, "spot_blend");
}
}
}
if (animType->camera != 0) {
Camera *cam = (Camera *)ob->data;
if (!cam->adt || !cam->adt->action) {
act = verify_adt_action(bmain, (ID *)&cam->id, 1);
}
else {
act = cam->adt->action;
}
ListBase *AnimCurves = &(act->curves);
const COLLADAFW::InstanceCameraPointerArray &nodeCameras = node->getInstanceCameras();
for (unsigned int i = 0; i < nodeCameras.getCount(); i++) {
const COLLADAFW::Camera *camera = (COLLADAFW::Camera *)
FW_object_map[nodeCameras[i]->getInstanciatedObjectId()];
if ((animType->camera & CAMERA_XFOV) != 0) {
const COLLADAFW::AnimatableFloat *xfov = &(camera->getXFov());
const COLLADAFW::UniqueId &listid = xfov->getAnimationList();
double aspect = get_aspect_ratio(camera);
Assign_lens_animations(listid, AnimCurves, aspect, cam, "lens", CAMERA_XFOV);
}
else if ((animType->camera & CAMERA_YFOV) != 0) {
const COLLADAFW::AnimatableFloat *yfov = &(camera->getYFov());
const COLLADAFW::UniqueId &listid = yfov->getAnimationList();
double aspect = get_aspect_ratio(camera);
Assign_lens_animations(listid, AnimCurves, aspect, cam, "lens", CAMERA_YFOV);
}
else if ((animType->camera & CAMERA_XMAG) != 0) {
const COLLADAFW::AnimatableFloat *xmag = &(camera->getXMag());
const COLLADAFW::UniqueId &listid = xmag->getAnimationList();
Assign_float_animations(listid, AnimCurves, "ortho_scale");
}
else if ((animType->camera & CAMERA_YMAG) != 0) {
const COLLADAFW::AnimatableFloat *ymag = &(camera->getYMag());
const COLLADAFW::UniqueId &listid = ymag->getAnimationList();
Assign_float_animations(listid, AnimCurves, "ortho_scale");
}
if ((animType->camera & CAMERA_ZFAR) != 0) {
const COLLADAFW::AnimatableFloat *zfar = &(camera->getFarClippingPlane());
const COLLADAFW::UniqueId &listid = zfar->getAnimationList();
Assign_float_animations(listid, AnimCurves, "clip_end");
}
if ((animType->camera & CAMERA_ZNEAR) != 0) {
const COLLADAFW::AnimatableFloat *znear = &(camera->getNearClippingPlane());
const COLLADAFW::UniqueId &listid = znear->getAnimationList();
Assign_float_animations(listid, AnimCurves, "clip_start");
}
}
}
if (animType->material != 0) {
Material *ma = give_current_material(ob, 1);
if (!ma->adt || !ma->adt->action) {
act = verify_adt_action(bmain, (ID *)&ma->id, 1);
}
else {
act = ma->adt->action;
}
const COLLADAFW::InstanceGeometryPointerArray &nodeGeoms = node->getInstanceGeometries();
for (unsigned int i = 0; i < nodeGeoms.getCount(); i++) {
const COLLADAFW::MaterialBindingArray &matBinds = nodeGeoms[i]->getMaterialBindings();
for (unsigned int j = 0; j < matBinds.getCount(); j++) {
const COLLADAFW::UniqueId &matuid = matBinds[j].getReferencedMaterial();
const COLLADAFW::Effect *ef = (COLLADAFW::Effect *)(FW_object_map[matuid]);
if (ef != NULL) { /* can be NULL [#28909] */
Material *ma = uid_material_map[matuid];
if (!ma) {
fprintf(stderr,
"Collada: Node %s refers to undefined material\n",
node->getName().c_str());
continue;
}
ListBase &AnimCurves = get_animation_curves(bmain, ma);
const COLLADAFW::CommonEffectPointerArray &commonEffects = ef->getCommonEffects();
COLLADAFW::EffectCommon *efc = commonEffects[0];
if ((animType->material & MATERIAL_SHININESS) != 0) {
const COLLADAFW::FloatOrParam *shin = &(efc->getShininess());
const COLLADAFW::UniqueId &listid = shin->getAnimationList();
Assign_float_animations(listid, &AnimCurves, "specular_hardness");
}
if ((animType->material & MATERIAL_IOR) != 0) {
const COLLADAFW::FloatOrParam *ior = &(efc->getIndexOfRefraction());
const COLLADAFW::UniqueId &listid = ior->getAnimationList();
Assign_float_animations(listid, &AnimCurves, "raytrace_transparency.ior");
}
if ((animType->material & MATERIAL_SPEC_COLOR) != 0) {
const COLLADAFW::ColorOrTexture *cot = &(efc->getSpecular());
const COLLADAFW::UniqueId &listid = cot->getColor().getAnimationList();
Assign_color_animations(listid, &AnimCurves, "specular_color");
}
if ((animType->material & MATERIAL_DIFF_COLOR) != 0) {
const COLLADAFW::ColorOrTexture *cot = &(efc->getDiffuse());
const COLLADAFW::UniqueId &listid = cot->getColor().getAnimationList();
Assign_color_animations(listid, &AnimCurves, "diffuse_color");
}
}
}
}
}
delete animType;
}
void AnimationImporter::add_bone_animation_sampled(Object *ob,
std::vector<FCurve *> &animcurves,
COLLADAFW::Node *root,
COLLADAFW::Node *node,
COLLADAFW::Transformation *tm)
{
const char *bone_name = bc_get_joint_name(node);
char joint_path[200];
armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path));
std::vector<float> frames;
find_frames(&frames, &animcurves);
/* convert degrees to radians */
if (tm->getTransformationType() == COLLADAFW::Transformation::ROTATE) {
std::vector<FCurve *>::iterator iter;
for (iter = animcurves.begin(); iter != animcurves.end(); iter++) {
FCurve *fcu = *iter;
fcurve_deg_to_rad(fcu);
}
}
float irest_dae[4][4];
float rest[4][4], irest[4][4];
get_joint_rest_mat(irest_dae, root, node);
invert_m4(irest_dae);
Bone *bone = BKE_armature_find_bone_name((bArmature *)ob->data, bone_name);
if (!bone) {
fprintf(stderr, "cannot find bone \"%s\"\n", bone_name);
return;
}
unit_m4(rest);
copy_m4_m4(rest, bone->arm_mat);
invert_m4_m4(irest, rest);
/* new curves to assign matrix transform animation */
FCurve *newcu[10]; /* if tm_type is matrix, then create 10 curves: 4 rot, 3 loc, 3 scale. */
unsigned int totcu = 10;
const char *tm_str = NULL;
char rna_path[200];
for (int i = 0; i < totcu; i++) {
int axis = i;
if (i < 4) {
tm_str = "rotation_quaternion";
axis = i;
}
else if (i < 7) {
tm_str = "location";
axis = i - 4;
}
else {
tm_str = "scale";
axis = i - 7;
}
BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, tm_str);
newcu[i] = create_fcurve(axis, rna_path);
newcu[i]->totvert = frames.size();
}
if (frames.size() == 0) {
return;
}
std::sort(frames.begin(), frames.end());
BCQuat qref;
std::vector<float>::iterator it;
/* sample values at each frame */
for (it = frames.begin(); it != frames.end(); it++) {
float fra = *it;
Matrix mat;
Matrix matfra;
unit_m4(matfra);
/* calc object-space mat */
evaluate_transform_at_frame(matfra, node, fra);
/* for joints, we need a special matrix
* special matrix: iR * M * iR_dae * R
* where R, iR are bone rest and inverse rest mats in world space (Blender bones),
* iR_dae is joint inverse rest matrix (DAE)
* and M is an evaluated joint world-space matrix (DAE). */
Matrix temp, par;
/* calc M */
calc_joint_parent_mat_rest(par, NULL, root, node);
mul_m4_m4m4(temp, par, matfra);
/* evaluate_joint_world_transform_at_frame(temp, NULL, node, fra); */
/* calc special matrix */
mul_m4_series(mat, irest, temp, irest_dae, rest);
Vector loc, scale;
qref.rotate_to(mat);
copy_v3_v3(loc, mat[3]);
mat4_to_size(scale, mat);
/* add keys */
for (int i = 0; i < totcu; i++) {
if (i < 4) {
add_bezt(newcu[i], fra, qref.quat()[i]);
}
else if (i < 7) {
add_bezt(newcu[i], fra, loc[i - 4]);
}
else {
add_bezt(newcu[i], fra, scale[i - 7]);
}
}
}
Main *bmain = CTX_data_main(mContext);
verify_adt_action(bmain, (ID *)&ob->id, 1);
/* add curves */
for (int i = 0; i < totcu; i++) {
add_bone_fcurve(ob, node, newcu[i]);
#if 0
fcurve_is_used(newcu[i]); /* never added to unused */
#endif
}
bPoseChannel *chan = BKE_pose_channel_find_name(ob->pose, bone_name);
chan->rotmode = ROT_MODE_QUAT;
}
/* Check if object is animated by checking if animlist_map
* holds the animlist_id of node transforms */
AnimationImporter::AnimMix *AnimationImporter::get_animation_type(
const COLLADAFW::Node *node,
std::map<COLLADAFW::UniqueId, const COLLADAFW::Object *> FW_object_map)
{
AnimMix *types = new AnimMix();
const COLLADAFW::TransformationPointerArray &nodeTransforms = node->getTransformations();
/* for each transformation in node */
for (unsigned int i = 0; i < nodeTransforms.getCount(); i++) {
COLLADAFW::Transformation *transform = nodeTransforms[i];
const COLLADAFW::UniqueId &listid = transform->getAnimationList();
/* check if transformation has animations */
if (animlist_map.find(listid) == animlist_map.end()) {
continue;
}
else {
types->transform = types->transform | BC_NODE_TRANSFORM;
break;
}
}
const COLLADAFW::InstanceLightPointerArray &nodeLights = node->getInstanceLights();
for (unsigned int i = 0; i < nodeLights.getCount(); i++) {
const COLLADAFW::Light *light = (COLLADAFW::Light *)
FW_object_map[nodeLights[i]->getInstanciatedObjectId()];
types->light = setAnimType(&(light->getColor()), (types->light), LIGHT_COLOR);
types->light = setAnimType(&(light->getFallOffAngle()), (types->light), LIGHT_FOA);
types->light = setAnimType(&(light->getFallOffExponent()), (types->light), LIGHT_FOE);
if (types->light != 0) {
break;
}
}
const COLLADAFW::InstanceCameraPointerArray &nodeCameras = node->getInstanceCameras();
for (unsigned int i = 0; i < nodeCameras.getCount(); i++) {
const COLLADAFW::Camera *camera = (COLLADAFW::Camera *)
FW_object_map[nodeCameras[i]->getInstanciatedObjectId()];
if (camera == NULL) {
/* Can happen if the node refers to an unknown camera. */
continue;
}
const bool is_perspective_type = camera->getCameraType() == COLLADAFW::Camera::PERSPECTIVE;
int addition;
const COLLADAFW::Animatable *mag;
const COLLADAFW::UniqueId listid = camera->getYMag().getAnimationList();
if (animlist_map.find(listid) != animlist_map.end()) {
mag = &(camera->getYMag());
addition = (is_perspective_type) ? CAMERA_YFOV : CAMERA_YMAG;
}
else {
mag = &(camera->getXMag());
addition = (is_perspective_type) ? CAMERA_XFOV : CAMERA_XMAG;
}
types->camera = setAnimType(mag, (types->camera), addition);
types->camera = setAnimType(&(camera->getFarClippingPlane()), (types->camera), CAMERA_ZFAR);
types->camera = setAnimType(&(camera->getNearClippingPlane()), (types->camera), CAMERA_ZNEAR);
if (types->camera != 0) {
break;
}
}
const COLLADAFW::InstanceGeometryPointerArray &nodeGeoms = node->getInstanceGeometries();
for (unsigned int i = 0; i < nodeGeoms.getCount(); i++) {
const COLLADAFW::MaterialBindingArray &matBinds = nodeGeoms[i]->getMaterialBindings();
for (unsigned int j = 0; j < matBinds.getCount(); j++) {
const COLLADAFW::UniqueId &matuid = matBinds[j].getReferencedMaterial();
const COLLADAFW::Effect *ef = (COLLADAFW::Effect *)(FW_object_map[matuid]);
if (ef != NULL) { /* can be NULL [#28909] */
const COLLADAFW::CommonEffectPointerArray &commonEffects = ef->getCommonEffects();
if (!commonEffects.empty()) {
COLLADAFW::EffectCommon *efc = commonEffects[0];
types->material = setAnimType(
&(efc->getShininess()), (types->material), MATERIAL_SHININESS);
types->material = setAnimType(
&(efc->getSpecular().getColor()), (types->material), MATERIAL_SPEC_COLOR);
types->material = setAnimType(
&(efc->getDiffuse().getColor()), (types->material), MATERIAL_DIFF_COLOR);
#if 0
types->material = setAnimType(&(efc->get()), (types->material), MATERIAL_TRANSPARENCY);
#endif
types->material = setAnimType(
&(efc->getIndexOfRefraction()), (types->material), MATERIAL_IOR);
}
}
}
}
return types;
}
int AnimationImporter::setAnimType(const COLLADAFW::Animatable *prop, int types, int addition)
{
int anim_type;
const COLLADAFW::UniqueId &listid = prop->getAnimationList();
if (animlist_map.find(listid) != animlist_map.end()) {
anim_type = types | addition;
}
else {
anim_type = types;
}
return anim_type;
}
/* Is not used anymore. */
void AnimationImporter::find_frames_old(std::vector<float> *frames,
COLLADAFW::Node *node,
COLLADAFW::Transformation::TransformationType tm_type)
{
bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE;
/* for each <rotate>, <translate>, etc. there is a separate Transformation */
const COLLADAFW::TransformationPointerArray &nodeTransforms = node->getTransformations();
unsigned int i;
/* find frames at which to sample plus convert all rotation keys to radians */
for (i = 0; i < nodeTransforms.getCount(); i++) {
COLLADAFW::Transformation *transform = nodeTransforms[i];
COLLADAFW::Transformation::TransformationType nodeTmType = transform->getTransformationType();
if (nodeTmType == tm_type) {
/* get animation bindings for the current transformation */
const COLLADAFW::UniqueId &listid = transform->getAnimationList();
/* if transform is animated its animlist must exist. */
if (animlist_map.find(listid) != animlist_map.end()) {
const COLLADAFW::AnimationList *animlist = animlist_map[listid];
const COLLADAFW::AnimationList::AnimationBindings &bindings =
animlist->getAnimationBindings();
if (bindings.getCount()) {
/* for each AnimationBinding get the fcurves which animate the transform */
for (unsigned int j = 0; j < bindings.getCount(); j++) {
std::vector<FCurve *> &curves = curve_map[bindings[j].animation];
bool xyz = ((nodeTmType == COLLADAFW::Transformation::TRANSLATE ||
nodeTmType == COLLADAFW::Transformation::SCALE) &&
bindings[j].animationClass == COLLADAFW::AnimationList::POSITION_XYZ);
if ((!xyz && curves.size() == 1) || (xyz && curves.size() == 3) || is_matrix) {
std::vector<FCurve *>::iterator iter;
for (iter = curves.begin(); iter != curves.end(); iter++) {
FCurve *fcu = *iter;
/* if transform is rotation the fcurves values must be turned in to radian. */
if (is_rotation) {
fcurve_deg_to_rad(fcu);
}
for (unsigned int k = 0; k < fcu->totvert; k++) {
/* get frame value from bezTriple */
float fra = fcu->bezt[k].vec[1][0];
/* if frame already not added add frame to frames */
if (std::find(frames->begin(), frames->end(), fra) == frames->end()) {
frames->push_back(fra);
}
}
}
}
else {
fprintf(stderr, "expected %d curves, got %d\n", xyz ? 3 : 1, (int)curves.size());
}
}
}
}
}
}
}
/* prerequisites:
* animlist_map - map animlist id -> animlist
* curve_map - map anim id -> curve(s) */
Object *AnimationImporter::translate_animation_OLD(
COLLADAFW::Node *node,
std::map<COLLADAFW::UniqueId, Object *> &object_map,
std::map<COLLADAFW::UniqueId, COLLADAFW::Node *> &root_map,
COLLADAFW::Transformation::TransformationType tm_type,
Object *par_job)
{
bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE;
bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX;
bool is_joint = node->getType() == COLLADAFW::Node::JOINT;
COLLADAFW::Node *root = root_map.find(node->getUniqueId()) == root_map.end() ?
node :
root_map[node->getUniqueId()];
Object *ob = is_joint ? armature_importer->get_armature_for_joint(node) :
object_map[node->getUniqueId()];
const char *bone_name = is_joint ? bc_get_joint_name(node) : NULL;
if (!ob) {
fprintf(stderr, "cannot find Object for Node with id=\"%s\"\n", node->getOriginalId().c_str());
return NULL;
}
/* frames at which to sample */
std::vector<float> frames;
find_frames_old(&frames, node, tm_type);
unsigned int i;
float irest_dae[4][4];
float rest[4][4], irest[4][4];
if (is_joint) {
get_joint_rest_mat(irest_dae, root, node);
invert_m4(irest_dae);
Bone *bone = BKE_armature_find_bone_name((bArmature *)ob->data, bone_name);
if (!bone) {
fprintf(stderr, "cannot find bone \"%s\"\n", bone_name);
return NULL;
}
unit_m4(rest);
copy_m4_m4(rest, bone->arm_mat);
invert_m4_m4(irest, rest);
}
Object *job = NULL;
#ifdef ARMATURE_TEST
FCurve *job_curves[10];
job = get_joint_object(root, node, par_job);
#endif
if (frames.size() == 0) {
return job;
}
std::sort(frames.begin(), frames.end());
const char *tm_str = NULL;
switch (tm_type) {
case COLLADAFW::Transformation::ROTATE:
tm_str = "rotation_quaternion";
break;
case COLLADAFW::Transformation::SCALE:
tm_str = "scale";
break;
case COLLADAFW::Transformation::TRANSLATE:
tm_str = "location";
break;
case COLLADAFW::Transformation::MATRIX:
break;
default:
return job;
}
char rna_path[200];
char joint_path[200];
if (is_joint) {
armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path));
}
/* new curves */
FCurve *newcu[10]; /* if tm_type is matrix, then create 10 curves: 4 rot, 3 loc, 3 scale */
unsigned int totcu = is_matrix ? 10 : (is_rotation ? 4 : 3);
for (i = 0; i < totcu; i++) {
int axis = i;
if (is_matrix) {
if (i < 4) {
tm_str = "rotation_quaternion";
axis = i;
}
else if (i < 7) {
tm_str = "location";
axis = i - 4;
}
else {
tm_str = "scale";
axis = i - 7;
}
}
if (is_joint) {
BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, tm_str);
}
else {
BLI_strncpy(rna_path, tm_str, sizeof(rna_path));
}
newcu[i] = create_fcurve(axis, rna_path);
#ifdef ARMATURE_TEST
if (is_joint) {
job_curves[i] = create_fcurve(axis, tm_str);
}
#endif
}
std::vector<float>::iterator it;
/* sample values at each frame */
for (it = frames.begin(); it != frames.end(); it++) {
float fra = *it;
float mat[4][4];
float matfra[4][4];
unit_m4(matfra);
/* calc object-space mat */
evaluate_transform_at_frame(matfra, node, fra);
/* for joints, we need a special matrix */
if (is_joint) {
/* special matrix: iR * M * iR_dae * R
* where R, iR are bone rest and inverse rest mats in world space (Blender bones),
* iR_dae is joint inverse rest matrix (DAE)
* and M is an evaluated joint world-space matrix (DAE). */
float temp[4][4], par[4][4];
/* calc M */
calc_joint_parent_mat_rest(par, NULL, root, node);
mul_m4_m4m4(temp, par, matfra);
/* evaluate_joint_world_transform_at_frame(temp, NULL, node, fra); */
/* calc special matrix */
mul_m4_series(mat, irest, temp, irest_dae, rest);
}
else {
copy_m4_m4(mat, matfra);
}
float val[4] = {};
float rot[4], loc[3], scale[3];
switch (tm_type) {
case COLLADAFW::Transformation::ROTATE:
mat4_to_quat(val, mat);
break;
case COLLADAFW::Transformation::SCALE:
mat4_to_size(val, mat);
break;
case COLLADAFW::Transformation::TRANSLATE:
copy_v3_v3(val, mat[3]);
break;
case COLLADAFW::Transformation::MATRIX:
mat4_to_quat(rot, mat);
copy_v3_v3(loc, mat[3]);
mat4_to_size(scale, mat);
break;
default:
break;
}
/* add keys */
for (i = 0; i < totcu; i++) {
if (is_matrix) {
if (i < 4) {
add_bezt(newcu[i], fra, rot[i]);
}
else if (i < 7) {
add_bezt(newcu[i], fra, loc[i - 4]);
}
else {
add_bezt(newcu[i], fra, scale[i - 7]);
}
}
else {
add_bezt(newcu[i], fra, val[i]);
}
}
#ifdef ARMATURE_TEST
if (is_joint) {
switch (tm_type) {
case COLLADAFW::Transformation::ROTATE:
mat4_to_quat(val, matfra);
break;
case COLLADAFW::Transformation::SCALE:
mat4_to_size(val, matfra);
break;
case COLLADAFW::Transformation::TRANSLATE:
copy_v3_v3(val, matfra[3]);
break;
case MATRIX:
mat4_to_quat(rot, matfra);
copy_v3_v3(loc, matfra[3]);
mat4_to_size(scale, matfra);
break;
default:
break;
}
for (i = 0; i < totcu; i++) {
if (is_matrix) {
if (i < 4) {
add_bezt(job_curves[i], fra, rot[i]);
}
else if (i < 7) {
add_bezt(job_curves[i], fra, loc[i - 4]);
}
else {
add_bezt(job_curves[i], fra, scale[i - 7]);
}
}
else {
add_bezt(job_curves[i], fra, val[i]);
}
}
}
#endif
}
Main *bmain = CTX_data_main(mContext);
verify_adt_action(bmain, (ID *)&ob->id, 1);
ListBase *curves = &ob->adt->action->curves;
/* add curves */
for (i = 0; i < totcu; i++) {
if (is_joint) {
add_bone_fcurve(ob, node, newcu[i]);
}
else {
BLI_addtail(curves, newcu[i]);
}
#ifdef ARMATURE_TEST
if (is_joint) {
BLI_addtail(&job->adt->action->curves, job_curves[i]);
}
#endif
}
if (is_rotation || is_matrix) {
if (is_joint) {
bPoseChannel *chan = BKE_pose_channel_find_name(ob->pose, bone_name);
chan->rotmode = (is_matrix) ? ROT_MODE_QUAT : ROT_MODE_EUL;
}
else {
ob->rotmode = (is_matrix) ? ROT_MODE_QUAT : ROT_MODE_EUL;
}
}
return job;
}
/* internal, better make it private
* warning: evaluates only rotation and only assigns matrix transforms now
* prerequisites: animlist_map, curve_map */
void AnimationImporter::evaluate_transform_at_frame(float mat[4][4],
COLLADAFW::Node *node,
float fra)
{
const COLLADAFW::TransformationPointerArray &tms = node->getTransformations();
unit_m4(mat);
for (unsigned int i = 0; i < tms.getCount(); i++) {
COLLADAFW::Transformation *tm = tms[i];
COLLADAFW::Transformation::TransformationType type = tm->getTransformationType();
float m[4][4];
unit_m4(m);
std::string nodename = node->getName().size() ? node->getName() : node->getOriginalId();
if (!evaluate_animation(tm, m, fra, nodename.c_str())) {
switch (type) {
case COLLADAFW::Transformation::ROTATE:
dae_rotate_to_mat4(tm, m);
break;
case COLLADAFW::Transformation::TRANSLATE:
dae_translate_to_mat4(tm, m);
break;
case COLLADAFW::Transformation::SCALE:
dae_scale_to_mat4(tm, m);
break;
case COLLADAFW::Transformation::MATRIX:
dae_matrix_to_mat4(tm, m);
break;
default:
fprintf(stderr, "unsupported transformation type %d\n", type);
}
}
float temp[4][4];
copy_m4_m4(temp, mat);
mul_m4_m4m4(mat, temp, m);
}
}
static void report_class_type_unsupported(const char *path,
const COLLADAFW::AnimationList::AnimationClass animclass,
const COLLADAFW::Transformation::TransformationType type)
{
if (animclass == COLLADAFW::AnimationList::UNKNOWN_CLASS) {
fprintf(stderr, "%s: UNKNOWN animation class\n", path);
}
else {
fprintf(stderr,
"%s: animation class %d is not supported yet for transformation type %d\n",
path,
animclass,
type);
}
}
/* return true to indicate that mat contains a sane value */
bool AnimationImporter::evaluate_animation(COLLADAFW::Transformation *tm,
float mat[4][4],
float fra,
const char *node_id)
{
const COLLADAFW::UniqueId &listid = tm->getAnimationList();
COLLADAFW::Transformation::TransformationType type = tm->getTransformationType();
if (type != COLLADAFW::Transformation::ROTATE && type != COLLADAFW::Transformation::SCALE &&
type != COLLADAFW::Transformation::TRANSLATE && type != COLLADAFW::Transformation::MATRIX) {
fprintf(stderr, "animation of transformation %d is not supported yet\n", type);
return false;
}
if (animlist_map.find(listid) == animlist_map.end()) {
return false;
}
const COLLADAFW::AnimationList *animlist = animlist_map[listid];
const COLLADAFW::AnimationList::AnimationBindings &bindings = animlist->getAnimationBindings();
if (bindings.getCount()) {
float vec[3];
bool is_scale = (type == COLLADAFW::Transformation::SCALE);
bool is_translate = (type == COLLADAFW::Transformation::TRANSLATE);
if (is_scale) {
dae_scale_to_v3(tm, vec);
}
else if (is_translate) {
dae_translate_to_v3(tm, vec);
}
for (unsigned int index = 0; index < bindings.getCount(); index++) {
const COLLADAFW::AnimationList::AnimationBinding &binding = bindings[index];
std::vector<FCurve *> &curves = curve_map[binding.animation];
COLLADAFW::AnimationList::AnimationClass animclass = binding.animationClass;
char path[100];
switch (type) {
case COLLADAFW::Transformation::ROTATE:
BLI_snprintf(path, sizeof(path), "%s.rotate (binding %u)", node_id, index);
break;
case COLLADAFW::Transformation::SCALE:
BLI_snprintf(path, sizeof(path), "%s.scale (binding %u)", node_id, index);
break;
case COLLADAFW::Transformation::TRANSLATE:
BLI_snprintf(path, sizeof(path), "%s.translate (binding %u)", node_id, index);
break;
case COLLADAFW::Transformation::MATRIX:
BLI_snprintf(path, sizeof(path), "%s.matrix (binding %u)", node_id, index);
break;
default:
break;
}
if (type == COLLADAFW::Transformation::ROTATE) {
if (curves.size() != 1) {
fprintf(stderr, "expected 1 curve, got %d\n", (int)curves.size());
return false;
}
/* TODO support other animclasses */
if (animclass != COLLADAFW::AnimationList::ANGLE) {
report_class_type_unsupported(path, animclass, type);
return false;
}
COLLADABU::Math::Vector3 &axis = ((COLLADAFW::Rotate *)tm)->getRotationAxis();
float ax[3] = {(float)axis[0], (float)axis[1], (float)axis[2]};
float angle = evaluate_fcurve(curves[0], fra);
axis_angle_to_mat4(mat, ax, angle);
return true;
}
else if (is_scale || is_translate) {
bool is_xyz = animclass == COLLADAFW::AnimationList::POSITION_XYZ;
if ((!is_xyz && curves.size() != 1) || (is_xyz && curves.size() != 3)) {
if (is_xyz) {
fprintf(stderr, "%s: expected 3 curves, got %d\n", path, (int)curves.size());
}
else {
fprintf(stderr, "%s: expected 1 curve, got %d\n", path, (int)curves.size());
}
return false;
}
switch (animclass) {
case COLLADAFW::AnimationList::POSITION_X:
vec[0] = evaluate_fcurve(curves[0], fra);
break;
case COLLADAFW::AnimationList::POSITION_Y:
vec[1] = evaluate_fcurve(curves[0], fra);
break;
case COLLADAFW::AnimationList::POSITION_Z:
vec[2] = evaluate_fcurve(curves[0], fra);
break;
case COLLADAFW::AnimationList::POSITION_XYZ:
vec[0] = evaluate_fcurve(curves[0], fra);
vec[1] = evaluate_fcurve(curves[1], fra);
vec[2] = evaluate_fcurve(curves[2], fra);
break;
default:
report_class_type_unsupported(path, animclass, type);
break;
}
}
else if (type == COLLADAFW::Transformation::MATRIX) {
/* for now, of matrix animation,
* support only the case when all values are packed into one animation */
if (curves.size() != 16) {
fprintf(stderr, "%s: expected 16 curves, got %d\n", path, (int)curves.size());
return false;
}
COLLADABU::Math::Matrix4 matrix;
int mi = 0, mj = 0;
for (std::vector<FCurve *>::iterator it = curves.begin(); it != curves.end(); it++) {
matrix.setElement(mi, mj, evaluate_fcurve(*it, fra));
mj++;
if (mj == 4) {
mi++;
mj = 0;
}
}
unit_converter->dae_matrix_to_mat4_(mat, matrix);
return true;
}
}
if (is_scale) {
size_to_mat4(mat, vec);
}
else {
copy_v3_v3(mat[3], vec);
}
return is_scale || is_translate;
}
return false;
}
/* gives a world-space mat of joint at rest position */
void AnimationImporter::get_joint_rest_mat(float mat[4][4],
COLLADAFW::Node *root,
COLLADAFW::Node *node)
{
/* if bind mat is not available,
* use "current" node transform, i.e. all those tms listed inside <node> */
if (!armature_importer->get_joint_bind_mat(mat, node)) {
float par[4][4], m[4][4];
calc_joint_parent_mat_rest(par, NULL, root, node);
get_node_mat(m, node, NULL, NULL);
mul_m4_m4m4(mat, par, m);
}
}
/* gives a world-space mat, end's mat not included */
bool AnimationImporter::calc_joint_parent_mat_rest(float mat[4][4],
float par[4][4],
COLLADAFW::Node *node,
COLLADAFW::Node *end)
{
float m[4][4];
if (node == end) {
par ? copy_m4_m4(mat, par) : unit_m4(mat);
return true;
}
/* use bind matrix if available or calc "current" world mat */
if (!armature_importer->get_joint_bind_mat(m, node)) {
if (par) {
float temp[4][4];
get_node_mat(temp, node, NULL, NULL);
mul_m4_m4m4(m, par, temp);
}
else {
get_node_mat(m, node, NULL, NULL);
}
}
COLLADAFW::NodePointerArray &children = node->getChildNodes();
for (unsigned int i = 0; i < children.getCount(); i++) {
if (calc_joint_parent_mat_rest(mat, m, children[i], end)) {
return true;
}
}
return false;
}
#ifdef ARMATURE_TEST
Object *AnimationImporter::get_joint_object(COLLADAFW::Node *root,
COLLADAFW::Node *node,
Object *par_job)
{
if (joint_objects.find(node->getUniqueId()) == joint_objects.end()) {
Object *job = bc_add_object(scene, OB_EMPTY, (char *)get_joint_name(node));
job->lay = BKE_scene_base_find(scene, job)->lay = 2;
mul_v3_fl(job->scale, 0.5f);
DEG_id_tag_update(&job->id, ID_RECALC_TRANSFORM);
verify_adt_action((ID *)&job->id, 1);
job->rotmode = ROT_MODE_QUAT;
float mat[4][4];
get_joint_rest_mat(mat, root, node);
if (par_job) {
float temp[4][4], ipar[4][4];
invert_m4_m4(ipar, par_job->obmat);
copy_m4_m4(temp, mat);
mul_m4_m4m4(mat, ipar, temp);
}
bc_decompose(mat, job->loc, NULL, job->quat, job->scale);
if (par_job) {
job->parent = par_job;
DEG_id_tag_update(&par_job->id, ID_RECALC_TRANSFORM);
job->parsubstr[0] = 0;
}
BKE_object_where_is_calc(scene, job);
/* after parenting and layer change */
DEG_relations_tag_update(CTX_data_main(C));
joint_objects[node->getUniqueId()] = job;
}
return joint_objects[node->getUniqueId()];
}
#endif
#if 0
/* recursively evaluates joint tree until end is found,
* mat then is world-space matrix of end mat must be identity on enter, node must be root. */
bool AnimationImporter::evaluate_joint_world_transform_at_frame(
float mat[4][4], float par[4][4], COLLADAFW::Node *node, COLLADAFW::Node *end, float fra)
{
float m[4][4];
if (par) {
float temp[4][4];
evaluate_transform_at_frame(temp, node, node == end ? fra : 0.0f);
mul_m4_m4m4(m, par, temp);
}
else {
evaluate_transform_at_frame(m, node, node == end ? fra : 0.0f);
}
if (node == end) {
copy_m4_m4(mat, m);
return true;
}
else {
COLLADAFW::NodePointerArray &children = node->getChildNodes();
for (int i = 0; i < children.getCount(); i++) {
if (evaluate_joint_world_transform_at_frame(mat, m, children[i], end, fra)) {
return true;
}
}
}
return false;
}
#endif
void AnimationImporter::add_bone_fcurve(Object *ob, COLLADAFW::Node *node, FCurve *fcu)
{
const char *bone_name = bc_get_joint_name(node);
bAction *act = ob->adt->action;
/* try to find group */
bActionGroup *grp = BKE_action_group_find_name(act, bone_name);
/* no matching groups, so add one */
if (grp == NULL) {
/* Add a new group, and make it active */
grp = (bActionGroup *)MEM_callocN(sizeof(bActionGroup), "bActionGroup");
grp->flag = AGRP_SELECTED;
BLI_strncpy(grp->name, bone_name, sizeof(grp->name));
BLI_addtail(&act->groups, grp);
BLI_uniquename(&act->groups,
grp,
CTX_DATA_(BLT_I18NCONTEXT_ID_ACTION, "Group"),
'.',
offsetof(bActionGroup, name),
64);
}
/* add F-Curve to group */
action_groups_add_channel(act, grp, fcu);
}
void AnimationImporter::set_import_from_version(std::string import_from_version)
{
this->import_from_version = import_from_version;
}
|