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