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