e29a81612fa59a28db552aac3ed197e022707e5e
[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_meshdata_types.h"
36 #include "DNA_node_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_texture_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_space_types.h"
41 #include "DNA_world_types.h"
42
43 #include "BKE_animsys.h"
44 #include "BKE_context.h"
45 #include "BKE_depsgraph.h"
46 #include "BKE_font.h"
47 #include "BKE_global.h"
48 #include "BKE_icons.h"
49 #include "BKE_image.h"
50 #include "BKE_library.h"
51 #include "BKE_main.h"
52 #include "BKE_material.h"
53 #include "BKE_node.h"
54 #include "BKE_report.h"
55 #include "BKE_scene.h"
56 #include "BKE_texture.h"
57 #include "BKE_utildefines.h"
58 #include "BKE_world.h"
59
60 #include "IMB_imbuf.h"
61 #include "IMB_imbuf_types.h"
62
63 #include "BLI_blenlib.h"
64 #include "BLI_math.h"
65 #include "BLI_editVert.h"
66 #include "BLI_listbase.h"
67
68 #include "GPU_material.h"
69
70 #include "RNA_access.h"
71 #include "RNA_enum_types.h"
72
73 #include "WM_api.h"
74 #include "WM_types.h"
75
76 #include "ED_curve.h"
77 #include "ED_mesh.h"
78 #include "ED_render.h"
79
80 #include "RNA_access.h"
81 #include "RNA_define.h"
82
83 #include "UI_interface.h"
84 #include "UI_resources.h"
85
86 #include "render_intern.h"      // own include
87
88 /***************************** Updates ***********************************
89  * ED_render_id_flush_update gets called from DAG_id_flush_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 *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 void ED_render_id_flush_update(Main *bmain, ID *id)
206 {
207         if(!id)
208                 return;
209
210         switch(GS(id->name)) {
211                 case ID_MA:
212                         material_changed(bmain, (Material*)id);
213                         break;
214                 case ID_TE:
215                         texture_changed(bmain, (Tex*)id);
216                         break;
217                 case ID_WO:
218                         world_changed(bmain, (World*)id);
219                         break;
220                 case ID_LA:
221                         lamp_changed(bmain, (Lamp*)id);
222                         break;
223                 default:
224                         break;
225         }
226 }
227
228 /********************** material slot operators *********************/
229
230 static int material_slot_add_exec(bContext *C, wmOperator *op)
231 {
232         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
233
234         if(!ob)
235                 return OPERATOR_CANCELLED;
236
237         object_add_material_slot(ob);
238         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
239         
240         return OPERATOR_FINISHED;
241 }
242
243 void OBJECT_OT_material_slot_add(wmOperatorType *ot)
244 {
245         /* identifiers */
246         ot->name= "Add Material Slot";
247         ot->idname= "OBJECT_OT_material_slot_add";
248         ot->description="Add a new material slot or duplicate the selected one";
249         
250         /* api callbacks */
251         ot->exec= material_slot_add_exec;
252
253         /* flags */
254         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
255 }
256
257 static int material_slot_remove_exec(bContext *C, wmOperator *op)
258 {
259         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
260
261         if(!ob)
262                 return OPERATOR_CANCELLED;
263
264         object_remove_material_slot(ob);
265         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
266         
267         return OPERATOR_FINISHED;
268 }
269
270 void OBJECT_OT_material_slot_remove(wmOperatorType *ot)
271 {
272         /* identifiers */
273         ot->name= "Remove Material Slot";
274         ot->idname= "OBJECT_OT_material_slot_remove";
275         ot->description="Remove the selected material slot";
276         
277         /* api callbacks */
278         ot->exec= material_slot_remove_exec;
279
280         /* flags */
281         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
282 }
283
284 static int material_slot_assign_exec(bContext *C, wmOperator *op)
285 {
286         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
287
288         if(!ob)
289                 return OPERATOR_CANCELLED;
290
291         if(ob && ob->actcol>0) {
292                 if(ob->type == OB_MESH) {
293                         EditMesh *em= ((Mesh*)ob->data)->edit_mesh;
294                         EditFace *efa;
295
296                         if(em) {
297                                 for(efa= em->faces.first; efa; efa=efa->next)
298                                         if(efa->f & SELECT)
299                                                 efa->mat_nr= ob->actcol-1;
300                         }
301                 }
302                 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
303                         ListBase *editnurb= ((Curve*)ob->data)->editnurb;
304                         Nurb *nu;
305
306                         if(editnurb) {
307                                 for(nu= editnurb->first; nu; nu= nu->next)
308                                         if(isNurbsel(nu))
309                                                 nu->mat_nr= nu->charidx= ob->actcol-1;
310                         }
311                 }
312                 else if(ob->type == OB_FONT) {
313                         EditFont *ef= ((Curve*)ob->data)->editfont;
314                 int i, selstart, selend;
315
316                         if(ef && BKE_font_getselection(ob, &selstart, &selend)) {
317                                 for(i=selstart; i<=selend; i++)
318                                         ef->textbufinfo[i].mat_nr = ob->actcol-1;
319                         }
320                 }
321         }
322
323     DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
324     WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
325         
326         return OPERATOR_FINISHED;
327 }
328
329 void OBJECT_OT_material_slot_assign(wmOperatorType *ot)
330 {
331         /* identifiers */
332         ot->name= "Assign Material Slot";
333         ot->idname= "OBJECT_OT_material_slot_assign";
334         ot->description="Assign the material in the selected material slot to the selected vertices";
335         
336         /* api callbacks */
337         ot->exec= material_slot_assign_exec;
338
339         /* flags */
340         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
341 }
342
343 static int material_slot_de_select(bContext *C, int select)
344 {
345         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
346
347         if(!ob)
348                 return OPERATOR_CANCELLED;
349
350         if(ob->type == OB_MESH) {
351                 EditMesh *em= ((Mesh*)ob->data)->edit_mesh;
352
353                 if(em) {
354                         if(select)
355                                 EM_select_by_material(em, ob->actcol-1);
356                         else
357                                 EM_deselect_by_material(em, ob->actcol-1);
358                 }
359         }
360         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
361                 ListBase *editnurb= ((Curve*)ob->data)->editnurb;
362                 Nurb *nu;
363                 BPoint *bp;
364                 BezTriple *bezt;
365                 int a;
366
367                 for(nu= editnurb->first; nu; nu=nu->next) {
368                         if(nu->mat_nr==ob->actcol-1) {
369                                 if(nu->bezt) {
370                                         a= nu->pntsu;
371                                         bezt= nu->bezt;
372                                         while(a--) {
373                                                 if(bezt->hide==0) {
374                                                         if(select) {
375                                                                 bezt->f1 |= SELECT;
376                                                                 bezt->f2 |= SELECT;
377                                                                 bezt->f3 |= SELECT;
378                                                         }
379                                                         else {
380                                                                 bezt->f1 &= ~SELECT;
381                                                                 bezt->f2 &= ~SELECT;
382                                                                 bezt->f3 &= ~SELECT;
383                                                         }
384                                                 }
385                                                 bezt++;
386                                         }
387                                 }
388                                 else if(nu->bp) {
389                                         a= nu->pntsu*nu->pntsv;
390                                         bp= nu->bp;
391                                         while(a--) {
392                                                 if(bp->hide==0) {
393                                                         if(select) bp->f1 |= SELECT;
394                                                         else bp->f1 &= ~SELECT;
395                                                 }
396                                                 bp++;
397                                         }
398                                 }
399                         }
400                 }
401         }
402
403     WM_event_add_notifier(C, NC_GEOM|ND_SELECT, ob->data);
404
405         return OPERATOR_FINISHED;
406 }
407
408 static int material_slot_select_exec(bContext *C, wmOperator *op)
409 {
410         return material_slot_de_select(C, 1);
411 }
412
413 void OBJECT_OT_material_slot_select(wmOperatorType *ot)
414 {
415         /* identifiers */
416         ot->name= "Select Material Slot";
417         ot->idname= "OBJECT_OT_material_slot_select";
418         ot->description="Select vertices assigned to the selected material slot";
419         
420         /* api callbacks */
421         ot->exec= material_slot_select_exec;
422
423         /* flags */
424         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
425 }
426
427 static int material_slot_deselect_exec(bContext *C, wmOperator *op)
428 {
429         return material_slot_de_select(C, 0);
430 }
431
432 void OBJECT_OT_material_slot_deselect(wmOperatorType *ot)
433 {
434         /* identifiers */
435         ot->name= "Deselect Material Slot";
436         ot->idname= "OBJECT_OT_material_slot_deselect";
437         ot->description="Deselect vertices assigned to the selected material slot";
438         
439         /* api callbacks */
440         ot->exec= material_slot_deselect_exec;
441
442         /* flags */
443         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
444 }
445
446
447 static int material_slot_copy_exec(bContext *C, wmOperator *op)
448 {
449         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
450         Material ***matar;
451
452         if(!ob || !(matar= give_matarar(ob)))
453                 return OPERATOR_CANCELLED;
454
455         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
456                 if(ob != ob_iter && give_matarar(ob_iter)) {
457                         if (ob->data != ob_iter->data)
458                                 assign_matarar(ob_iter, matar, ob->totcol);
459                         
460                         if(ob_iter->totcol==ob->totcol) {
461                                 ob_iter->actcol= ob->actcol;
462                                 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob_iter);
463                         }
464                 }
465         }
466         CTX_DATA_END;
467
468         return OPERATOR_FINISHED;
469 }
470
471
472 void OBJECT_OT_material_slot_copy(wmOperatorType *ot)
473 {
474         /* identifiers */
475         ot->name= "Copy Material to Others";
476         ot->idname= "OBJECT_OT_material_slot_copy";
477         ot->description="Copies materials to other selected objects";
478
479         /* api callbacks */
480         ot->exec= material_slot_copy_exec;
481
482         /* flags */
483         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
484 }
485
486 /********************** new material operator *********************/
487
488 static int new_material_exec(bContext *C, wmOperator *op)
489 {
490         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
491         PointerRNA ptr, idptr;
492         PropertyRNA *prop;
493
494         /* add or copy material */
495         if(ma)
496                 ma= copy_material(ma);
497         else
498                 ma= add_material("Material");
499
500         /* hook into UI */
501         uiIDContextProperty(C, &ptr, &prop);
502
503         if(prop) {
504                 /* when creating new ID blocks, use is already 1, but RNA
505                  * pointer se also increases user, so this compensates it */
506                 ma->id.us--;
507
508                 RNA_id_pointer_create(&ma->id, &idptr);
509                 RNA_property_pointer_set(&ptr, prop, idptr);
510                 RNA_property_update(C, &ptr, prop);
511         }
512
513         WM_event_add_notifier(C, NC_MATERIAL|NA_ADDED, ma);
514         
515         return OPERATOR_FINISHED;
516 }
517
518 void MATERIAL_OT_new(wmOperatorType *ot)
519 {
520         /* identifiers */
521         ot->name= "New Material";
522         ot->idname= "MATERIAL_OT_new";
523         ot->description="Add a new material";
524         
525         /* api callbacks */
526         ot->exec= new_material_exec;
527
528         /* flags */
529         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
530 }
531
532 /********************** new texture operator *********************/
533
534 static int new_texture_exec(bContext *C, wmOperator *op)
535 {
536         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
537         PointerRNA ptr, idptr;
538         PropertyRNA *prop;
539
540         /* add or copy texture */
541         if(tex)
542                 tex= copy_texture(tex);
543         else
544                 tex= add_texture("Texture");
545
546         /* hook into UI */
547         uiIDContextProperty(C, &ptr, &prop);
548
549         if(prop) {
550                 /* when creating new ID blocks, use is already 1, but RNA
551                  * pointer se also increases user, so this compensates it */
552                 tex->id.us--;
553
554                 RNA_id_pointer_create(&tex->id, &idptr);
555                 RNA_property_pointer_set(&ptr, prop, idptr);
556                 RNA_property_update(C, &ptr, prop);
557         }
558
559         WM_event_add_notifier(C, NC_TEXTURE|NA_ADDED, tex);
560         
561         return OPERATOR_FINISHED;
562 }
563
564 void TEXTURE_OT_new(wmOperatorType *ot)
565 {
566         /* identifiers */
567         ot->name= "New Texture";
568         ot->idname= "TEXTURE_OT_new";
569         ot->description="Add a new texture";
570         
571         /* api callbacks */
572         ot->exec= new_texture_exec;
573
574         /* flags */
575         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
576 }
577
578 /********************** new world operator *********************/
579
580 static int new_world_exec(bContext *C, wmOperator *op)
581 {
582         World *wo= CTX_data_pointer_get_type(C, "world", &RNA_World).data;
583         PointerRNA ptr, idptr;
584         PropertyRNA *prop;
585
586         /* add or copy world */
587         if(wo)
588                 wo= copy_world(wo);
589         else
590                 wo= add_world("World");
591
592         /* hook into UI */
593         uiIDContextProperty(C, &ptr, &prop);
594
595         if(prop) {
596                 /* when creating new ID blocks, use is already 1, but RNA
597                  * pointer se also increases user, so this compensates it */
598                 wo->id.us--;
599
600                 RNA_id_pointer_create(&wo->id, &idptr);
601                 RNA_property_pointer_set(&ptr, prop, idptr);
602                 RNA_property_update(C, &ptr, prop);
603         }
604
605         WM_event_add_notifier(C, NC_WORLD|NA_ADDED, wo);
606         
607         return OPERATOR_FINISHED;
608 }
609
610 void WORLD_OT_new(wmOperatorType *ot)
611 {
612         /* identifiers */
613         ot->name= "New World";
614         ot->idname= "WORLD_OT_new";
615         ot->description= "Add a new world";
616         
617         /* api callbacks */
618         ot->exec= new_world_exec;
619
620         /* flags */
621         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
622 }
623
624 /********************** render layer operators *********************/
625
626 static int render_layer_add_exec(bContext *C, wmOperator *op)
627 {
628         Scene *scene= CTX_data_scene(C);
629
630         scene_add_render_layer(scene);
631         scene->r.actlay= BLI_countlist(&scene->r.layers) - 1;
632
633         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
634         
635         return OPERATOR_FINISHED;
636 }
637
638 void SCENE_OT_render_layer_add(wmOperatorType *ot)
639 {
640         /* identifiers */
641         ot->name= "Add Render Layer";
642         ot->idname= "SCENE_OT_render_layer_add";
643         ot->description="Add a render layer";
644         
645         /* api callbacks */
646         ot->exec= render_layer_add_exec;
647
648         /* flags */
649         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
650 }
651
652 static int render_layer_remove_exec(bContext *C, wmOperator *op)
653 {
654         Scene *scene= CTX_data_scene(C);
655         SceneRenderLayer *rl;
656         int act= scene->r.actlay;
657
658         if(BLI_countlist(&scene->r.layers) <= 1)
659                 return OPERATOR_CANCELLED;
660         
661         rl= BLI_findlink(&scene->r.layers, scene->r.actlay);
662         BLI_remlink(&scene->r.layers, rl);
663         MEM_freeN(rl);
664
665         scene->r.actlay= 0;
666         
667         if(scene->nodetree) {
668                 bNode *node;
669                 for(node= scene->nodetree->nodes.first; node; node= node->next) {
670                         if(node->type==CMP_NODE_R_LAYERS && node->id==NULL) {
671                                 if(node->custom1==act)
672                                         node->custom1= 0;
673                                 else if(node->custom1>act)
674                                         node->custom1--;
675                         }
676                 }
677         }
678
679         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
680         
681         return OPERATOR_FINISHED;
682 }
683
684 void SCENE_OT_render_layer_remove(wmOperatorType *ot)
685 {
686         /* identifiers */
687         ot->name= "Remove Render Layer";
688         ot->idname= "SCENE_OT_render_layer_remove";
689         ot->description="Remove the selected render layer";
690         
691         /* api callbacks */
692         ot->exec= render_layer_remove_exec;
693
694         /* flags */
695         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
696 }
697
698 static int texture_slot_move(bContext *C, wmOperator *op)
699 {
700         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
701         Material *ma = (Material *)id;
702
703         if(id) {
704                 MTex **mtex_ar, *mtexswap;
705                 short act;
706                 int type= RNA_enum_get(op->ptr, "type");
707
708                 give_active_mtex(id, &mtex_ar, &act);
709
710                 if(type == -1) { /* Up */
711                         if(act > 0) {
712                                 mtexswap = mtex_ar[act];
713                                 mtex_ar[act] = mtex_ar[act-1];
714                                 mtex_ar[act-1] = mtexswap;
715                                 
716                                 BKE_animdata_fix_paths_rename(id, ma->adt, "texture_slots", NULL, NULL, act-1, -1, 0);
717                                 BKE_animdata_fix_paths_rename(id, ma->adt, "texture_slots", NULL, NULL, act, act-1, 0);
718                                 BKE_animdata_fix_paths_rename(id, ma->adt, "texture_slots", NULL, NULL, -1, act, 0);
719
720                                 if(GS(id->name)==ID_MA) {
721                                         Material *ma= (Material *)id;
722                                         int mtexuse = ma->septex & (1<<act);
723                                         ma->septex &= ~(1<<act);
724                                         ma->septex |= (ma->septex & (1<<(act-1))) << 1;
725                                         ma->septex &= ~(1<<(act-1));
726                                         ma->septex |= mtexuse >> 1;
727                                 }
728                                 
729                                 set_active_mtex(id, act-1);
730                         }
731                 }
732                 else { /* Down */
733                         if(act < MAX_MTEX-1) {
734                                 mtexswap = mtex_ar[act];
735                                 mtex_ar[act] = mtex_ar[act+1];
736                                 mtex_ar[act+1] = mtexswap;
737                                 
738                                 BKE_animdata_fix_paths_rename(id, ma->adt, "texture_slots", NULL, NULL, act+1, -1, 0);
739                                 BKE_animdata_fix_paths_rename(id, ma->adt, "texture_slots", NULL, NULL, act, act+1, 0);
740                                 BKE_animdata_fix_paths_rename(id, ma->adt, "texture_slots", NULL, NULL, -1, act, 0);
741
742                                 if(GS(id->name)==ID_MA) {
743                                         Material *ma= (Material *)id;
744                                         int mtexuse = ma->septex & (1<<act);
745                                         ma->septex &= ~(1<<act);
746                                         ma->septex |= (ma->septex & (1<<(act+1))) >> 1;
747                                         ma->septex &= ~(1<<(act+1));
748                                         ma->septex |= mtexuse << 1;
749                                 }
750                                 
751                                 set_active_mtex(id, act+1);
752                         }
753                 }
754
755                 WM_event_add_notifier(C, NC_TEXTURE, CTX_data_scene(C));
756         }
757
758         return OPERATOR_FINISHED;
759 }
760
761 void TEXTURE_OT_slot_move(wmOperatorType *ot)
762 {
763         static EnumPropertyItem slot_move[] = {
764                 {-1, "UP", 0, "Up", ""},
765                 {1, "DOWN", 0, "Down", ""},
766                 {0, NULL, 0, NULL, NULL}
767         };
768
769         /* identifiers */
770         ot->name= "Move Texture Slot";
771         ot->idname= "TEXTURE_OT_slot_move";
772         ot->description="Move texture slots up and down";
773
774         /* api callbacks */
775         ot->exec= texture_slot_move;
776
777         /* flags */
778         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
779
780         RNA_def_enum(ot->srna, "type", slot_move, 0, "Type", "");
781 }
782
783
784
785 /********************** environment map operators *********************/
786
787 static int save_envmap(wmOperator *op, Scene *scene, EnvMap *env, char *str, int imtype)
788 {
789         ImBuf *ibuf;
790         int dx;
791         int retval;
792         
793         if(env->cube[1]==NULL) {
794                 BKE_report(op->reports, RPT_ERROR, "There is no generated environment map available to save");
795                 return OPERATOR_CANCELLED;
796         }
797         
798         dx= env->cube[1]->x;
799         
800         if (env->type == ENV_CUBE) {
801                 ibuf = IMB_allocImBuf(3*dx, 2*dx, 24, IB_rectfloat, 0);
802
803                 IMB_rectcpy(ibuf, env->cube[0], 0, 0, 0, 0, dx, dx);
804                 IMB_rectcpy(ibuf, env->cube[1], dx, 0, 0, 0, dx, dx);
805                 IMB_rectcpy(ibuf, env->cube[2], 2*dx, 0, 0, 0, dx, dx);
806                 IMB_rectcpy(ibuf, env->cube[3], 0, dx, 0, 0, dx, dx);
807                 IMB_rectcpy(ibuf, env->cube[4], dx, dx, 0, 0, dx, dx);
808                 IMB_rectcpy(ibuf, env->cube[5], 2*dx, dx, 0, 0, dx, dx);
809         }
810         else if (env->type == ENV_PLANE) {
811                 ibuf = IMB_allocImBuf(dx, dx, 24, IB_rectfloat, 0);
812                 IMB_rectcpy(ibuf, env->cube[1], 0, 0, 0, 0, dx, dx);            
813         }
814         
815         if (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
816                 ibuf->profile = IB_PROFILE_LINEAR_RGB;
817         
818         if (BKE_write_ibuf(scene, ibuf, str, imtype, scene->r.subimtype, scene->r.quality)) {
819                 retval = OPERATOR_FINISHED;
820         }
821         else {
822                 BKE_reportf(op->reports, RPT_ERROR, "Error saving environment map to %s.", str);
823                 retval = OPERATOR_CANCELLED;
824         }
825         
826         IMB_freeImBuf(ibuf);
827         ibuf = NULL;
828         
829         return retval;
830 }
831
832 static int envmap_save_exec(bContext *C, wmOperator *op)
833 {
834         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
835         Scene *scene = CTX_data_scene(C);
836         //int imtype = RNA_enum_get(op->ptr, "file_type");
837         int imtype = scene->r.imtype;
838         char path[FILE_MAX];
839         
840         RNA_string_get(op->ptr, "path", path);
841         
842         if(scene->r.scemode & R_EXTENSION)  {
843                 BKE_add_image_extension(path, imtype);
844         }
845         
846         WM_cursor_wait(1);
847         
848         save_envmap(op, scene, tex->env, path, imtype);
849         
850         WM_cursor_wait(0);
851         
852         WM_event_add_notifier(C, NC_TEXTURE, tex);
853         
854         return OPERATOR_FINISHED;
855 }
856
857 static int envmap_save_invoke(bContext *C, wmOperator *op, wmEvent *event)
858 {
859         //Scene *scene= CTX_data_scene(C);
860         
861         if(!RNA_property_is_set(op->ptr, "relative_path"))
862                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
863         
864         if(RNA_property_is_set(op->ptr, "path"))
865                 return envmap_save_exec(C, op);
866
867         //RNA_enum_set(op->ptr, "file_type", scene->r.imtype);
868         
869         RNA_string_set(op->ptr, "path", G.sce);
870         WM_event_add_fileselect(C, op);
871         
872         return OPERATOR_RUNNING_MODAL;
873 }
874
875 static int envmap_save_poll(bContext *C)
876 {
877         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
878
879         if (!tex) 
880                 return 0;
881         if (!tex->env || !tex->env->ok)
882                 return 0;
883         if (tex->env->cube[1]==NULL)
884                 return 0;
885         
886         return 1;
887 }
888
889 void TEXTURE_OT_envmap_save(wmOperatorType *ot)
890 {
891         /* identifiers */
892         ot->name= "Save Environment Map";
893         ot->idname= "TEXTURE_OT_envmap_save";
894         ot->description="Save the current generated Environment map to an image file";
895         
896         /* api callbacks */
897         ot->exec= envmap_save_exec;
898         ot->invoke= envmap_save_invoke;
899         ot->poll= envmap_save_poll;
900         
901         /* flags */
902         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
903         
904         /* properties */
905         //RNA_def_enum(ot->srna, "file_type", image_file_type_items, R_PNG, "File Type", "File type to save image as.");
906         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_SAVE);
907         
908         RNA_def_boolean(ot->srna, "relative_path", 0, "Relative Path", "Save image with relative path to current .blend file");
909 }
910
911 static int envmap_clear_exec(bContext *C, wmOperator *op)
912 {
913         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
914         
915         BKE_free_envmapdata(tex->env);
916         
917         WM_event_add_notifier(C, NC_TEXTURE|NA_EDITED, tex);
918         
919         return OPERATOR_FINISHED;
920 }
921
922 static int envmap_clear_poll(bContext *C)
923 {
924         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
925         
926         if (!tex) 
927                 return 0;
928         if (!tex->env || !tex->env->ok)
929                 return 0;
930         if (tex->env->cube[1]==NULL)
931                 return 0;
932         
933         return 1;
934 }
935
936 void TEXTURE_OT_envmap_clear(wmOperatorType *ot)
937 {
938         /* identifiers */
939         ot->name= "Clear Environment Map";
940         ot->idname= "TEXTURE_OT_envmap_clear";
941         ot->description="Discard the environment map and free it from memory";
942         
943         /* api callbacks */
944         ot->exec= envmap_clear_exec;
945         ot->poll= envmap_clear_poll;
946         
947         /* flags */
948         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
949 }
950
951 static int envmap_clear_all_exec(bContext *C, wmOperator *op)
952 {
953         Main *bmain = CTX_data_main(C);
954         Tex *tex;
955         
956         for (tex=bmain->tex.first; tex; tex=tex->id.next)
957                 if (tex->env)
958                         BKE_free_envmapdata(tex->env);
959         
960         WM_event_add_notifier(C, NC_TEXTURE|NA_EDITED, tex);
961         
962         return OPERATOR_FINISHED;
963 }
964
965 void TEXTURE_OT_envmap_clear_all(wmOperatorType *ot)
966 {
967         /* identifiers */
968         ot->name= "Clear All Environment Maps";
969         ot->idname= "TEXTURE_OT_envmap_clear_all";
970         ot->description="Discard all environment maps in the .blend file and free them from memory";
971         
972         /* api callbacks */
973         ot->exec= envmap_clear_all_exec;
974         ot->poll= envmap_clear_poll;
975         
976         /* flags */
977         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
978 }
979
980 /********************** material operators *********************/
981
982 /* material copy/paste */
983 static int copy_material_exec(bContext *C, wmOperator *op)
984 {
985         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
986
987         if(ma==NULL)
988                 return OPERATOR_CANCELLED;
989
990         copy_matcopybuf(ma);
991
992         WM_event_add_notifier(C, NC_MATERIAL, ma);
993
994         return OPERATOR_FINISHED;
995 }
996
997 void MATERIAL_OT_copy(wmOperatorType *ot)
998 {
999         /* identifiers */
1000         ot->name= "Copy Material";
1001         ot->idname= "MATERIAL_OT_copy";
1002         ot->description="Copy the material settings and nodes";
1003
1004         /* api callbacks */
1005         ot->exec= copy_material_exec;
1006
1007         /* flags */
1008         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1009 }
1010
1011 static int paste_material_exec(bContext *C, wmOperator *op)
1012 {
1013         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
1014
1015         if(ma==NULL)
1016                 return OPERATOR_CANCELLED;
1017
1018         paste_matcopybuf(ma);
1019
1020         WM_event_add_notifier(C, NC_MATERIAL|ND_SHADING_DRAW, ma);
1021
1022         return OPERATOR_FINISHED;
1023 }
1024
1025 void MATERIAL_OT_paste(wmOperatorType *ot)
1026 {
1027         /* identifiers */
1028         ot->name= "Paste Material";
1029         ot->idname= "MATERIAL_OT_paste";
1030         ot->description="Copy the material settings and nodes";
1031
1032         /* api callbacks */
1033         ot->exec= paste_material_exec;
1034
1035         /* flags */
1036         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1037 }
1038
1039 static int copy_material_mtex_exec(bContext *C, wmOperator *op)
1040 {
1041         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
1042
1043         if(ma==NULL)
1044                 return OPERATOR_CANCELLED;
1045
1046         copy_mat_mtex_copybuf(&ma->id);
1047
1048         WM_event_add_notifier(C, NC_MATERIAL, ma);
1049
1050         return OPERATOR_FINISHED;
1051 }
1052
1053 void MATERIAL_OT_mtex_copy(wmOperatorType *ot)
1054 {
1055         /* identifiers */
1056         ot->name= "Copy Material Texture Settings";
1057         ot->idname= "MATERIAL_OT_mtex_copy";
1058         ot->description="Copy the material texture settings and nodes";
1059
1060         /* api callbacks */
1061         ot->exec= copy_material_mtex_exec;
1062
1063         /* flags */
1064         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1065 }
1066
1067 static int paste_material_mtex_exec(bContext *C, wmOperator *op)
1068 {
1069         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
1070
1071         if(ma==NULL)
1072                 return OPERATOR_CANCELLED;
1073
1074         paste_mat_mtex_copybuf(&ma->id);
1075
1076         WM_event_add_notifier(C, NC_MATERIAL|ND_SHADING_DRAW, ma);
1077
1078         return OPERATOR_FINISHED;
1079 }
1080
1081 void MATERIAL_OT_mtex_paste(wmOperatorType *ot)
1082 {
1083         /* identifiers */
1084         ot->name= "Paste Material Texture Settings";
1085         ot->idname= "MATERIAL_OT_mtex_paste";
1086         ot->description="Copy the material texture settings and nodes";
1087
1088         /* api callbacks */
1089         ot->exec= paste_material_mtex_exec;
1090
1091         /* flags */
1092         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1093 }