Cycles: svn merge -r41266:41467 ^/trunk/blender
[blender.git] / source / blender / editors / render / render_shading.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software Foundation,
15  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16  *
17  * The Original Code is Copyright (C) 2009 Blender Foundation.
18  * All rights reserved.
19  *
20  * Contributor(s): Blender Foundation
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file blender/editors/render/render_shading.c
26  *  \ingroup edrend
27  */
28
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_curve_types.h"
35 #include "DNA_lamp_types.h"
36 #include "DNA_material_types.h"
37 #include "DNA_node_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_particle_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_space_types.h"
42 #include "DNA_world_types.h"
43
44 #include "BLI_blenlib.h"
45 #include "BLI_math.h"
46 #include "BLI_editVert.h"
47 #include "BLI_utildefines.h"
48
49 #include "BKE_animsys.h"
50 #include "BKE_context.h"
51 #include "BKE_curve.h"
52 #include "BKE_depsgraph.h"
53 #include "BKE_font.h"
54 #include "BKE_global.h"
55 #include "BKE_icons.h"
56 #include "BKE_image.h"
57 #include "BKE_library.h"
58 #include "BKE_main.h"
59 #include "BKE_material.h"
60 #include "BKE_node.h"
61 #include "BKE_report.h"
62 #include "BKE_scene.h"
63 #include "BKE_texture.h"
64 #include "BKE_world.h"
65
66 #include "IMB_imbuf.h"
67 #include "IMB_imbuf_types.h"
68
69 #include "GPU_material.h"
70
71 #include "RNA_access.h"
72
73 #include "WM_api.h"
74 #include "WM_types.h"
75
76 #include "ED_curve.h"
77 #include "ED_mesh.h"
78 #include "ED_node.h"
79 #include "ED_render.h"
80 #include "ED_screen.h"
81
82 #include "RNA_define.h"
83
84 #include "UI_interface.h"
85
86 #include "RE_pipeline.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= 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= 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         Scene *scene= CTX_data_scene(C);
367         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
368         PointerRNA ptr, idptr;
369         PropertyRNA *prop;
370
371         /* add or copy material */
372         if(ma) {
373                 ma= copy_material(ma);
374         }
375         else {
376                 ma= add_material("Material");
377
378                 if(scene_use_new_shading_nodes(scene)) {
379                         ED_node_shader_default(scene, &ma->id);
380                         ma->use_nodes= 1;
381                 }
382         }
383
384         /* hook into UI */
385         uiIDContextProperty(C, &ptr, &prop);
386
387         if(prop) {
388                 /* when creating new ID blocks, use is already 1, but RNA
389                  * pointer se also increases user, so this compensates it */
390                 ma->id.us--;
391
392                 RNA_id_pointer_create(&ma->id, &idptr);
393                 RNA_property_pointer_set(&ptr, prop, idptr);
394                 RNA_property_update(C, &ptr, prop);
395         }
396
397         WM_event_add_notifier(C, NC_MATERIAL|NA_ADDED, ma);
398         
399         return OPERATOR_FINISHED;
400 }
401
402 void MATERIAL_OT_new(wmOperatorType *ot)
403 {
404         /* identifiers */
405         ot->name= "New Material";
406         ot->idname= "MATERIAL_OT_new";
407         ot->description="Add a new material";
408         
409         /* api callbacks */
410         ot->exec= new_material_exec;
411
412         /* flags */
413         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
414 }
415
416 /********************** new texture operator *********************/
417
418 static int new_texture_exec(bContext *C, wmOperator *UNUSED(op))
419 {
420         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
421         PointerRNA ptr, idptr;
422         PropertyRNA *prop;
423
424         /* add or copy texture */
425         if(tex)
426                 tex= copy_texture(tex);
427         else
428                 tex= add_texture("Texture");
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         Scene *scene= CTX_data_scene(C);
467         World *wo= CTX_data_pointer_get_type(C, "world", &RNA_World).data;
468         PointerRNA ptr, idptr;
469         PropertyRNA *prop;
470
471         /* add or copy world */
472         if(wo) {
473                 wo= copy_world(wo);
474         }
475         else {
476                 wo= add_world("World");
477
478                 if(scene_use_new_shading_nodes(scene)) {
479                         ED_node_shader_default(scene, &wo->id);
480                         wo->use_nodes= 1;
481                 }
482         }
483
484         /* hook into UI */
485         uiIDContextProperty(C, &ptr, &prop);
486
487         if(prop) {
488                 /* when creating new ID blocks, use is already 1, but RNA
489                  * pointer se also increases user, so this compensates it */
490                 wo->id.us--;
491
492                 RNA_id_pointer_create(&wo->id, &idptr);
493                 RNA_property_pointer_set(&ptr, prop, idptr);
494                 RNA_property_update(C, &ptr, prop);
495         }
496
497         WM_event_add_notifier(C, NC_WORLD|NA_ADDED, wo);
498         
499         return OPERATOR_FINISHED;
500 }
501
502 void WORLD_OT_new(wmOperatorType *ot)
503 {
504         /* identifiers */
505         ot->name= "New World";
506         ot->idname= "WORLD_OT_new";
507         ot->description= "Add a new world";
508         
509         /* api callbacks */
510         ot->exec= new_world_exec;
511
512         /* flags */
513         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
514 }
515
516 /********************** render layer operators *********************/
517
518 static int render_layer_add_exec(bContext *C, wmOperator *UNUSED(op))
519 {
520         Scene *scene= CTX_data_scene(C);
521
522         scene_add_render_layer(scene);
523         scene->r.actlay= BLI_countlist(&scene->r.layers) - 1;
524
525         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
526         
527         return OPERATOR_FINISHED;
528 }
529
530 void SCENE_OT_render_layer_add(wmOperatorType *ot)
531 {
532         /* identifiers */
533         ot->name= "Add Render Layer";
534         ot->idname= "SCENE_OT_render_layer_add";
535         ot->description="Add a render layer";
536         
537         /* api callbacks */
538         ot->exec= render_layer_add_exec;
539
540         /* flags */
541         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
542 }
543
544 static int render_layer_remove_exec(bContext *C, wmOperator *UNUSED(op))
545 {
546         Scene *scene = CTX_data_scene(C), *sce;
547         SceneRenderLayer *rl;
548         int act= scene->r.actlay;
549
550         if(BLI_countlist(&scene->r.layers) <= 1)
551                 return OPERATOR_CANCELLED;
552         
553         rl= BLI_findlink(&scene->r.layers, scene->r.actlay);
554         BLI_remlink(&scene->r.layers, rl);
555         MEM_freeN(rl);
556
557         scene->r.actlay= 0;
558
559         for(sce = CTX_data_main(C)->scene.first; sce; sce = sce->id.next) {
560                 if(sce->nodetree) {
561                         bNode *node;
562                         for(node = sce->nodetree->nodes.first; node; node = node->next) {
563                                 if(node->type==CMP_NODE_R_LAYERS && (Scene*)node->id==scene) {
564                                         if(node->custom1==act)
565                                                 node->custom1= 0;
566                                         else if(node->custom1>act)
567                                                 node->custom1--;
568                                 }
569                         }
570                 }
571         }
572
573         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
574         
575         return OPERATOR_FINISHED;
576 }
577
578 void SCENE_OT_render_layer_remove(wmOperatorType *ot)
579 {
580         /* identifiers */
581         ot->name= "Remove Render Layer";
582         ot->idname= "SCENE_OT_render_layer_remove";
583         ot->description="Remove the selected render layer";
584         
585         /* api callbacks */
586         ot->exec= render_layer_remove_exec;
587
588         /* flags */
589         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
590 }
591
592 static int texture_slot_move(bContext *C, wmOperator *op)
593 {
594         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
595
596         if(id) {
597                 MTex **mtex_ar, *mtexswap;
598                 short act;
599                 int type= RNA_enum_get(op->ptr, "type");
600                 struct AnimData *adt= BKE_animdata_from_id(id);
601
602                 give_active_mtex(id, &mtex_ar, &act);
603
604                 if(type == -1) { /* Up */
605                         if(act > 0) {
606                                 mtexswap = mtex_ar[act];
607                                 mtex_ar[act] = mtex_ar[act-1];
608                                 mtex_ar[act-1] = mtexswap;
609                                 
610                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, act-1, -1, 0);
611                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, act, act-1, 0);
612                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, -1, act, 0);
613
614                                 if(GS(id->name)==ID_MA) {
615                                         Material *ma= (Material *)id;
616                                         int mtexuse = ma->septex & (1<<act);
617                                         ma->septex &= ~(1<<act);
618                                         ma->septex |= (ma->septex & (1<<(act-1))) << 1;
619                                         ma->septex &= ~(1<<(act-1));
620                                         ma->septex |= mtexuse >> 1;
621                                 }
622                                 
623                                 set_active_mtex(id, act-1);
624                         }
625                 }
626                 else { /* Down */
627                         if(act < MAX_MTEX-1) {
628                                 mtexswap = mtex_ar[act];
629                                 mtex_ar[act] = mtex_ar[act+1];
630                                 mtex_ar[act+1] = mtexswap;
631                                 
632                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, act+1, -1, 0);
633                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, act, act+1, 0);
634                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, -1, act, 0);
635
636                                 if(GS(id->name)==ID_MA) {
637                                         Material *ma= (Material *)id;
638                                         int mtexuse = ma->septex & (1<<act);
639                                         ma->septex &= ~(1<<act);
640                                         ma->septex |= (ma->septex & (1<<(act+1))) >> 1;
641                                         ma->septex &= ~(1<<(act+1));
642                                         ma->septex |= mtexuse << 1;
643                                 }
644                                 
645                                 set_active_mtex(id, act+1);
646                         }
647                 }
648
649                 DAG_id_tag_update(id, 0);
650                 WM_event_add_notifier(C, NC_TEXTURE, CTX_data_scene(C));
651         }
652
653         return OPERATOR_FINISHED;
654 }
655
656 void TEXTURE_OT_slot_move(wmOperatorType *ot)
657 {
658         static EnumPropertyItem slot_move[] = {
659                 {-1, "UP", 0, "Up", ""},
660                 {1, "DOWN", 0, "Down", ""},
661                 {0, NULL, 0, NULL, NULL}
662         };
663
664         /* identifiers */
665         ot->name= "Move Texture Slot";
666         ot->idname= "TEXTURE_OT_slot_move";
667         ot->description="Move texture slots up and down";
668
669         /* api callbacks */
670         ot->exec= texture_slot_move;
671
672         /* flags */
673         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
674
675         RNA_def_enum(ot->srna, "type", slot_move, 0, "Type", "");
676 }
677
678
679
680 /********************** environment map operators *********************/
681
682 static int save_envmap(wmOperator *op, Scene *scene, EnvMap *env, char *path, int imtype)
683 {
684         float layout[12];
685         if ( RNA_struct_find_property(op->ptr, "layout") )
686                 RNA_float_get_array(op->ptr, "layout",layout);
687         else
688                 memcpy(layout, default_envmap_layout, sizeof(layout));
689
690         if (RE_WriteEnvmapResult(op->reports, scene, env, path, imtype, layout)) {
691                 return OPERATOR_FINISHED;
692         }
693         else {
694                 return OPERATOR_CANCELLED;
695         }
696
697 }
698
699 static int envmap_save_exec(bContext *C, wmOperator *op)
700 {
701         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
702         Scene *scene = CTX_data_scene(C);
703         //int imtype = RNA_enum_get(op->ptr, "file_type");
704         int imtype = scene->r.imtype;
705         char path[FILE_MAX];
706         
707         RNA_string_get(op->ptr, "filepath", path);
708         
709         if(scene->r.scemode & R_EXTENSION)  {
710                 BKE_add_image_extension(path, imtype);
711         }
712         
713         WM_cursor_wait(1);
714         
715         save_envmap(op, scene, tex->env, path, imtype);
716         
717         WM_cursor_wait(0);
718         
719         WM_event_add_notifier(C, NC_TEXTURE, tex);
720         
721         return OPERATOR_FINISHED;
722 }
723
724 static int envmap_save_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
725 {
726         //Scene *scene= CTX_data_scene(C);
727         
728         if(RNA_property_is_set(op->ptr, "filepath"))
729                 return envmap_save_exec(C, op);
730
731         //RNA_enum_set(op->ptr, "file_type", scene->r.imtype);
732         RNA_string_set(op->ptr, "filepath", G.main->name);
733         WM_event_add_fileselect(C, op);
734         
735         return OPERATOR_RUNNING_MODAL;
736 }
737
738 static int envmap_save_poll(bContext *C)
739 {
740         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
741
742         if (!tex) 
743                 return 0;
744         if (!tex->env || !tex->env->ok)
745                 return 0;
746         if (tex->env->cube[1]==NULL)
747                 return 0;
748         
749         return 1;
750 }
751
752 void TEXTURE_OT_envmap_save(wmOperatorType *ot)
753 {
754         PropertyRNA *prop;
755         /* identifiers */
756         ot->name= "Save Environment Map";
757         ot->idname= "TEXTURE_OT_envmap_save";
758         ot->description="Save the current generated Environment map to an image file";
759         
760         /* api callbacks */
761         ot->exec= envmap_save_exec;
762         ot->invoke= envmap_save_invoke;
763         ot->poll= envmap_save_poll;
764         
765         /* flags */
766         ot->flag= OPTYPE_REGISTER; /* no undo since this doesnt modify the env-map */
767         
768         /* properties */
769         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);
770         RNA_def_property_flag(prop, PROP_HIDDEN);
771
772         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_SAVE, WM_FILESEL_FILEPATH);
773 }
774
775 static int envmap_clear_exec(bContext *C, wmOperator *UNUSED(op))
776 {
777         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
778         
779         BKE_free_envmapdata(tex->env);
780         
781         WM_event_add_notifier(C, NC_TEXTURE|NA_EDITED, tex);
782         
783         return OPERATOR_FINISHED;
784 }
785
786 static int envmap_clear_poll(bContext *C)
787 {
788         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
789         
790         if (!tex) 
791                 return 0;
792         if (!tex->env || !tex->env->ok)
793                 return 0;
794         if (tex->env->cube[1]==NULL)
795                 return 0;
796         
797         return 1;
798 }
799
800 void TEXTURE_OT_envmap_clear(wmOperatorType *ot)
801 {
802         /* identifiers */
803         ot->name= "Clear Environment Map";
804         ot->idname= "TEXTURE_OT_envmap_clear";
805         ot->description="Discard the environment map and free it from memory";
806         
807         /* api callbacks */
808         ot->exec= envmap_clear_exec;
809         ot->poll= envmap_clear_poll;
810         
811         /* flags */
812         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
813 }
814
815 static int envmap_clear_all_exec(bContext *C, wmOperator *UNUSED(op))
816 {
817         Main *bmain = CTX_data_main(C);
818         Tex *tex;
819         
820         for (tex=bmain->tex.first; tex; tex=tex->id.next)
821                 if (tex->env)
822                         BKE_free_envmapdata(tex->env);
823         
824         WM_event_add_notifier(C, NC_TEXTURE|NA_EDITED, tex);
825         
826         return OPERATOR_FINISHED;
827 }
828
829 void TEXTURE_OT_envmap_clear_all(wmOperatorType *ot)
830 {
831         /* identifiers */
832         ot->name= "Clear All Environment Maps";
833         ot->idname= "TEXTURE_OT_envmap_clear_all";
834         ot->description="Discard all environment maps in the .blend file and free them from memory";
835         
836         /* api callbacks */
837         ot->exec= envmap_clear_all_exec;
838         ot->poll= envmap_clear_poll;
839         
840         /* flags */
841         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
842 }
843
844 /********************** material operators *********************/
845
846 /* material copy/paste */
847 static int copy_material_exec(bContext *C, wmOperator *UNUSED(op))
848 {
849         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
850
851         if(ma==NULL)
852                 return OPERATOR_CANCELLED;
853
854         copy_matcopybuf(ma);
855
856         return OPERATOR_FINISHED;
857 }
858
859 void MATERIAL_OT_copy(wmOperatorType *ot)
860 {
861         /* identifiers */
862         ot->name= "Copy Material";
863         ot->idname= "MATERIAL_OT_copy";
864         ot->description="Copy the material settings and nodes";
865
866         /* api callbacks */
867         ot->exec= copy_material_exec;
868
869         /* flags */
870         ot->flag= OPTYPE_REGISTER; /* no undo needed since no changes are made to the material */
871 }
872
873 static int paste_material_exec(bContext *C, wmOperator *UNUSED(op))
874 {
875         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
876
877         if(ma==NULL)
878                 return OPERATOR_CANCELLED;
879
880         paste_matcopybuf(ma);
881
882         WM_event_add_notifier(C, NC_MATERIAL|ND_SHADING_DRAW, ma);
883
884         return OPERATOR_FINISHED;
885 }
886
887 void MATERIAL_OT_paste(wmOperatorType *ot)
888 {
889         /* identifiers */
890         ot->name= "Paste Material";
891         ot->idname= "MATERIAL_OT_paste";
892         ot->description="Paste the material settings and nodes";
893
894         /* api callbacks */
895         ot->exec= paste_material_exec;
896
897         /* flags */
898         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
899 }
900
901
902 static short mtexcopied=0; /* must be reset on file load */
903 static MTex mtexcopybuf;
904
905 void ED_render_clear_mtex_copybuf(void)
906 {       /* use for file reload */
907         mtexcopied= 0;
908 }
909
910 static void copy_mtex_copybuf(ID *id)
911 {
912         MTex **mtex= NULL;
913         
914         switch(GS(id->name)) {
915                 case ID_MA:
916                         mtex= &(((Material *)id)->mtex[(int)((Material *)id)->texact]);
917                         break;
918                 case ID_LA:
919                         mtex= &(((Lamp *)id)->mtex[(int)((Lamp *)id)->texact]);
920                         // la->mtex[(int)la->texact] // TODO
921                         break;
922                 case ID_WO:
923                         mtex= &(((World *)id)->mtex[(int)((World *)id)->texact]);
924                         // mtex= wrld->mtex[(int)wrld->texact]; // TODO
925                         break;
926                 case ID_PA:
927                         mtex= &(((ParticleSettings *)id)->mtex[(int)((ParticleSettings *)id)->texact]);
928                         break;
929         }
930         
931         if(mtex && *mtex) {
932                 memcpy(&mtexcopybuf, *mtex, sizeof(MTex));
933                 mtexcopied= 1;
934         }
935         else {
936                 mtexcopied= 0;
937         }
938 }
939
940 static void paste_mtex_copybuf(ID *id)
941 {
942         MTex **mtex= NULL;
943         
944         if(mtexcopied == 0 || mtexcopybuf.tex==NULL)
945                 return;
946         
947         switch(GS(id->name)) {
948                 case ID_MA:
949                         mtex= &(((Material *)id)->mtex[(int)((Material *)id)->texact]);
950                         break;
951                 case ID_LA:
952                         mtex= &(((Lamp *)id)->mtex[(int)((Lamp *)id)->texact]);
953                         // la->mtex[(int)la->texact] // TODO
954                         break;
955                 case ID_WO:
956                         mtex= &(((World *)id)->mtex[(int)((World *)id)->texact]);
957                         // mtex= wrld->mtex[(int)wrld->texact]; // TODO
958                         break;
959                 case ID_PA:
960                         mtex= &(((ParticleSettings *)id)->mtex[(int)((ParticleSettings *)id)->texact]);
961                         break;
962                 default:
963                         BLI_assert("invalid id type");
964                         return;
965         }
966         
967         if(mtex) {
968                 if(*mtex==NULL) {
969                         *mtex= MEM_mallocN(sizeof(MTex), "mtex copy");
970                 }
971                 else if((*mtex)->tex) {
972                         (*mtex)->tex->id.us--;
973                 }
974                 
975                 memcpy(*mtex, &mtexcopybuf, sizeof(MTex));
976                 
977                 id_us_plus((ID *)mtexcopybuf.tex);
978         }
979 }
980
981
982 static int copy_mtex_exec(bContext *C, wmOperator *UNUSED(op))
983 {
984         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
985
986         if(id==NULL) {
987                 /* copying empty slot */
988                 ED_render_clear_mtex_copybuf();
989                 return OPERATOR_CANCELLED;
990         }
991
992         copy_mtex_copybuf(id);
993
994         return OPERATOR_FINISHED;
995 }
996
997 static int copy_mtex_poll(bContext *C)
998 {
999         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
1000         
1001         return (id != NULL);
1002 }
1003
1004 void TEXTURE_OT_slot_copy(wmOperatorType *ot)
1005 {
1006         /* identifiers */
1007         ot->name= "Copy Texture Slot Settings";
1008         ot->idname= "TEXTURE_OT_slot_copy";
1009         ot->description="Copy the material texture settings and nodes";
1010
1011         /* api callbacks */
1012         ot->exec= copy_mtex_exec;
1013         ot->poll= copy_mtex_poll;
1014         
1015         /* flags */
1016         ot->flag= OPTYPE_REGISTER; /* no undo needed since no changes are made to the mtex */
1017 }
1018
1019 static int paste_mtex_exec(bContext *C, wmOperator *UNUSED(op))
1020 {
1021         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
1022
1023         if(id==NULL) {
1024                 Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
1025                 Lamp *la= CTX_data_pointer_get_type(C, "lamp", &RNA_Lamp).data;
1026                 World *wo= CTX_data_pointer_get_type(C, "world", &RNA_World).data;
1027                 ParticleSystem *psys= CTX_data_pointer_get_type(C, "particle_system", &RNA_ParticleSystem).data;
1028                 
1029                 if (ma)
1030                         id = &ma->id;
1031                 else if (la)
1032                         id = &la->id;
1033                 else if (wo)
1034                         id = &wo->id;
1035                 else if (psys)
1036                         id = &psys->part->id;
1037                 
1038                 if (id==NULL)
1039                         return OPERATOR_CANCELLED;
1040         }
1041
1042         paste_mtex_copybuf(id);
1043
1044         WM_event_add_notifier(C, NC_TEXTURE|ND_SHADING_DRAW, NULL);
1045
1046         return OPERATOR_FINISHED;
1047 }
1048
1049 void TEXTURE_OT_slot_paste(wmOperatorType *ot)
1050 {
1051         /* identifiers */
1052         ot->name= "Paste Texture Slot Settings";
1053         ot->idname= "TEXTURE_OT_slot_paste";
1054         ot->description="Copy the texture settings and nodes";
1055
1056         /* api callbacks */
1057         ot->exec= paste_mtex_exec;
1058
1059         /* flags */
1060         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1061 }
1062