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