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