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

render_shading.c « render « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6c996bcbd677b588e3095477c2bc5a5f24686ab6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
/*
 * ***** 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. 
 *
 * 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) 2009 Blender Foundation.
 * All rights reserved.
 *
 * Contributor(s): Blender Foundation
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/editors/render/render_shading.c
 *  \ingroup edrend
 */

#include <stdlib.h>
#include <string.h>

#include "MEM_guardedalloc.h"

#include "DNA_curve_types.h"
#include "DNA_lamp_types.h"
#include "DNA_material_types.h"
#include "DNA_node_types.h"
#include "DNA_object_types.h"
#include "DNA_particle_types.h"
#include "DNA_scene_types.h"
#include "DNA_space_types.h"
#include "DNA_world_types.h"

#include "BLI_blenlib.h"
#include "BLI_utildefines.h"

#include "BLF_translation.h"

#include "BKE_animsys.h"
#include "BKE_context.h"
#include "BKE_curve.h"
#include "BKE_depsgraph.h"
#include "BKE_font.h"
#include "BKE_freestyle.h"
#include "BKE_global.h"
#include "BKE_image.h"
#include "BKE_library.h"
#include "BKE_linestyle.h"
#include "BKE_main.h"
#include "BKE_material.h"
#include "BKE_paint.h"
#include "BKE_report.h"
#include "BKE_scene.h"
#include "BKE_texture.h"
#include "BKE_world.h"
#include "BKE_editmesh.h"

#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"

#include "GPU_material.h"

#ifdef WITH_FREESTYLE
#  include "FRS_freestyle.h"
#endif

#include "RNA_access.h"
#include "RNA_enum_types.h"

#include "WM_api.h"
#include "WM_types.h"

#include "ED_object.h"
#include "ED_curve.h"
#include "ED_mesh.h"
#include "ED_node.h"
#include "ED_render.h"
#include "ED_screen.h"

#include "RNA_define.h"

#include "UI_interface.h"

#include "RE_pipeline.h"

#include "render_intern.h"  // own include

/********************** material slot operators *********************/

static int material_slot_add_exec(bContext *C, wmOperator *UNUSED(op))
{
	Object *ob = ED_object_context(C);

	if (!ob)
		return OPERATOR_CANCELLED;
	
	object_add_material_slot(ob);

	if (ob->mode & OB_MODE_TEXTURE_PAINT) {
		Scene *scene = CTX_data_scene(C);
		BKE_paint_proj_mesh_data_check(scene, ob, NULL, NULL, NULL, NULL);
		WM_event_add_notifier(C, NC_SCENE | ND_TOOLSETTINGS, NULL);
	}
	
	WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
	WM_event_add_notifier(C, NC_OBJECT | ND_OB_SHADING, ob);
	WM_event_add_notifier(C, NC_MATERIAL | ND_SHADING_PREVIEW, ob);
	
	return OPERATOR_FINISHED;
}

void OBJECT_OT_material_slot_add(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Add Material Slot";
	ot->idname = "OBJECT_OT_material_slot_add";
	ot->description = "Add a new material slot";
	
	/* api callbacks */
	ot->exec = material_slot_add_exec;
	ot->poll = ED_operator_object_active_editable;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
}

static int material_slot_remove_exec(bContext *C, wmOperator *op)
{
	Object *ob = ED_object_context(C);

	if (!ob)
		return OPERATOR_CANCELLED;

	/* Removing material slots in edit mode screws things up, see bug #21822.*/
	if (ob == CTX_data_edit_object(C)) {
		BKE_report(op->reports, RPT_ERROR, "Unable to remove material slot in edit mode");
		return OPERATOR_CANCELLED;
	}
	
	object_remove_material_slot(ob);

	if (ob->mode & OB_MODE_TEXTURE_PAINT) {
		Scene *scene = CTX_data_scene(C);
		BKE_paint_proj_mesh_data_check(scene, ob, NULL, NULL, NULL, NULL);
		WM_event_add_notifier(C, NC_SCENE | ND_TOOLSETTINGS, NULL);
	}
	
	DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
	WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
	WM_event_add_notifier(C, NC_OBJECT | ND_OB_SHADING, ob);
	WM_event_add_notifier(C, NC_MATERIAL | ND_SHADING_PREVIEW, ob);
	
	return OPERATOR_FINISHED;
}

void OBJECT_OT_material_slot_remove(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Remove Material Slot";
	ot->idname = "OBJECT_OT_material_slot_remove";
	ot->description = "Remove the selected material slot";
	
	/* api callbacks */
	ot->exec = material_slot_remove_exec;
	ot->poll = ED_operator_object_active_editable;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
}

static int material_slot_assign_exec(bContext *C, wmOperator *UNUSED(op))
{
	Object *ob = ED_object_context(C);

	if (!ob)
		return OPERATOR_CANCELLED;

	if (ob && ob->actcol > 0) {
		if (ob->type == OB_MESH) {
			BMEditMesh *em = BKE_editmesh_from_object(ob);
			BMFace *efa;
			BMIter iter;

			if (em) {
				BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
					if (BM_elem_flag_test(efa, BM_ELEM_SELECT))
						efa->mat_nr = ob->actcol - 1;
				}
			}
		}
		else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
			Nurb *nu;
			ListBase *nurbs = BKE_curve_editNurbs_get((Curve *)ob->data);

			if (nurbs) {
				for (nu = nurbs->first; nu; nu = nu->next)
					if (isNurbsel(nu))
						nu->mat_nr = nu->charidx = ob->actcol - 1;
			}
		}
		else if (ob->type == OB_FONT) {
			EditFont *ef = ((Curve *)ob->data)->editfont;
			int i, selstart, selend;

			if (ef && BKE_vfont_select_get(ob, &selstart, &selend)) {
				for (i = selstart; i <= selend; i++)
					ef->textbufinfo[i].mat_nr = ob->actcol;
			}
		}
	}

	DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
	WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
	
	return OPERATOR_FINISHED;
}

void OBJECT_OT_material_slot_assign(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Assign Material Slot";
	ot->idname = "OBJECT_OT_material_slot_assign";
	ot->description = "Assign active material slot to selection";
	
	/* api callbacks */
	ot->exec = material_slot_assign_exec;
	ot->poll = ED_operator_object_active_editable;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
}

