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