Merged changes in the trunk up to revision 30952.
[blender-staging.git] / source / blender / editors / render / render_shading.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * The Original Code is Copyright (C) 2009 Blender Foundation.
20  * All rights reserved.
21  *
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "MEM_guardedalloc.h"
31
32 #include "DNA_curve_types.h"
33 #include "DNA_lamp_types.h"
34 #include "DNA_material_types.h"
35 #include "DNA_node_types.h"
36 #include "DNA_object_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_space_types.h"
39 #include "DNA_world_types.h"
40
41 #include "BKE_animsys.h"
42 #include "BKE_context.h"
43 #include "BKE_depsgraph.h"
44 #include "BKE_font.h"
45 #include "BKE_global.h"
46 #include "BKE_icons.h"
47 #include "BKE_image.h"
48 #include "BKE_library.h"
49 #include "BKE_linestyle.h"
50 #include "BKE_main.h"
51 #include "BKE_material.h"
52 #include "BKE_node.h"
53 #include "BKE_report.h"
54 #include "BKE_scene.h"
55 #include "BKE_texture.h"
56 #include "BKE_utildefines.h"
57 #include "BKE_world.h"
58
59 #include "IMB_imbuf.h"
60 #include "IMB_imbuf_types.h"
61
62 #include "BLI_blenlib.h"
63 #include "BLI_math.h"
64 #include "BLI_editVert.h"
65 #include "BLI_listbase.h"
66
67 #include "GPU_material.h"
68
69 #include "FRS_freestyle.h"
70
71 #include "RNA_access.h"
72 #include "RNA_enum_types.h"
73
74 #include "WM_api.h"
75 #include "WM_types.h"
76
77 #include "ED_curve.h"
78 #include "ED_mesh.h"
79
80 #include "RNA_define.h"
81
82 #include "UI_interface.h"
83
84 #include "render_intern.h"      // own include
85
86 /***************************** Updates ***********************************
87  * ED_render_id_flush_update gets called from DAG_id_flush_update, to do *
88  * editor level updates when the ID changes. when these ID blocks are in *
89  * the dependency graph, we can get rid of the manual dependency checks  */
90
91 static int mtex_use_tex(MTex **mtex, int tot, Tex *tex)
92 {
93         int a;
94
95         if(!mtex)
96                 return 0;
97
98         for(a=0; a<tot; a++)
99                 if(mtex[a] && mtex[a]->tex == tex)
100                         return 1;
101         
102         return 0;
103 }
104
105 static int nodes_use_tex(bNodeTree *ntree, Tex *tex)
106 {
107         bNode *node;
108
109         for(node=ntree->nodes.first; node; node= node->next) {
110                 if(node->id) {
111                         if(node->id == (ID*)tex) {
112                                 return 1;
113                         }
114                         else if(node->type==NODE_GROUP) {
115                                 if(nodes_use_tex((bNodeTree *)node->id, tex))
116                                         return 1;
117                         }
118                 }
119         }
120
121         return 0;
122 }
123
124 static void material_changed(Main *bmain, Material *ma)
125 {
126         /* icons */
127         BKE_icon_changed(BKE_icon_getid(&ma->id));
128
129         /* glsl */
130         if(ma->gpumaterial.first)
131                 GPU_material_free(ma);
132 }
133
134 static void texture_changed(Main *bmain, Tex *tex)
135 {
136         Material *ma;
137         Lamp *la;
138         World *wo;
139
140         /* icons */
141         BKE_icon_changed(BKE_icon_getid(&tex->id));
142
143         /* find materials */
144         for(ma=bmain->mat.first; ma; ma=ma->id.next) {
145                 if(mtex_use_tex(ma->mtex, MAX_MTEX, tex));
146                 else if(ma->use_nodes && ma->nodetree && nodes_use_tex(ma->nodetree, tex));
147                 else continue;
148
149                 BKE_icon_changed(BKE_icon_getid(&ma->id));
150
151                 if(ma->gpumaterial.first)
152                         GPU_material_free(ma);
153         }
154
155         /* find lamps */
156         for(la=bmain->lamp.first; la; la=la->id.next) {
157                 if(mtex_use_tex(la->mtex, MAX_MTEX, tex));
158                 else continue;
159
160                 BKE_icon_changed(BKE_icon_getid(&la->id));
161         }
162
163         /* find worlds */
164         for(wo=bmain->world.first; wo; wo=wo->id.next) {
165                 if(mtex_use_tex(wo->mtex, MAX_MTEX, tex));
166                 else continue;
167
168                 BKE_icon_changed(BKE_icon_getid(&wo->id));
169         }
170 }
171
172 static void lamp_changed(Main *bmain, Lamp *la)
173 {
174         Object *ob;
175         Material *ma;
176
177         /* icons */
178         BKE_icon_changed(BKE_icon_getid(&la->id));
179
180         /* glsl */
181         for(ob=bmain->object.first; ob; ob=ob->id.next)
182                 if(ob->data == la && ob->gpulamp.first)
183                         GPU_lamp_free(ob);
184
185         for(ma=bmain->mat.first; ma; ma=ma->id.next)
186                 if(ma->gpumaterial.first)
187                         GPU_material_free(ma);
188 }
189
190 static void world_changed(Main *bmain, World *wo)
191 {
192         Material *ma;
193
194         /* icons */
195         BKE_icon_changed(BKE_icon_getid(&wo->id));
196
197         /* glsl */
198         for(ma=bmain->mat.first; ma; ma=ma->id.next)
199                 if(ma->gpumaterial.first)
200                         GPU_material_free(ma);
201 }
202
203 static void image_changed(Main *bmain, Image *ima)
204 {
205         Tex *tex;
206
207         /* icons */
208         BKE_icon_changed(BKE_icon_getid(&ima->id));
209
210         /* textures */
211         for(tex=bmain->tex.first; tex; tex=tex->id.next)
212                 if(tex->ima == ima)
213                         texture_changed(bmain, tex);
214 }
215
216 static void scene_changed(Main *bmain, Scene *sce)
217 {
218         Object *ob;
219         Material *ma;
220
221         /* glsl */
222         for(ob=bmain->object.first; ob; ob=ob->id.next)
223                 if(ob->gpulamp.first)
224                         GPU_lamp_free(ob);
225
226         for(ma=bmain->mat.first; ma; ma=ma->id.next)
227                 if(ma->gpumaterial.first)
228                         GPU_material_free(ma);
229 }
230
231 void ED_render_id_flush_update(Main *bmain, ID *id)
232 {
233         if(!id)
234                 return;
235
236         switch(GS(id->name)) {
237                 case ID_MA:
238                         material_changed(bmain, (Material*)id);
239                         break;
240                 case ID_TE:
241                         texture_changed(bmain, (Tex*)id);
242                         break;
243                 case ID_WO:
244                         world_changed(bmain, (World*)id);
245                         break;
246                 case ID_LA:
247                         lamp_changed(bmain, (Lamp*)id);
248                         break;
249                 case ID_IM:
250                         image_changed(bmain, (Image*)id);
251                         break;
252                 case ID_SCE:
253                         scene_changed(bmain, (Scene*)id);
254                         break;
255                 default:
256                         break;
257         }
258 }
259
260 /********************** material slot operators *********************/
261
262 static int material_slot_add_exec(bContext *C, wmOperator *op)
263 {
264         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
265
266         if(!ob)
267                 return OPERATOR_CANCELLED;
268
269         object_add_material_slot(ob);
270         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
271         
272         return OPERATOR_FINISHED;
273 }
274
275 void OBJECT_OT_material_slot_add(wmOperatorType *ot)
276 {
277         /* identifiers */
278         ot->name= "Add Material Slot";
279         ot->idname= "OBJECT_OT_material_slot_add";
280         ot->description="Add a new material slot or duplicate the selected one";
281         
282         /* api callbacks */
283         ot->exec= material_slot_add_exec;
284
285         /* flags */
286         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
287 }
288
289 static int material_slot_remove_exec(bContext *C, wmOperator *op)
290 {
291         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
292
293         if(!ob)
294                 return OPERATOR_CANCELLED;
295
296         object_remove_material_slot(ob);
297         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
298         
299         return OPERATOR_FINISHED;
300 }
301
302 void OBJECT_OT_material_slot_remove(wmOperatorType *ot)
303 {
304         /* identifiers */
305         ot->name= "Remove Material Slot";
306         ot->idname= "OBJECT_OT_material_slot_remove";
307         ot->description="Remove the selected material slot";
308         
309         /* api callbacks */
310         ot->exec= material_slot_remove_exec;
311
312         /* flags */
313         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
314 }
315
316 static int material_slot_assign_exec(bContext *C, wmOperator *op)
317 {
318         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
319
320         if(!ob)
321                 return OPERATOR_CANCELLED;
322
323         if(ob && ob->actcol>0) {
324                 if(ob->type == OB_MESH) {
325                         EditMesh *em= ((Mesh*)ob->data)->edit_mesh;
326                         EditFace *efa;
327
328                         if(em) {
329                                 for(efa= em->faces.first; efa; efa=efa->next)
330                                         if(efa->f & SELECT)
331                                                 efa->mat_nr= ob->actcol-1;
332                         }
333                 }
334                 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
335                         Nurb *nu;
336                         ListBase *nurbs= ED_curve_editnurbs((Curve*)ob->data);
337
338                         if(nurbs) {
339                                 for(nu= nurbs->first; nu; nu= nu->next)
340                                         if(isNurbsel(nu))
341                                                 nu->mat_nr= nu->charidx= ob->actcol-1;
342                         }
343                 }
344                 else if(ob->type == OB_FONT) {
345                         EditFont *ef= ((Curve*)ob->data)->editfont;
346                         int i, selstart, selend;
347
348                         if(ef && BKE_font_getselection(ob, &selstart, &selend)) {
349                                 for(i=selstart; i<=selend; i++)
350                                         ef->textbufinfo[i].mat_nr = ob->actcol-1;
351                         }
352                 }
353         }
354
355         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
356         WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
357         
358         return OPERATOR_FINISHED;
359 }
360
361 void OBJECT_OT_material_slot_assign(wmOperatorType *ot)
362 {
363         /* identifiers */
364         ot->name= "Assign Material Slot";
365         ot->idname= "OBJECT_OT_material_slot_assign";
366         ot->description="Assign the material in the selected material slot to the selected vertices";
367         
368         /* api callbacks */
369         ot->exec= material_slot_assign_exec;
370
371         /* flags */
372         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
373 }
374
375 static int material_slot_de_select(bContext *C, int select)
376 {
377         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
378
379         if(!ob)
380                 return OPERATOR_CANCELLED;
381
382         if(ob->type == OB_MESH) {
383                 EditMesh *em= ((Mesh*)ob->data)->edit_mesh;
384
385                 if(em) {
386                         if(select)
387                                 EM_select_by_material(em, ob->actcol-1);
388                         else
389                                 EM_deselect_by_material(em, ob->actcol-1);
390                 }
391         }
392         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
393                 ListBase *nurbs= ED_curve_editnurbs((Curve*)ob->data);
394                 Nurb *nu;
395                 BPoint *bp;
396                 BezTriple *bezt;
397                 int a;
398
399                 for(nu= nurbs->first; nu; nu=nu->next) {
400                         if(nu->mat_nr==ob->actcol-1) {
401                                 if(nu->bezt) {
402                                         a= nu->pntsu;
403                                         bezt= nu->bezt;
404                                         while(a--) {
405                                                 if(bezt->hide==0) {
406                                                         if(select) {
407                                                                 bezt->f1 |= SELECT;
408                                                                 bezt->f2 |= SELECT;
409                                                                 bezt->f3 |= SELECT;
410                                                         }
411                                                         else {
412                                                                 bezt->f1 &= ~SELECT;
413                                                                 bezt->f2 &= ~SELECT;
414                                                                 bezt->f3 &= ~SELECT;
415                                                         }
416                                                 }
417                                                 bezt++;
418                                         }
419                                 }
420                                 else if(nu->bp) {
421                                         a= nu->pntsu*nu->pntsv;
422                                         bp= nu->bp;
423                                         while(a--) {
424                                                 if(bp->hide==0) {
425                                                         if(select) bp->f1 |= SELECT;
426                                                         else bp->f1 &= ~SELECT;
427                                                 }
428                                                 bp++;
429                                         }
430                                 }
431                         }
432                 }
433         }
434
435         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, ob->data);
436
437         return OPERATOR_FINISHED;
438 }
439
440 static int material_slot_select_exec(bContext *C, wmOperator *op)
441 {
442         return material_slot_de_select(C, 1);
443 }
444
445 void OBJECT_OT_material_slot_select(wmOperatorType *ot)
446 {
447         /* identifiers */
448         ot->name= "Select Material Slot";
449         ot->idname= "OBJECT_OT_material_slot_select";
450         ot->description="Select vertices assigned to the selected material slot";
451         
452         /* api callbacks */
453         ot->exec= material_slot_select_exec;
454
455         /* flags */
456         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
457 }
458
459 static int material_slot_deselect_exec(bContext *C, wmOperator *op)
460 {
461         return material_slot_de_select(C, 0);
462 }
463
464 void OBJECT_OT_material_slot_deselect(wmOperatorType *ot)
465 {
466         /* identifiers */
467         ot->name= "Deselect Material Slot";
468         ot->idname= "OBJECT_OT_material_slot_deselect";
469         ot->description="Deselect vertices assigned to the selected material slot";
470         
471         /* api callbacks */
472         ot->exec= material_slot_deselect_exec;
473
474         /* flags */
475         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
476 }
477
478
479 static int material_slot_copy_exec(bContext *C, wmOperator *op)
480 {
481         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
482         Material ***matar;
483
484         if(!ob || !(matar= give_matarar(ob)))
485                 return OPERATOR_CANCELLED;
486
487         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
488                 if(ob != ob_iter && give_matarar(ob_iter)) {
489                         if (ob->data != ob_iter->data)
490                                 assign_matarar(ob_iter, matar, ob->totcol);
491                         
492                         if(ob_iter->totcol==ob->totcol) {
493                                 ob_iter->actcol= ob->actcol;
494                                 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob_iter);
495                         }
496                 }
497         }
498         CTX_DATA_END;
499
500         return OPERATOR_FINISHED;
501 }
502
503
504 void OBJECT_OT_material_slot_copy(wmOperatorType *ot)
505 {
506         /* identifiers */
507         ot->name= "Copy Material to Others";
508         ot->idname= "OBJECT_OT_material_slot_copy";
509         ot->description="Copies materials to other selected objects";
510
511         /* api callbacks */
512         ot->exec= material_slot_copy_exec;
513
514         /* flags */
515         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
516 }
517
518 /********************** new material operator *********************/
519
520 static int new_material_exec(bContext *C, wmOperator *op)
521 {
522         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
523         PointerRNA ptr, idptr;
524         PropertyRNA *prop;
525
526         /* add or copy material */
527         if(ma)
528                 ma= copy_material(ma);
529         else
530                 ma= add_material("Material");
531
532         /* hook into UI */
533         uiIDContextProperty(C, &ptr, &prop);
534
535         if(prop) {
536                 /* when creating new ID blocks, use is already 1, but RNA
537                  * pointer se also increases user, so this compensates it */
538                 ma->id.us--;
539
540                 RNA_id_pointer_create(&ma->id, &idptr);
541                 RNA_property_pointer_set(&ptr, prop, idptr);
542                 RNA_property_update(C, &ptr, prop);
543         }
544
545         WM_event_add_notifier(C, NC_MATERIAL|NA_ADDED, ma);
546         
547         return OPERATOR_FINISHED;
548 }
549
550 void MATERIAL_OT_new(wmOperatorType *ot)
551 {
552         /* identifiers */
553         ot->name= "New Material";
554         ot->idname= "MATERIAL_OT_new";
555         ot->description="Add a new material";
556         
557         /* api callbacks */
558         ot->exec= new_material_exec;
559
560         /* flags */
561         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
562 }
563
564 /********************** new texture operator *********************/
565
566 static int new_texture_exec(bContext *C, wmOperator *op)
567 {
568         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
569         PointerRNA ptr, idptr;
570         PropertyRNA *prop;
571
572         /* add or copy texture */
573         if(tex)
574                 tex= copy_texture(tex);
575         else
576                 tex= add_texture("Texture");
577
578         /* hook into UI */
579         uiIDContextProperty(C, &ptr, &prop);
580
581         if(prop) {
582                 /* when creating new ID blocks, use is already 1, but RNA
583                  * pointer se also increases user, so this compensates it */
584                 tex->id.us--;
585
586                 RNA_id_pointer_create(&tex->id, &idptr);
587                 RNA_property_pointer_set(&ptr, prop, idptr);
588                 RNA_property_update(C, &ptr, prop);
589         }
590
591         WM_event_add_notifier(C, NC_TEXTURE|NA_ADDED, tex);
592         
593         return OPERATOR_FINISHED;
594 }
595
596 void TEXTURE_OT_new(wmOperatorType *ot)
597 {
598         /* identifiers */
599         ot->name= "New Texture";
600         ot->idname= "TEXTURE_OT_new";
601         ot->description="Add a new texture";
602         
603         /* api callbacks */
604         ot->exec= new_texture_exec;
605
606         /* flags */
607         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
608 }
609
610 /********************** new world operator *********************/
611
612 static int new_world_exec(bContext *C, wmOperator *op)
613 {
614         World *wo= CTX_data_pointer_get_type(C, "world", &RNA_World).data;
615         PointerRNA ptr, idptr;
616         PropertyRNA *prop;
617
618         /* add or copy world */
619         if(wo)
620                 wo= copy_world(wo);
621         else
622                 wo= add_world("World");
623
624         /* hook into UI */
625         uiIDContextProperty(C, &ptr, &prop);
626
627         if(prop) {
628                 /* when creating new ID blocks, use is already 1, but RNA
629                  * pointer se also increases user, so this compensates it */
630                 wo->id.us--;
631
632                 RNA_id_pointer_create(&wo->id, &idptr);
633                 RNA_property_pointer_set(&ptr, prop, idptr);
634                 RNA_property_update(C, &ptr, prop);
635         }
636
637         WM_event_add_notifier(C, NC_WORLD|NA_ADDED, wo);
638         
639         return OPERATOR_FINISHED;
640 }
641
642 void WORLD_OT_new(wmOperatorType *ot)
643 {
644         /* identifiers */
645         ot->name= "New World";
646         ot->idname= "WORLD_OT_new";
647         ot->description= "Add a new world";
648         
649         /* api callbacks */
650         ot->exec= new_world_exec;
651
652         /* flags */
653         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
654 }
655
656 /********************** render layer operators *********************/
657
658 static int render_layer_add_exec(bContext *C, wmOperator *op)
659 {
660         Scene *scene= CTX_data_scene(C);
661
662         scene_add_render_layer(scene);
663         scene->r.actlay= BLI_countlist(&scene->r.layers) - 1;
664
665         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
666         
667         return OPERATOR_FINISHED;
668 }
669
670 void SCENE_OT_render_layer_add(wmOperatorType *ot)
671 {
672         /* identifiers */
673         ot->name= "Add Render Layer";
674         ot->idname= "SCENE_OT_render_layer_add";
675         ot->description="Add a render layer";
676         
677         /* api callbacks */
678         ot->exec= render_layer_add_exec;
679
680         /* flags */
681         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
682 }
683
684 static int render_layer_remove_exec(bContext *C, wmOperator *op)
685 {
686         Scene *scene= CTX_data_scene(C);
687         SceneRenderLayer *rl;
688         int act= scene->r.actlay;
689
690         if(BLI_countlist(&scene->r.layers) <= 1)
691                 return OPERATOR_CANCELLED;
692         
693         rl= BLI_findlink(&scene->r.layers, scene->r.actlay);
694         BLI_remlink(&scene->r.layers, rl);
695         MEM_freeN(rl);
696
697         scene->r.actlay= 0;
698         
699         if(scene->nodetree) {
700                 bNode *node;
701                 for(node= scene->nodetree->nodes.first; node; node= node->next) {
702                         if(node->type==CMP_NODE_R_LAYERS && node->id==NULL) {
703                                 if(node->custom1==act)
704                                         node->custom1= 0;
705                                 else if(node->custom1>act)
706                                         node->custom1--;
707                         }
708                 }
709         }
710
711         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
712         
713         return OPERATOR_FINISHED;
714 }
715
716 void SCENE_OT_render_layer_remove(wmOperatorType *ot)
717 {
718         /* identifiers */
719         ot->name= "Remove Render Layer";
720         ot->idname= "SCENE_OT_render_layer_remove";
721         ot->description="Remove the selected render layer";
722         
723         /* api callbacks */
724         ot->exec= render_layer_remove_exec;
725
726         /* flags */
727         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
728 }
729
730 static int freestyle_module_add_exec(bContext *C, wmOperator *op)
731 {
732         Scene *scene= CTX_data_scene(C);
733         SceneRenderLayer *srl = (SceneRenderLayer*) BLI_findlink(&scene->r.layers, scene->r.actlay);
734
735         FRS_add_module(&srl->freestyleConfig);
736
737         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
738         
739         return OPERATOR_FINISHED;
740 }
741
742 void SCENE_OT_freestyle_module_add(wmOperatorType *ot)
743 {
744         /* identifiers */
745         ot->name= "Add Freestyle Module";
746         ot->idname= "SCENE_OT_freestyle_module_add";
747         ot->description="Add a style module into the list of modules.";
748         
749         /* api callbacks */
750         ot->exec= freestyle_module_add_exec;
751
752         /* flags */
753         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
754 }
755
756 static int freestyle_module_remove_exec(bContext *C, wmOperator *op)
757 {
758         Scene *scene= CTX_data_scene(C);
759         SceneRenderLayer *srl = (SceneRenderLayer*) BLI_findlink(&scene->r.layers, scene->r.actlay);
760         PointerRNA ptr= CTX_data_pointer_get_type(C, "freestyle_module", &RNA_FreestyleModuleSettings);
761         FreestyleModuleConfig *module= ptr.data;
762
763         FRS_delete_module(&srl->freestyleConfig, module);
764
765         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
766         
767         return OPERATOR_FINISHED;
768 }
769
770 void SCENE_OT_freestyle_module_remove(wmOperatorType *ot)
771 {
772         /* identifiers */
773         ot->name= "Remove Freestyle Module";
774         ot->idname= "SCENE_OT_freestyle_module_remove";
775         ot->description="Remove the style module from the stack.";
776         
777         /* api callbacks */
778         ot->exec= freestyle_module_remove_exec;
779
780         /* flags */
781         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
782 }
783
784 static int freestyle_module_move_exec(bContext *C, wmOperator *op)
785 {
786         Scene *scene= CTX_data_scene(C);
787         SceneRenderLayer *srl = (SceneRenderLayer*) BLI_findlink(&scene->r.layers, scene->r.actlay);
788         PointerRNA ptr= CTX_data_pointer_get_type(C, "freestyle_module", &RNA_FreestyleModuleSettings);
789         FreestyleModuleConfig *module= ptr.data;
790         int dir= RNA_enum_get(op->ptr, "direction");
791
792         if (dir == 1) {
793                 FRS_move_module_up(&srl->freestyleConfig, module);
794         } else {
795                 FRS_move_module_down(&srl->freestyleConfig, module);
796         }
797         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
798         
799         return OPERATOR_FINISHED;
800 }
801
802 void SCENE_OT_freestyle_module_move(wmOperatorType *ot)
803 {
804         static EnumPropertyItem direction_items[] = {
805                 {1, "UP", 0, "Up", ""},
806                 {-1, "DOWN", 0, "Down", ""},
807                 {0, NULL, 0, NULL, NULL}
808         };
809
810         /* identifiers */
811         ot->name= "Move Freestyle Module";
812         ot->idname= "SCENE_OT_freestyle_module_move";
813         ot->description="Change the position of the style module within in the list of style modules.";
814         
815         /* api callbacks */
816         ot->exec= freestyle_module_move_exec;
817
818         /* flags */
819         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
820
821         /* props */
822         RNA_def_enum(ot->srna, "direction", direction_items, 0, "Direction", "Direction to move, UP or DOWN");
823 }
824
825 static int freestyle_lineset_add_exec(bContext *C, wmOperator *op)
826 {
827         Scene *scene= CTX_data_scene(C);
828         SceneRenderLayer *srl = (SceneRenderLayer*) BLI_findlink(&scene->r.layers, scene->r.actlay);
829
830         FRS_add_lineset(&srl->freestyleConfig);
831
832         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
833         
834         return OPERATOR_FINISHED;
835 }
836
837 void SCENE_OT_freestyle_lineset_add(wmOperatorType *ot)
838 {
839         /* identifiers */
840         ot->name= "Add Line Set";
841         ot->idname= "SCENE_OT_freestyle_lineset_add";
842         ot->description="Add a line set into the list of line sets.";
843         
844         /* api callbacks */
845         ot->exec= freestyle_lineset_add_exec;
846
847         /* flags */
848         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
849 }
850
851 static int freestyle_active_lineset_poll(bContext *C)
852 {
853         Scene *scene= CTX_data_scene(C);
854         SceneRenderLayer *srl = (SceneRenderLayer*) BLI_findlink(&scene->r.layers, scene->r.actlay);
855
856         return FRS_get_active_lineset(&srl->freestyleConfig) != NULL;
857 }
858
859 static int freestyle_lineset_remove_exec(bContext *C, wmOperator *op)
860 {
861         Scene *scene= CTX_data_scene(C);
862         SceneRenderLayer *srl = (SceneRenderLayer*) BLI_findlink(&scene->r.layers, scene->r.actlay);
863
864         FRS_delete_active_lineset(&srl->freestyleConfig);
865
866         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
867         
868         return OPERATOR_FINISHED;
869 }
870
871 void SCENE_OT_freestyle_lineset_remove(wmOperatorType *ot)
872 {
873         /* identifiers */
874         ot->name= "Remove Line Set";
875         ot->idname= "SCENE_OT_freestyle_lineset_remove";
876         ot->description="Remove the active line set from the list of line sets.";
877         
878         /* api callbacks */
879         ot->exec= freestyle_lineset_remove_exec;
880         ot->poll= freestyle_active_lineset_poll;
881
882         /* flags */
883         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
884 }
885
886 static int freestyle_lineset_move_exec(bContext *C, wmOperator *op)
887 {
888         Scene *scene= CTX_data_scene(C);
889         SceneRenderLayer *srl = (SceneRenderLayer*) BLI_findlink(&scene->r.layers, scene->r.actlay);
890         int dir= RNA_enum_get(op->ptr, "direction");
891
892         if (dir == 1) {
893                 FRS_move_active_lineset_up(&srl->freestyleConfig);
894         } else {
895                 FRS_move_active_lineset_down(&srl->freestyleConfig);
896         }
897         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
898         
899         return OPERATOR_FINISHED;
900 }
901
902 void SCENE_OT_freestyle_lineset_move(wmOperatorType *ot)
903 {
904         static EnumPropertyItem direction_items[] = {
905                 {1, "UP", 0, "Up", ""},
906                 {-1, "DOWN", 0, "Down", ""},
907                 {0, NULL, 0, NULL, NULL}
908         };
909
910         /* identifiers */
911         ot->name= "Move Line Set";
912         ot->idname= "SCENE_OT_freestyle_lineset_move";
913         ot->description="Change the position of the active line set within the list of line sets.";
914         
915         /* api callbacks */
916         ot->exec= freestyle_lineset_move_exec;
917         ot->poll= freestyle_active_lineset_poll;
918
919         /* flags */
920         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
921
922         /* props */
923         RNA_def_enum(ot->srna, "direction", direction_items, 0, "Direction", "Direction to move, UP or DOWN");
924 }
925
926 static int freestyle_linestyle_new_exec(bContext *C, wmOperator *op)
927 {
928         Scene *scene= CTX_data_scene(C);
929         SceneRenderLayer *srl = (SceneRenderLayer*) BLI_findlink(&scene->r.layers, scene->r.actlay);
930         FreestyleLineSet *lineset = FRS_get_active_lineset(&srl->freestyleConfig);
931
932         if (!lineset) {
933                 BKE_report(op->reports, RPT_ERROR, "No active lineset to add a new line style to.");
934                 return OPERATOR_CANCELLED;
935         }
936         lineset->linestyle->id.us--;
937         lineset->linestyle = FRS_new_linestyle("LineStyle", NULL);
938
939         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
940         
941         return OPERATOR_FINISHED;
942 }
943
944 void SCENE_OT_freestyle_linestyle_new(wmOperatorType *ot)
945 {
946         /* identifiers */
947         ot->name= "New Line Style";
948         ot->idname= "SCENE_OT_freestyle_linestyle_new";
949         ot->description="Create a new line style, reusable by multiple line sets.";
950         
951         /* api callbacks */
952         ot->exec= freestyle_linestyle_new_exec;
953         ot->poll= freestyle_active_lineset_poll;
954
955         /* flags */
956         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
957 }
958
959 static int freestyle_color_modifier_add_exec(bContext *C, wmOperator *op)
960 {
961         Scene *scene= CTX_data_scene(C);
962         SceneRenderLayer *srl = (SceneRenderLayer*) BLI_findlink(&scene->r.layers, scene->r.actlay);
963         FreestyleLineSet *lineset = FRS_get_active_lineset(&srl->freestyleConfig);
964         int type= RNA_enum_get(op->ptr, "type");
965
966         if (!lineset) {
967                 BKE_report(op->reports, RPT_ERROR, "No active lineset and associated line style to add the modifier to.");
968                 return OPERATOR_CANCELLED;
969         }
970         if (FRS_add_linestyle_color_modifier(lineset->linestyle, type) < 0) {
971                 BKE_report(op->reports, RPT_ERROR, "Unknown line color modifier type.");
972                 return OPERATOR_CANCELLED;
973         }
974         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
975         
976         return OPERATOR_FINISHED;
977 }
978
979 void SCENE_OT_freestyle_color_modifier_add(wmOperatorType *ot)
980 {
981         /* identifiers */
982         ot->name= "Add Line Color Modifier";
983         ot->idname= "SCENE_OT_freestyle_color_modifier_add";
984         ot->description = "Add a line color modifier to the line style associated with the active lineset.";
985         
986         /* api callbacks */
987         ot->invoke= WM_menu_invoke;
988         ot->exec= freestyle_color_modifier_add_exec;
989         ot->poll= freestyle_active_lineset_poll;
990         
991         /* flags */
992         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
993         
994         /* properties */
995         ot->prop= RNA_def_enum(ot->srna, "type", linestyle_color_modifier_type_items, 0, "Type", "");
996 }
997
998 static int freestyle_alpha_modifier_add_exec(bContext *C, wmOperator *op)
999 {
1000         Scene *scene= CTX_data_scene(C);
1001         SceneRenderLayer *srl = (SceneRenderLayer*) BLI_findlink(&scene->r.layers, scene->r.actlay);
1002         FreestyleLineSet *lineset = FRS_get_active_lineset(&srl->freestyleConfig);
1003         int type= RNA_enum_get(op->ptr, "type");
1004
1005         if (!lineset) {
1006                 BKE_report(op->reports, RPT_ERROR, "No active lineset and associated line style to add the modifier to.");
1007                 return OPERATOR_CANCELLED;
1008         }
1009         if (FRS_add_linestyle_alpha_modifier(lineset->linestyle, type) < 0) {
1010                 BKE_report(op->reports, RPT_ERROR, "Unknown alpha transparency modifier type.");
1011                 return OPERATOR_CANCELLED;
1012         }
1013         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
1014         
1015         return OPERATOR_FINISHED;
1016 }
1017
1018 void SCENE_OT_freestyle_alpha_modifier_add(wmOperatorType *ot)
1019 {
1020         /* identifiers */
1021         ot->name= "Add Alpha Transparency Modifier";
1022         ot->idname= "SCENE_OT_freestyle_alpha_modifier_add";
1023         ot->description = "Add an alpha transparency modifier to the line style associated with the active lineset.";
1024         
1025         /* api callbacks */
1026         ot->invoke= WM_menu_invoke;
1027         ot->exec= freestyle_alpha_modifier_add_exec;
1028         ot->poll= freestyle_active_lineset_poll;
1029         
1030         /* flags */
1031         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1032         
1033         /* properties */
1034         ot->prop= RNA_def_enum(ot->srna, "type", linestyle_alpha_modifier_type_items, 0, "Type", "");
1035 }
1036
1037 static int freestyle_thickness_modifier_add_exec(bContext *C, wmOperator *op)
1038 {
1039         Scene *scene= CTX_data_scene(C);
1040         SceneRenderLayer *srl = (SceneRenderLayer*) BLI_findlink(&scene->r.layers, scene->r.actlay);
1041         FreestyleLineSet *lineset = FRS_get_active_lineset(&srl->freestyleConfig);
1042         int type= RNA_enum_get(op->ptr, "type");
1043
1044         if (!lineset) {
1045                 BKE_report(op->reports, RPT_ERROR, "No active lineset and associated line style to add the modifier to.");
1046                 return OPERATOR_CANCELLED;
1047         }
1048         if (FRS_add_linestyle_thickness_modifier(lineset->linestyle, type) < 0) {
1049                 BKE_report(op->reports, RPT_ERROR, "Unknown line thickness modifier type.");
1050                 return OPERATOR_CANCELLED;
1051         }
1052         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
1053         
1054         return OPERATOR_FINISHED;
1055 }
1056
1057 void SCENE_OT_freestyle_thickness_modifier_add(wmOperatorType *ot)
1058 {
1059         /* identifiers */
1060         ot->name= "Add Line Thickness Modifier";
1061         ot->idname= "SCENE_OT_freestyle_thickness_modifier_add";
1062         ot->description = "Add a line thickness modifier to the line style associated with the active lineset.";
1063         
1064         /* api callbacks */
1065         ot->invoke= WM_menu_invoke;
1066         ot->exec= freestyle_thickness_modifier_add_exec;
1067         ot->poll= freestyle_active_lineset_poll;
1068         
1069         /* flags */
1070         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1071         
1072         /* properties */
1073         ot->prop= RNA_def_enum(ot->srna, "type", linestyle_thickness_modifier_type_items, 0, "Type", "");
1074 }
1075
1076 static int freestyle_modifier_toggle_fold_exec(bContext *C, wmOperator *op)
1077 {
1078         Scene *scene= CTX_data_scene(C);
1079         PointerRNA ptr= CTX_data_pointer_get_type(C, "modifier", &RNA_LineStyleModifier);
1080         LineStyleModifier *modifier= ptr.data;
1081
1082         if (modifier->flags & LS_MODIFIER_EXPANDED)
1083                 modifier->flags &= ~LS_MODIFIER_EXPANDED;
1084         else
1085                 modifier->flags |= LS_MODIFIER_EXPANDED;
1086
1087         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
1088         
1089         return OPERATOR_FINISHED;
1090 }
1091
1092 void SCENE_OT_freestyle_modifier_toggle_fold(wmOperatorType *ot)
1093 {
1094         /* identifiers */
1095         ot->name= "Toggle Modifier Fold";
1096         ot->idname= "SCENE_OT_freestyle_modifier_toggle_fold";
1097         ot->description="Fold/expand the modifier tab.";
1098         
1099         /* api callbacks */
1100         ot->exec= freestyle_modifier_toggle_fold_exec;
1101
1102         /* flags */
1103         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1104 }
1105
1106 static int freestyle_get_modifier_type(PointerRNA *ptr)
1107 {
1108         if (RNA_struct_is_a(ptr->type, &RNA_LineStyleColorModifier))
1109                 return LS_MODIFIER_TYPE_COLOR;
1110         else if (RNA_struct_is_a(ptr->type, &RNA_LineStyleAlphaModifier))
1111                 return LS_MODIFIER_TYPE_ALPHA;
1112         else if (RNA_struct_is_a(ptr->type, &RNA_LineStyleThicknessModifier))
1113                 return LS_MODIFIER_TYPE_THICKNESS;
1114         return -1;
1115 }
1116
1117 static int freestyle_modifier_remove_exec(bContext *C, wmOperator *op)
1118 {
1119         Scene *scene= CTX_data_scene(C);
1120         SceneRenderLayer *srl = (SceneRenderLayer*) BLI_findlink(&scene->r.layers, scene->r.actlay);
1121         FreestyleLineSet *lineset = FRS_get_active_lineset(&srl->freestyleConfig);
1122         PointerRNA ptr= CTX_data_pointer_get_type(C, "modifier", &RNA_LineStyleModifier);
1123         LineStyleModifier *modifier= ptr.data;
1124
1125         if (!lineset) {
1126                 BKE_report(op->reports, RPT_ERROR, "No active lineset and associated line style the modifier belongs to.");
1127                 return OPERATOR_CANCELLED;
1128         }
1129         switch (freestyle_get_modifier_type(&ptr)) {
1130         case LS_MODIFIER_TYPE_COLOR:
1131                 FRS_remove_linestyle_color_modifier(lineset->linestyle, modifier);
1132                 break;
1133         case LS_MODIFIER_TYPE_ALPHA:
1134                 FRS_remove_linestyle_alpha_modifier(lineset->linestyle, modifier);
1135                 break;
1136         case LS_MODIFIER_TYPE_THICKNESS:
1137                 FRS_remove_linestyle_thickness_modifier(lineset->linestyle, modifier);
1138                 break;
1139         default:
1140                 BKE_report(op->reports, RPT_ERROR, "The object the data pointer refers to is not a valid modifier.");
1141                 return OPERATOR_CANCELLED;
1142         }
1143         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
1144         
1145         return OPERATOR_FINISHED;
1146 }
1147
1148 void SCENE_OT_freestyle_modifier_remove(wmOperatorType *ot)
1149 {
1150         /* identifiers */
1151         ot->name= "Remove Modifier";
1152         ot->idname= "SCENE_OT_freestyle_modifier_remove";
1153         ot->description="Remove the modifier from the list of modifiers.";
1154         
1155         /* api callbacks */
1156         ot->exec= freestyle_modifier_remove_exec;
1157         ot->poll= freestyle_active_lineset_poll;
1158
1159         /* flags */
1160         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1161 }
1162
1163 static int freestyle_modifier_move_exec(bContext *C, wmOperator *op)
1164 {
1165         Scene *scene= CTX_data_scene(C);
1166         SceneRenderLayer *srl = (SceneRenderLayer*) BLI_findlink(&scene->r.layers, scene->r.actlay);
1167         FreestyleLineSet *lineset = FRS_get_active_lineset(&srl->freestyleConfig);
1168         PointerRNA ptr= CTX_data_pointer_get_type(C, "modifier", &RNA_LineStyleModifier);
1169         LineStyleModifier *modifier= ptr.data;
1170         int dir= RNA_enum_get(op->ptr, "direction");
1171
1172         if (!lineset) {
1173                 BKE_report(op->reports, RPT_ERROR, "No active lineset and associated line style the modifier belongs to.");
1174                 return OPERATOR_CANCELLED;
1175         }
1176         switch (freestyle_get_modifier_type(&ptr)) {
1177         case LS_MODIFIER_TYPE_COLOR:
1178                 FRS_move_linestyle_color_modifier(lineset->linestyle, modifier, dir);
1179                 break;
1180         case LS_MODIFIER_TYPE_ALPHA:
1181                 FRS_move_linestyle_alpha_modifier(lineset->linestyle, modifier, dir);
1182                 break;
1183         case LS_MODIFIER_TYPE_THICKNESS:
1184                 FRS_move_linestyle_thickness_modifier(lineset->linestyle, modifier, dir);
1185                 break;
1186         default:
1187                 BKE_report(op->reports, RPT_ERROR, "The object the data pointer refers to is not a valid modifier.");
1188                 return OPERATOR_CANCELLED;
1189         }
1190         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
1191         
1192         return OPERATOR_FINISHED;
1193 }
1194
1195 void SCENE_OT_freestyle_modifier_move(wmOperatorType *ot)
1196 {
1197         static EnumPropertyItem direction_items[] = {
1198                 {1, "UP", 0, "Up", ""},
1199                 {-1, "DOWN", 0, "Down", ""},
1200                 {0, NULL, 0, NULL, NULL}
1201         };
1202
1203         /* identifiers */
1204         ot->name= "Move Modifier";
1205         ot->idname= "SCENE_OT_freestyle_modifier_move";
1206         ot->description="Move the modifier within the list of modifiers.";
1207         
1208         /* api callbacks */
1209         ot->exec= freestyle_modifier_move_exec;
1210         ot->poll= freestyle_active_lineset_poll;
1211
1212         /* flags */
1213         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1214
1215         /* props */
1216         RNA_def_enum(ot->srna, "direction", direction_items, 0, "Direction", "Direction to move, UP or DOWN");
1217 }
1218
1219 static int texture_slot_move(bContext *C, wmOperator *op)
1220 {
1221         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
1222         Material *ma = (Material *)id;
1223
1224         if(id) {
1225                 MTex **mtex_ar, *mtexswap;
1226                 short act;
1227                 int type= RNA_enum_get(op->ptr, "type");
1228
1229                 give_active_mtex(id, &mtex_ar, &act);
1230
1231                 if(type == -1) { /* Up */
1232                         if(act > 0) {
1233                                 mtexswap = mtex_ar[act];
1234                                 mtex_ar[act] = mtex_ar[act-1];
1235                                 mtex_ar[act-1] = mtexswap;
1236                                 
1237                                 BKE_animdata_fix_paths_rename(id, ma->adt, "texture_slots", NULL, NULL, act-1, -1, 0);
1238                                 BKE_animdata_fix_paths_rename(id, ma->adt, "texture_slots", NULL, NULL, act, act-1, 0);
1239                                 BKE_animdata_fix_paths_rename(id, ma->adt, "texture_slots", NULL, NULL, -1, act, 0);
1240
1241                                 if(GS(id->name)==ID_MA) {
1242                                         Material *ma= (Material *)id;
1243                                         int mtexuse = ma->septex & (1<<act);
1244                                         ma->septex &= ~(1<<act);
1245                                         ma->septex |= (ma->septex & (1<<(act-1))) << 1;
1246                                         ma->septex &= ~(1<<(act-1));
1247                                         ma->septex |= mtexuse >> 1;
1248                                 }
1249                                 
1250                                 set_active_mtex(id, act-1);
1251                         }
1252                 }
1253                 else { /* Down */
1254                         if(act < MAX_MTEX-1) {
1255                                 mtexswap = mtex_ar[act];
1256                                 mtex_ar[act] = mtex_ar[act+1];
1257                                 mtex_ar[act+1] = mtexswap;
1258                                 
1259                                 BKE_animdata_fix_paths_rename(id, ma->adt, "texture_slots", NULL, NULL, act+1, -1, 0);
1260                                 BKE_animdata_fix_paths_rename(id, ma->adt, "texture_slots", NULL, NULL, act, act+1, 0);
1261                                 BKE_animdata_fix_paths_rename(id, ma->adt, "texture_slots", NULL, NULL, -1, act, 0);
1262
1263                                 if(GS(id->name)==ID_MA) {
1264                                         Material *ma= (Material *)id;
1265                                         int mtexuse = ma->septex & (1<<act);
1266                                         ma->septex &= ~(1<<act);
1267                                         ma->septex |= (ma->septex & (1<<(act+1))) >> 1;
1268                                         ma->septex &= ~(1<<(act+1));
1269                                         ma->septex |= mtexuse << 1;
1270                                 }
1271                                 
1272                                 set_active_mtex(id, act+1);
1273                         }
1274                 }
1275
1276                 WM_event_add_notifier(C, NC_TEXTURE, CTX_data_scene(C));
1277         }
1278
1279         return OPERATOR_FINISHED;
1280 }
1281
1282 void TEXTURE_OT_slot_move(wmOperatorType *ot)
1283 {
1284         static EnumPropertyItem slot_move[] = {
1285                 {-1, "UP", 0, "Up", ""},
1286                 {1, "DOWN", 0, "Down", ""},
1287                 {0, NULL, 0, NULL, NULL}
1288         };
1289
1290         /* identifiers */
1291         ot->name= "Move Texture Slot";
1292         ot->idname= "TEXTURE_OT_slot_move";
1293         ot->description="Move texture slots up and down";
1294
1295         /* api callbacks */
1296         ot->exec= texture_slot_move;
1297
1298         /* flags */
1299         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1300
1301         RNA_def_enum(ot->srna, "type", slot_move, 0, "Type", "");
1302 }
1303
1304
1305
1306 /********************** environment map operators *********************/
1307
1308 static int save_envmap(wmOperator *op, Scene *scene, EnvMap *env, char *str, int imtype)
1309 {
1310         ImBuf *ibuf=NULL;
1311         int dx;
1312         int retval;
1313         
1314         if(env->cube[1]==NULL) {
1315                 BKE_report(op->reports, RPT_ERROR, "There is no generated environment map available to save");
1316                 return OPERATOR_CANCELLED;
1317         }
1318         
1319         dx= env->cube[1]->x;
1320         
1321         if (env->type == ENV_CUBE) {
1322                 ibuf = IMB_allocImBuf(3*dx, 2*dx, 24, IB_rectfloat, 0);
1323
1324                 IMB_rectcpy(ibuf, env->cube[0], 0, 0, 0, 0, dx, dx);
1325                 IMB_rectcpy(ibuf, env->cube[1], dx, 0, 0, 0, dx, dx);
1326                 IMB_rectcpy(ibuf, env->cube[2], 2*dx, 0, 0, 0, dx, dx);
1327                 IMB_rectcpy(ibuf, env->cube[3], 0, dx, 0, 0, dx, dx);
1328                 IMB_rectcpy(ibuf, env->cube[4], dx, dx, 0, 0, dx, dx);
1329                 IMB_rectcpy(ibuf, env->cube[5], 2*dx, dx, 0, 0, dx, dx);
1330         }
1331         else if (env->type == ENV_PLANE) {
1332                 ibuf = IMB_allocImBuf(dx, dx, 24, IB_rectfloat, 0);
1333                 IMB_rectcpy(ibuf, env->cube[1], 0, 0, 0, 0, dx, dx);            
1334         }
1335         
1336         if (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
1337                 ibuf->profile = IB_PROFILE_LINEAR_RGB;
1338         
1339         if (BKE_write_ibuf(scene, ibuf, str, imtype, scene->r.subimtype, scene->r.quality)) {
1340                 retval = OPERATOR_FINISHED;
1341         }
1342         else {
1343                 BKE_reportf(op->reports, RPT_ERROR, "Error saving environment map to %s.", str);
1344                 retval = OPERATOR_CANCELLED;
1345         }
1346         
1347         IMB_freeImBuf(ibuf);
1348         ibuf = NULL;
1349         
1350         return retval;
1351 }
1352
1353 static int envmap_save_exec(bContext *C, wmOperator *op)
1354 {
1355         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
1356         Scene *scene = CTX_data_scene(C);
1357         //int imtype = RNA_enum_get(op->ptr, "file_type");
1358         int imtype = scene->r.imtype;
1359         char path[FILE_MAX];
1360         
1361         RNA_string_get(op->ptr, "filepath", path);
1362         
1363         if(scene->r.scemode & R_EXTENSION)  {
1364                 BKE_add_image_extension(path, imtype);
1365         }
1366         
1367         WM_cursor_wait(1);
1368         
1369         save_envmap(op, scene, tex->env, path, imtype);
1370         
1371         WM_cursor_wait(0);
1372         
1373         WM_event_add_notifier(C, NC_TEXTURE, tex);
1374         
1375         return OPERATOR_FINISHED;
1376 }
1377
1378 static int envmap_save_invoke(bContext *C, wmOperator *op, wmEvent *event)
1379 {
1380         //Scene *scene= CTX_data_scene(C);
1381         
1382         if(!RNA_property_is_set(op->ptr, "relative_path"))
1383                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
1384         
1385         if(RNA_property_is_set(op->ptr, "filepath"))
1386                 return envmap_save_exec(C, op);
1387
1388         //RNA_enum_set(op->ptr, "file_type", scene->r.imtype);
1389         
1390         RNA_string_set(op->ptr, "filepath", G.sce);
1391         WM_event_add_fileselect(C, op);
1392         
1393         return OPERATOR_RUNNING_MODAL;
1394 }
1395
1396 static int envmap_save_poll(bContext *C)
1397 {
1398         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
1399
1400         if (!tex) 
1401                 return 0;
1402         if (!tex->env || !tex->env->ok)
1403                 return 0;
1404         if (tex->env->cube[1]==NULL)
1405                 return 0;
1406         
1407         return 1;
1408 }
1409
1410 void TEXTURE_OT_envmap_save(wmOperatorType *ot)
1411 {
1412         /* identifiers */
1413         ot->name= "Save Environment Map";
1414         ot->idname= "TEXTURE_OT_envmap_save";
1415         ot->description="Save the current generated Environment map to an image file";
1416         
1417         /* api callbacks */
1418         ot->exec= envmap_save_exec;
1419         ot->invoke= envmap_save_invoke;
1420         ot->poll= envmap_save_poll;
1421         
1422         /* flags */
1423         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1424         
1425         /* properties */
1426         //RNA_def_enum(ot->srna, "file_type", image_file_type_items, R_PNG, "File Type", "File type to save image as.");
1427         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_SAVE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
1428 }
1429
1430 static int envmap_clear_exec(bContext *C, wmOperator *op)
1431 {
1432         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
1433         
1434         BKE_free_envmapdata(tex->env);
1435         
1436         WM_event_add_notifier(C, NC_TEXTURE|NA_EDITED, tex);
1437         
1438         return OPERATOR_FINISHED;
1439 }
1440
1441 static int envmap_clear_poll(bContext *C)
1442 {
1443         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
1444         
1445         if (!tex) 
1446                 return 0;
1447         if (!tex->env || !tex->env->ok)
1448                 return 0;
1449         if (tex->env->cube[1]==NULL)
1450                 return 0;
1451         
1452         return 1;
1453 }
1454
1455 void TEXTURE_OT_envmap_clear(wmOperatorType *ot)
1456 {
1457         /* identifiers */
1458         ot->name= "Clear Environment Map";
1459         ot->idname= "TEXTURE_OT_envmap_clear";
1460         ot->description="Discard the environment map and free it from memory";
1461         
1462         /* api callbacks */
1463         ot->exec= envmap_clear_exec;
1464         ot->poll= envmap_clear_poll;
1465         
1466         /* flags */
1467         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1468 }
1469
1470 static int envmap_clear_all_exec(bContext *C, wmOperator *op)
1471 {
1472         Main *bmain = CTX_data_main(C);
1473         Tex *tex;
1474         
1475         for (tex=bmain->tex.first; tex; tex=tex->id.next)
1476                 if (tex->env)
1477                         BKE_free_envmapdata(tex->env);
1478         
1479         WM_event_add_notifier(C, NC_TEXTURE|NA_EDITED, tex);
1480         
1481         return OPERATOR_FINISHED;
1482 }
1483
1484 void TEXTURE_OT_envmap_clear_all(wmOperatorType *ot)
1485 {
1486         /* identifiers */
1487         ot->name= "Clear All Environment Maps";
1488         ot->idname= "TEXTURE_OT_envmap_clear_all";
1489         ot->description="Discard all environment maps in the .blend file and free them from memory";
1490         
1491         /* api callbacks */
1492         ot->exec= envmap_clear_all_exec;
1493         ot->poll= envmap_clear_poll;
1494         
1495         /* flags */
1496         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1497 }
1498
1499 /********************** material operators *********************/
1500
1501 /* material copy/paste */
1502 static int copy_material_exec(bContext *C, wmOperator *op)
1503 {
1504         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
1505
1506         if(ma==NULL)
1507                 return OPERATOR_CANCELLED;
1508
1509         copy_matcopybuf(ma);
1510
1511         WM_event_add_notifier(C, NC_MATERIAL, ma);
1512
1513         return OPERATOR_FINISHED;
1514 }
1515
1516 void MATERIAL_OT_copy(wmOperatorType *ot)
1517 {
1518         /* identifiers */
1519         ot->name= "Copy Material";
1520         ot->idname= "MATERIAL_OT_copy";
1521         ot->description="Copy the material settings and nodes";
1522
1523         /* api callbacks */
1524         ot->exec= copy_material_exec;
1525
1526         /* flags */
1527         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1528 }
1529
1530 static int paste_material_exec(bContext *C, wmOperator *op)
1531 {
1532         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
1533
1534         if(ma==NULL)
1535                 return OPERATOR_CANCELLED;
1536
1537         paste_matcopybuf(ma);
1538
1539         WM_event_add_notifier(C, NC_MATERIAL|ND_SHADING_DRAW, ma);
1540
1541         return OPERATOR_FINISHED;
1542 }
1543
1544 void MATERIAL_OT_paste(wmOperatorType *ot)
1545 {
1546         /* identifiers */
1547         ot->name= "Paste Material";
1548         ot->idname= "MATERIAL_OT_paste";
1549         ot->description="Paste the material settings and nodes";
1550
1551         /* api callbacks */
1552         ot->exec= paste_material_exec;
1553
1554         /* flags */
1555         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1556 }
1557
1558
1559 static short mtexcopied=0; /* must be reset on file load */
1560 static MTex mtexcopybuf;
1561
1562 void ED_render_clear_mtex_copybuf(void)
1563 {       /* use for file reload */
1564         mtexcopied= 0;
1565 }
1566
1567 void copy_mtex_copybuf(ID *id)
1568 {
1569         MTex **mtex= NULL;
1570         
1571         switch(GS(id->name)) {
1572                 case ID_MA:
1573                         mtex= &(((Material *)id)->mtex[(int)((Material *)id)->texact]);
1574                         break;
1575                 case ID_LA:
1576                         mtex= &(((Lamp *)id)->mtex[(int)((Lamp *)id)->texact]);
1577                         // la->mtex[(int)la->texact] // TODO
1578                         break;
1579                 case ID_WO:
1580                         mtex= &(((World *)id)->mtex[(int)((World *)id)->texact]);
1581                         // mtex= wrld->mtex[(int)wrld->texact]; // TODO
1582                         break;
1583         }
1584         
1585         if(mtex && *mtex) {
1586                 memcpy(&mtexcopybuf, *mtex, sizeof(MTex));
1587                 mtexcopied= 1;
1588         }
1589         else {
1590                 mtexcopied= 0;
1591         }
1592 }
1593
1594 void paste_mtex_copybuf(ID *id)
1595 {
1596         MTex **mtex= NULL;
1597         
1598         if(mtexcopied == 0 || mtexcopybuf.tex==NULL)
1599                 return;
1600         
1601         switch(GS(id->name)) {
1602                 case ID_MA:
1603                         mtex= &(((Material *)id)->mtex[(int)((Material *)id)->texact]);
1604                         break;
1605                 case ID_LA:
1606                         mtex= &(((Lamp *)id)->mtex[(int)((Lamp *)id)->texact]);
1607                         // la->mtex[(int)la->texact] // TODO
1608                         break;
1609                 case ID_WO:
1610                         mtex= &(((World *)id)->mtex[(int)((World *)id)->texact]);
1611                         // mtex= wrld->mtex[(int)wrld->texact]; // TODO
1612                         break;
1613         }
1614         
1615         if(mtex) {
1616                 if(*mtex==NULL) {
1617                         *mtex= MEM_mallocN(sizeof(MTex), "mtex copy");
1618                 }
1619                 else if((*mtex)->tex) {
1620                         (*mtex)->tex->id.us--;
1621                 }
1622                 
1623                 memcpy(*mtex, &mtexcopybuf, sizeof(MTex));
1624                 
1625                 id_us_plus((ID *)mtexcopybuf.tex);
1626         }
1627 }
1628
1629
1630 static int copy_mtex_exec(bContext *C, wmOperator *op)
1631 {
1632         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
1633
1634         if(id==NULL) {
1635                 /* copying empty slot */
1636                 ED_render_clear_mtex_copybuf();
1637                 return OPERATOR_CANCELLED;
1638         }
1639
1640         copy_mtex_copybuf(id);
1641
1642         WM_event_add_notifier(C, NC_TEXTURE, NULL);
1643
1644         return OPERATOR_FINISHED;
1645 }
1646
1647 static int copy_mtex_poll(bContext *C)
1648 {
1649         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
1650         
1651         return (id != NULL);
1652 }
1653
1654 void TEXTURE_OT_slot_copy(wmOperatorType *ot)
1655 {
1656         /* identifiers */
1657         ot->name= "Copy Texture Slot Settings";
1658         ot->idname= "TEXTURE_OT_slot_copy";
1659         ot->description="Copy the material texture settings and nodes";
1660
1661         /* api callbacks */
1662         ot->exec= copy_mtex_exec;
1663         ot->poll= copy_mtex_poll;
1664         
1665         /* flags */
1666         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1667 }
1668
1669 static int paste_mtex_exec(bContext *C, wmOperator *op)
1670 {
1671         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
1672
1673         if(id==NULL) {
1674                 Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
1675                 Lamp *la= CTX_data_pointer_get_type(C, "lamp", &RNA_Lamp).data;
1676                 World *wo= CTX_data_pointer_get_type(C, "world", &RNA_World).data;
1677                 
1678                 if (ma)
1679                         id = &ma->id;
1680                 else if (la)
1681                         id = &la->id;
1682                 else if (wo)
1683                         id = &wo->id;
1684                 
1685                 if (id==NULL)
1686                         return OPERATOR_CANCELLED;
1687         }
1688
1689         paste_mtex_copybuf(id);
1690
1691         WM_event_add_notifier(C, NC_TEXTURE|ND_SHADING_DRAW, NULL);
1692
1693         return OPERATOR_FINISHED;
1694 }
1695
1696 void TEXTURE_OT_slot_paste(wmOperatorType *ot)
1697 {
1698         /* identifiers */
1699         ot->name= "Paste Texture Slot Settings";
1700         ot->idname= "TEXTURE_OT_slot_paste";
1701         ot->description="Copy the texture settings and nodes";
1702
1703         /* api callbacks */
1704         ot->exec= paste_mtex_exec;
1705
1706         /* flags */
1707         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1708 }