static int material_slot_de_select(bContext *C, bool select)
{
	Object *ob = ED_object_context(C);

	if (!ob)
		return OPERATOR_CANCELLED;

	if (ob->type == OB_MESH) {
		BMEditMesh *em = BKE_editmesh_from_object(ob);

		if (em) {
			EDBM_deselect_by_material(em, ob->actcol - 1, select);
		}
	}
	else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
		ListBase *nurbs = BKE_curve_editNurbs_get((Curve *)ob->data);
		Nurb *nu;
		BPoint *bp;
		BezTriple *bezt;
		int a;

		if (nurbs) {
			for (nu = nurbs->first; nu; nu = nu->next) {
				if (nu->mat_nr == ob->actcol - 1) {
					if (nu->bezt) {
						a = nu->pntsu;
						bezt = nu->bezt;
						while (a--) {
							if (bezt->hide == 0) {
								if (select) {
									bezt->f1 |= SELECT;
									bezt->f2 |= SELECT;
									bezt->f3 |= SELECT;
								}
								else {
									bezt->f1 &= ~SELECT;
									bezt->f2 &= ~SELECT;
									bezt->f3 &= ~SELECT;
								}
							}
							bezt++;
						}
					}
					else if (nu->bp) {
						a = nu->pntsu * nu->pntsv;
						bp = nu->bp;
						while (a--) {
							if (bp->hide == 0) {
								if (select) bp->f1 |= SELECT;
								else bp->f1 &= ~SELECT;
							}
							bp++;
						}
					}
				}
			}
		}
	}

	WM_event_add_notifier(C, NC_GEOM | ND_SELECT, ob->data);

	return OPERATOR_FINISHED;
}

static int material_slot_select_exec(bContext *C, wmOperator *UNUSED(op))
{
	return material_slot_de_select(C, true);
}

void OBJECT_OT_material_slot_select(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Select Material Slot";
	ot->idname = "OBJECT_OT_material_slot_select";
	ot->description = "Select by active material slot";
	
	/* api callbacks */
	ot->exec = material_slot_select_exec;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
}

static int material_slot_deselect_exec(bContext *C, wmOperator *UNUSED(op))
{
	return material_slot_de_select(C, false);
}

void OBJECT_OT_material_slot_deselect(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Deselect Material Slot";
	ot->idname = "OBJECT_OT_material_slot_deselect";
	ot->description = "Deselect by active material slot";
	
	/* api callbacks */
	ot->exec = material_slot_deselect_exec;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
}


static int material_slot_copy_exec(bContext *C, wmOperator *UNUSED(op))
{
	Object *ob = ED_object_context(C);
	Material ***matar;

	if (!ob || !(matar = give_matarar(ob)))
		return OPERATOR_CANCELLED;

	CTX_DATA_BEGIN (C, Object *, ob_iter, selected_editable_objects)
	{
		if (ob != ob_iter && give_matarar(ob_iter)) {
			if (ob->data != ob_iter->data)
				assign_matarar(ob_iter, matar, ob->totcol);
			
			if (ob_iter->totcol == ob->totcol) {
				ob_iter->actcol = ob->actcol;
				DAG_id_tag_update(&ob_iter->id, OB_RECALC_DATA);
				WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob_iter);
			}
		}
	}
	CTX_DATA_END;

	return OPERATOR_FINISHED;
}


void OBJECT_OT_material_slot_copy(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Copy Material to Others";
	ot->idname = "OBJECT_OT_material_slot_copy";
	ot->description = "Copies materials to other selected objects";

	/* api callbacks */
	ot->exec = material_slot_copy_exec;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
}

/********************** new material operator *********************/

static int new_material_exec(bContext *C, wmOperator *UNUSED(op))
{
	Scene *scene = CTX_data_scene(C);
	Material *ma = CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
	Main *bmain = CTX_data_main(C);
	PointerRNA ptr, idptr;
	PropertyRNA *prop;

	/* add or copy material */
	if (ma) {
		ma = BKE_material_copy(ma);
	}
	else {
		ma = BKE_material_add(bmain, DATA_("Material"));

		if (BKE_scene_use_new_shading_nodes(scene)) {
			ED_node_shader_default(C, &ma->id);
			ma->use_nodes = true;
		}
	}

	/* hook into UI */
	uiIDContextProperty(C, &ptr, &prop);

	if (prop) {
		/* when creating new ID blocks, use is already 1, but RNA
		 * pointer se also increases user, so this compensates it */
		ma->id.us--;

		RNA_id_pointer_create(&ma->id, &idptr);
		RNA_property_pointer_set(&ptr, prop, idptr);
		RNA_property_update(C, &ptr, prop);
	}

	WM_event_add_notifier(C, NC_MATERIAL | NA_ADDED, ma);
	
	return OPERATOR_FINISHED;
}

void MATERIAL_OT_new(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "New Material";
	ot->idname = "MATERIAL_OT_new";
	ot->description = "Add a new material";
	
	/* api callbacks */
	ot->exec = new_material_exec;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
}

/********************** new texture operator *********************/

static int new_texture_exec(bContext *C, wmOperator *UNUSED(op))
{
	Tex *tex = CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
	Main *bmain = CTX_data_main(C);
	PointerRNA ptr, idptr;
	PropertyRNA *prop;

	/* add or copy texture */
	if (tex) {
		tex = BKE_texture_copy(tex);
	}
	else {
		tex = add_texture(bmain, DATA_("Texture"));
	}

	/* hook into UI */
	uiIDContextProperty(C, &ptr, &prop);

	if (prop) {
		/* when creating new ID blocks, use is already 1, but RNA
		 * pointer se also increases user, so this compensates it */
		tex->id.us--;

		if (ptr.id.data && GS(((ID *)ptr.id.data)->name) == ID_MA &&
		    RNA_property_pointer_get(&ptr, prop).id.data == NULL)
		{
			/* In case we are assigning new texture to a material, and active slot was empty, reset 'use' flag. */
			Material *ma = (Material *)ptr.id.data;
			ma->septex &= ~(1 << ma->texact);
		}

		RNA_id_pointer_create(&tex->id, &idptr);
		RNA_property_pointer_set(&ptr, prop, idptr);
		RNA_property_update(C, &ptr, prop);
	}

	WM_event_add_notifier(C, NC_TEXTURE | NA_ADDED, tex);
	
	return OPERATOR_FINISHED;
}

void TEXTURE_OT_new(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "New Texture";
	ot->idname = "TEXTURE_OT_new";
	ot->description = "Add a new texture";
	
	/* api callbacks */
	ot->exec = new_texture_exec;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
}

/********************** new world operator *********************/

static int new_world_exec(bContext *C, wmOperator *UNUSED(op))
{
	Scene *scene = CTX_data_scene(C);
	World *wo = CTX_data_pointer_get_type(C, "world", &RNA_World).data;
	Main *bmain = CTX_data_main(C);
	PointerRNA ptr, idptr;
	PropertyRNA *prop;

	/* add or copy world */
	if (wo) {
		wo = BKE_world_copy(wo);
	}
	else {
		wo = add_world(bmain, DATA_("World"));

		if (BKE_scene_use_new_shading_nodes(scene)) {
			ED_node_shader_default(C, &wo->id);
			wo->use_nodes = true;
		}
	}

	/* hook into UI */
	uiIDContextProperty(C, &ptr, &prop);

	if (prop) {
		/* when creating new ID blocks, use is already 1, but RNA
		 * pointer se also increases user, so this compensates it */
		wo->id.us--;

		RNA_id_pointer_create(&wo->id, &idptr);
		RNA_property_pointer_set(&ptr, prop, idptr);
		RNA_property_update(C, &ptr, prop);
	}

	WM_event_add_notifier(C, NC_WORLD | NA_ADDED, wo);
	
	return OPERATOR_FINISHED;
}

