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