9fd0074b9f1c20d066b1305d0a84cb8dfa5478eb
[blender-staging.git] / source / blender / editors / render / render_shading.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * The Original Code is Copyright (C) 2009 Blender Foundation.
20  * All rights reserved.
21  *
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "MEM_guardedalloc.h"
31
32 #include "DNA_curve_types.h"
33 #include "DNA_lamp_types.h"
34 #include "DNA_material_types.h"
35 #include "DNA_node_types.h"
36 #include "DNA_object_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_space_types.h"
39 #include "DNA_world_types.h"
40
41 #include "BKE_animsys.h"
42 #include "BKE_context.h"
43 #include "BKE_depsgraph.h"
44 #include "BKE_font.h"
45 #include "BKE_global.h"
46 #include "BKE_icons.h"
47 #include "BKE_image.h"
48 #include "BKE_library.h"
49 #include "BKE_main.h"
50 #include "BKE_material.h"
51 #include "BKE_node.h"
52 #include "BKE_report.h"
53 #include "BKE_scene.h"
54 #include "BKE_texture.h"
55 #include "BKE_utildefines.h"
56 #include "BKE_world.h"
57
58 #include "IMB_imbuf.h"
59 #include "IMB_imbuf_types.h"
60
61 #include "BLI_blenlib.h"
62 #include "BLI_math.h"
63 #include "BLI_editVert.h"
64 #include "BLI_listbase.h"
65
66 #include "GPU_material.h"
67
68 #include "RNA_access.h"
69
70 #include "WM_api.h"
71 #include "WM_types.h"
72
73 #include "ED_curve.h"
74 #include "ED_mesh.h"
75
76 #include "RNA_define.h"
77
78 #include "UI_interface.h"
79
80 #include "render_intern.h"      // own include
81
82 /***************************** Updates ***********************************
83  * ED_render_id_flush_update gets called from DAG_id_flush_update, to do *
84  * editor level updates when the ID changes. when these ID blocks are in *
85  * the dependency graph, we can get rid of the manual dependency checks  */
86
87 static int mtex_use_tex(MTex **mtex, int tot, Tex *tex)
88 {
89         int a;
90
91         if(!mtex)
92                 return 0;
93
94         for(a=0; a<tot; a++)
95                 if(mtex[a] && mtex[a]->tex == tex)
96                         return 1;
97         
98         return 0;
99 }
100
101 static int nodes_use_tex(bNodeTree *ntree, Tex *tex)
102 {
103         bNode *node;
104
105         for(node=ntree->nodes.first; node; node= node->next) {
106                 if(node->id) {
107                         if(node->id == (ID*)tex) {
108                                 return 1;
109                         }
110                         else if(node->type==NODE_GROUP) {
111                                 if(nodes_use_tex((bNodeTree *)node->id, tex))
112                                         return 1;
113                         }
114                 }
115         }
116
117         return 0;
118 }
119
120 static void material_changed(Main *bmain, Material *ma)
121 {
122         /* icons */
123         BKE_icon_changed(BKE_icon_getid(&ma->id));
124
125         /* glsl */
126         if(ma->gpumaterial.first)
127                 GPU_material_free(ma);
128 }
129
130 static void texture_changed(Main *bmain, Tex *tex)
131 {
132         Material *ma;
133         Lamp *la;
134         World *wo;
135
136         /* icons */
137         BKE_icon_changed(BKE_icon_getid(&tex->id));
138
139         /* find materials */
140         for(ma=bmain->mat.first; ma; ma=ma->id.next) {
141                 if(mtex_use_tex(ma->mtex, MAX_MTEX, tex));
142                 else if(ma->use_nodes && ma->nodetree && nodes_use_tex(ma->nodetree, tex));
143                 else continue;
144
145                 BKE_icon_changed(BKE_icon_getid(&ma->id));
146
147                 if(ma->gpumaterial.first)
148                         GPU_material_free(ma);
149         }
150
151         /* find lamps */
152         for(la=bmain->lamp.first; la; la=la->id.next) {
153                 if(mtex_use_tex(la->mtex, MAX_MTEX, tex));
154                 else continue;
155
156                 BKE_icon_changed(BKE_icon_getid(&la->id));
157         }
158
159         /* find worlds */
160         for(wo=bmain->world.first; wo; wo=wo->id.next) {
161                 if(mtex_use_tex(wo->mtex, MAX_MTEX, tex));
162                 else continue;
163
164                 BKE_icon_changed(BKE_icon_getid(&wo->id));
165         }
166 }
167
168 static void lamp_changed(Main *bmain, Lamp *la)
169 {
170         Object *ob;
171         Material *ma;
172
173         /* icons */
174         BKE_icon_changed(BKE_icon_getid(&la->id));
175
176         /* glsl */
177         for(ob=bmain->object.first; ob; ob=ob->id.next)
178                 if(ob->data == la && ob->gpulamp.first)
179                         GPU_lamp_free(ob);
180
181         for(ma=bmain->mat.first; ma; ma=ma->id.next)
182                 if(ma->gpumaterial.first)
183                         GPU_material_free(ma);
184 }
185
186 static void world_changed(Main *bmain, World *wo)
187 {
188         Material *ma;
189
190         /* icons */
191         BKE_icon_changed(BKE_icon_getid(&wo->id));
192
193         /* glsl */
194         for(ma=bmain->mat.first; ma; ma=ma->id.next)
195                 if(ma->gpumaterial.first)
196                         GPU_material_free(ma);
197 }
198
199 static void image_changed(Main *bmain, Image *ima)
200 {
201         Tex *tex;
202
203         /* icons */
204         BKE_icon_changed(BKE_icon_getid(&ima->id));
205
206         /* textures */
207         for(tex=bmain->tex.first; tex; tex=tex->id.next)
208                 if(tex->ima == ima)
209                         texture_changed(bmain, tex);
210 }
211
212 static void scene_changed(Main *bmain, Scene *sce)
213 {
214         Object *ob;
215         Material *ma;
216
217         /* glsl */
218         for(ob=bmain->object.first; ob; ob=ob->id.next)
219                 if(ob->gpulamp.first)
220                         GPU_lamp_free(ob);
221
222         for(ma=bmain->mat.first; ma; ma=ma->id.next)
223                 if(ma->gpumaterial.first)
224                         GPU_material_free(ma);
225 }
226
227 void ED_render_id_flush_update(Main *bmain, ID *id)
228 {
229         if(!id)
230                 return;
231
232         switch(GS(id->name)) {
233                 case ID_MA:
234                         material_changed(bmain, (Material*)id);
235                         break;
236                 case ID_TE:
237                         texture_changed(bmain, (Tex*)id);
238                         break;
239                 case ID_WO:
240                         world_changed(bmain, (World*)id);
241                         break;
242                 case ID_LA:
243                         lamp_changed(bmain, (Lamp*)id);
244                         break;
245                 case ID_IM:
246                         image_changed(bmain, (Image*)id);
247                         break;
248                 case ID_SCE:
249                         scene_changed(bmain, (Scene*)id);
250                         break;
251                 default:
252                         break;
253         }
254 }
255
256 /********************** material slot operators *********************/
257
258 static int material_slot_add_exec(bContext *C, wmOperator *op)
259 {
260         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
261
262         if(!ob)
263                 return OPERATOR_CANCELLED;
264
265         object_add_material_slot(ob);
266         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
267         
268         return OPERATOR_FINISHED;
269 }
270
271 void OBJECT_OT_material_slot_add(wmOperatorType *ot)
272 {
273         /* identifiers */
274         ot->name= "Add Material Slot";
275         ot->idname= "OBJECT_OT_material_slot_add";
276         ot->description="Add a new material slot or duplicate the selected one";
277         
278         /* api callbacks */
279         ot->exec= material_slot_add_exec;
280
281         /* flags */
282         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
283 }
284
285 static int material_slot_remove_exec(bContext *C, wmOperator *op)
286 {
287         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
288
289         if(!ob)
290                 return OPERATOR_CANCELLED;
291
292         object_remove_material_slot(ob);
293         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
294         
295         return OPERATOR_FINISHED;
296 }
297
298 void OBJECT_OT_material_slot_remove(wmOperatorType *ot)
299 {
300         /* identifiers */
301         ot->name= "Remove Material Slot";
302         ot->idname= "OBJECT_OT_material_slot_remove";
303         ot->description="Remove the selected material slot";
304         
305         /* api callbacks */
306         ot->exec= material_slot_remove_exec;
307
308         /* flags */
309         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
310 }
311
312 static int material_slot_assign_exec(bContext *C, wmOperator *op)
313 {
314         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
315
316         if(!ob)
317                 return OPERATOR_CANCELLED;
318
319         if(ob && ob->actcol>0) {
320                 if(ob->type == OB_MESH) {
321                         EditMesh *em= ((Mesh*)ob->data)->edit_mesh;
322                         EditFace *efa;
323
324                         if(em) {
325                                 for(efa= em->faces.first; efa; efa=efa->next)
326                                         if(efa->f & SELECT)
327                                                 efa->mat_nr= ob->actcol-1;
328                         }
329                 }
330                 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
331                         Nurb *nu;
332                         ListBase *nurbs= ED_curve_editnurbs((Curve*)ob->data);
333
334                         if(nurbs) {
335                                 for(nu= nurbs->first; nu; nu= nu->next)
336                                         if(isNurbsel(nu))
337                                                 nu->mat_nr= nu->charidx= ob->actcol-1;
338                         }
339                 }
340                 else if(ob->type == OB_FONT) {
341                         EditFont *ef= ((Curve*)ob->data)->editfont;
342                         int i, selstart, selend;
343
344                         if(ef && BKE_font_getselection(ob, &selstart, &selend)) {
345                                 for(i=selstart; i<=selend; i++)
346                                         ef->textbufinfo[i].mat_nr = ob->actcol-1;
347                         }
348                 }
349         }
350
351         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
352         WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
353         
354         return OPERATOR_FINISHED;
355 }
356
357 void OBJECT_OT_material_slot_assign(wmOperatorType *ot)
358 {
359         /* identifiers */
360         ot->name= "Assign Material Slot";
361         ot->idname= "OBJECT_OT_material_slot_assign";
362         ot->description="Assign the material in the selected material slot to the selected vertices";
363         
364         /* api callbacks */
365         ot->exec= material_slot_assign_exec;
366
367         /* flags */
368         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
369 }
370
371 static int material_slot_de_select(bContext *C, int select)
372 {
373         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
374
375         if(!ob)
376                 return OPERATOR_CANCELLED;
377
378         if(ob->type == OB_MESH) {
379                 EditMesh *em= ((Mesh*)ob->data)->edit_mesh;
380
381                 if(em) {
382                         if(select)
383                                 EM_select_by_material(em, ob->actcol-1);
384                         else
385                                 EM_deselect_by_material(em, ob->actcol-1);
386                 }
387         }
388         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
389                 ListBase *nurbs= ED_curve_editnurbs((Curve*)ob->data);
390                 Nurb *nu;
391                 BPoint *bp;
392                 BezTriple *bezt;
393                 int a;
394
395                 for(nu= nurbs->first; nu; nu=nu->next) {
396                         if(nu->mat_nr==ob->actcol-1) {
397                                 if(nu->bezt) {
398                                         a= nu->pntsu;
399                                         bezt= nu->bezt;
400                                         while(a--) {
401                                                 if(bezt->hide==0) {
402                                                         if(select) {
403                                                                 bezt->f1 |= SELECT;
404                                                                 bezt->f2 |= SELECT;
405                                                                 bezt->f3 |= SELECT;
406                                                         }
407                                                         else {
408                                                                 bezt->f1 &= ~SELECT;
409                                                                 bezt->f2 &= ~SELECT;
410                                                                 bezt->f3 &= ~SELECT;
411                                                         }
412                                                 }
413                                                 bezt++;
414                                         }
415                                 }
416                                 else if(nu->bp) {
417                                         a= nu->pntsu*nu->pntsv;
418                                         bp= nu->bp;
419                                         while(a--) {
420                                                 if(bp->hide==0) {
421                                                         if(select) bp->f1 |= SELECT;
422                                                         else bp->f1 &= ~SELECT;
423                                                 }
424                                                 bp++;
425                                         }
426                                 }
427                         }
428                 }
429         }
430
431         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, ob->data);
432
433         return OPERATOR_FINISHED;
434 }
435
436 static int material_slot_select_exec(bContext *C, wmOperator *op)
437 {
438         return material_slot_de_select(C, 1);
439 }
440
441 void OBJECT_OT_material_slot_select(wmOperatorType *ot)
442 {
443         /* identifiers */
444         ot->name= "Select Material Slot";
445         ot->idname= "OBJECT_OT_material_slot_select";
446         ot->description="Select vertices assigned to the selected material slot";
447         
448         /* api callbacks */
449         ot->exec= material_slot_select_exec;
450
451         /* flags */
452         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
453 }
454
455 static int material_slot_deselect_exec(bContext *C, wmOperator *op)
456 {
457         return material_slot_de_select(C, 0);
458 }
459
460 void OBJECT_OT_material_slot_deselect(wmOperatorType *ot)
461 {
462         /* identifiers */
463         ot->name= "Deselect Material Slot";
464         ot->idname= "OBJECT_OT_material_slot_deselect";
465         ot->description="Deselect vertices assigned to the selected material slot";
466         
467         /* api callbacks */
468         ot->exec= material_slot_deselect_exec;
469
470         /* flags */
471         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
472 }
473
474
475 static int material_slot_copy_exec(bContext *C, wmOperator *op)
476 {
477         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
478         Material ***matar;
479
480         if(!ob || !(matar= give_matarar(ob)))
481                 return OPERATOR_CANCELLED;
482
483         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
484                 if(ob != ob_iter && give_matarar(ob_iter)) {
485                         if (ob->data != ob_iter->data)
486                                 assign_matarar(ob_iter, matar, ob->totcol);
487                         
488                         if(ob_iter->totcol==ob->totcol) {
489                                 ob_iter->actcol= ob->actcol;
490                                 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob_iter);
491                         }
492                 }
493         }
494         CTX_DATA_END;
495
496         return OPERATOR_FINISHED;
497 }
498
499
500 void OBJECT_OT_material_slot_copy(wmOperatorType *ot)
501 {
502         /* identifiers */
503         ot->name= "Copy Material to Others";
504         ot->idname= "OBJECT_OT_material_slot_copy";
505         ot->description="Copies materials to other selected objects";
506
507         /* api callbacks */
508         ot->exec= material_slot_copy_exec;
509
510         /* flags */
511         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
512 }
513
514 /********************** new material operator *********************/
515
516 static int new_material_exec(bContext *C, wmOperator *op)
517 {
518         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
519         PointerRNA ptr, idptr;
520         PropertyRNA *prop;
521
522         /* add or copy material */
523         if(ma)
524                 ma= copy_material(ma);
525         else
526                 ma= add_material("Material");
527
528         /* hook into UI */
529         uiIDContextProperty(C, &ptr, &prop);
530
531         if(prop) {
532                 /* when creating new ID blocks, use is already 1, but RNA
533                  * pointer se also increases user, so this compensates it */
534                 ma->id.us--;
535
536                 RNA_id_pointer_create(&ma->id, &idptr);
537                 RNA_property_pointer_set(&ptr, prop, idptr);
538                 RNA_property_update(C, &ptr, prop);
539         }
540
541         WM_event_add_notifier(C, NC_MATERIAL|NA_ADDED, ma);
542         
543         return OPERATOR_FINISHED;
544 }
545
546 void MATERIAL_OT_new(wmOperatorType *ot)
547 {
548         /* identifiers */
549         ot->name= "New Material";
550         ot->idname= "MATERIAL_OT_new";
551         ot->description="Add a new material";
552         
553         /* api callbacks */
554         ot->exec= new_material_exec;
555
556         /* flags */
557         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
558 }
559
560 /********************** new texture operator *********************/
561
562 static int new_texture_exec(bContext *C, wmOperator *op)
563 {
564         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
565         PointerRNA ptr, idptr;
566         PropertyRNA *prop;
567
568         /* add or copy texture */
569         if(tex)
570                 tex= copy_texture(tex);
571         else
572                 tex= add_texture("Texture");
573
574         /* hook into UI */
575         uiIDContextProperty(C, &ptr, &prop);
576
577         if(prop) {
578                 /* when creating new ID blocks, use is already 1, but RNA
579                  * pointer se also increases user, so this compensates it */
580                 tex->id.us--;
581
582                 RNA_id_pointer_create(&tex->id, &idptr);
583                 RNA_property_pointer_set(&ptr, prop, idptr);
584                 RNA_property_update(C, &ptr, prop);
585         }
586
587         WM_event_add_notifier(C, NC_TEXTURE|NA_ADDED, tex);
588         
589         return OPERATOR_FINISHED;
590 }
591
592 void TEXTURE_OT_new(wmOperatorType *ot)
593 {
594         /* identifiers */
595         ot->name= "New Texture";
596         ot->idname= "TEXTURE_OT_new";
597         ot->description="Add a new texture";
598         
599         /* api callbacks */
600         ot->exec= new_texture_exec;
601
602         /* flags */
603         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
604 }
605
606 /********************** new world operator *********************/
607
608 static int new_world_exec(bContext *C, wmOperator *op)
609 {
610         World *wo= CTX_data_pointer_get_type(C, "world", &RNA_World).data;
611         PointerRNA ptr, idptr;
612         PropertyRNA *prop;
613
614         /* add or copy world */
615         if(wo)
616                 wo= copy_world(wo);
617         else
618                 wo= add_world("World");
619
620         /* hook into UI */
621         uiIDContextProperty(C, &ptr, &prop);
622
623         if(prop) {
624                 /* when creating new ID blocks, use is already 1, but RNA
625                  * pointer se also increases user, so this compensates it */
626                 wo->id.us--;
627
628                 RNA_id_pointer_create(&wo->id, &idptr);
629                 RNA_property_pointer_set(&ptr, prop, idptr);
630                 RNA_property_update(C, &ptr, prop);
631         }
632
633         WM_event_add_notifier(C, NC_WORLD|NA_ADDED, wo);
634         
635         return OPERATOR_FINISHED;
636 }
637
638 void WORLD_OT_new(wmOperatorType *ot)
639 {
640         /* identifiers */
641         ot->name= "New World";
642         ot->idname= "WORLD_OT_new";
643         ot->description= "Add a new world";
644         
645         /* api callbacks */
646         ot->exec= new_world_exec;
647
648         /* flags */
649         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
650 }
651
652 /********************** render layer operators *********************/
653
654 static int render_layer_add_exec(bContext *C, wmOperator *op)
655 {
656         Scene *scene= CTX_data_scene(C);
657
658         scene_add_render_layer(scene);
659         scene->r.actlay= BLI_countlist(&scene->r.layers) - 1;
660
661         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
662         
663         return OPERATOR_FINISHED;
664 }
665
666 void SCENE_OT_render_layer_add(wmOperatorType *ot)
667 {
668         /* identifiers */
669         ot->name= "Add Render Layer";
670         ot->idname= "SCENE_OT_render_layer_add";
671         ot->description="Add a render layer";
672         
673         /* api callbacks */
674         ot->exec= render_layer_add_exec;
675
676         /* flags */
677         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
678 }
679
680 static int render_layer_remove_exec(bContext *C, wmOperator *op)
681 {
682         Scene *scene= CTX_data_scene(C);
683         SceneRenderLayer *rl;
684         int act= scene->r.actlay;
685
686         if(BLI_countlist(&scene->r.layers) <= 1)
687                 return OPERATOR_CANCELLED;
688         
689         rl= BLI_findlink(&scene->r.layers, scene->r.actlay);
690         BLI_remlink(&scene->r.layers, rl);
691         MEM_freeN(rl);
692
693         scene->r.actlay= 0;
694         
695         if(scene->nodetree) {
696                 bNode *node;
697                 for(node= scene->nodetree->nodes.first; node; node= node->next) {
698                         if(node->type==CMP_NODE_R_LAYERS && node->id==NULL) {
699                                 if(node->custom1==act)
700                                         node->custom1= 0;
701                                 else if(node->custom1>act)
702                                         node->custom1--;
703                         }
704                 }
705         }
706
707         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
708         
709         return OPERATOR_FINISHED;
710 }
711
712 void SCENE_OT_render_layer_remove(wmOperatorType *ot)
713 {
714         /* identifiers */
715         ot->name= "Remove Render Layer";
716         ot->idname= "SCENE_OT_render_layer_remove";
717         ot->description="Remove the selected render layer";
718         
719         /* api callbacks */
720         ot->exec= render_layer_remove_exec;
721
722         /* flags */
723         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
724 }
725
726 static int texture_slot_move(bContext *C, wmOperator *op)
727 {
728         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
729         Material *ma = (Material *)id;
730
731         if(id) {
732                 MTex **mtex_ar, *mtexswap;
733                 short act;
734                 int type= RNA_enum_get(op->ptr, "type");
735
736                 give_active_mtex(id, &mtex_ar, &act);
737
738                 if(type == -1) { /* Up */
739                         if(act > 0) {
740                                 mtexswap = mtex_ar[act];
741                                 mtex_ar[act] = mtex_ar[act-1];
742                                 mtex_ar[act-1] = mtexswap;
743                                 
744                                 BKE_animdata_fix_paths_rename(id, ma->adt, "texture_slots", NULL, NULL, act-1, -1, 0);
745                                 BKE_animdata_fix_paths_rename(id, ma->adt, "texture_slots", NULL, NULL, act, act-1, 0);
746                                 BKE_animdata_fix_paths_rename(id, ma->adt, "texture_slots", NULL, NULL, -1, act, 0);
747
748                                 if(GS(id->name)==ID_MA) {
749                                         Material *ma= (Material *)id;
750                                         int mtexuse = ma->septex & (1<<act);
751                                         ma->septex &= ~(1<<act);
752                                         ma->septex |= (ma->septex & (1<<(act-1))) << 1;
753                                         ma->septex &= ~(1<<(act-1));
754                                         ma->septex |= mtexuse >> 1;
755                                 }
756                                 
757                                 set_active_mtex(id, act-1);
758                         }
759                 }
760                 else { /* Down */
761                         if(act < MAX_MTEX-1) {
762                                 mtexswap = mtex_ar[act];
763                                 mtex_ar[act] = mtex_ar[act+1];
764                                 mtex_ar[act+1] = mtexswap;
765                                 
766                                 BKE_animdata_fix_paths_rename(id, ma->adt, "texture_slots", NULL, NULL, act+1, -1, 0);
767                                 BKE_animdata_fix_paths_rename(id, ma->adt, "texture_slots", NULL, NULL, act, act+1, 0);
768                                 BKE_animdata_fix_paths_rename(id, ma->adt, "texture_slots", NULL, NULL, -1, act, 0);
769
770                                 if(GS(id->name)==ID_MA) {
771                                         Material *ma= (Material *)id;
772                                         int mtexuse = ma->septex & (1<<act);
773                                         ma->septex &= ~(1<<act);
774                                         ma->septex |= (ma->septex & (1<<(act+1))) >> 1;
775                                         ma->septex &= ~(1<<(act+1));
776                                         ma->septex |= mtexuse << 1;
777                                 }
778                                 
779                                 set_active_mtex(id, act+1);
780                         }
781                 }
782
783                 WM_event_add_notifier(C, NC_TEXTURE, CTX_data_scene(C));
784         }
785
786         return OPERATOR_FINISHED;
787 }
788
789 void TEXTURE_OT_slot_move(wmOperatorType *ot)
790 {
791         static EnumPropertyItem slot_move[] = {
792                 {-1, "UP", 0, "Up", ""},
793                 {1, "DOWN", 0, "Down", ""},
794                 {0, NULL, 0, NULL, NULL}
795         };
796
797         /* identifiers */
798         ot->name= "Move Texture Slot";
799         ot->idname= "TEXTURE_OT_slot_move";
800         ot->description="Move texture slots up and down";
801
802         /* api callbacks */
803         ot->exec= texture_slot_move;
804
805         /* flags */
806         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
807
808         RNA_def_enum(ot->srna, "type", slot_move, 0, "Type", "");
809 }
810
811
812
813 /********************** environment map operators *********************/
814
815 static int save_envmap(wmOperator *op, Scene *scene, EnvMap *env, char *str, int imtype)
816 {
817         ImBuf *ibuf=NULL;
818         int dx;
819         int retval;
820         
821         if(env->cube[1]==NULL) {
822                 BKE_report(op->reports, RPT_ERROR, "There is no generated environment map available to save");
823                 return OPERATOR_CANCELLED;
824         }
825         
826         dx= env->cube[1]->x;
827         
828         if (env->type == ENV_CUBE) {
829                 ibuf = IMB_allocImBuf(3*dx, 2*dx, 24, IB_rectfloat, 0);
830
831                 IMB_rectcpy(ibuf, env->cube[0], 0, 0, 0, 0, dx, dx);
832                 IMB_rectcpy(ibuf, env->cube[1], dx, 0, 0, 0, dx, dx);
833                 IMB_rectcpy(ibuf, env->cube[2], 2*dx, 0, 0, 0, dx, dx);
834                 IMB_rectcpy(ibuf, env->cube[3], 0, dx, 0, 0, dx, dx);
835                 IMB_rectcpy(ibuf, env->cube[4], dx, dx, 0, 0, dx, dx);
836                 IMB_rectcpy(ibuf, env->cube[5], 2*dx, dx, 0, 0, dx, dx);
837         }
838         else if (env->type == ENV_PLANE) {
839                 ibuf = IMB_allocImBuf(dx, dx, 24, IB_rectfloat, 0);
840                 IMB_rectcpy(ibuf, env->cube[1], 0, 0, 0, 0, dx, dx);            
841         }
842         
843         if (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
844                 ibuf->profile = IB_PROFILE_LINEAR_RGB;
845         
846         if (BKE_write_ibuf(scene, ibuf, str, imtype, scene->r.subimtype, scene->r.quality)) {
847                 retval = OPERATOR_FINISHED;
848         }
849         else {
850                 BKE_reportf(op->reports, RPT_ERROR, "Error saving environment map to %s.", str);
851                 retval = OPERATOR_CANCELLED;
852         }
853         
854         IMB_freeImBuf(ibuf);
855         ibuf = NULL;
856         
857         return retval;
858 }
859
860 static int envmap_save_exec(bContext *C, wmOperator *op)
861 {
862         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
863         Scene *scene = CTX_data_scene(C);
864         //int imtype = RNA_enum_get(op->ptr, "file_type");
865         int imtype = scene->r.imtype;
866         char path[FILE_MAX];
867         
868         RNA_string_get(op->ptr, "filepath", path);
869         
870         if(scene->r.scemode & R_EXTENSION)  {
871                 BKE_add_image_extension(path, imtype);
872         }
873         
874         WM_cursor_wait(1);
875         
876         save_envmap(op, scene, tex->env, path, imtype);
877         
878         WM_cursor_wait(0);
879         
880         WM_event_add_notifier(C, NC_TEXTURE, tex);
881         
882         return OPERATOR_FINISHED;
883 }
884
885 static int envmap_save_invoke(bContext *C, wmOperator *op, wmEvent *event)
886 {
887         //Scene *scene= CTX_data_scene(C);
888         
889         if(!RNA_property_is_set(op->ptr, "relative_path"))
890                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
891         
892         if(RNA_property_is_set(op->ptr, "filepath"))
893                 return envmap_save_exec(C, op);
894
895         //RNA_enum_set(op->ptr, "file_type", scene->r.imtype);
896         
897         RNA_string_set(op->ptr, "filepath", G.sce);
898         WM_event_add_fileselect(C, op);
899         
900         return OPERATOR_RUNNING_MODAL;
901 }
902
903 static int envmap_save_poll(bContext *C)
904 {
905         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
906
907         if (!tex) 
908                 return 0;
909         if (!tex->env || !tex->env->ok)
910                 return 0;
911         if (tex->env->cube[1]==NULL)
912                 return 0;
913         
914         return 1;
915 }
916
917 void TEXTURE_OT_envmap_save(wmOperatorType *ot)
918 {
919         /* identifiers */
920         ot->name= "Save Environment Map";
921         ot->idname= "TEXTURE_OT_envmap_save";
922         ot->description="Save the current generated Environment map to an image file";
923         
924         /* api callbacks */
925         ot->exec= envmap_save_exec;
926         ot->invoke= envmap_save_invoke;
927         ot->poll= envmap_save_poll;
928         
929         /* flags */
930         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
931         
932         /* properties */
933         //RNA_def_enum(ot->srna, "file_type", image_file_type_items, R_PNG, "File Type", "File type to save image as.");
934         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_SAVE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
935 }
936
937 static int envmap_clear_exec(bContext *C, wmOperator *op)
938 {
939         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
940         
941         BKE_free_envmapdata(tex->env);
942         
943         WM_event_add_notifier(C, NC_TEXTURE|NA_EDITED, tex);
944         
945         return OPERATOR_FINISHED;
946 }
947
948 static int envmap_clear_poll(bContext *C)
949 {
950         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
951         
952         if (!tex) 
953                 return 0;
954         if (!tex->env || !tex->env->ok)
955                 return 0;
956         if (tex->env->cube[1]==NULL)
957                 return 0;
958         
959         return 1;
960 }
961
962 void TEXTURE_OT_envmap_clear(wmOperatorType *ot)
963 {
964         /* identifiers */
965         ot->name= "Clear Environment Map";
966         ot->idname= "TEXTURE_OT_envmap_clear";
967         ot->description="Discard the environment map and free it from memory";
968         
969         /* api callbacks */
970         ot->exec= envmap_clear_exec;
971         ot->poll= envmap_clear_poll;
972         
973         /* flags */
974         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
975 }
976
977 static int envmap_clear_all_exec(bContext *C, wmOperator *op)
978 {
979         Main *bmain = CTX_data_main(C);
980         Tex *tex;
981         
982         for (tex=bmain->tex.first; tex; tex=tex->id.next)
983                 if (tex->env)
984                         BKE_free_envmapdata(tex->env);
985         
986         WM_event_add_notifier(C, NC_TEXTURE|NA_EDITED, tex);
987         
988         return OPERATOR_FINISHED;
989 }
990
991 void TEXTURE_OT_envmap_clear_all(wmOperatorType *ot)
992 {
993         /* identifiers */
994         ot->name= "Clear All Environment Maps";
995         ot->idname= "TEXTURE_OT_envmap_clear_all";
996         ot->description="Discard all environment maps in the .blend file and free them from memory";
997         
998         /* api callbacks */
999         ot->exec= envmap_clear_all_exec;
1000         ot->poll= envmap_clear_poll;
1001         
1002         /* flags */
1003         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1004 }
1005
1006 /********************** material operators *********************/
1007
1008 /* material copy/paste */
1009 static int copy_material_exec(bContext *C, wmOperator *op)
1010 {
1011         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
1012
1013         if(ma==NULL)
1014                 return OPERATOR_CANCELLED;
1015
1016         copy_matcopybuf(ma);
1017
1018         WM_event_add_notifier(C, NC_MATERIAL, ma);
1019
1020         return OPERATOR_FINISHED;
1021 }
1022
1023 void MATERIAL_OT_copy(wmOperatorType *ot)
1024 {
1025         /* identifiers */
1026         ot->name= "Copy Material";
1027         ot->idname= "MATERIAL_OT_copy";
1028         ot->description="Copy the material settings and nodes";
1029
1030         /* api callbacks */
1031         ot->exec= copy_material_exec;
1032
1033         /* flags */
1034         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1035 }
1036
1037 static int paste_material_exec(bContext *C, wmOperator *op)
1038 {
1039         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
1040
1041         if(ma==NULL)
1042                 return OPERATOR_CANCELLED;
1043
1044         paste_matcopybuf(ma);
1045
1046         WM_event_add_notifier(C, NC_MATERIAL|ND_SHADING_DRAW, ma);
1047
1048         return OPERATOR_FINISHED;
1049 }
1050
1051 void MATERIAL_OT_paste(wmOperatorType *ot)
1052 {
1053         /* identifiers */
1054         ot->name= "Paste Material";
1055         ot->idname= "MATERIAL_OT_paste";
1056         ot->description="Paste the material settings and nodes";
1057
1058         /* api callbacks */
1059         ot->exec= paste_material_exec;
1060
1061         /* flags */
1062         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1063 }
1064
1065
1066 static short mtexcopied=0; /* must be reset on file load */
1067 static MTex mtexcopybuf;
1068
1069 void ED_render_clear_mtex_copybuf(void)
1070 {       /* use for file reload */
1071         mtexcopied= 0;
1072 }
1073
1074 void copy_mtex_copybuf(ID *id)
1075 {
1076         MTex **mtex= NULL;
1077         
1078         switch(GS(id->name)) {
1079                 case ID_MA:
1080                         mtex= &(((Material *)id)->mtex[(int)((Material *)id)->texact]);
1081                         break;
1082                 case ID_LA:
1083                         mtex= &(((Lamp *)id)->mtex[(int)((Lamp *)id)->texact]);
1084                         // la->mtex[(int)la->texact] // TODO
1085                         break;
1086                 case ID_WO:
1087                         mtex= &(((World *)id)->mtex[(int)((World *)id)->texact]);
1088                         // mtex= wrld->mtex[(int)wrld->texact]; // TODO
1089                         break;
1090         }
1091         
1092         if(mtex && *mtex) {
1093                 memcpy(&mtexcopybuf, *mtex, sizeof(MTex));
1094                 mtexcopied= 1;
1095         }
1096         else {
1097                 mtexcopied= 0;
1098         }
1099 }
1100
1101 void paste_mtex_copybuf(ID *id)
1102 {
1103         MTex **mtex= NULL;
1104         
1105         if(mtexcopied == 0 || mtexcopybuf.tex==NULL)
1106                 return;
1107         
1108         switch(GS(id->name)) {
1109                 case ID_MA:
1110                         mtex= &(((Material *)id)->mtex[(int)((Material *)id)->texact]);
1111                         break;
1112                 case ID_LA:
1113                         mtex= &(((Lamp *)id)->mtex[(int)((Lamp *)id)->texact]);
1114                         // la->mtex[(int)la->texact] // TODO
1115                         break;
1116                 case ID_WO:
1117                         mtex= &(((World *)id)->mtex[(int)((World *)id)->texact]);
1118                         // mtex= wrld->mtex[(int)wrld->texact]; // TODO
1119                         break;
1120         }
1121         
1122         if(mtex) {
1123                 if(*mtex==NULL) {
1124                         *mtex= MEM_mallocN(sizeof(MTex), "mtex copy");
1125                 }
1126                 else if((*mtex)->tex) {
1127                         (*mtex)->tex->id.us--;
1128                 }
1129                 
1130                 memcpy(*mtex, &mtexcopybuf, sizeof(MTex));
1131                 
1132                 id_us_plus((ID *)mtexcopybuf.tex);
1133         }
1134 }
1135
1136
1137 static int copy_mtex_exec(bContext *C, wmOperator *op)
1138 {
1139         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
1140
1141         if(id==NULL) {
1142                 /* copying empty slot */
1143                 ED_render_clear_mtex_copybuf();
1144                 return OPERATOR_CANCELLED;
1145         }
1146
1147         copy_mtex_copybuf(id);
1148
1149         WM_event_add_notifier(C, NC_TEXTURE, NULL);
1150
1151         return OPERATOR_FINISHED;
1152 }
1153
1154 static int copy_mtex_poll(bContext *C)
1155 {
1156         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
1157         
1158         return (id != NULL);
1159 }
1160
1161 void TEXTURE_OT_slot_copy(wmOperatorType *ot)
1162 {
1163         /* identifiers */
1164         ot->name= "Copy Texture Slot Settings";
1165         ot->idname= "TEXTURE_OT_slot_copy";
1166         ot->description="Copy the material texture settings and nodes";
1167
1168         /* api callbacks */
1169         ot->exec= copy_mtex_exec;
1170         ot->poll= copy_mtex_poll;
1171         
1172         /* flags */
1173         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1174 }
1175
1176 static int paste_mtex_exec(bContext *C, wmOperator *op)
1177 {
1178         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
1179
1180         if(id==NULL) {
1181                 Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
1182                 Lamp *la= CTX_data_pointer_get_type(C, "lamp", &RNA_Lamp).data;
1183                 World *wo= CTX_data_pointer_get_type(C, "world", &RNA_World).data;
1184                 
1185                 if (ma)
1186                         id = &ma->id;
1187                 else if (la)
1188                         id = &la->id;
1189                 else if (wo)
1190                         id = &wo->id;
1191                 
1192                 if (id==NULL)
1193                         return OPERATOR_CANCELLED;
1194         }
1195
1196         paste_mtex_copybuf(id);
1197
1198         WM_event_add_notifier(C, NC_TEXTURE|ND_SHADING_DRAW, NULL);
1199
1200         return OPERATOR_FINISHED;
1201 }
1202
1203 void TEXTURE_OT_slot_paste(wmOperatorType *ot)
1204 {
1205         /* identifiers */
1206         ot->name= "Paste Texture Slot Settings";
1207         ot->idname= "TEXTURE_OT_slot_paste";
1208         ot->description="Copy the texture settings and nodes";
1209
1210         /* api callbacks */
1211         ot->exec= paste_mtex_exec;
1212
1213         /* flags */
1214         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1215 }