void WORLD_OT_new(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "New World";
	ot->idname = "WORLD_OT_new";
	ot->description = "Add a new world";
	
	/* api callbacks */
	ot->exec = new_world_exec;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
}

/********************** render layer operators *********************/

static int render_layer_add_exec(bContext *C, wmOperator *UNUSED(op))
{
	Scene *scene = CTX_data_scene(C);

	BKE_scene_add_render_layer(scene, NULL);
	scene->r.actlay = BLI_countlist(&scene->r.layers) - 1;

	DAG_id_tag_update(&scene->id, 0);
	WM_event_add_notifier(C, NC_SCENE | ND_RENDER_OPTIONS, scene);
	
	return OPERATOR_FINISHED;
}

void SCENE_OT_render_layer_add(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Add Render Layer";
	ot->idname = "SCENE_OT_render_layer_add";
	ot->description = "Add a render layer";
	
	/* api callbacks */
	ot->exec = render_layer_add_exec;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
}

static int render_layer_remove_exec(bContext *C, wmOperator *UNUSED(op))
{
	Scene *scene = CTX_data_scene(C);
	SceneRenderLayer *rl = BLI_findlink(&scene->r.layers, scene->r.actlay);

	if (!BKE_scene_remove_render_layer(CTX_data_main(C), scene, rl))
		return OPERATOR_CANCELLED;

	DAG_id_tag_update(&scene->id, 0);
	WM_event_add_notifier(C, NC_SCENE | ND_RENDER_OPTIONS, scene);
	
	return OPERATOR_FINISHED;
}

void SCENE_OT_render_layer_remove(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Remove Render Layer";
	ot->idname = "SCENE_OT_render_layer_remove";
	ot->description = "Remove the selected render layer";
	
	/* api callbacks */
	ot->exec = render_layer_remove_exec;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
}

#ifdef WITH_FREESTYLE

static bool freestyle_linestyle_check_report(FreestyleLineSet *lineset, ReportList *reports)
{
	if (!lineset) {
		BKE_report(reports, RPT_ERROR, "No active lineset and associated line style to manipulate the modifier");
		return false;
	}
	if (!lineset->linestyle) {
		BKE_report(reports, RPT_ERROR, "The active lineset does not have a line style (indicating data corruption)");
		return false;
	}

	return true;
}

static int freestyle_active_module_poll(bContext *C)
{
	PointerRNA ptr = CTX_data_pointer_get_type(C, "freestyle_module", &RNA_FreestyleModuleSettings);
	FreestyleModuleConfig *module = ptr.data;

	return module != NULL;
}

static int freestyle_module_add_exec(bContext *C, wmOperator *UNUSED(op))
{
	Scene *scene = CTX_data_scene(C);
	SceneRenderLayer *srl = BLI_findlink(&scene->r.layers, scene->r.actlay);

	BKE_freestyle_module_add(&srl->freestyleConfig);

	WM_event_add_notifier(C, NC_SCENE | ND_RENDER_OPTIONS, scene);

	return OPERATOR_FINISHED;
}

void SCENE_OT_freestyle_module_add(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Add Freestyle Module";
	ot->idname = "SCENE_OT_freestyle_module_add";
	ot->description = "Add a style module into the list of modules";

	/* api callbacks */
	ot->exec = freestyle_module_add_exec;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
}

static int freestyle_module_remove_exec(bContext *C, wmOperator *UNUSED(op))
{
	Scene *scene = CTX_data_scene(C);
	SceneRenderLayer *srl = BLI_findlink(&scene->r.layers, scene->r.actlay);
	PointerRNA ptr = CTX_data_pointer_get_type(C, "freestyle_module", &RNA_FreestyleModuleSettings);
	FreestyleModuleConfig *module = ptr.data;

	BKE_freestyle_module_delete(&srl->freestyleConfig, module);

	DAG_id_tag_update(&scene->id, 0);
	WM_event_add_notifier(C, NC_SCENE | ND_RENDER_OPTIONS, scene);

	return OPERATOR_FINISHED;
}

void SCENE_OT_freestyle_module_remove(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Remove Freestyle Module";
	ot->idname = "SCENE_OT_freestyle_module_remove";
	ot->description = "Remove the style module from the stack";

	/* api callbacks */
	ot->poll = freestyle_active_module_poll;
	ot->exec = freestyle_module_remove_exec;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
}

static int freestyle_module_move_exec(bContext *C, wmOperator *op)
{
	Scene *scene = CTX_data_scene(C);
	SceneRenderLayer *srl = BLI_findlink(&scene->r.layers, scene->r.actlay);
	PointerRNA ptr = CTX_data_pointer_get_type(C, "freestyle_module", &RNA_FreestyleModuleSettings);
	FreestyleModuleConfig *module = ptr.data;
	int dir = RNA_enum_get(op->ptr, "direction");

	if (dir == 1) {
		BKE_freestyle_module_move_up(&srl->freestyleConfig, module);
	}
	else {
		BKE_freestyle_module_move_down(&srl->freestyleConfig, module);
	}
	DAG_id_tag_update(&scene->id, 0);
	WM_event_add_notifier(C, NC_SCENE | ND_RENDER_OPTIONS, scene);

	return OPERATOR_FINISHED;
}

void SCENE_OT_freestyle_module_move(wmOperatorType *ot)
{
	static EnumPropertyItem direction_items[] = {
		{1, "UP", 0, "Up", ""},
		{-1, "DOWN", 0, "Down", ""},
		{0, NULL, 0, NULL, NULL}
	};

	/* identifiers */
	ot->name = "Move Freestyle Module";
	ot->idname = "SCENE_OT_freestyle_module_move";
	ot->description = "Change the position of the style module within in the list of style modules";

	/* api callbacks */
	ot->poll = freestyle_active_module_poll;
	ot->exec = freestyle_module_move_exec;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;

	/* props */
	RNA_def_enum(ot->srna, "direction", direction_items, 0, "Direction", "Direction to move, UP or DOWN");
}

static int freestyle_lineset_add_exec(bContext *C, wmOperator *UNUSED(op))
{
	Scene *scene = CTX_data_scene(C);
	SceneRenderLayer *srl = BLI_findlink(&scene->r.layers, scene->r.actlay);

	BKE_freestyle_lineset_add(&srl->freestyleConfig, NULL);

	DAG_id_tag_update(&scene->id, 0);
	WM_event_add_notifier(C, NC_SCENE | ND_RENDER_OPTIONS, scene);

	return OPERATOR_FINISHED;
}

void SCENE_OT_freestyle_lineset_add(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Add Line Set";
	ot->idname = "SCENE_OT_freestyle_lineset_add";
	ot->description = "Add a line set into the list of line sets";

	/* api callbacks */
	ot->exec = freestyle_lineset_add_exec;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
}

