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
|
/*
* ***** 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) 2005 Blender Foundation.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): David Millan Escriva, Juho Vepsäläinen, Nathan Letwory
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/editors/space_node/node_edit.c
* \ingroup spnode
*/
#include "MEM_guardedalloc.h"
#include "DNA_lamp_types.h"
#include "DNA_material_types.h"
#include "DNA_node_types.h"
#include "DNA_object_types.h"
#include "DNA_world_types.h"
#include "BLI_math.h"
#include "BLI_blenlib.h"
#include "BKE_context.h"
#include "BKE_depsgraph.h"
#include "BKE_global.h"
#include "BKE_image.h"
#include "BKE_library.h"
#include "BKE_main.h"
#include "BKE_node.h"
#include "BKE_material.h"
#include "BKE_paint.h"
#include "BKE_scene.h"
#include "BKE_screen.h"
#include "BKE_texture.h"
#include "RE_pipeline.h"
#include "IMB_imbuf_types.h"
#include "ED_node.h" /* own include */
#include "ED_image.h"
#include "ED_screen.h"
#include "ED_space_api.h"
#include "ED_render.h"
#include "RNA_access.h"
#include "RNA_define.h"
#include "WM_api.h"
#include "WM_types.h"
#include "UI_view2d.h"
#include "IMB_imbuf.h"
#include "GPU_material.h"
#include "node_intern.h" /* own include */
/* ***************** composite job manager ********************** */
typedef struct CompoJob {
Scene *scene;
bNodeTree *ntree;
bNodeTree *localtree;
short *stop;
short *do_update;
float *progress;
} CompoJob;
/* called by compo, only to check job 'stop' value */
static int compo_breakjob(void *cjv)
{
CompoJob *cj = cjv;
return *(cj->stop);
}
/* called by compo, wmJob sends notifier */
static void compo_redrawjob(void *cjv, char *UNUSED(str))
{
CompoJob *cj = cjv;
*(cj->do_update) = TRUE;
}
static void compo_freejob(void *cjv)
{
CompoJob *cj = cjv;
if (cj->localtree) {
ntreeLocalMerge(cj->localtree, cj->ntree);
}
MEM_freeN(cj);
}
/* only now we copy the nodetree, so adding many jobs while
* sliding buttons doesn't frustrate */
static void compo_initjob(void *cjv)
{
CompoJob *cj = cjv;
cj->localtree = ntreeLocalize(cj->ntree);
}
/* called before redraw notifiers, it moves finished previews over */
static void compo_updatejob(void *cjv)
{
CompoJob *cj = cjv;
ntreeLocalSync(cj->localtree, cj->ntree);
}
static void compo_progressjob(void *cjv, float progress)
{
CompoJob *cj = cjv;
*(cj->progress) = progress;
}
/* only this runs inside thread */
static void compo_startjob(void *cjv, short *stop, short *do_update, float *progress)
{
CompoJob *cj = cjv;
bNodeTree *ntree = cj->localtree;
if (cj->scene->use_nodes == FALSE)
return;
cj->stop = stop;
cj->do_update = do_update;
cj->progress = progress;
ntree->test_break = compo_breakjob;
ntree->tbh = cj;
ntree->stats_draw = compo_redrawjob;
ntree->sdh = cj;
ntree->progress = compo_progressjob;
ntree->prh = cj;
// XXX BIF_store_spare();
ntreeCompositExecTree(ntree, &cj->scene->r, 0, 1); /* 1 is do_previews */
ntree->test_break = NULL;
ntree->stats_draw = NULL;
ntree->progress = NULL;
}
void snode_composite_job(const bContext *C, ScrArea *sa)
{
SpaceNode *snode = sa->spacedata.first;
wmJob *steve;
CompoJob *cj;
steve = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), sa, "Compositing", WM_JOB_EXCL_RENDER | WM_JOB_PROGRESS);
cj = MEM_callocN(sizeof(CompoJob), "compo job");
/* customdata for preview thread */
cj->scene = CTX_data_scene(C);
cj->ntree = snode->nodetree;
/* setup job */
WM_jobs_customdata(steve, cj, compo_freejob);
WM_jobs_timer(steve, 0.1, NC_SCENE, NC_SCENE | ND_COMPO_RESULT);
WM_jobs_callbacks(steve, compo_startjob, compo_initjob, compo_updatejob, NULL);
WM_jobs_start(CTX_wm_manager(C), steve);
}
/* ***************************************** */
/* operator poll callback */
int composite_node_active(bContext *C)
{
if (ED_operator_node_active(C)) {
SpaceNode *snode = CTX_wm_space_node(C);
if (snode->treetype == NTREE_COMPOSIT)
return 1;
}
return 0;
}
/* also checks for edited groups */
bNode *editnode_get_active(bNodeTree *ntree)
{
bNode *node;
/* check for edited group */
for (node = ntree->nodes.first; node; node = node->next)
if (nodeGroupEditGet(node))
break;
if (node)
return nodeGetActive((bNodeTree *)node->id);
else
return nodeGetActive(ntree);
}
static int has_nodetree(bNodeTree *ntree, bNodeTree *lookup)
{
bNode *node;
if (ntree == lookup)
return 1;
for (node = ntree->nodes.first; node; node = node->next)
if (node->type == NODE_GROUP && node->id)
if (has_nodetree((bNodeTree *)node->id, lookup))
return 1;
return 0;
}
static void snode_dag_update_group(void *calldata, ID *owner_id, bNodeTree *ntree)
{
if (has_nodetree(ntree, calldata))
DAG_id_tag_update(owner_id, 0);
}
void snode_dag_update(bContext *C, SpaceNode *snode)
{
Main *bmain = CTX_data_main(C);
/* for groups, update all ID's using this */
if (snode->edittree != snode->nodetree) {
bNodeTreeType *tti = ntreeGetType(snode->edittree->type);
tti->foreach_nodetree(bmain, snode->edittree, snode_dag_update_group);
}
DAG_id_tag_update(snode->id, 0);
}
void snode_notify(bContext *C, SpaceNode *snode)
{
WM_event_add_notifier(C, NC_NODE | NA_EDITED, NULL);
if (snode->treetype == NTREE_SHADER)
WM_event_add_notifier(C, NC_MATERIAL | ND_NODES, snode->id);
else if (snode->treetype == NTREE_COMPOSIT)
WM_event_add_notifier(C, NC_SCENE | ND_NODES, snode->id);
else if (snode->treetype == NTREE_TEXTURE)
WM_event_add_notifier(C, NC_TEXTURE | ND_NODES, snode->id);
}
bNode *node_tree_get_editgroup(bNodeTree *nodetree)
{
bNode *gnode;
/* get the groupnode */
for (gnode = nodetree->nodes.first; gnode; gnode = gnode->next)
if (nodeGroupEditGet(gnode))
break;
return gnode;
}
/* assumes nothing being done in ntree yet, sets the default in/out node */
/* called from shading buttons or header */
void ED_node_shader_default(Scene *scene, ID *id)
{
bNode *in, *out;
bNodeSocket *fromsock, *tosock, *sock;
bNodeTree *ntree;
bNodeTemplate ntemp;
int output_type, shader_type;
float color[3], strength = 1.0f;
ntree = ntreeAddTree("Shader Nodetree", NTREE_SHADER, 0);
switch (GS(id->name)) {
case ID_MA: {
Material *ma = (Material *)id;
ma->nodetree = ntree;
if (BKE_scene_use_new_shading_nodes(scene)) {
output_type = SH_NODE_OUTPUT_MATERIAL;
shader_type = SH_NODE_BSDF_DIFFUSE;
}
else {
output_type = SH_NODE_OUTPUT;
shader_type = SH_NODE_MATERIAL;
}
copy_v3_v3(color, &ma->r);
strength = 0.0f;
break;
}
case ID_WO: {
World *wo = (World *)id;
wo->nodetree = ntree;
output_type = SH_NODE_OUTPUT_WORLD;
shader_type = SH_NODE_BACKGROUND;
copy_v3_v3(color, &wo->horr);
strength = 1.0f;
break;
}
case ID_LA: {
Lamp *la = (Lamp *)id;
la->nodetree = ntree;
output_type = SH_NODE_OUTPUT_LAMP;
shader_type = SH_NODE_EMISSION;
copy_v3_v3(color, &la->r);
if (la->type == LA_LOCAL || la->type == LA_SPOT || la->type == LA_AREA)
strength = 100.0f;
else
strength = 1.0f;
break;
}
default:
printf("ED_node_shader_default called on wrong ID type.\n");
return;
}
ntemp.type = output_type;
out = nodeAddNode(ntree, &ntemp);
out->locx = 300.0f; out->locy = 300.0f;
ntemp.type = shader_type;
in = nodeAddNode(ntree, &ntemp);
in->locx = 10.0f; in->locy = 300.0f;
nodeSetActive(ntree, in);
/* only a link from color to color */
fromsock = in->outputs.first;
tosock = out->inputs.first;
nodeAddLink(ntree, in, fromsock, out, tosock);
/* default values */
if (BKE_scene_use_new_shading_nodes(scene)) {
sock = in->inputs.first;
copy_v3_v3(((bNodeSocketValueRGBA *)sock->default_value)->value, color);
if (strength != 0.0f) {
sock = in->inputs.last;
((bNodeSocketValueFloat *)sock->default_value)->value = strength;
}
}
ntreeUpdateTree(ntree);
}
/* assumes nothing being done in ntree yet, sets the default in/out node */
/* called from shading buttons or header */
void ED_node_composit_default(Scene *sce)
{
bNode *in, *out;
bNodeSocket *fromsock, *tosock;
bNodeTemplate ntemp;
/* but lets check it anyway */
if (sce->nodetree) {
if (G.debug & G_DEBUG)
printf("error in composite initialize\n");
return;
}
sce->nodetree = ntreeAddTree("Compositing Nodetree", NTREE_COMPOSIT, 0);
sce->nodetree->chunksize = 256;
sce->nodetree->edit_quality = NTREE_QUALITY_HIGH;
sce->nodetree->render_quality = NTREE_QUALITY_HIGH;
ntemp.type = CMP_NODE_COMPOSITE;
out = nodeAddNode(sce->nodetree, &ntemp);
out->locx = 300.0f; out->locy = 400.0f;
out->id = &sce->id;
id_us_plus(out->id);
ntemp.type = CMP_NODE_R_LAYERS;
in = nodeAddNode(sce->nodetree, &ntemp);
in->locx = 10.0f; in->locy = 400.0f;
in->id = &sce->id;
id_us_plus(in->id);
nodeSetActive(sce->nodetree, in);
/* links from color to color */
fromsock = in->outputs.first;
tosock = out->inputs.first;
nodeAddLink(sce->nodetree, in, fromsock, out, tosock);
ntreeUpdateTree(sce->nodetree);
// XXX ntreeCompositForceHidden(sce->nodetree);
}
/* assumes nothing being done in ntree yet, sets the default in/out node */
/* called from shading buttons or header */
void ED_node_texture_default(Tex *tx)
{
bNode *in, *out;
bNodeSocket *fromsock, *tosock;
bNodeTemplate ntemp;
/* but lets check it anyway */
if (tx->nodetree) {
if (G.debug & G_DEBUG)
printf("error in texture initialize\n");
return;
}
tx->nodetree = ntreeAddTree("Texture Nodetree", NTREE_TEXTURE, 0);
ntemp.type = TEX_NODE_OUTPUT;
out = nodeAddNode(tx->nodetree, &ntemp);
out->locx = 300.0f; out->locy = 300.0f;
ntemp.type = TEX_NODE_CHECKER;
in = nodeAddNode(tx->nodetree, &ntemp);
in->locx = 10.0f; in->locy = 300.0f;
nodeSetActive(tx->nodetree, in);
fromsock = in->outputs.first;
tosock = out->inputs.first;
nodeAddLink(tx->nodetree, in, fromsock, out, tosock);
ntreeUpdateTree(tx->nodetree);
}
/* id is supposed to contain a node tree */
void node_tree_from_ID(ID *id, bNodeTree **ntree, bNodeTree **edittree, int *treetype)
{
if (id) {
bNode *node = NULL;
short idtype = GS(id->name);
if (idtype == ID_NT) {
*ntree = (bNodeTree *)id;
if (treetype) *treetype = (*ntree)->type;
}
else if (idtype == ID_MA) {
*ntree = ((Material *)id)->nodetree;
if (treetype) *treetype = NTREE_SHADER;
}
else if (idtype == ID_LA) {
*ntree = ((Lamp *)id)->nodetree;
if (treetype) *treetype = NTREE_SHADER;
}
else if (idtype == ID_WO) {
*ntree = ((World *)id)->nodetree;
if (treetype) *treetype = NTREE_SHADER;
}
else if (idtype == ID_SCE) {
*ntree = ((Scene *)id)->nodetree;
if (treetype) *treetype = NTREE_COMPOSIT;
}
else if (idtype == ID_TE) {
*ntree = ((Tex *)id)->nodetree;
if (treetype) *treetype = NTREE_TEXTURE;
}
else {
if (treetype) *treetype = 0;
return;
}
/* find editable group */
if (edittree) {
if (*ntree)
for (node = (*ntree)->nodes.first; node; node = node->next)
if (nodeGroupEditGet(node))
break;
if (node && node->id)
*edittree = (bNodeTree *)node->id;
else
*edittree = *ntree;
}
}
else {
*ntree = NULL;
*edittree = NULL;
if (treetype) *treetype = 0;
}
}
/* Here we set the active tree(s), even called for each redraw now, so keep it fast :) */
void snode_set_context(SpaceNode *snode, Scene *scene)
{
Object *ob = OBACT;
snode->id = snode->from = NULL;
if (snode->treetype == NTREE_SHADER) {
/* need active object, or we allow pinning... */
if (snode->shaderfrom == SNODE_SHADER_OBJECT) {
if (ob) {
if (ob->type == OB_LAMP) {
snode->from = &ob->id;
snode->id = ob->data;
}
else {
Material *ma = give_current_material(ob, ob->actcol);
if (ma) {
snode->from = &ob->id;
snode->id = &ma->id;
}
}
}
}
else { /* SNODE_SHADER_WORLD */
if (scene->world) {
snode->from = NULL;
snode->id = &scene->world->id;
}
}
}
else if (snode->treetype == NTREE_COMPOSIT) {
snode->id = &scene->id;
/* update output sockets based on available layers */
ntreeCompositForceHidden(scene->nodetree, scene);
}
else if (snode->treetype == NTREE_TEXTURE) {
Tex *tx = NULL;
if (snode->texfrom == SNODE_TEX_OBJECT) {
if (ob) {
tx = give_current_object_texture(ob);
if (ob->type == OB_LAMP)
snode->from = (ID *)ob->data;
else
snode->from = (ID *)give_current_material(ob, ob->actcol);
/* from is not set fully for material nodes, should be ID + Node then */
snode->id = &tx->id;
}
}
else if (snode->texfrom == SNODE_TEX_WORLD) {
tx = give_current_world_texture(scene->world);
snode->from = (ID *)scene->world;
snode->id = &tx->id;
}
else {
struct Brush *brush = NULL;
if (ob && (ob->mode & OB_MODE_SCULPT))
brush = paint_brush(&scene->toolsettings->sculpt->paint);
else
brush = paint_brush(&scene->toolsettings->imapaint.paint);
if (brush) {
snode->from = (ID *)brush;
tx = give_current_brush_texture(brush);
snode->id = &tx->id;
}
}
}
else {
if (snode->nodetree && snode->nodetree->type == snode->treetype)
snode->id = &snode->nodetree->id;
else
snode->id = NULL;
}
node_tree_from_ID(snode->id, &snode->nodetree, &snode->edittree, NULL);
}
void snode_update(SpaceNode *snode, bNode *node)
{
bNode *gnode;
if (node)
nodeUpdate(snode->edittree, node);
/* if inside group, tag entire group */
gnode = node_tree_get_editgroup(snode->nodetree);
if (gnode)
nodeUpdateID(snode->nodetree, gnode->id);
}
void ED_node_set_active(Main *bmain, bNodeTree *ntree, bNode *node)
{
int was_active_texture = (node->flag & NODE_ACTIVE_TEXTURE);
nodeSetActive(ntree, node);
if (node->type != NODE_GROUP) {
int was_output = (node->flag & NODE_DO_OUTPUT);
/* tree specific activate calls */
if (ntree->type == NTREE_SHADER) {
/* when we select a material, active texture is cleared, for buttons */
if (node->id && ELEM3(GS(node->id->name), ID_MA, ID_LA, ID_WO))
nodeClearActiveID(ntree, ID_TE);
if (node->type == SH_NODE_OUTPUT) {
bNode *tnode;
for (tnode = ntree->nodes.first; tnode; tnode = tnode->next)
if (tnode->type == SH_NODE_OUTPUT)
tnode->flag &= ~NODE_DO_OUTPUT;
node->flag |= NODE_DO_OUTPUT;
if (was_output == 0)
ED_node_generic_update(bmain, ntree, node);
}
/* if active texture changed, free glsl materials */
if ((node->flag & NODE_ACTIVE_TEXTURE) && !was_active_texture) {
Material *ma;
for (ma = bmain->mat.first; ma; ma = ma->id.next)
if (ma->nodetree && ma->use_nodes && has_nodetree(ma->nodetree, ntree))
GPU_material_free(ma);
WM_main_add_notifier(NC_IMAGE, NULL);
}
WM_main_add_notifier(NC_MATERIAL | ND_NODES, node->id);
}
else if (ntree->type == NTREE_COMPOSIT) {
/* make active viewer, currently only 1 supported... */
if (ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) {
bNode *tnode;
for (tnode = ntree->nodes.first; tnode; tnode = tnode->next)
if (ELEM(tnode->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
tnode->flag &= ~NODE_DO_OUTPUT;
node->flag |= NODE_DO_OUTPUT;
if (was_output == 0)
ED_node_generic_update(bmain, ntree, node);
/* addnode() doesnt link this yet... */
node->id = (ID *)BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
}
else if (node->type == CMP_NODE_R_LAYERS) {
Scene *scene;
for (scene = bmain->scene.first; scene; scene = scene->id.next) {
if (scene->nodetree && scene->use_nodes && has_nodetree(scene->nodetree, ntree)) {
if (node->id == NULL || node->id == (ID *)scene) {
scene->r.actlay = node->custom1;
}
}
}
}
else if (node->type == CMP_NODE_COMPOSITE) {
if (was_output == 0) {
bNode *tnode;
for (tnode = ntree->nodes.first; tnode; tnode = tnode->next)
if (tnode->type == CMP_NODE_COMPOSITE)
tnode->flag &= ~NODE_DO_OUTPUT;
node->flag |= NODE_DO_OUTPUT;
ED_node_generic_update(bmain, ntree, node);
}
}
}
else if (ntree->type == NTREE_TEXTURE) {
// XXX
#if 0
if (node->id)
; // XXX BIF_preview_changed(-1);
// allqueue(REDRAWBUTSSHADING, 1);
// allqueue(REDRAWIPO, 0);
#endif
}
}
}
void ED_node_post_apply_transform(bContext *UNUSED(C), bNodeTree *UNUSED(ntree))
{
/* XXX This does not work due to layout functions relying on node->block,
* which only exists during actual drawing. Can we rely on valid totr rects?
*/
/* make sure nodes have correct bounding boxes after transform */
/* node_update_nodetree(C, ntree, 0.0f, 0.0f); */
}
/* ***************** generic operator functions for nodes ***************** */
#if 0 /* UNUSED */
static int edit_node_poll(bContext *C)
{
return ED_operator_node_active(C);
}
static void edit_node_properties(wmOperatorType *ot)
{
/* XXX could node be a context pointer? */
RNA_def_string(ot->srna, "node", "", MAX_NAME, "Node", "");
RNA_def_int(ot->srna, "socket", 0, 0, MAX_SOCKET, "Socket", "", 0, MAX_SOCKET);
RNA_def_enum(ot->srna, "in_out", socket_in_out_items, SOCK_IN, "Socket Side", "");
}
static int edit_node_invoke_properties(bContext *C, wmOperator *op)
{
if (!RNA_struct_property_is_set(op->ptr, "node")) {
bNode *node = CTX_data_pointer_get_type(C, "node", &RNA_Node).data;
if (!node)
return 0;
else
RNA_string_set(op->ptr, "node", node->name);
}
if (!RNA_struct_property_is_set(op->ptr, "in_out"))
RNA_enum_set(op->ptr, "in_out", SOCK_IN);
if (!RNA_struct_property_is_set(op->ptr, "socket"))
RNA_int_set(op->ptr, "socket", 0);
return 1;
}
static void edit_node_properties_get(wmOperator *op, bNodeTree *ntree, bNode **rnode, bNodeSocket **rsock, int *rin_out)
{
bNode *node;
bNodeSocket *sock = NULL;
char nodename[MAX_NAME];
int sockindex;
int in_out;
RNA_string_get(op->ptr, "node", nodename);
node = nodeFindNodebyName(ntree, nodename);
in_out = RNA_enum_get(op->ptr, "in_out");
sockindex = RNA_int_get(op->ptr, "socket");
switch (in_out) {
case SOCK_IN: sock = BLI_findlink(&node->inputs, sockindex); break;
case SOCK_OUT: sock = BLI_findlink(&node->outputs, sockindex); break;
}
if (rnode)
*rnode = node;
if (rsock)
*rsock = sock;
if (rin_out)
*rin_out = in_out;
}
#endif
/* ************************** Node generic ************** */
/* is rct in visible part of node? */
static bNode *visible_node(SpaceNode *snode, rctf *rct)
{
bNode *node;
for (node = snode->edittree->nodes.last; node; node = node->prev) {
if (BLI_rctf_isect(&node->totr, rct, NULL))
break;
}
return node;
}
/* **************************** */
typedef struct NodeViewMove {
int mvalo[2];
int xmin, ymin, xmax, ymax;
} NodeViewMove;
static int snode_bg_viewmove_modal(bContext *C, wmOperator *op, wmEvent *event)
{
SpaceNode *snode = CTX_wm_space_node(C);
ARegion *ar = CTX_wm_region(C);
NodeViewMove *nvm = op->customdata;
switch (event->type) {
case MOUSEMOVE:
snode->xof -= (nvm->mvalo[0] - event->mval[0]);
snode->yof -= (nvm->mvalo[1] - event->mval[1]);
nvm->mvalo[0] = event->mval[0];
nvm->mvalo[1] = event->mval[1];
/* prevent dragging image outside of the window and losing it! */
CLAMP(snode->xof, nvm->xmin, nvm->xmax);
CLAMP(snode->yof, nvm->ymin, nvm->ymax);
ED_region_tag_redraw(ar);
break;
case LEFTMOUSE:
case MIDDLEMOUSE:
case RIGHTMOUSE:
MEM_freeN(nvm);
op->customdata = NULL;
return OPERATOR_FINISHED;
}
return OPERATOR_RUNNING_MODAL;
}
static int snode_bg_viewmove_invoke(bContext *C, wmOperator *op, wmEvent *event)
{
SpaceNode *snode = CTX_wm_space_node(C);
ARegion *ar = CTX_wm_region(C);
NodeViewMove *nvm;
Image *ima;
ImBuf *ibuf;
const float pad = 32.0f; /* better be bigger then scrollbars */
void *lock;
ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
if (ibuf == NULL) {
BKE_image_release_ibuf(ima, lock);
return OPERATOR_CANCELLED;
}
nvm = MEM_callocN(sizeof(NodeViewMove), "NodeViewMove struct");
op->customdata = nvm;
nvm->mvalo[0] = event->mval[0];
nvm->mvalo[1] = event->mval[1];
nvm->xmin = -(ar->winx / 2) - (ibuf->x * (0.5f * snode->zoom)) + pad;
nvm->xmax = (ar->winx / 2) + (ibuf->x * (0.5f * snode->zoom)) - pad;
nvm->ymin = -(ar->winy / 2) - (ibuf->y * (0.5f * snode->zoom)) + pad;
nvm->ymax = (ar->winy / 2) + (ibuf->y * (0.5f * snode->zoom)) - pad;
BKE_image_release_ibuf(ima, lock);
/* add modal handler */
WM_event_add_modal_handler(C, op);
return OPERATOR_RUNNING_MODAL;
}
static int snode_bg_viewmove_cancel(bContext *UNUSED(C), wmOperator *op)
{
MEM_freeN(op->customdata);
op->customdata = NULL;
return OPERATOR_CANCELLED;
}
void NODE_OT_backimage_move(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Background Image Move";
ot->description = "Move Node backdrop";
ot->idname = "NODE_OT_backimage_move";
/* api callbacks */
ot->invoke = snode_bg_viewmove_invoke;
ot->modal = snode_bg_viewmove_modal;
ot->poll = composite_node_active;
ot->cancel = snode_bg_viewmove_cancel;
/* flags */
ot->flag = OPTYPE_BLOCKING | OPTYPE_GRAB_POINTER;
}
static int backimage_zoom(bContext *C, wmOperator *op)
{
SpaceNode *snode = CTX_wm_space_node(C);
ARegion *ar = CTX_wm_region(C);
float fac = RNA_float_get(op->ptr, "factor");
snode->zoom *= fac;
ED_region_tag_redraw(ar);
return OPERATOR_FINISHED;
}
void NODE_OT_backimage_zoom(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Background Image Zoom";
ot->idname = "NODE_OT_backimage_zoom";
ot->description = "Zoom in/out the background image";
/* api callbacks */
ot->exec = backimage_zoom;
ot->poll = composite_node_active;
/* flags */
ot->flag = OPTYPE_BLOCKING;
/* internal */
RNA_def_float(ot->srna, "factor", 1.2f, 0.0f, 10.0f, "Factor", "", 0.0f, 10.0f);
}
/******************** sample backdrop operator ********************/
typedef struct ImageSampleInfo {
ARegionType *art;
void *draw_handle;
int x, y;
int channels;
int color_manage;
unsigned char col[4];
float colf[4];
int draw;
} ImageSampleInfo;
static void sample_draw(const bContext *C, ARegion *ar, void *arg_info)
{
Scene *scene = CTX_data_scene(C);
ImageSampleInfo *info = arg_info;
if (info->draw) {
ED_image_draw_info(ar, (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT), info->channels,
info->x, info->y, info->col, info->colf,
NULL, NULL /* zbuf - unused for nodes */
);
}
}
static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
{
SpaceNode *snode = CTX_wm_space_node(C);
ARegion *ar = CTX_wm_region(C);
ImageSampleInfo *info = op->customdata;
void *lock;
Image *ima;
ImBuf *ibuf;
float fx, fy, bufx, bufy;
ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
if (!ibuf) {
info->draw = 0;
return;
}
if (!ibuf->rect) {
if (info->color_manage)
ibuf->profile = IB_PROFILE_LINEAR_RGB;
else
ibuf->profile = IB_PROFILE_NONE;
IMB_rect_from_float(ibuf);
}
/* map the mouse coords to the backdrop image space */
bufx = ibuf->x * snode->zoom;
bufy = ibuf->y * snode->zoom;
fx = (bufx > 0.0f ? ((float)event->mval[0] - 0.5f * ar->winx - snode->xof) / bufx + 0.5f : 0.0f);
fy = (bufy > 0.0f ? ((float)event->mval[1] - 0.5f * ar->winy - snode->yof) / bufy + 0.5f : 0.0f);
if (fx >= 0.0f && fy >= 0.0f && fx < 1.0f && fy < 1.0f) {
float *fp;
char *cp;
int x = (int)(fx * ibuf->x), y = (int)(fy * ibuf->y);
CLAMP(x, 0, ibuf->x - 1);
CLAMP(y, 0, ibuf->y - 1);
info->x = x;
info->y = y;
info->draw = 1;
info->channels = ibuf->channels;
if (ibuf->rect) {
cp = (char *)(ibuf->rect + y * ibuf->x + x);
info->col[0] = cp[0];
info->col[1] = cp[1];
info->col[2] = cp[2];
info->col[3] = cp[3];
info->colf[0] = (float)cp[0] / 255.0f;
info->colf[1] = (float)cp[1] / 255.0f;
info->colf[2] = (float)cp[2] / 255.0f;
info->colf[3] = (float)cp[3] / 255.0f;
}
if (ibuf->rect_float) {
fp = (ibuf->rect_float + (ibuf->channels) * (y * ibuf->x + x));
info->colf[0] = fp[0];
info->colf[1] = fp[1];
info->colf[2] = fp[2];
info->colf[3] = fp[3];
}
ED_node_sample_set(info->colf);
}
else {
info->draw = 0;
ED_node_sample_set(NULL);
}
BKE_image_release_ibuf(ima, lock);
ED_area_tag_redraw(CTX_wm_area(C));
}
static void sample_exit(bContext *C, wmOperator *op)
{
ImageSampleInfo *info = op->customdata;
ED_node_sample_set(NULL);
ED_region_draw_cb_exit(info->art, info->draw_handle);
ED_area_tag_redraw(CTX_wm_area(C));
MEM_freeN(info);
}
static int sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
{
SpaceNode *snode = CTX_wm_space_node(C);
ARegion *ar = CTX_wm_region(C);
ImageSampleInfo *info;
if (snode->treetype != NTREE_COMPOSIT || !(snode->flag & SNODE_BACKDRAW))
return OPERATOR_CANCELLED;
info = MEM_callocN(sizeof(ImageSampleInfo), "ImageSampleInfo");
info->art = ar->type;
info->draw_handle = ED_region_draw_cb_activate(ar->type, sample_draw, info, REGION_DRAW_POST_PIXEL);
op->customdata = info;
sample_apply(C, op, event);
WM_event_add_modal_handler(C, op);
return OPERATOR_RUNNING_MODAL;
}
static int sample_modal(bContext *C, wmOperator *op, wmEvent *event)
{
switch (event->type) {
case LEFTMOUSE:
case RIGHTMOUSE: // XXX hardcoded
sample_exit(C, op);
return OPERATOR_CANCELLED;
case MOUSEMOVE:
sample_apply(C, op, event);
break;
}
return OPERATOR_RUNNING_MODAL;
}
static int sample_cancel(bContext *C, wmOperator *op)
{
sample_exit(C, op);
return OPERATOR_CANCELLED;
}
void NODE_OT_backimage_sample(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Backimage Sample";
ot->idname = "NODE_OT_backimage_sample";
ot->description = "Use mouse to sample background image";
/* api callbacks */
ot->invoke = sample_invoke;
ot->modal = sample_modal;
ot->cancel = sample_cancel;
ot->poll = ED_operator_node_active;
/* flags */
ot->flag = OPTYPE_BLOCKING;
}
/* ********************** size widget operator ******************** */
typedef struct NodeSizeWidget {
float mxstart, mystart;
float oldlocx, oldlocy;
float oldoffsetx, oldoffsety;
float oldwidth, oldheight;
float oldminiwidth;
int directions;
} NodeSizeWidget;
static void node_resize_init(bContext *C, wmOperator *op, wmEvent *UNUSED(event), bNode *node, int dir)
{
SpaceNode *snode = CTX_wm_space_node(C);
NodeSizeWidget *nsw = MEM_callocN(sizeof(NodeSizeWidget), "size widget op data");
op->customdata = nsw;
nsw->mxstart = snode->mx;
nsw->mystart = snode->my;
/* store old */
nsw->oldlocx = node->locx;
nsw->oldlocy = node->locy;
nsw->oldoffsetx = node->offsetx;
nsw->oldoffsety = node->offsety;
nsw->oldwidth = node->width;
nsw->oldheight = node->height;
nsw->oldminiwidth = node->miniwidth;
nsw->directions = dir;
WM_cursor_modal(CTX_wm_window(C), node_get_resize_cursor(dir));
/* add modal handler */
WM_event_add_modal_handler(C, op);
}
static void node_resize_exit(bContext *C, wmOperator *op, int UNUSED(cancel))
{
WM_cursor_restore(CTX_wm_window(C));
MEM_freeN(op->customdata);
op->customdata = NULL;
}
static int node_resize_modal(bContext *C, wmOperator *op, wmEvent *event)
{
SpaceNode *snode = CTX_wm_space_node(C);
ARegion *ar = CTX_wm_region(C);
bNode *node = editnode_get_active(snode->edittree);
NodeSizeWidget *nsw = op->customdata;
float mx, my, dx, dy;
switch (event->type) {
case MOUSEMOVE:
UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &mx, &my);
dx = mx - nsw->mxstart;
dy = my - nsw->mystart;
if (node) {
if (node->flag & NODE_HIDDEN) {
float widthmin = 0.0f;
float widthmax = 100.0f;
if (nsw->directions & NODE_RESIZE_RIGHT) {
node->miniwidth = nsw->oldminiwidth + dx;
CLAMP(node->miniwidth, widthmin, widthmax);
}
if (nsw->directions & NODE_RESIZE_LEFT) {
float locmax = nsw->oldlocx + nsw->oldminiwidth;
node->locx = nsw->oldlocx + dx;
CLAMP(node->locx, locmax - widthmax, locmax - widthmin);
node->miniwidth = locmax - node->locx;
}
}
else {
float widthmin = UI_DPI_FAC * node->typeinfo->minwidth;
float widthmax = UI_DPI_FAC * node->typeinfo->maxwidth;
if (nsw->directions & NODE_RESIZE_RIGHT) {
node->width = nsw->oldwidth + dx;
CLAMP(node->width, widthmin, widthmax);
}
if (nsw->directions & NODE_RESIZE_LEFT) {
float locmax = nsw->oldlocx + nsw->oldwidth;
node->locx = nsw->oldlocx + dx;
CLAMP(node->locx, locmax - widthmax, locmax - widthmin);
node->width = locmax - node->locx;
}
}
/* height works the other way round ... */
{
float heightmin = UI_DPI_FAC * node->typeinfo->minheight;
float heightmax = UI_DPI_FAC * node->typeinfo->maxheight;
if (nsw->directions & NODE_RESIZE_TOP) {
float locmin = nsw->oldlocy - nsw->oldheight;
node->locy = nsw->oldlocy + dy;
CLAMP(node->locy, locmin + heightmin, locmin + heightmax);
node->height = node->locy - locmin;
}
if (nsw->directions & NODE_RESIZE_BOTTOM) {
node->height = nsw->oldheight - dy;
CLAMP(node->height, heightmin, heightmax);
}
}
/* XXX make callback? */
if (node->type == NODE_FRAME) {
/* keep the offset symmetric around center point */
if (nsw->directions & NODE_RESIZE_LEFT) {
node->locx = nsw->oldlocx + 0.5f * dx;
node->offsetx = nsw->oldoffsetx + 0.5f * dx;
}
if (nsw->directions & NODE_RESIZE_RIGHT) {
node->locx = nsw->oldlocx + 0.5f * dx;
node->offsetx = nsw->oldoffsetx - 0.5f * dx;
}
if (nsw->directions & NODE_RESIZE_TOP) {
node->locy = nsw->oldlocy + 0.5f * dy;
node->offsety = nsw->oldoffsety + 0.5f * dy;
}
if (nsw->directions & NODE_RESIZE_BOTTOM) {
node->locy = nsw->oldlocy + 0.5f * dy;
node->offsety = nsw->oldoffsety - 0.5f * dy;
}
}
}
ED_region_tag_redraw(ar);
break;
case LEFTMOUSE:
case MIDDLEMOUSE:
case RIGHTMOUSE:
node_resize_exit(C, op, 0);
ED_node_post_apply_transform(C, snode->edittree);
return OPERATOR_FINISHED;
}
return OPERATOR_RUNNING_MODAL;
}
static int node_resize_invoke(bContext *C, wmOperator *op, wmEvent *event)
{
SpaceNode *snode = CTX_wm_space_node(C);
ARegion *ar = CTX_wm_region(C);
bNode *node = editnode_get_active(snode->edittree);
int dir;
if (node) {
/* convert mouse coordinates to v2d space */
UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1],
&snode->mx, &snode->my);
dir = node->typeinfo->resize_area_func(node, snode->mx, snode->my);
if (dir != 0) {
node_resize_init(C, op, event, node, dir);
return OPERATOR_RUNNING_MODAL;
}
}
return OPERATOR_CANCELLED | OPERATOR_PASS_THROUGH;
}
static int node_resize_cancel(bContext *C, wmOperator *op)
{
node_resize_exit(C, op, 1);
return OPERATOR_CANCELLED;
}
void NODE_OT_resize(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Resize Node";
ot->idname = "NODE_OT_resize";
ot->description = "Resize a node";
/* api callbacks */
ot->invoke = node_resize_invoke;
ot->modal = node_resize_modal;
ot->poll = ED_operator_node_active;
ot->cancel = node_resize_cancel;
/* flags */
ot->flag = OPTYPE_BLOCKING;
}
/* ********************** hidden sockets ******************** */
int node_has_hidden_sockets(bNode *node)
{
bNodeSocket *sock;
for (sock = node->inputs.first; sock; sock = sock->next)
if (sock->flag & SOCK_HIDDEN)
return 1;
for (sock = node->outputs.first; sock; sock = sock->next)
if (sock->flag & SOCK_HIDDEN)
return 1;
return 0;
}
void node_set_hidden_sockets(SpaceNode *snode, bNode *node, int set)
{
bNodeSocket *sock;
if (set == 0) {
for (sock = node->inputs.first; sock; sock = sock->next)
sock->flag &= ~SOCK_HIDDEN;
for (sock = node->outputs.first; sock; sock = sock->next)
sock->flag &= ~SOCK_HIDDEN;
}
else {
/* hide unused sockets */
for (sock = node->inputs.first; sock; sock = sock->next) {
if (sock->link == NULL)
sock->flag |= SOCK_HIDDEN;
}
for (sock = node->outputs.first; sock; sock = sock->next) {
if (nodeCountSocketLinks(snode->edittree, sock) == 0)
sock->flag |= SOCK_HIDDEN;
}
}
}
/* return 0, nothing done */
static int UNUSED_FUNCTION(node_mouse_groupheader) (SpaceNode * snode)
{
bNode *gnode;
float mx = 0, my = 0;
// XXX int mval[2];
gnode = node_tree_get_editgroup(snode->nodetree);
if (gnode == NULL) return 0;
// XXX getmouseco_areawin(mval);
// XXX areamouseco_to_ipoco(G.v2d, mval, &mx, &my);
/* click in header or outside? */
if (BLI_in_rctf(&gnode->totr, mx, my) == 0) {
rctf rect = gnode->totr;
rect.ymax += NODE_DY;
if (BLI_in_rctf(&rect, mx, my) == 0)
snode_make_group_editable(snode, NULL); /* toggles, so exits editmode */
// else
// XXX transform_nodes(snode->nodetree, 'g', "Move group");
return 1;
}
return 0;
}
/* checks snode->mouse position, and returns found node/socket */
/* type is SOCK_IN and/or SOCK_OUT */
int node_find_indicated_socket(SpaceNode *snode, bNode **nodep, bNodeSocket **sockp, int in_out)
{
bNode *node;
bNodeSocket *sock;
rctf rect;
*nodep = NULL;
*sockp = NULL;
/* check if we click in a socket */
for (node = snode->edittree->nodes.first; node; node = node->next) {
rect.xmin = snode->mx - (NODE_SOCKSIZE + 4);
rect.ymin = snode->my - (NODE_SOCKSIZE + 4);
rect.xmax = snode->mx + (NODE_SOCKSIZE + 4);
rect.ymax = snode->my + (NODE_SOCKSIZE + 4);
if (!(node->flag & NODE_HIDDEN)) {
/* extra padding inside and out - allow dragging on the text areas too */
if (in_out == SOCK_IN) {
rect.xmax += NODE_SOCKSIZE;
rect.xmin -= NODE_SOCKSIZE * 4;
}
else if (in_out == SOCK_OUT) {
rect.xmax += NODE_SOCKSIZE * 4;
rect.xmin -= NODE_SOCKSIZE;
}
}
if (in_out & SOCK_IN) {
for (sock = node->inputs.first; sock; sock = sock->next) {
if (!nodeSocketIsHidden(sock)) {
if (BLI_in_rctf(&rect, sock->locx, sock->locy)) {
if (node == visible_node(snode, &rect)) {
*nodep = node;
*sockp = sock;
return 1;
}
}
}
}
}
if (in_out & SOCK_OUT) {
for (sock = node->outputs.first; sock; sock = sock->next) {
if (!nodeSocketIsHidden(sock)) {
if (BLI_in_rctf(&rect, sock->locx, sock->locy)) {
if (node == visible_node(snode, &rect)) {
*nodep = node;
*sockp = sock;
return 1;
}
}
}
}
}
}
/* check group sockets
* NB: using ngroup->outputs as input sockets and vice versa here!
*/
if (in_out & SOCK_IN) {
for (sock = snode->edittree->outputs.first; sock; sock = sock->next) {
if (!nodeSocketIsHidden(sock)) {
if (BLI_in_rctf(&rect, sock->locx, sock->locy)) {
*nodep = NULL; /* NULL node pointer indicates group socket */
*sockp = sock;
return 1;
}
}
}
}
if (in_out & SOCK_OUT) {
for (sock = snode->edittree->inputs.first; sock; sock = sock->next) {
if (!nodeSocketIsHidden(sock)) {
if (BLI_in_rctf(&rect, sock->locx, sock->locy)) {
*nodep = NULL; /* NULL node pointer indicates group socket */
*sockp = sock;
return 1;
}
}
}
}
return 0;
}
/* ****************** Duplicate *********************** */
static void node_duplicate_reparent_recursive(bNode *node)
{
bNode *parent;
node->flag |= NODE_TEST;
/* find first selected parent */
for (parent = node->parent; parent; parent = parent->parent) {
if (parent->flag & SELECT) {
if (!(parent->flag & NODE_TEST))
node_duplicate_reparent_recursive(parent);
break;
}
}
/* reparent node copy to parent copy */
if (parent) {
nodeDetachNode(node->new_node);
nodeAttachNode(node->new_node, parent->new_node);
}
}
static int node_duplicate_exec(bContext *C, wmOperator *op)
{
SpaceNode *snode = CTX_wm_space_node(C);
bNodeTree *ntree = snode->edittree;
bNode *node, *newnode, *lastnode;
bNodeLink *link, *newlink, *lastlink;
int keep_inputs = RNA_boolean_get(op->ptr, "keep_inputs");
ED_preview_kill_jobs(C);
lastnode = ntree->nodes.last;
for (node = ntree->nodes.first; node; node = node->next) {
if (node->flag & SELECT) {
newnode = nodeCopyNode(ntree, node);
if (newnode->id) {
/* simple id user adjustment, node internal functions don't touch this
* but operators and readfile.c do. */
id_us_plus(newnode->id);
/* to ensure redraws or rerenders happen */
ED_node_changed_update(snode->id, newnode);
}
}
/* make sure we don't copy new nodes again! */
if (node == lastnode)
break;
}
/* copy links between selected nodes
* NB: this depends on correct node->new_node and sock->new_sock pointers from above copy!
*/
lastlink = ntree->links.last;
for (link = ntree->links.first; link; link = link->next) {
/* This creates new links between copied nodes.
* If keep_inputs is set, also copies input links from unselected (when fromnode==NULL)!
*/
if (link->tonode && (link->tonode->flag & NODE_SELECT) &&
(keep_inputs || (link->fromnode && (link->fromnode->flag & NODE_SELECT))))
{
newlink = MEM_callocN(sizeof(bNodeLink), "bNodeLink");
newlink->flag = link->flag;
newlink->tonode = link->tonode->new_node;
newlink->tosock = link->tosock->new_sock;
if (link->fromnode && (link->fromnode->flag & NODE_SELECT)) {
newlink->fromnode = link->fromnode->new_node;
newlink->fromsock = link->fromsock->new_sock;
}
else {
/* input node not copied, this keeps the original input linked */
newlink->fromnode = link->fromnode;
newlink->fromsock = link->fromsock;
}
BLI_addtail(&ntree->links, newlink);
}
/* make sure we don't copy new links again! */
if (link == lastlink)
break;
}
/* clear flags for recursive depth-first iteration */
for (node = ntree->nodes.first; node; node = node->next)
node->flag &= ~NODE_TEST;
/* reparent copied nodes */
for (node = ntree->nodes.first; node; node = node->next) {
if ((node->flag & SELECT) && !(node->flag & NODE_TEST))
node_duplicate_reparent_recursive(node);
/* only has to check old nodes */
if (node == lastnode)
break;
}
/* deselect old nodes, select the copies instead */
for (node = ntree->nodes.first; node; node = node->next) {
if (node->flag & SELECT) {
/* has been set during copy above */
newnode = node->new_node;
node_deselect(node);
node->flag &= ~NODE_ACTIVE;
node_select(newnode);
}
/* make sure we don't copy new nodes again! */
if (node == lastnode)
break;
}
ntreeUpdateTree(snode->edittree);
snode_notify(C, snode);
snode_dag_update(C, snode);
return OPERATOR_FINISHED;
}
void NODE_OT_duplicate(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Duplicate Nodes";
ot->description = "Duplicate selected nodes";
ot->idname = "NODE_OT_duplicate";
/* api callbacks */
ot->exec = node_duplicate_exec;
ot->poll = ED_operator_node_active;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
RNA_def_boolean(ot->srna, "keep_inputs", 0, "Keep Inputs", "Keep the input links to duplicated nodes");
}
int ED_node_select_check(ListBase *lb)
{
bNode *node;
for (node = lb->first; node; node = node->next) {
if (node->flag & NODE_SELECT) {
return TRUE;
}
}
return FALSE;
}
/* ******************************** */
// XXX some code needing updating to operators...
/* goes over all scenes, reads render layers */
static int node_read_renderlayers_exec(bContext *C, wmOperator *UNUSED(op))
{
Main *bmain = CTX_data_main(C);
SpaceNode *snode = CTX_wm_space_node(C);
Scene *curscene = CTX_data_scene(C), *scene;
bNode *node;
ED_preview_kill_jobs(C);
/* first tag scenes unread */
for (scene = bmain->scene.first; scene; scene = scene->id.next)
scene->id.flag |= LIB_DOIT;
for (node = snode->edittree->nodes.first; node; node = node->next) {
if (node->type == CMP_NODE_R_LAYERS) {
ID *id = node->id;
if (id->flag & LIB_DOIT) {
RE_ReadRenderResult(curscene, (Scene *)id);
ntreeCompositTagRender((Scene *)id);
id->flag &= ~LIB_DOIT;
}
}
}
snode_notify(C, snode);
snode_dag_update(C, snode);
return OPERATOR_FINISHED;
}
void NODE_OT_read_renderlayers(wmOperatorType *ot)
{
ot->name = "Read Render Layers";
ot->idname = "NODE_OT_read_renderlayers";
ot->description = "Read all render layers of all used scenes";
ot->exec = node_read_renderlayers_exec;
ot->poll = composite_node_active;
/* flags */
ot->flag = 0;
}
static int node_read_fullsamplelayers_exec(bContext *C, wmOperator *UNUSED(op))
{
Main *bmain = CTX_data_main(C);
SpaceNode *snode = CTX_wm_space_node(C);
Scene *curscene = CTX_data_scene(C);
Render *re = RE_NewRender(curscene->id.name);
WM_cursor_wait(1);
RE_MergeFullSample(re, bmain, curscene, snode->nodetree);
WM_cursor_wait(0);
/* note we are careful to send the right notifier, as otherwise the
* compositor would reexecute and overwrite the full sample result */
WM_event_add_notifier(C, NC_SCENE | ND_COMPO_RESULT, NULL);
return OPERATOR_FINISHED;
}
void NODE_OT_read_fullsamplelayers(wmOperatorType *ot)
{
ot->name = "Read Full Sample Layers";
ot->idname = "NODE_OT_read_fullsamplelayers";
ot->description = "Read all render layers of current scene, in full sample";
ot->exec = node_read_fullsamplelayers_exec;
ot->poll = composite_node_active;
/* flags */
ot->flag = 0;
}
int node_render_changed_exec(bContext *C, wmOperator *UNUSED(op))
{
Scene *sce = CTX_data_scene(C);
bNode *node;
for (node = sce->nodetree->nodes.first; node; node = node->next) {
if (node->id == (ID *)sce && node->need_exec) {
break;
}
}
if (node) {
SceneRenderLayer *srl = BLI_findlink(&sce->r.layers, node->custom1);
if (srl) {
PointerRNA op_ptr;
WM_operator_properties_create(&op_ptr, "RENDER_OT_render");
RNA_string_set(&op_ptr, "layer", srl->name);
RNA_string_set(&op_ptr, "scene", sce->id.name + 2);
/* to keep keypositions */
sce->r.scemode |= R_NO_FRAME_UPDATE;
WM_operator_name_call(C, "RENDER_OT_render", WM_OP_INVOKE_DEFAULT, &op_ptr);
WM_operator_properties_free(&op_ptr);
return OPERATOR_FINISHED;
}
}
return OPERATOR_CANCELLED;
}
void NODE_OT_render_changed(wmOperatorType *ot)
{
ot->name = "Render Changed Layer";
ot->idname = "NODE_OT_render_changed";
ot->description = "Render current scene, when input node's layer has been changed";
ot->exec = node_render_changed_exec;
ot->poll = composite_node_active;
/* flags */
ot->flag = 0;
}
/* ****************** Hide operator *********************** */
static void node_flag_toggle_exec(SpaceNode *snode, int toggle_flag)
{
bNode *node;
int tot_eq = 0, tot_neq = 0;
/* Toggles the flag on all selected nodes.
* If the flag is set on all nodes it is unset.
* If the flag is not set on all nodes, it is set.
*/
for (node = snode->edittree->nodes.first; node; node = node->next) {
if (node->flag & SELECT) {
if (toggle_flag == NODE_PREVIEW && (node->typeinfo->flag & NODE_PREVIEW) == 0)
continue;
if (toggle_flag == NODE_OPTIONS && (node->typeinfo->flag & NODE_OPTIONS) == 0)
continue;
if (node->flag & toggle_flag)
tot_eq++;
else
tot_neq++;
}
}
for (node = snode->edittree->nodes.first; node; node = node->next) {
if (node->flag & SELECT) {
if (toggle_flag == NODE_PREVIEW && (node->typeinfo->flag & NODE_PREVIEW) == 0)
continue;
if (toggle_flag == NODE_OPTIONS && (node->typeinfo->flag & NODE_OPTIONS) == 0)
continue;
if ( (tot_eq && tot_neq) || tot_eq == 0)
node->flag |= toggle_flag;
else
node->flag &= ~toggle_flag;
}
}
}
static int node_hide_toggle_exec(bContext *C, wmOperator *UNUSED(op))
{
SpaceNode *snode = CTX_wm_space_node(C);
/* sanity checking (poll callback checks this already) */
if ((snode == NULL) || (snode->edittree == NULL))
return OPERATOR_CANCELLED;
node_flag_toggle_exec(snode, NODE_HIDDEN);
WM_event_add_notifier(C, NC_NODE | ND_DISPLAY, NULL);
return OPERATOR_FINISHED;
}
void NODE_OT_hide_toggle(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Hide";
ot->description = "Toggle hiding of selected nodes";
ot->idname = "NODE_OT_hide_toggle";
/* callbacks */
ot->exec = node_hide_toggle_exec;
ot->poll = ED_operator_node_active;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
static int node_preview_toggle_exec(bContext *C, wmOperator *UNUSED(op))
{
SpaceNode *snode = CTX_wm_space_node(C);
/* sanity checking (poll callback checks this already) */
if ((snode == NULL) || (snode->edittree == NULL))
return OPERATOR_CANCELLED;
ED_preview_kill_jobs(C);
node_flag_toggle_exec(snode, NODE_PREVIEW);
snode_notify(C, snode);
return OPERATOR_FINISHED;
}
void NODE_OT_preview_toggle(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Toggle Node Preview";
ot->description = "Toggle preview display for selected nodes";
ot->idname = "NODE_OT_preview_toggle";
/* callbacks */
ot->exec = node_preview_toggle_exec;
ot->poll = ED_operator_node_active;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
static int node_options_toggle_exec(bContext *C, wmOperator *UNUSED(op))
{
SpaceNode *snode = CTX_wm_space_node(C);
/* sanity checking (poll callback checks this already) */
if ((snode == NULL) || (snode->edittree == NULL))
return OPERATOR_CANCELLED;
node_flag_toggle_exec(snode, NODE_OPTIONS);
WM_event_add_notifier(C, NC_NODE | ND_DISPLAY, NULL);
return OPERATOR_FINISHED;
}
void NODE_OT_options_toggle(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Toggle Node Options";
ot->description = "Toggle option buttons display for selected nodes";
ot->idname = "NODE_OT_options_toggle";
/* callbacks */
ot->exec = node_options_toggle_exec;
ot->poll = ED_operator_node_active;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
static int node_socket_toggle_exec(bContext *C, wmOperator *UNUSED(op))
{
SpaceNode *snode = CTX_wm_space_node(C);
bNode *node;
int hidden;
/* sanity checking (poll callback checks this already) */
if ((snode == NULL) || (snode->edittree == NULL))
return OPERATOR_CANCELLED;
ED_preview_kill_jobs(C);
/* Toggle for all selected nodes */
hidden = 0;
for (node = snode->edittree->nodes.first; node; node = node->next) {
if (node->flag & SELECT) {
if (node_has_hidden_sockets(node)) {
hidden = 1;
break;
}
}
}
for (node = snode->edittree->nodes.first; node; node = node->next) {
if (node->flag & SELECT) {
node_set_hidden_sockets(snode, node, !hidden);
}
}
ntreeUpdateTree(snode->edittree);
WM_event_add_notifier(C, NC_NODE | ND_DISPLAY, NULL);
return OPERATOR_FINISHED;
}
void NODE_OT_hide_socket_toggle(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Toggle Hidden Node Sockets";
ot->description = "Toggle unused node socket display";
ot->idname = "NODE_OT_hide_socket_toggle";
/* callbacks */
ot->exec = node_socket_toggle_exec;
ot->poll = ED_operator_node_active;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/* ****************** Mute operator *********************** */
static int node_mute_exec(bContext *C, wmOperator *UNUSED(op))
{
SpaceNode *snode = CTX_wm_space_node(C);
bNode *node;
ED_preview_kill_jobs(C);
for (node = snode->edittree->nodes.first; node; node = node->next) {
/* Only allow muting of nodes having a mute func! */
if ((node->flag & SELECT) && node->typeinfo->internal_connect) {
node->flag ^= NODE_MUTED;
snode_update(snode, node);
}
}
snode_notify(C, snode);
snode_dag_update(C, snode);
return OPERATOR_FINISHED;
}
void NODE_OT_mute_toggle(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Toggle Node Mute";
ot->description = "Toggle muting of the nodes";
ot->idname = "NODE_OT_mute_toggle";
/* callbacks */
ot->exec = node_mute_exec;
ot->poll = ED_operator_node_active;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/* ****************** Delete operator ******************* */
static int node_delete_exec(bContext *C, wmOperator *UNUSED(op))
{
SpaceNode *snode = CTX_wm_space_node(C);
bNode *node, *next;
ED_preview_kill_jobs(C);
for (node = snode->edittree->nodes.first; node; node = next) {
next = node->next;
if (node->flag & SELECT) {
/* check id user here, nodeFreeNode is called for free dbase too */
if (node->id)
node->id->us--;
nodeFreeNode(snode->edittree, node);
}
}
ntreeUpdateTree(snode->edittree);
snode_notify(C, snode);
snode_dag_update(C, snode);
return OPERATOR_FINISHED;
}
void NODE_OT_delete(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Delete";
ot->description = "Delete selected nodes";
ot->idname = "NODE_OT_delete";
/* api callbacks */
ot->exec = node_delete_exec;
ot->poll = ED_operator_node_active;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/* ****************** Delete with reconnect ******************* */
static int node_delete_reconnect_exec(bContext *C, wmOperator *UNUSED(op))
{
SpaceNode *snode = CTX_wm_space_node(C);
bNode *node, *next;
ED_preview_kill_jobs(C);
for (node = snode->edittree->nodes.first; node; node = next) {
next = node->next;
if (node->flag & SELECT) {
nodeInternalRelink(snode->edittree, node);
/* check id user here, nodeFreeNode is called for free dbase too */
if (node->id)
node->id->us--;
nodeFreeNode(snode->edittree, node);
}
}
ntreeUpdateTree(snode->edittree);
snode_notify(C, snode);
snode_dag_update(C, snode);
return OPERATOR_FINISHED;
}
void NODE_OT_delete_reconnect(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Delete with reconnect";
ot->description = "Delete nodes; will reconnect nodes as if deletion was muted";
ot->idname = "NODE_OT_delete_reconnect";
/* api callbacks */
ot->exec = node_delete_reconnect_exec;
ot->poll = ED_operator_node_active;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/* ****************** File Output Add Socket ******************* */
static int node_output_file_add_socket_exec(bContext *C, wmOperator *op)
{
Scene *scene = CTX_data_scene(C);
SpaceNode *snode = CTX_wm_space_node(C);
PointerRNA ptr;
bNodeTree *ntree;
bNode *node;
char file_path[MAX_NAME];
ptr = CTX_data_pointer_get(C, "node");
if (!ptr.data)
return OPERATOR_CANCELLED;
node = ptr.data;
ntree = ptr.id.data;
RNA_string_get(op->ptr, "file_path", file_path);
ntreeCompositOutputFileAddSocket(ntree, node, file_path, &scene->r.im_format);
snode_notify(C, snode);
return OPERATOR_FINISHED;
}
void NODE_OT_output_file_add_socket(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Add File Node Socket";
ot->description = "Add a new input to a file output node";
ot->idname = "NODE_OT_output_file_add_socket";
/* callbacks */
ot->exec = node_output_file_add_socket_exec;
ot->poll = composite_node_active;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
RNA_def_string(ot->srna, "file_path", "Image", MAX_NAME, "File Path", "Sub-path of the output file");
}
/* ****************** Multi File Output Remove Socket ******************* */
static int node_output_file_remove_active_socket_exec(bContext *C, wmOperator *UNUSED(op))
{
SpaceNode *snode = CTX_wm_space_node(C);
PointerRNA ptr = CTX_data_pointer_get(C, "node");
bNodeTree *ntree;
bNode *node;
if (!ptr.data)
return OPERATOR_CANCELLED;
node = ptr.data;
ntree = ptr.id.data;
if (!ntreeCompositOutputFileRemoveActiveSocket(ntree, node))
return OPERATOR_CANCELLED;
snode_notify(C, snode);
return OPERATOR_FINISHED;
}
void NODE_OT_output_file_remove_active_socket(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Remove File Node Socket";
ot->description = "Remove active input from a file output node";
ot->idname = "NODE_OT_output_file_remove_active_socket";
/* callbacks */
ot->exec = node_output_file_remove_active_socket_exec;
ot->poll = composite_node_active;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/* ****************** Multi File Output Move Socket ******************* */
static int node_output_file_move_active_socket_exec(bContext *C, wmOperator *op)
{
SpaceNode *snode = CTX_wm_space_node(C);
PointerRNA ptr = CTX_data_pointer_get(C, "node");
bNode *node;
NodeImageMultiFile *nimf;
bNodeSocket *sock;
int direction;
if (!ptr.data)
return OPERATOR_CANCELLED;
node = ptr.data;
nimf = node->storage;
sock = BLI_findlink(&node->inputs, nimf->active_input);
if (!sock)
return OPERATOR_CANCELLED;
direction = RNA_enum_get(op->ptr, "direction");
if (direction == 1) {
bNodeSocket *before = sock->prev;
if (!before)
return OPERATOR_CANCELLED;
BLI_remlink(&node->inputs, sock);
BLI_insertlinkbefore(&node->inputs, before, sock);
--nimf->active_input;
}
else {
bNodeSocket *after = sock->next;
if (!after)
return OPERATOR_CANCELLED;
BLI_remlink(&node->inputs, sock);
BLI_insertlinkafter(&node->inputs, after, sock);
++nimf->active_input;
}
snode_notify(C, snode);
return OPERATOR_FINISHED;
}
void NODE_OT_output_file_move_active_socket(wmOperatorType *ot)
{
static EnumPropertyItem direction_items[] = {
{1, "UP", 0, "Up", ""},
{2, "DOWN", 0, "Down", ""},
{ 0, NULL, 0, NULL, NULL }
};
/* identifiers */
ot->name = "Move File Node Socket";
ot->description = "Move the active input of a file output node up or down the list";
ot->idname = "NODE_OT_output_file_move_active_socket";
/* callbacks */
ot->exec = node_output_file_move_active_socket_exec;
ot->poll = composite_node_active;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
RNA_def_enum(ot->srna, "direction", direction_items, 2, "Direction", "");
}
/* ****************** Copy Node Color ******************* */
static int node_copy_color_exec(bContext *C, wmOperator *UNUSED(op))
{
SpaceNode *snode = CTX_wm_space_node(C);
bNodeTree *ntree = snode->edittree;
bNode *node, *tnode;
if (!ntree)
return OPERATOR_CANCELLED;
node = nodeGetActive(ntree);
if (!node)
return OPERATOR_CANCELLED;
for (tnode = ntree->nodes.first; tnode; tnode = tnode->next) {
if (tnode->flag & NODE_SELECT && tnode != node) {
if (node->flag & NODE_CUSTOM_COLOR) {
tnode->flag |= NODE_CUSTOM_COLOR;
copy_v3_v3(tnode->color, node->color);
}
else
tnode->flag &= ~NODE_CUSTOM_COLOR;
}
}
ED_node_sort(ntree);
WM_event_add_notifier(C, NC_NODE | ND_DISPLAY, NULL);
return OPERATOR_FINISHED;
}
void NODE_OT_node_copy_color(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Copy Color";
ot->description = "Copy color to all selected nodes";
ot->idname = "NODE_OT_node_copy_color";
/* api callbacks */
ot->exec = node_copy_color_exec;
ot->poll = ED_operator_node_active;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
|