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