static int freestyle_active_lineset_poll(bContext *C)
{
	Scene *scene = CTX_data_scene(C);
	SceneRenderLayer *srl = BLI_findlink(&scene->r.layers, scene->r.actlay);

	if (!srl) {
		return false;
	}

	return BKE_freestyle_lineset_get_active(&srl->freestyleConfig) != NULL;
}

static int freestyle_lineset_copy_exec(bContext *C, wmOperator *UNUSED(op))
{
	Scene *scene = CTX_data_scene(C);
	SceneRenderLayer *srl = BLI_findlink(&scene->r.layers, scene->r.actlay);

	FRS_copy_active_lineset(&srl->freestyleConfig);

	return OPERATOR_FINISHED;
}

void SCENE_OT_freestyle_lineset_copy(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Copy Line Set";
	ot->idname = "SCENE_OT_freestyle_lineset_copy";
	ot->description = "Copy the active line set to a buffer";

	/* api callbacks */
	ot->exec = freestyle_lineset_copy_exec;
	ot->poll = freestyle_active_lineset_poll;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
}

static int freestyle_lineset_paste_exec(bContext *C, wmOperator *UNUSED(op))
{
	Scene *scene = CTX_data_scene(C);
	SceneRenderLayer *srl = BLI_findlink(&scene->r.layers, scene->r.actlay);

	FRS_paste_active_lineset(&srl->freestyleConfig);

	DAG_id_tag_update(&scene->id, 0);
	WM_event_add_notifier(C, NC_SCENE | ND_RENDER_OPTIONS, scene);

	return OPERATOR_FINISHED;
}

void SCENE_OT_freestyle_lineset_paste(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Paste Line Set";
	ot->idname = "SCENE_OT_freestyle_lineset_paste";
	ot->description = "Paste the buffer content to the active line set";

	/* api callbacks */
	ot->exec = freestyle_lineset_paste_exec;
	ot->poll = freestyle_active_lineset_poll;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
}

static int freestyle_lineset_remove_exec(bContext *C, wmOperator *UNUSED(op))
{
	Scene *scene = CTX_data_scene(C);
	SceneRenderLayer *srl = BLI_findlink(&scene->r.layers, scene->r.actlay);

	FRS_delete_active_lineset(&srl->freestyleConfig);

	DAG_id_tag_update(&scene->id, 0);
	WM_event_add_notifier(C, NC_SCENE | ND_RENDER_OPTIONS, scene);

	return OPERATOR_FINISHED;
}

void SCENE_OT_freestyle_lineset_remove(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Remove Line Set";
	ot->idname = "SCENE_OT_freestyle_lineset_remove";
	ot->description = "Remove the active line set from the list of line sets";

	/* api callbacks */
	ot->exec = freestyle_lineset_remove_exec;
	ot->poll = freestyle_active_lineset_poll;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
}

static int freestyle_lineset_move_exec(bContext *C, wmOperator *op)
{
	Scene *scene = CTX_data_scene(C);
	SceneRenderLayer *srl = BLI_findlink(&scene->r.layers, scene->r.actlay);
	int dir = RNA_enum_get(op->ptr, "direction");

	if (dir == 1) {
		FRS_move_active_lineset_up(&srl->freestyleConfig);
	}
	else {
		FRS_move_active_lineset_down(&srl->freestyleConfig);
	}
	DAG_id_tag_update(&scene->id, 0);
	WM_event_add_notifier(C, NC_SCENE | ND_RENDER_OPTIONS, scene);

	return OPERATOR_FINISHED;
}

void SCENE_OT_freestyle_lineset_move(wmOperatorType *ot)
{
	static EnumPropertyItem direction_items[] = {
		{1, "UP", 0, "Up", ""},
		{-1, "DOWN", 0, "Down", ""},
		{0, NULL, 0, NULL, NULL}
	};

	/* identifiers */
	ot->name = "Move Line Set";
	ot->idname = "SCENE_OT_freestyle_lineset_move";
	ot->description = "Change the position of the active line set within the list of line sets";

	/* api callbacks */
	ot->exec = freestyle_lineset_move_exec;
	ot->poll = freestyle_active_lineset_poll;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;

	/* props */
	RNA_def_enum(ot->srna, "direction", direction_items, 0, "Direction", "Direction to move, UP or DOWN");
}

static int freestyle_linestyle_new_exec(bContext *C, wmOperator *op)
{
	Scene *scene = CTX_data_scene(C);
	SceneRenderLayer *srl = BLI_findlink(&scene->r.layers, scene->r.actlay);
	FreestyleLineSet *lineset = BKE_freestyle_lineset_get_active(&srl->freestyleConfig);

	if (!lineset) {
		BKE_report(op->reports, RPT_ERROR, "No active lineset to add a new line style to");
		return OPERATOR_CANCELLED;
	}
	if (lineset->linestyle) {
		lineset->linestyle->id.us--;
		lineset->linestyle = BKE_linestyle_copy(lineset->linestyle);
	}
	else {
		lineset->linestyle = BKE_linestyle_new("LineStyle", NULL);
	}
	DAG_id_tag_update(&lineset->linestyle->id, 0);
	WM_event_add_notifier(C, NC_LINESTYLE, lineset->linestyle);

	return OPERATOR_FINISHED;
}

void SCENE_OT_freestyle_linestyle_new(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "New Line Style";
	ot->idname = "SCENE_OT_freestyle_linestyle_new";
	ot->description = "Create a new line style, reusable by multiple line sets";

	/* api callbacks */
	ot->exec = freestyle_linestyle_new_exec;
	ot->poll = freestyle_active_lineset_poll;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
}

static int freestyle_color_modifier_add_exec(bContext *C, wmOperator *op)
{
	Scene *scene = CTX_data_scene(C);
	SceneRenderLayer *srl = BLI_findlink(&scene->r.layers, scene->r.actlay);
	FreestyleLineSet *lineset = BKE_freestyle_lineset_get_active(&srl->freestyleConfig);
	int type = RNA_enum_get(op->ptr, "type");

	if (!freestyle_linestyle_check_report(lineset, op->reports)) {
		return OPERATOR_CANCELLED;
	}

	if (BKE_linestyle_color_modifier_add(lineset->linestyle, NULL, type) == NULL) {
		BKE_report(op->reports, RPT_ERROR, "Unknown line color modifier type");
		return OPERATOR_CANCELLED;
	}
	DAG_id_tag_update(&lineset->linestyle->id, 0);
	WM_event_add_notifier(C, NC_LINESTYLE, lineset->linestyle);

	return OPERATOR_FINISHED;
}

void SCENE_OT_freestyle_color_modifier_add(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Add Line Color Modifier";
	ot->idname = "SCENE_OT_freestyle_color_modifier_add";
	ot->description = "Add a line color modifier to the line style associated with the active lineset";

	/* api callbacks */
	ot->invoke = WM_menu_invoke;
	ot->exec = freestyle_color_modifier_add_exec;
	ot->poll = freestyle_active_lineset_poll;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;

	/* properties */
	ot->prop = RNA_def_enum(ot->srna, "type", linestyle_color_modifier_type_items, 0, "Type", "");
}

