remove unused vars
[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_main.h"
62 #include "BKE_material.h"
63 #include "BKE_node.h"
64 #include "BKE_report.h"
65 #include "BKE_scene.h"
66 #include "BKE_texture.h"
67 #include "BKE_world.h"
68 #include "BKE_tessmesh.h"
69
70 #include "IMB_imbuf.h"
71 #include "IMB_imbuf_types.h"
72
73 #include "GPU_material.h"
74
75 #include "RNA_access.h"
76
77 #include "WM_api.h"
78 #include "WM_types.h"
79
80 #include "ED_curve.h"
81 #include "ED_mesh.h"
82 #include "ED_render.h"
83 #include "ED_screen.h"
84
85 #include "RNA_define.h"
86
87 #include "UI_interface.h"
88
89 #include "render_intern.h"      // own include
90
91 /***************************** Updates ***********************************
92  * ED_render_id_flush_update gets called from DAG_id_tag_update, to do *
93  * editor level updates when the ID changes. when these ID blocks are in *
94  * the dependency graph, we can get rid of the manual dependency checks  */
95
96 static int mtex_use_tex(MTex **mtex, int tot, Tex *tex)
97 {
98         int a;
99
100         if(!mtex)
101                 return 0;
102
103         for(a=0; a<tot; a++)
104                 if(mtex[a] && mtex[a]->tex == tex)
105                         return 1;
106         
107         return 0;
108 }
109
110 static int nodes_use_tex(bNodeTree *ntree, Tex *tex)
111 {
112         bNode *node;
113
114         for(node=ntree->nodes.first; node; node= node->next) {
115                 if(node->id) {
116                         if(node->id == (ID*)tex) {
117                                 return 1;
118                         }
119                         else if(node->type==NODE_GROUP) {
120                                 if(nodes_use_tex((bNodeTree *)node->id, tex))
121                                         return 1;
122                         }
123                 }
124         }
125
126         return 0;
127 }
128
129 static void material_changed(Main *UNUSED(bmain), Material *ma)
130 {
131         /* icons */
132         BKE_icon_changed(BKE_icon_getid(&ma->id));
133
134         /* glsl */
135         if(ma->gpumaterial.first)
136                 GPU_material_free(ma);
137 }
138
139 static void texture_changed(Main *bmain, Tex *tex)
140 {
141         Material *ma;
142         Lamp *la;
143         World *wo;
144
145         /* icons */
146         BKE_icon_changed(BKE_icon_getid(&tex->id));
147
148         /* find materials */
149         for(ma=bmain->mat.first; ma; ma=ma->id.next) {
150                 if(mtex_use_tex(ma->mtex, MAX_MTEX, tex));
151                 else if(ma->use_nodes && ma->nodetree && nodes_use_tex(ma->nodetree, tex));
152                 else continue;
153
154                 BKE_icon_changed(BKE_icon_getid(&ma->id));
155
156                 if(ma->gpumaterial.first)
157                         GPU_material_free(ma);
158         }
159
160         /* find lamps */
161         for(la=bmain->lamp.first; la; la=la->id.next) {
162                 if(mtex_use_tex(la->mtex, MAX_MTEX, tex));
163                 else continue;
164
165                 BKE_icon_changed(BKE_icon_getid(&la->id));
166         }
167
168         /* find worlds */
169         for(wo=bmain->world.first; wo; wo=wo->id.next) {
170                 if(mtex_use_tex(wo->mtex, MAX_MTEX, tex));
171                 else continue;
172
173                 BKE_icon_changed(BKE_icon_getid(&wo->id));
174         }
175 }
176
177 static void lamp_changed(Main *bmain, Lamp *la)
178 {
179         Object *ob;
180         Material *ma;
181
182         /* icons */
183         BKE_icon_changed(BKE_icon_getid(&la->id));
184
185         /* glsl */
186         for(ob=bmain->object.first; ob; ob=ob->id.next)
187                 if(ob->data == la && ob->gpulamp.first)
188                         GPU_lamp_free(ob);
189
190         for(ma=bmain->mat.first; ma; ma=ma->id.next)
191                 if(ma->gpumaterial.first)
192                         GPU_material_free(ma);
193 }
194
195 static void world_changed(Main *bmain, World *wo)
196 {
197         Material *ma;
198
199         /* icons */
200         BKE_icon_changed(BKE_icon_getid(&wo->id));
201
202         /* glsl */
203         for(ma=bmain->mat.first; ma; ma=ma->id.next)
204                 if(ma->gpumaterial.first)
205                         GPU_material_free(ma);
206 }
207
208 static void image_changed(Main *bmain, Image *ima)
209 {
210         Tex *tex;
211
212         /* icons */
213         BKE_icon_changed(BKE_icon_getid(&ima->id));
214
215         /* textures */
216         for(tex=bmain->tex.first; tex; tex=tex->id.next)
217                 if(tex->ima == ima)
218                         texture_changed(bmain, tex);
219 }
220
221 static void scene_changed(Main *bmain, Scene *UNUSED(scene))
222 {
223         Object *ob;
224         Material *ma;
225
226         /* glsl */
227         for(ob=bmain->object.first; ob; ob=ob->id.next)
228                 if(ob->gpulamp.first)
229                         GPU_lamp_free(ob);
230
231         for(ma=bmain->mat.first; ma; ma=ma->id.next)
232                 if(ma->gpumaterial.first)
233                         GPU_material_free(ma);
234 }
235
236 void ED_render_id_flush_update(Main *bmain, ID *id)
237 {
238         if(!id)
239                 return;
240
241         switch(GS(id->name)) {
242                 case ID_MA:
243                         material_changed(bmain, (Material*)id);
244                         break;
245                 case ID_TE:
246                         texture_changed(bmain, (Tex*)id);
247                         break;
248                 case ID_WO:
249                         world_changed(bmain, (World*)id);
250                         break;
251                 case ID_LA:
252                         lamp_changed(bmain, (Lamp*)id);
253                         break;
254                 case ID_IM:
255                         image_changed(bmain, (Image*)id);
256                         break;
257                 case ID_SCE:
258                         scene_changed(bmain, (Scene*)id);
259                         break;
260                 default:
261                         break;
262         }
263 }
264
265 /********************** material slot operators *********************/
266
267 static int material_slot_add_exec(bContext *C, wmOperator *UNUSED(op))
268 {
269         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
270
271         if(!ob)
272                 return OPERATOR_CANCELLED;
273
274         object_add_material_slot(ob);
275         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
276         WM_event_add_notifier(C, NC_OBJECT|ND_OB_SHADING, ob);
277         
278         return OPERATOR_FINISHED;
279 }
280
281 void OBJECT_OT_material_slot_add(wmOperatorType *ot)
282 {
283         /* identifiers */
284         ot->name= "Add Material Slot";
285         ot->idname= "OBJECT_OT_material_slot_add";
286         ot->description="Add a new material slot or duplicate the selected one";
287         
288         /* api callbacks */
289         ot->exec= material_slot_add_exec;
290         ot->poll= ED_operator_object_active_editable;
291
292         /* flags */
293         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
294 }
295
296 static int material_slot_remove_exec(bContext *C, wmOperator *op)
297 {
298         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
299
300         if(!ob)
301                 return OPERATOR_CANCELLED;
302
303         /* Removing material slots in edit mode screws things up, see bug #21822.*/
304         if(ob == CTX_data_edit_object(C)) {
305                 BKE_report(op->reports, RPT_ERROR, "Unable to remove material slot in edit mode.");
306                 return OPERATOR_CANCELLED;
307         }
308
309         object_remove_material_slot(ob);
310         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
311         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
312         WM_event_add_notifier(C, NC_OBJECT|ND_OB_SHADING, ob);
313         
314         return OPERATOR_FINISHED;
315 }
316
317 void OBJECT_OT_material_slot_remove(wmOperatorType *ot)
318 {
319         /* identifiers */
320         ot->name= "Remove Material Slot";
321         ot->idname= "OBJECT_OT_material_slot_remove";
322         ot->description="Remove the selected material slot";
323         
324         /* api callbacks */
325         ot->exec= material_slot_remove_exec;
326         ot->poll= ED_operator_object_active_editable;
327
328         /* flags */
329         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
330 }
331
332 static int material_slot_assign_exec(bContext *C, wmOperator *UNUSED(op))
333 {
334         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
335
336         if(!ob)
337                 return OPERATOR_CANCELLED;
338
339         if(ob && ob->actcol>0) {
340                 if(ob->type == OB_MESH) {
341                         BMEditMesh *em= ((Mesh*)ob->data)->edit_btmesh;
342                         BMFace *efa;
343                         BMIter iter;
344
345                         if(em) {
346                                 BM_ITER(efa, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
347                                         if(BM_TestHFlag(efa, BM_SELECT))
348                                                 efa->mat_nr= ob->actcol-1;
349                                 }
350                         }
351                 }
352                 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
353                         Nurb *nu;
354                         ListBase *nurbs= ED_curve_editnurbs((Curve*)ob->data);
355
356                         if(nurbs) {
357                                 for(nu= nurbs->first; nu; nu= nu->next)
358                                         if(isNurbsel(nu))
359                                                 nu->mat_nr= nu->charidx= ob->actcol-1;
360                         }
361                 }
362                 else if(ob->type == OB_FONT) {
363                         EditFont *ef= ((Curve*)ob->data)->editfont;
364                         int i, selstart, selend;
365
366                         if(ef && BKE_font_getselection(ob, &selstart, &selend)) {
367                                 for(i=selstart; i<=selend; i++)
368                                         ef->textbufinfo[i].mat_nr = ob->actcol;
369                         }
370                 }
371         }
372
373         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
374         WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
375         
376         return OPERATOR_FINISHED;
377 }
378
379 void OBJECT_OT_material_slot_assign(wmOperatorType *ot)
380 {
381         /* identifiers */
382         ot->name= "Assign Material Slot";
383         ot->idname= "OBJECT_OT_material_slot_assign";
384         ot->description="Assign the material in the selected material slot to the selected vertices";
385         
386         /* api callbacks */
387         ot->exec= material_slot_assign_exec;
388         ot->poll= ED_operator_object_active_editable;
389
390         /* flags */
391         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
392 }
393
394 static int material_slot_de_select(bContext *C, int select)
395 {
396         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
397
398         if(!ob)
399                 return OPERATOR_CANCELLED;
400
401         if(ob->type == OB_MESH) {
402 /*BMESH_TODO
403                 BMEditMesh *em= ((Mesh*)ob->data)->edit_btmesh;
404                 if(em) {
405                         if(select)
406                                 EM_select_by_material(em, ob->actcol-1);
407                         else
408                                 EM_deselect_by_material(em, ob->actcol-1);
409                 }
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 texture_slot_move(bContext *C, wmOperator *op)
753 {
754         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
755
756         if(id) {
757                 MTex **mtex_ar, *mtexswap;
758                 short act;
759                 int type= RNA_enum_get(op->ptr, "type");
760                 struct AnimData *adt= BKE_animdata_from_id(id);
761
762                 give_active_mtex(id, &mtex_ar, &act);
763
764                 if(type == -1) { /* Up */
765                         if(act > 0) {
766                                 mtexswap = mtex_ar[act];
767                                 mtex_ar[act] = mtex_ar[act-1];
768                                 mtex_ar[act-1] = mtexswap;
769                                 
770                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, act-1, -1, 0);
771                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, act, act-1, 0);
772                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, -1, act, 0);
773
774                                 if(GS(id->name)==ID_MA) {
775                                         Material *ma= (Material *)id;
776                                         int mtexuse = ma->septex & (1<<act);
777                                         ma->septex &= ~(1<<act);
778                                         ma->septex |= (ma->septex & (1<<(act-1))) << 1;
779                                         ma->septex &= ~(1<<(act-1));
780                                         ma->septex |= mtexuse >> 1;
781                                 }
782                                 
783                                 set_active_mtex(id, act-1);
784                         }
785                 }
786                 else { /* Down */
787                         if(act < MAX_MTEX-1) {
788                                 mtexswap = mtex_ar[act];
789                                 mtex_ar[act] = mtex_ar[act+1];
790                                 mtex_ar[act+1] = mtexswap;
791                                 
792                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, act+1, -1, 0);
793                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, act, act+1, 0);
794                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, -1, act, 0);
795
796                                 if(GS(id->name)==ID_MA) {
797                                         Material *ma= (Material *)id;
798                                         int mtexuse = ma->septex & (1<<act);
799                                         ma->septex &= ~(1<<act);
800                                         ma->septex |= (ma->septex & (1<<(act+1))) >> 1;
801                                         ma->septex &= ~(1<<(act+1));
802                                         ma->septex |= mtexuse << 1;
803                                 }
804                                 
805                                 set_active_mtex(id, act+1);
806                         }
807                 }
808
809                 DAG_id_tag_update(id, 0);
810                 WM_event_add_notifier(C, NC_TEXTURE, CTX_data_scene(C));
811         }
812
813         return OPERATOR_FINISHED;
814 }
815
816 void TEXTURE_OT_slot_move(wmOperatorType *ot)
817 {
818         static EnumPropertyItem slot_move[] = {
819                 {-1, "UP", 0, "Up", ""},
820                 {1, "DOWN", 0, "Down", ""},
821                 {0, NULL, 0, NULL, NULL}
822         };
823
824         /* identifiers */
825         ot->name= "Move Texture Slot";
826         ot->idname= "TEXTURE_OT_slot_move";
827         ot->description="Move texture slots up and down";
828
829         /* api callbacks */
830         ot->exec= texture_slot_move;
831
832         /* flags */
833         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
834
835         RNA_def_enum(ot->srna, "type", slot_move, 0, "Type", "");
836 }
837
838
839
840 /********************** environment map operators *********************/
841
842 static int save_envmap(wmOperator *op, Scene *scene, EnvMap *env, char *str, int imtype)
843 {
844         ImBuf *ibuf=NULL;
845         int dx;
846         int retval;
847         int relative= (RNA_struct_find_property(op->ptr, "relative_path") && RNA_boolean_get(op->ptr, "relative_path"));
848         
849         if(env->cube[1]==NULL) {
850                 BKE_report(op->reports, RPT_ERROR, "There is no generated environment map available to save");
851                 return OPERATOR_CANCELLED;
852         }
853         
854         dx= env->cube[1]->x;
855         
856         if (env->type == ENV_CUBE) {
857                 ibuf = IMB_allocImBuf(3*dx, 2*dx, 24, IB_rectfloat);
858
859                 IMB_rectcpy(ibuf, env->cube[0], 0, 0, 0, 0, dx, dx);
860                 IMB_rectcpy(ibuf, env->cube[1], dx, 0, 0, 0, dx, dx);
861                 IMB_rectcpy(ibuf, env->cube[2], 2*dx, 0, 0, 0, dx, dx);
862                 IMB_rectcpy(ibuf, env->cube[3], 0, dx, 0, 0, dx, dx);
863                 IMB_rectcpy(ibuf, env->cube[4], dx, dx, 0, 0, dx, dx);
864                 IMB_rectcpy(ibuf, env->cube[5], 2*dx, dx, 0, 0, dx, dx);
865         }
866         else if (env->type == ENV_PLANE) {
867                 ibuf = IMB_allocImBuf(dx, dx, 24, IB_rectfloat);
868                 IMB_rectcpy(ibuf, env->cube[1], 0, 0, 0, 0, dx, dx);            
869         }
870         else {
871                 BKE_report(op->reports, RPT_ERROR, "Invalid environment map type");
872                 return OPERATOR_CANCELLED;
873         }
874         
875         if (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
876                 ibuf->profile = IB_PROFILE_LINEAR_RGB;
877         
878         /* to save, we first get absolute path */
879         BLI_path_abs(str, G.main->name);
880         
881         if (BKE_write_ibuf(ibuf, str, imtype, scene->r.subimtype, scene->r.quality)) {
882                 retval = OPERATOR_FINISHED;
883         }
884         else {
885                 BKE_reportf(op->reports, RPT_ERROR, "Error saving environment map to %s.", str);
886                 retval = OPERATOR_CANCELLED;
887         }
888         /* in case we were saving with relative paths, change back again */
889         if(relative)
890                 BLI_path_rel(str, G.main->name);
891         
892         IMB_freeImBuf(ibuf);
893         ibuf = NULL;
894         
895         return retval;
896 }
897
898 static int envmap_save_exec(bContext *C, wmOperator *op)
899 {
900         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
901         Scene *scene = CTX_data_scene(C);
902         //int imtype = RNA_enum_get(op->ptr, "file_type");
903         int imtype = scene->r.imtype;
904         char path[FILE_MAX];
905         
906         RNA_string_get(op->ptr, "filepath", path);
907         
908         if(scene->r.scemode & R_EXTENSION)  {
909                 BKE_add_image_extension(path, imtype);
910         }
911         
912         WM_cursor_wait(1);
913         
914         save_envmap(op, scene, tex->env, path, imtype);
915         
916         WM_cursor_wait(0);
917         
918         WM_event_add_notifier(C, NC_TEXTURE, tex);
919         
920         return OPERATOR_FINISHED;
921 }
922
923 static int envmap_save_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
924 {
925         //Scene *scene= CTX_data_scene(C);
926         
927         if(!RNA_property_is_set(op->ptr, "relative_path"))
928                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
929         
930         if(RNA_property_is_set(op->ptr, "filepath"))
931                 return envmap_save_exec(C, op);
932
933         //RNA_enum_set(op->ptr, "file_type", scene->r.imtype);
934         
935         RNA_string_set(op->ptr, "filepath", G.main->name);
936         WM_event_add_fileselect(C, op);
937         
938         return OPERATOR_RUNNING_MODAL;
939 }
940
941 static int envmap_save_poll(bContext *C)
942 {
943         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
944
945         if (!tex) 
946                 return 0;
947         if (!tex->env || !tex->env->ok)
948                 return 0;
949         if (tex->env->cube[1]==NULL)
950                 return 0;
951         
952         return 1;
953 }
954
955 void TEXTURE_OT_envmap_save(wmOperatorType *ot)
956 {
957         /* identifiers */
958         ot->name= "Save Environment Map";
959         ot->idname= "TEXTURE_OT_envmap_save";
960         ot->description="Save the current generated Environment map to an image file";
961         
962         /* api callbacks */
963         ot->exec= envmap_save_exec;
964         ot->invoke= envmap_save_invoke;
965         ot->poll= envmap_save_poll;
966         
967         /* flags */
968         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
969         
970         /* properties */
971         //RNA_def_enum(ot->srna, "file_type", image_file_type_items, R_PNG, "File Type", "File type to save image as.");
972         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_SAVE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
973 }
974
975 static int envmap_clear_exec(bContext *C, wmOperator *UNUSED(op))
976 {
977         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
978         
979         BKE_free_envmapdata(tex->env);
980         
981         WM_event_add_notifier(C, NC_TEXTURE|NA_EDITED, tex);
982         
983         return OPERATOR_FINISHED;
984 }
985
986 static int envmap_clear_poll(bContext *C)
987 {
988         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
989         
990         if (!tex) 
991                 return 0;
992         if (!tex->env || !tex->env->ok)
993                 return 0;
994         if (tex->env->cube[1]==NULL)
995                 return 0;
996         
997         return 1;
998 }
999
1000 void TEXTURE_OT_envmap_clear(wmOperatorType *ot)
1001 {
1002         /* identifiers */
1003         ot->name= "Clear Environment Map";
1004         ot->idname= "TEXTURE_OT_envmap_clear";
1005         ot->description="Discard the environment map and free it from memory";
1006         
1007         /* api callbacks */
1008         ot->exec= envmap_clear_exec;
1009         ot->poll= envmap_clear_poll;
1010         
1011         /* flags */
1012         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1013 }
1014
1015 static int envmap_clear_all_exec(bContext *C, wmOperator *UNUSED(op))
1016 {
1017         Main *bmain = CTX_data_main(C);
1018         Tex *tex;
1019         
1020         for (tex=bmain->tex.first; tex; tex=tex->id.next)
1021                 if (tex->env)
1022                         BKE_free_envmapdata(tex->env);
1023         
1024         WM_event_add_notifier(C, NC_TEXTURE|NA_EDITED, tex);
1025         
1026         return OPERATOR_FINISHED;
1027 }
1028
1029 void TEXTURE_OT_envmap_clear_all(wmOperatorType *ot)
1030 {
1031         /* identifiers */
1032         ot->name= "Clear All Environment Maps";
1033         ot->idname= "TEXTURE_OT_envmap_clear_all";
1034         ot->description="Discard all environment maps in the .blend file and free them from memory";
1035         
1036         /* api callbacks */
1037         ot->exec= envmap_clear_all_exec;
1038         ot->poll= envmap_clear_poll;
1039         
1040         /* flags */
1041         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1042 }
1043
1044 /********************** material operators *********************/
1045
1046 /* material copy/paste */
1047 static int copy_material_exec(bContext *C, wmOperator *UNUSED(op))
1048 {
1049         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
1050
1051         if(ma==NULL)
1052                 return OPERATOR_CANCELLED;
1053
1054         copy_matcopybuf(ma);
1055
1056         WM_event_add_notifier(C, NC_MATERIAL, ma);
1057
1058         return OPERATOR_FINISHED;
1059 }
1060
1061 void MATERIAL_OT_copy(wmOperatorType *ot)
1062 {
1063         /* identifiers */
1064         ot->name= "Copy Material";
1065         ot->idname= "MATERIAL_OT_copy";
1066         ot->description="Copy the material settings and nodes";
1067
1068         /* api callbacks */
1069         ot->exec= copy_material_exec;
1070
1071         /* flags */
1072         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1073 }
1074
1075 static int paste_material_exec(bContext *C, wmOperator *UNUSED(op))
1076 {
1077         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
1078
1079         if(ma==NULL)
1080                 return OPERATOR_CANCELLED;
1081
1082         paste_matcopybuf(ma);
1083
1084         WM_event_add_notifier(C, NC_MATERIAL|ND_SHADING_DRAW, ma);
1085
1086         return OPERATOR_FINISHED;
1087 }
1088
1089 void MATERIAL_OT_paste(wmOperatorType *ot)
1090 {
1091         /* identifiers */
1092         ot->name= "Paste Material";
1093         ot->idname= "MATERIAL_OT_paste";
1094         ot->description="Paste the material settings and nodes";
1095
1096         /* api callbacks */
1097         ot->exec= paste_material_exec;
1098
1099         /* flags */
1100         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1101 }
1102
1103
1104 static short mtexcopied=0; /* must be reset on file load */
1105 static MTex mtexcopybuf;
1106
1107 void ED_render_clear_mtex_copybuf(void)
1108 {       /* use for file reload */
1109         mtexcopied= 0;
1110 }
1111
1112 static void copy_mtex_copybuf(ID *id)
1113 {
1114         MTex **mtex= NULL;
1115         
1116         switch(GS(id->name)) {
1117                 case ID_MA:
1118                         mtex= &(((Material *)id)->mtex[(int)((Material *)id)->texact]);
1119                         break;
1120                 case ID_LA:
1121                         mtex= &(((Lamp *)id)->mtex[(int)((Lamp *)id)->texact]);
1122                         // la->mtex[(int)la->texact] // TODO
1123                         break;
1124                 case ID_WO:
1125                         mtex= &(((World *)id)->mtex[(int)((World *)id)->texact]);
1126                         // mtex= wrld->mtex[(int)wrld->texact]; // TODO
1127                         break;
1128                 case ID_PA:
1129                         mtex= &(((ParticleSettings *)id)->mtex[(int)((ParticleSettings *)id)->texact]);
1130                         break;
1131         }
1132         
1133         if(mtex && *mtex) {
1134                 memcpy(&mtexcopybuf, *mtex, sizeof(MTex));
1135                 mtexcopied= 1;
1136         }
1137         else {
1138                 mtexcopied= 0;
1139         }
1140 }
1141
1142 static void paste_mtex_copybuf(ID *id)
1143 {
1144         MTex **mtex= NULL;
1145         
1146         if(mtexcopied == 0 || mtexcopybuf.tex==NULL)
1147                 return;
1148         
1149         switch(GS(id->name)) {
1150                 case ID_MA:
1151                         mtex= &(((Material *)id)->mtex[(int)((Material *)id)->texact]);
1152                         break;
1153                 case ID_LA:
1154                         mtex= &(((Lamp *)id)->mtex[(int)((Lamp *)id)->texact]);
1155                         // la->mtex[(int)la->texact] // TODO
1156                         break;
1157                 case ID_WO:
1158                         mtex= &(((World *)id)->mtex[(int)((World *)id)->texact]);
1159                         // mtex= wrld->mtex[(int)wrld->texact]; // TODO
1160                         break;
1161                 case ID_PA:
1162                         mtex= &(((ParticleSettings *)id)->mtex[(int)((ParticleSettings *)id)->texact]);
1163                         break;
1164                 default:
1165                         BLI_assert("invalid id type");
1166                         return;
1167         }
1168         
1169         if(mtex) {
1170                 if(*mtex==NULL) {
1171                         *mtex= MEM_mallocN(sizeof(MTex), "mtex copy");
1172                 }
1173                 else if((*mtex)->tex) {
1174                         (*mtex)->tex->id.us--;
1175                 }
1176                 
1177                 memcpy(*mtex, &mtexcopybuf, sizeof(MTex));
1178                 
1179                 id_us_plus((ID *)mtexcopybuf.tex);
1180         }
1181 }
1182
1183
1184 static int copy_mtex_exec(bContext *C, wmOperator *UNUSED(op))
1185 {
1186         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
1187
1188         if(id==NULL) {
1189                 /* copying empty slot */
1190                 ED_render_clear_mtex_copybuf();
1191                 return OPERATOR_CANCELLED;
1192         }
1193
1194         copy_mtex_copybuf(id);
1195
1196         WM_event_add_notifier(C, NC_TEXTURE, NULL);
1197
1198         return OPERATOR_FINISHED;
1199 }
1200
1201 static int copy_mtex_poll(bContext *C)
1202 {
1203         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
1204         
1205         return (id != NULL);
1206 }
1207
1208 void TEXTURE_OT_slot_copy(wmOperatorType *ot)
1209 {
1210         /* identifiers */
1211         ot->name= "Copy Texture Slot Settings";
1212         ot->idname= "TEXTURE_OT_slot_copy";
1213         ot->description="Copy the material texture settings and nodes";
1214
1215         /* api callbacks */
1216         ot->exec= copy_mtex_exec;
1217         ot->poll= copy_mtex_poll;
1218         
1219         /* flags */
1220         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1221 }
1222
1223 static int paste_mtex_exec(bContext *C, wmOperator *UNUSED(op))
1224 {
1225         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
1226
1227         if(id==NULL) {
1228                 Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
1229                 Lamp *la= CTX_data_pointer_get_type(C, "lamp", &RNA_Lamp).data;
1230                 World *wo= CTX_data_pointer_get_type(C, "world", &RNA_World).data;
1231                 ParticleSystem *psys= CTX_data_pointer_get_type(C, "particle_system", &RNA_ParticleSystem).data;
1232                 
1233                 if (ma)
1234                         id = &ma->id;
1235                 else if (la)
1236                         id = &la->id;
1237                 else if (wo)
1238                         id = &wo->id;
1239                 else if (psys)
1240                         id = &psys->part->id;
1241                 
1242                 if (id==NULL)
1243                         return OPERATOR_CANCELLED;
1244         }
1245
1246         paste_mtex_copybuf(id);
1247
1248         WM_event_add_notifier(C, NC_TEXTURE|ND_SHADING_DRAW, NULL);
1249
1250         return OPERATOR_FINISHED;
1251 }
1252
1253 void TEXTURE_OT_slot_paste(wmOperatorType *ot)
1254 {
1255         /* identifiers */
1256         ot->name= "Paste Texture Slot Settings";
1257         ot->idname= "TEXTURE_OT_slot_paste";
1258         ot->description="Copy the texture settings and nodes";
1259
1260         /* api callbacks */
1261         ot->exec= paste_mtex_exec;
1262
1263         /* flags */
1264         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1265 }