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