static int freestyle_alpha_modifier_add_exec(bContext *C, wmOperator *op)
{
	Scene *scene = CTX_data_scene(C);
	SceneRenderLayer *srl = BLI_findlink(&scene->r.layers, scene->r.actlay);
	FreestyleLineSet *lineset = BKE_freestyle_lineset_get_active(&srl->freestyleConfig);
	int type = RNA_enum_get(op->ptr, "type");

	if (!freestyle_linestyle_check_report(lineset, op->reports)) {
		return OPERATOR_CANCELLED;
	}

	if (BKE_linestyle_alpha_modifier_add(lineset->linestyle, NULL, type) == NULL) {
		BKE_report(op->reports, RPT_ERROR, "Unknown alpha transparency modifier type");
		return OPERATOR_CANCELLED;
	}
	DAG_id_tag_update(&lineset->linestyle->id, 0);
	WM_event_add_notifier(C, NC_LINESTYLE, lineset->linestyle);

	return OPERATOR_FINISHED;
}

void SCENE_OT_freestyle_alpha_modifier_add(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Add Alpha Transparency Modifier";
	ot->idname = "SCENE_OT_freestyle_alpha_modifier_add";
	ot->description = "Add an alpha transparency modifier to the line style associated with the active lineset";

	/* api callbacks */
	ot->invoke = WM_menu_invoke;
	ot->exec = freestyle_alpha_modifier_add_exec;
	ot->poll = freestyle_active_lineset_poll;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;

	/* properties */
	ot->prop = RNA_def_enum(ot->srna, "type", linestyle_alpha_modifier_type_items, 0, "Type", "");
}

static int freestyle_thickness_modifier_add_exec(bContext *C, wmOperator *op)
{
	Scene *scene = CTX_data_scene(C);
	SceneRenderLayer *srl = BLI_findlink(&scene->r.layers, scene->r.actlay);
	FreestyleLineSet *lineset = BKE_freestyle_lineset_get_active(&srl->freestyleConfig);
	int type = RNA_enum_get(op->ptr, "type");

	if (!freestyle_linestyle_check_report(lineset, op->reports)) {
		return OPERATOR_CANCELLED;
	}

	if (BKE_linestyle_thickness_modifier_add(lineset->linestyle, NULL, type) == NULL) {
		BKE_report(op->reports, RPT_ERROR, "Unknown line thickness modifier type");
		return OPERATOR_CANCELLED;
	}
	DAG_id_tag_update(&lineset->linestyle->id, 0);
	WM_event_add_notifier(C, NC_LINESTYLE, lineset->linestyle);

	return OPERATOR_FINISHED;
}

void SCENE_OT_freestyle_thickness_modifier_add(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Add Line Thickness Modifier";
	ot->idname = "SCENE_OT_freestyle_thickness_modifier_add";
	ot->description = "Add a line thickness modifier to the line style associated with the active lineset";

	/* api callbacks */
	ot->invoke = WM_menu_invoke;
	ot->exec = freestyle_thickness_modifier_add_exec;
	ot->poll = freestyle_active_lineset_poll;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;

	/* properties */
	ot->prop = RNA_def_enum(ot->srna, "type", linestyle_thickness_modifier_type_items, 0, "Type", "");
}

static int freestyle_geometry_modifier_add_exec(bContext *C, wmOperator *op)
{
	Scene *scene = CTX_data_scene(C);
	SceneRenderLayer *srl = BLI_findlink(&scene->r.layers, scene->r.actlay);
	FreestyleLineSet *lineset = BKE_freestyle_lineset_get_active(&srl->freestyleConfig);
	int type = RNA_enum_get(op->ptr, "type");

	if (!freestyle_linestyle_check_report(lineset, op->reports)) {
		return OPERATOR_CANCELLED;
	}

	if (BKE_linestyle_geometry_modifier_add(lineset->linestyle, NULL, type) == NULL) {
		BKE_report(op->reports, RPT_ERROR, "Unknown stroke geometry modifier type");
		return OPERATOR_CANCELLED;
	}
	DAG_id_tag_update(&lineset->linestyle->id, 0);
	WM_event_add_notifier(C, NC_LINESTYLE, lineset->linestyle);

	return OPERATOR_FINISHED;
}

void SCENE_OT_freestyle_geometry_modifier_add(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Add Stroke Geometry Modifier";
	ot->idname = "SCENE_OT_freestyle_geometry_modifier_add";
	ot->description = "Add a stroke geometry modifier to the line style associated with the active lineset";

	/* api callbacks */
	ot->invoke = WM_menu_invoke;
	ot->exec = freestyle_geometry_modifier_add_exec;
	ot->poll = freestyle_active_lineset_poll;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;

	/* properties */
	ot->prop = RNA_def_enum(ot->srna, "type", linestyle_geometry_modifier_type_items, 0, "Type", "");
}

static int freestyle_get_modifier_type(PointerRNA *ptr)
{
	if (RNA_struct_is_a(ptr->type, &RNA_LineStyleColorModifier))
		return LS_MODIFIER_TYPE_COLOR;
	else if (RNA_struct_is_a(ptr->type, &RNA_LineStyleAlphaModifier))
		return LS_MODIFIER_TYPE_ALPHA;
	else if (RNA_struct_is_a(ptr->type, &RNA_LineStyleThicknessModifier))
		return LS_MODIFIER_TYPE_THICKNESS;
	else if (RNA_struct_is_a(ptr->type, &RNA_LineStyleGeometryModifier))
		return LS_MODIFIER_TYPE_GEOMETRY;
	return -1;
}

static int freestyle_modifier_remove_exec(bContext *C, wmOperator *op)
{
	Scene *scene = CTX_data_scene(C);
	SceneRenderLayer *srl = BLI_findlink(&scene->r.layers, scene->r.actlay);
	FreestyleLineSet *lineset = BKE_freestyle_lineset_get_active(&srl->freestyleConfig);
	PointerRNA ptr = CTX_data_pointer_get_type(C, "modifier", &RNA_LineStyleModifier);
	LineStyleModifier *modifier = ptr.data;

	if (!freestyle_linestyle_check_report(lineset, op->reports)) {
		return OPERATOR_CANCELLED;
	}

	switch (freestyle_get_modifier_type(&ptr)) {
		case LS_MODIFIER_TYPE_COLOR:
			BKE_linestyle_color_modifier_remove(lineset->linestyle, modifier);
			break;
		case LS_MODIFIER_TYPE_ALPHA:
			BKE_linestyle_alpha_modifier_remove(lineset->linestyle, modifier);
			break;
		case LS_MODIFIER_TYPE_THICKNESS:
			BKE_linestyle_thickness_modifier_remove(lineset->linestyle, modifier);
			break;
		case LS_MODIFIER_TYPE_GEOMETRY:
			BKE_linestyle_geometry_modifier_remove(lineset->linestyle, modifier);
			break;
		default:
			BKE_report(op->reports, RPT_ERROR, "The object the data pointer refers to is not a valid modifier");
			return OPERATOR_CANCELLED;
	}
	DAG_id_tag_update(&lineset->linestyle->id, 0);
	WM_event_add_notifier(C, NC_LINESTYLE, lineset->linestyle);

	return OPERATOR_FINISHED;
}

void SCENE_OT_freestyle_modifier_remove(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Remove Modifier";
	ot->idname = "SCENE_OT_freestyle_modifier_remove";
	ot->description = "Remove the modifier from the list of modifiers";

	/* api callbacks */
	ot->exec = freestyle_modifier_remove_exec;
	ot->poll = freestyle_active_lineset_poll;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
}

static int freestyle_modifier_copy_exec(bContext *C, wmOperator *op)
{
	Scene *scene = CTX_data_scene(C);
	SceneRenderLayer *srl = BLI_findlink(&scene->r.layers, scene->r.actlay);
	FreestyleLineSet *lineset = BKE_freestyle_lineset_get_active(&srl->freestyleConfig);
	PointerRNA ptr = CTX_data_pointer_get_type(C, "modifier", &RNA_LineStyleModifier);
	LineStyleModifier *modifier = ptr.data;

	if (!freestyle_linestyle_check_report(lineset, op->reports)) {
		return OPERATOR_CANCELLED;
	}

	switch (freestyle_get_modifier_type(&ptr)) {
		case LS_MODIFIER_TYPE_COLOR:
			BKE_linestyle_color_modifier_copy(lineset->linestyle, modifier);
			break;
		case LS_MODIFIER_TYPE_ALPHA:
			BKE_linestyle_alpha_modifier_copy(lineset->linestyle, modifier);
			break;
		case LS_MODIFIER_TYPE_THICKNESS:
			BKE_linestyle_thickness_modifier_copy(lineset->linestyle, modifier);
			break;
		case LS_MODIFIER_TYPE_GEOMETRY:
			BKE_linestyle_geometry_modifier_copy(lineset->linestyle, modifier);
			break;
		default:
			BKE_report(op->reports, RPT_ERROR, "The object the data pointer refers to is not a valid modifier");
			return OPERATOR_CANCELLED;
	}
	DAG_id_tag_update(&lineset->linestyle->id, 0);
	WM_event_add_notifier(C, NC_LINESTYLE, lineset->linestyle);

	return OPERATOR_FINISHED;
}

void SCENE_OT_freestyle_modifier_copy(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Copy Modifier";
	ot->idname = "SCENE_OT_freestyle_modifier_copy";
	ot->description = "Duplicate the modifier within the list of modifiers";

	/* api callbacks */
	ot->exec = freestyle_modifier_copy_exec;
	ot->poll = freestyle_active_lineset_poll;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
}

static int freestyle_modifier_move_exec(bContext *C, wmOperator *op)
{
	Scene *scene = CTX_data_scene(C);
	SceneRenderLayer *srl = BLI_findlink(&scene->r.layers, scene->r.actlay);
	FreestyleLineSet *lineset = BKE_freestyle_lineset_get_active(&srl->freestyleConfig);
	PointerRNA ptr = CTX_data_pointer_get_type(C, "modifier", &RNA_LineStyleModifier);
	LineStyleModifier *modifier = ptr.data;
	int dir = RNA_enum_get(op->ptr, "direction");

	if (!freestyle_linestyle_check_report(lineset, op->reports)) {
		return OPERATOR_CANCELLED;
	}

	switch (freestyle_get_modifier_type(&ptr)) {
		case LS_MODIFIER_TYPE_COLOR:
			BKE_linestyle_color_modifier_move(lineset->linestyle, modifier, dir);
			break;
		case LS_MODIFIER_TYPE_ALPHA:
			BKE_linestyle_alpha_modifier_move(lineset->linestyle, modifier, dir);
			break;
		case LS_MODIFIER_TYPE_THICKNESS:
			BKE_linestyle_thickness_modifier_move(lineset->linestyle, modifier, dir);
			break;
		case LS_MODIFIER_TYPE_GEOMETRY:
			BKE_linestyle_geometry_modifier_move(lineset->linestyle, modifier, dir);
			break;
		default:
			BKE_report(op->reports, RPT_ERROR, "The object the data pointer refers to is not a valid modifier");
			return OPERATOR_CANCELLED;
	}
	DAG_id_tag_update(&lineset->linestyle->id, 0);
	WM_event_add_notifier(C, NC_LINESTYLE, lineset->linestyle);

	return OPERATOR_FINISHED;
}

void SCENE_OT_freestyle_modifier_move(wmOperatorType *ot)
{
	static EnumPropertyItem direction_items[] = {
		{1, "UP", 0, "Up", ""},
		{-1, "DOWN", 0, "Down", ""},
		{0, NULL, 0, NULL, NULL}
	};

	/* identifiers */
	ot->name = "Move Modifier";
	ot->idname = "SCENE_OT_freestyle_modifier_move";
	ot->description = "Move the modifier within the list of modifiers";

	/* api callbacks */
	ot->exec = freestyle_modifier_move_exec;
	ot->poll = freestyle_active_lineset_poll;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;

	/* props */
	RNA_def_enum(ot->srna, "direction", direction_items, 0, "Direction", "Direction to move, UP or DOWN");
}

static int freestyle_stroke_material_create_exec(bContext *C, wmOperator *op)
{
	Main *bmain = CTX_data_main(C);
	Scene *scene = CTX_data_scene(C);
	FreestyleLineStyle *linestyle = BKE_linestyle_active_from_scene(scene);

	if (!linestyle) {
		BKE_report(op->reports, RPT_ERROR, "No active line style in the current scene");
		return OPERATOR_CANCELLED;
	}

	FRS_create_stroke_material(bmain, linestyle);

	return OPERATOR_FINISHED;
}

void SCENE_OT_freestyle_stroke_material_create(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Create Freestyle Stroke Material";
	ot->idname = "SCENE_OT_freestyle_stroke_material_create";
	ot->description = "Create Freestyle stroke material for testing";

	/* api callbacks */
	ot->exec = freestyle_stroke_material_create_exec;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}

#endif /* WITH_FREESTYLE */

static int texture_slot_move_exec(bContext *C, wmOperator *op)
{
	ID *id = CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;

	if (id) {
		MTex **mtex_ar, *mtexswap;
		short act;
		int type = RNA_enum_get(op->ptr, "type");
		struct AnimData *adt = BKE_animdata_from_id(id);

		give_active_mtex(id, &mtex_ar, &act);

		if (type == -1) { /* Up */
			if (act > 0) {
				mtexswap = mtex_ar[act];
				mtex_ar[act] = mtex_ar[act - 1];
				mtex_ar[act - 1] = mtexswap;
				
				BKE_animdata_fix_paths_rename(id, adt, NULL, "texture_slots", NULL, NULL, act - 1, -1, 0);
				BKE_animdata_fix_paths_rename(id, adt, NULL, "texture_slots", NULL, NULL, act, act - 1, 0);
				BKE_animdata_fix_paths_rename(id, adt, NULL, "texture_slots", NULL, NULL, -1, act, 0);

				if (GS(id->name) == ID_MA) {
					Material *ma = (Material *)id;
					int mtexuse = ma->septex & (1 << act);
					ma->septex &= ~(1 << act);
					ma->septex |= (ma->septex & (1 << (act - 1))) << 1;
					ma->septex &= ~(1 << (act - 1));
					ma->septex |= mtexuse >> 1;
				}
				
				set_active_mtex(id, act - 1);
			}
		}
		else { /* Down */
			if (act < MAX_MTEX - 1) {
				mtexswap = mtex_ar[act];
				mtex_ar[act] = mtex_ar[act + 1];
				mtex_ar[act + 1] = mtexswap;
				
				BKE_animdata_fix_paths_rename(id, adt, NULL, "texture_slots", NULL, NULL, act + 1, -1, 0);
				BKE_animdata_fix_paths_rename(id, adt, NULL, "texture_slots", NULL, NULL, act, act + 1, 0);
				BKE_animdata_fix_paths_rename(id, adt, NULL, "texture_slots", NULL, NULL, -1, act, 0);

				if (GS(id->name) == ID_MA) {
					Material *ma = (Material *)id;
					int mtexuse = ma->septex & (1 << act);
					ma->septex &= ~(1 << act);
					ma->septex |= (ma->septex & (1 << (act + 1))) >> 1;
					ma->septex &= ~(1 << (act + 1));
					ma->septex |= mtexuse << 1;
				}
				
				set_active_mtex(id, act + 1);
			}
		}

		DAG_id_tag_update(id, 0);
		WM_event_add_notifier(C, NC_TEXTURE, CTX_data_scene(C));
	}

	return OPERATOR_FINISHED;
}

void TEXTURE_OT_slot_move(wmOperatorType *ot)
{
	static EnumPropertyItem slot_move[] = {
		{-1, "UP", 0, "Up", ""},
		{1, "DOWN", 0, "Down", ""},
		{0, NULL, 0, NULL, NULL}
	};

	/* identifiers */
	ot->name = "Move Texture Slot";
	ot->idname = "TEXTURE_OT_slot_move";
	ot->description = "Move texture slots up and down";

	/* api callbacks */
	ot->exec = texture_slot_move_exec;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;

	RNA_def_enum(ot->srna, "type", slot_move, 0, "Type", "");
}



/********************** environment map operators *********************/

static int save_envmap(wmOperator *op, Scene *scene, EnvMap *env, char *path, const char imtype)
{
	float layout[12];
	if (RNA_struct_find_property(op->ptr, "layout") )
		RNA_float_get_array(op->ptr, "layout", layout);
	else
		memcpy(layout, default_envmap_layout, sizeof(layout));

	if (RE_WriteEnvmapResult(op->reports, scene, env, path, imtype, layout)) {
		return OPERATOR_FINISHED;
	}
	else {
		return OPERATOR_CANCELLED;
	}

}

static int envmap_save_exec(bContext *C, wmOperator *op)
{
	Tex *tex = CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
	Scene *scene = CTX_data_scene(C);
	//int imtype = RNA_enum_get(op->ptr, "file_type");
	char imtype = scene->r.im_format.imtype;
	char path[FILE_MAX];
	
	RNA_string_get(op->ptr, "filepath", path);
	
	if (scene->r.scemode & R_EXTENSION) {
		BKE_add_image_extension(path, &scene->r.im_format);
	}
	
	WM_cursor_wait(1);
	
	save_envmap(op, scene, tex->env, path, imtype);
	
	WM_cursor_wait(0);
	
	WM_event_add_notifier(C, NC_TEXTURE, tex);
	
	return OPERATOR_FINISHED;
}

static int envmap_save_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
{
	//Scene *scene= CTX_data_scene(C);
	
	if (RNA_struct_property_is_set(op->ptr, "filepath"))
		return envmap_save_exec(C, op);

	//RNA_enum_set(op->ptr, "file_type", scene->r.im_format.imtype);
	RNA_string_set(op->ptr, "filepath", G.main->name);
	WM_event_add_fileselect(C, op);
	
	return OPERATOR_RUNNING_MODAL;
}

static int envmap_save_poll(bContext *C)
{
	Tex *tex = CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;

	if (!tex) 
		return 0;
	if (!tex->env || !tex->env->ok)
		return 0;
	if (tex->env->cube[1] == NULL)
		return 0;
	
	return 1;
}

void TEXTURE_OT_envmap_save(wmOperatorType *ot)
{
	PropertyRNA *prop;
	/* identifiers */
	ot->name = "Save Environment Map";
	ot->idname = "TEXTURE_OT_envmap_save";
	ot->description = "Save the current generated Environment map to an image file";
	
	/* api callbacks */
	ot->exec = envmap_save_exec;
	ot->invoke = envmap_save_invoke;
	ot->poll = envmap_save_poll;
	
	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_INTERNAL; /* no undo since this doesnt modify the env-map */
	
	/* properties */
	prop = RNA_def_float_array(ot->srna, "layout", 12, default_envmap_layout, 0.0f, 0.0f,
	                           "File layout",
	                           "Flat array describing the X,Y position of each cube face in the output image, "
	                           "where 1 is the size of a face - order is [+Z -Z +Y -X -Y +X] "
	                           "(use -1 to skip a face)", 0.0f, 0.0f);
	RNA_def_property_flag(prop, PROP_HIDDEN);

	WM_operator_properties_filesel(ot, FOLDERFILE | IMAGEFILE | MOVIEFILE, FILE_SPECIAL, FILE_SAVE,
	                               WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY);
}

static int envmap_clear_exec(bContext *C, wmOperator *UNUSED(op))
{
	Tex *tex = CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
	
	BKE_free_envmapdata(tex->env);
	
	WM_event_add_notifier(C, NC_TEXTURE | NA_EDITED, tex);
	
	return OPERATOR_FINISHED;
}

static int envmap_clear_poll(bContext *C)
{
	Tex *tex = CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
	
	if (!tex) 
		return 0;
	if (!tex->env || !tex->env->ok)
		return 0;
	if (tex->env->cube[1] == NULL)
		return 0;
	
	return 1;
}

void TEXTURE_OT_envmap_clear(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Clear Environment Map";
	ot->idname = "TEXTURE_OT_envmap_clear";
	ot->description = "Discard the environment map and free it from memory";
	
	/* api callbacks */
	ot->exec = envmap_clear_exec;
	ot->poll = envmap_clear_poll;
	
	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
}

static int envmap_clear_all_exec(bContext *C, wmOperator *UNUSED(op))
{
	Main *bmain = CTX_data_main(C);
	Tex *tex;
	
	for (tex = bmain->tex.first; tex; tex = tex->id.next)
		if (tex->env)
			BKE_free_envmapdata(tex->env);
	
	WM_event_add_notifier(C, NC_TEXTURE | NA_EDITED, tex);
	
	return OPERATOR_FINISHED;
}

void TEXTURE_OT_envmap_clear_all(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Clear All Environment Maps";
	ot->idname = "TEXTURE_OT_envmap_clear_all";
	ot->description = "Discard all environment maps in the .blend file and free them from memory";
	
	/* api callbacks */
	ot->exec = envmap_clear_all_exec;
	ot->poll = envmap_clear_poll;
	
	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}

/********************** material operators *********************/

/* material copy/paste */
static int copy_material_exec(bContext *C, wmOperator *UNUSED(op))
{
	Material *ma = CTX_data_pointer_get_type(C, "material", &RNA_Material).data;

	if (ma == NULL)
		return OPERATOR_CANCELLED;

	copy_matcopybuf(ma);

	return OPERATOR_FINISHED;
}

void MATERIAL_OT_copy(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Copy Material";
	ot->idname = "MATERIAL_OT_copy";
	ot->description = "Copy the material settings and nodes";

	/* api callbacks */
	ot->exec = copy_material_exec;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_INTERNAL; /* no undo needed since no changes are made to the material */
}

static int paste_material_exec(bContext *C, wmOperator *UNUSED(op))
{
	Material *ma = CTX_data_pointer_get_type(C, "material", &RNA_Material).data;

	if (ma == NULL)
		return OPERATOR_CANCELLED;

	paste_matcopybuf(ma);

	WM_event_add_notifier(C, NC_MATERIAL | ND_SHADING_LINKS, ma);

	return OPERATOR_FINISHED;
}

void MATERIAL_OT_paste(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Paste Material";
	ot->idname = "MATERIAL_OT_paste";
	ot->description = "Paste the material settings and nodes";

	/* api callbacks */
	ot->exec = paste_material_exec;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
}


static short mtexcopied = 0; /* must be reset on file load */
static MTex mtexcopybuf;

void ED_render_clear_mtex_copybuf(void)
{   /* use for file reload */
	mtexcopied = 0;
}

static void copy_mtex_copybuf(ID *id)
{
	MTex **mtex = NULL;
	
	switch (GS(id->name)) {
		case ID_MA:
			mtex = &(((Material *)id)->mtex[(int)((Material *)id)->texact]);
			break;
		case ID_LA:
			mtex = &(((Lamp *)id)->mtex[(int)((Lamp *)id)->texact]);
			// la->mtex[(int)la->texact] // TODO
			break;
		case ID_WO:
			mtex = &(((World *)id)->mtex[(int)((World *)id)->texact]);
			// mtex= wrld->mtex[(int)wrld->texact]; // TODO
			break;
		case ID_PA:
			mtex = &(((ParticleSettings *)id)->mtex[(int)((ParticleSettings *)id)->texact]);
			break;
		case ID_LS:
			mtex = &(((FreestyleLineStyle *)id)->mtex[(int)((FreestyleLineStyle *)id)->texact]);
			break;
	}
	
	if (mtex && *mtex) {
		memcpy(&mtexcopybuf, *mtex, sizeof(MTex));
		mtexcopied = 1;
	}
	else {
		mtexcopied = 0;
	}
}

static void paste_mtex_copybuf(ID *id)
{
	MTex **mtex = NULL;
	
	if (mtexcopied == 0 || mtexcopybuf.tex == NULL)
		return;
	
	switch (GS(id->name)) {
		case ID_MA:
			mtex = &(((Material *)id)->mtex[(int)((Material *)id)->texact]);
			break;
		case ID_LA:
			mtex = &(((Lamp *)id)->mtex[(int)((Lamp *)id)->texact]);
			// la->mtex[(int)la->texact] // TODO
			break;
		case ID_WO:
			mtex = &(((World *)id)->mtex[(int)((World *)id)->texact]);
			// mtex= wrld->mtex[(int)wrld->texact]; // TODO
			break;
		case ID_PA:
			mtex = &(((ParticleSettings *)id)->mtex[(int)((ParticleSettings *)id)->texact]);
			break;
		case ID_LS:
			mtex = &(((FreestyleLineStyle *)id)->mtex[(int)((FreestyleLineStyle *)id)->texact]);
			break;
		default:
			BLI_assert("invalid id type");
			return;
	}
	
	if (mtex) {
		if (*mtex == NULL) {
			*mtex = MEM_mallocN(sizeof(MTex), "mtex copy");
		}
		else if ((*mtex)->tex) {
			(*mtex)->tex->id.us--;
		}
		
		memcpy(*mtex, &mtexcopybuf, sizeof(MTex));
		
		id_us_plus((ID *)mtexcopybuf.tex);
	}
}


static int copy_mtex_exec(bContext *C, wmOperator *UNUSED(op))
{
	ID *id = CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;

	if (id == NULL) {
		/* copying empty slot */
		ED_render_clear_mtex_copybuf();
		return OPERATOR_CANCELLED;
	}

	copy_mtex_copybuf(id);

	return OPERATOR_FINISHED;
}

static int copy_mtex_poll(bContext *C)
{
	ID *id = CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
	
	return (id != NULL);
}

void TEXTURE_OT_slot_copy(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Copy Texture Slot Settings";
	ot->idname = "TEXTURE_OT_slot_copy";
	ot->description = "Copy the material texture settings and nodes";

	/* api callbacks */
	ot->exec = copy_mtex_exec;
	ot->poll = copy_mtex_poll;
	
	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_INTERNAL; /* no undo needed since no changes are made to the mtex */
}

static int paste_mtex_exec(bContext *C, wmOperator *UNUSED(op))
{
	ID *id = CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;

	if (id == NULL) {
		Material *ma = CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
		Lamp *la = CTX_data_pointer_get_type(C, "lamp", &RNA_Lamp).data;
		World *wo = CTX_data_pointer_get_type(C, "world", &RNA_World).data;
		ParticleSystem *psys = CTX_data_pointer_get_type(C, "particle_system", &RNA_ParticleSystem).data;
		FreestyleLineStyle *linestyle = CTX_data_pointer_get_type(C, "line_style", &RNA_FreestyleLineStyle).data;

		if (ma)
			id = &ma->id;
		else if (la)
			id = &la->id;
		else if (wo)
			id = &wo->id;
		else if (psys)
			id = &psys->part->id;
		else if (linestyle)
			id = &linestyle->id;
		
		if (id == NULL)
			return OPERATOR_CANCELLED;
	}

	paste_mtex_copybuf(id);

	WM_event_add_notifier(C, NC_TEXTURE | ND_SHADING_LINKS, NULL);

	return OPERATOR_FINISHED;
}

void TEXTURE_OT_slot_paste(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Paste Texture Slot Settings";
	ot->idname = "TEXTURE_OT_slot_paste";
	ot->description = "Copy the texture settings and nodes";

	/* api callbacks */
	ot->exec = paste_mtex_exec;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
}