Cycles: svn merge -r41182:41205 ^/trunk/blender
[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_utildefines.h"
50
51 #include "BKE_animsys.h"
52 #include "BKE_context.h"
53 #include "BKE_curve.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 "RE_pipeline.h"
89
90 #include "render_intern.h"      // own include
91
92 /********************** material slot operators *********************/
93
94 static int material_slot_add_exec(bContext *C, wmOperator *UNUSED(op))
95 {
96         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
97
98         if(!ob)
99                 return OPERATOR_CANCELLED;
100
101         object_add_material_slot(ob);
102         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
103         WM_event_add_notifier(C, NC_OBJECT|ND_OB_SHADING, ob);
104         
105         return OPERATOR_FINISHED;
106 }
107
108 void OBJECT_OT_material_slot_add(wmOperatorType *ot)
109 {
110         /* identifiers */
111         ot->name= "Add Material Slot";
112         ot->idname= "OBJECT_OT_material_slot_add";
113         ot->description="Add a new material slot";
114         
115         /* api callbacks */
116         ot->exec= material_slot_add_exec;
117         ot->poll= ED_operator_object_active_editable;
118
119         /* flags */
120         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
121 }
122
123 static int material_slot_remove_exec(bContext *C, wmOperator *op)
124 {
125         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
126
127         if(!ob)
128                 return OPERATOR_CANCELLED;
129
130         /* Removing material slots in edit mode screws things up, see bug #21822.*/
131         if(ob == CTX_data_edit_object(C)) {
132                 BKE_report(op->reports, RPT_ERROR, "Unable to remove material slot in edit mode");
133                 return OPERATOR_CANCELLED;
134         }
135
136         object_remove_material_slot(ob);
137         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
138         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
139         WM_event_add_notifier(C, NC_OBJECT|ND_OB_SHADING, ob);
140         
141         return OPERATOR_FINISHED;
142 }
143
144 void OBJECT_OT_material_slot_remove(wmOperatorType *ot)
145 {
146         /* identifiers */
147         ot->name= "Remove Material Slot";
148         ot->idname= "OBJECT_OT_material_slot_remove";
149         ot->description="Remove the selected material slot";
150         
151         /* api callbacks */
152         ot->exec= material_slot_remove_exec;
153         ot->poll= ED_operator_object_active_editable;
154
155         /* flags */
156         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
157 }
158
159 static int material_slot_assign_exec(bContext *C, wmOperator *UNUSED(op))
160 {
161         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
162
163         if(!ob)
164                 return OPERATOR_CANCELLED;
165
166         if(ob && ob->actcol>0) {
167                 if(ob->type == OB_MESH) {
168                         EditMesh *em= ((Mesh*)ob->data)->edit_mesh;
169                         EditFace *efa;
170
171                         if(em) {
172                                 for(efa= em->faces.first; efa; efa=efa->next)
173                                         if(efa->f & SELECT)
174                                                 efa->mat_nr= ob->actcol-1;
175                         }
176                 }
177                 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
178                         Nurb *nu;
179                         ListBase *nurbs= curve_editnurbs((Curve*)ob->data);
180
181                         if(nurbs) {
182                                 for(nu= nurbs->first; nu; nu= nu->next)
183                                         if(isNurbsel(nu))
184                                                 nu->mat_nr= nu->charidx= ob->actcol-1;
185                         }
186                 }
187                 else if(ob->type == OB_FONT) {
188                         EditFont *ef= ((Curve*)ob->data)->editfont;
189                         int i, selstart, selend;
190
191                         if(ef && BKE_font_getselection(ob, &selstart, &selend)) {
192                                 for(i=selstart; i<=selend; i++)
193                                         ef->textbufinfo[i].mat_nr = ob->actcol;
194                         }
195                 }
196         }
197
198         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
199         WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
200         
201         return OPERATOR_FINISHED;
202 }
203
204 void OBJECT_OT_material_slot_assign(wmOperatorType *ot)
205 {
206         /* identifiers */
207         ot->name= "Assign Material Slot";
208         ot->idname= "OBJECT_OT_material_slot_assign";
209         ot->description="Assign the material in the selected material slot to the selected vertices";
210         
211         /* api callbacks */
212         ot->exec= material_slot_assign_exec;
213         ot->poll= ED_operator_object_active_editable;
214
215         /* flags */
216         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
217 }
218
219 static int material_slot_de_select(bContext *C, int select)
220 {
221         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
222
223         if(!ob)
224                 return OPERATOR_CANCELLED;
225
226         if(ob->type == OB_MESH) {
227                 EditMesh *em= ((Mesh*)ob->data)->edit_mesh;
228
229                 if(em) {
230                         if(select)
231                                 EM_select_by_material(em, ob->actcol-1);
232                         else
233                                 EM_deselect_by_material(em, ob->actcol-1);
234                 }
235         }
236         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
237                 ListBase *nurbs= curve_editnurbs((Curve*)ob->data);
238                 Nurb *nu;
239                 BPoint *bp;
240                 BezTriple *bezt;
241                 int a;
242
243                 if(nurbs) {
244                         for(nu= nurbs->first; nu; nu=nu->next) {
245                                 if(nu->mat_nr==ob->actcol-1) {
246                                         if(nu->bezt) {
247                                                 a= nu->pntsu;
248                                                 bezt= nu->bezt;
249                                                 while(a--) {
250                                                         if(bezt->hide==0) {
251                                                                 if(select) {
252                                                                         bezt->f1 |= SELECT;
253                                                                         bezt->f2 |= SELECT;
254                                                                         bezt->f3 |= SELECT;
255                                                                 }
256                                                                 else {
257                                                                         bezt->f1 &= ~SELECT;
258                                                                         bezt->f2 &= ~SELECT;
259                                                                         bezt->f3 &= ~SELECT;
260                                                                 }
261                                                         }
262                                                         bezt++;
263                                                 }
264                                         }
265                                         else if(nu->bp) {
266                                                 a= nu->pntsu*nu->pntsv;
267                                                 bp= nu->bp;
268                                                 while(a--) {
269                                                         if(bp->hide==0) {
270                                                                 if(select) bp->f1 |= SELECT;
271                                                                 else bp->f1 &= ~SELECT;
272                                                         }
273                                                         bp++;
274                                                 }
275                                         }
276                                 }
277                         }
278                 }
279         }
280
281         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, ob->data);
282
283         return OPERATOR_FINISHED;
284 }
285
286 static int material_slot_select_exec(bContext *C, wmOperator *UNUSED(op))
287 {
288         return material_slot_de_select(C, 1);
289 }
290
291 void OBJECT_OT_material_slot_select(wmOperatorType *ot)
292 {
293         /* identifiers */
294         ot->name= "Select Material Slot";
295         ot->idname= "OBJECT_OT_material_slot_select";
296         ot->description="Select vertices assigned to the selected material slot";
297         
298         /* api callbacks */
299         ot->exec= material_slot_select_exec;
300
301         /* flags */
302         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
303 }
304
305 static int material_slot_deselect_exec(bContext *C, wmOperator *UNUSED(op))
306 {
307         return material_slot_de_select(C, 0);
308 }
309
310 void OBJECT_OT_material_slot_deselect(wmOperatorType *ot)
311 {
312         /* identifiers */
313         ot->name= "Deselect Material Slot";
314         ot->idname= "OBJECT_OT_material_slot_deselect";
315         ot->description="Deselect vertices assigned to the selected material slot";
316         
317         /* api callbacks */
318         ot->exec= material_slot_deselect_exec;
319
320         /* flags */
321         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
322 }
323
324
325 static int material_slot_copy_exec(bContext *C, wmOperator *UNUSED(op))
326 {
327         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
328         Material ***matar;
329
330         if(!ob || !(matar= give_matarar(ob)))
331                 return OPERATOR_CANCELLED;
332
333         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
334                 if(ob != ob_iter && give_matarar(ob_iter)) {
335                         if (ob->data != ob_iter->data)
336                                 assign_matarar(ob_iter, matar, ob->totcol);
337                         
338                         if(ob_iter->totcol==ob->totcol) {
339                                 ob_iter->actcol= ob->actcol;
340                                 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob_iter);
341                         }
342                 }
343         }
344         CTX_DATA_END;
345
346         return OPERATOR_FINISHED;
347 }
348
349
350 void OBJECT_OT_material_slot_copy(wmOperatorType *ot)
351 {
352         /* identifiers */
353         ot->name= "Copy Material to Others";
354         ot->idname= "OBJECT_OT_material_slot_copy";
355         ot->description="Copies materials to other selected objects";
356
357         /* api callbacks */
358         ot->exec= material_slot_copy_exec;
359
360         /* flags */
361         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
362 }
363
364 /********************** new material operator *********************/
365
366 static int new_material_exec(bContext *C, wmOperator *UNUSED(op))
367 {
368         Scene *scene= CTX_data_scene(C);
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
380                 if(scene_use_new_shading_nodes(scene)) {
381                         ED_node_shader_default(scene, &ma->id);
382                         ma->use_nodes= 1;
383                 }
384         }
385
386         /* hook into UI */
387         uiIDContextProperty(C, &ptr, &prop);
388
389         if(prop) {
390                 /* when creating new ID blocks, use is already 1, but RNA
391                  * pointer se also increases user, so this compensates it */
392                 ma->id.us--;
393
394                 RNA_id_pointer_create(&ma->id, &idptr);
395                 RNA_property_pointer_set(&ptr, prop, idptr);
396                 RNA_property_update(C, &ptr, prop);
397         }
398
399         WM_event_add_notifier(C, NC_MATERIAL|NA_ADDED, ma);
400         
401         return OPERATOR_FINISHED;
402 }
403
404 void MATERIAL_OT_new(wmOperatorType *ot)
405 {
406         /* identifiers */
407         ot->name= "New Material";
408         ot->idname= "MATERIAL_OT_new";
409         ot->description="Add a new material";
410         
411         /* api callbacks */
412         ot->exec= new_material_exec;
413
414         /* flags */
415         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
416 }
417
418 /********************** new texture operator *********************/
419
420 static int new_texture_exec(bContext *C, wmOperator *UNUSED(op))
421 {
422         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
423         PointerRNA ptr, idptr;
424         PropertyRNA *prop;
425
426         /* add or copy texture */
427         if(tex)
428                 tex= copy_texture(tex);
429         else
430                 tex= add_texture("Texture");
431
432         /* hook into UI */
433         uiIDContextProperty(C, &ptr, &prop);
434
435         if(prop) {
436                 /* when creating new ID blocks, use is already 1, but RNA
437                  * pointer se also increases user, so this compensates it */
438                 tex->id.us--;
439
440                 RNA_id_pointer_create(&tex->id, &idptr);
441                 RNA_property_pointer_set(&ptr, prop, idptr);
442                 RNA_property_update(C, &ptr, prop);
443         }
444
445         WM_event_add_notifier(C, NC_TEXTURE|NA_ADDED, tex);
446         
447         return OPERATOR_FINISHED;
448 }
449
450 void TEXTURE_OT_new(wmOperatorType *ot)
451 {
452         /* identifiers */
453         ot->name= "New Texture";
454         ot->idname= "TEXTURE_OT_new";
455         ot->description="Add a new texture";
456         
457         /* api callbacks */
458         ot->exec= new_texture_exec;
459
460         /* flags */
461         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
462 }
463
464 /********************** new world operator *********************/
465
466 static int new_world_exec(bContext *C, wmOperator *UNUSED(op))
467 {
468         Scene *scene= CTX_data_scene(C);
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
480                 if(scene_use_new_shading_nodes(scene)) {
481                         ED_node_shader_default(scene, &wo->id);
482                         wo->use_nodes= 1;
483                 }
484         }
485
486         /* hook into UI */
487         uiIDContextProperty(C, &ptr, &prop);
488
489         if(prop) {
490                 /* when creating new ID blocks, use is already 1, but RNA
491                  * pointer se also increases user, so this compensates it */
492                 wo->id.us--;
493
494                 RNA_id_pointer_create(&wo->id, &idptr);
495                 RNA_property_pointer_set(&ptr, prop, idptr);
496                 RNA_property_update(C, &ptr, prop);
497         }
498
499         WM_event_add_notifier(C, NC_WORLD|NA_ADDED, wo);
500         
501         return OPERATOR_FINISHED;
502 }
503
504 void WORLD_OT_new(wmOperatorType *ot)
505 {
506         /* identifiers */
507         ot->name= "New World";
508         ot->idname= "WORLD_OT_new";
509         ot->description= "Add a new world";
510         
511         /* api callbacks */
512         ot->exec= new_world_exec;
513
514         /* flags */
515         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
516 }
517
518 /********************** render layer operators *********************/
519
520 static int render_layer_add_exec(bContext *C, wmOperator *UNUSED(op))
521 {
522         Scene *scene= CTX_data_scene(C);
523
524         scene_add_render_layer(scene);
525         scene->r.actlay= BLI_countlist(&scene->r.layers) - 1;
526
527         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
528         
529         return OPERATOR_FINISHED;
530 }
531
532 void SCENE_OT_render_layer_add(wmOperatorType *ot)
533 {
534         /* identifiers */
535         ot->name= "Add Render Layer";
536         ot->idname= "SCENE_OT_render_layer_add";
537         ot->description="Add a render layer";
538         
539         /* api callbacks */
540         ot->exec= render_layer_add_exec;
541
542         /* flags */
543         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
544 }
545
546 static int render_layer_remove_exec(bContext *C, wmOperator *UNUSED(op))
547 {
548         Scene *scene = CTX_data_scene(C), *sce;
549         SceneRenderLayer *rl;
550         int act= scene->r.actlay;
551
552         if(BLI_countlist(&scene->r.layers) <= 1)
553                 return OPERATOR_CANCELLED;
554         
555         rl= BLI_findlink(&scene->r.layers, scene->r.actlay);
556         BLI_remlink(&scene->r.layers, rl);
557         MEM_freeN(rl);
558
559         scene->r.actlay= 0;
560
561         for(sce = CTX_data_main(C)->scene.first; sce; sce = sce->id.next) {
562                 if(sce->nodetree) {
563                         bNode *node;
564                         for(node = sce->nodetree->nodes.first; node; node = node->next) {
565                                 if(node->type==CMP_NODE_R_LAYERS && (Scene*)node->id==scene) {
566                                         if(node->custom1==act)
567                                                 node->custom1= 0;
568                                         else if(node->custom1>act)
569                                                 node->custom1--;
570                                 }
571                         }
572                 }
573         }
574
575         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
576         
577         return OPERATOR_FINISHED;
578 }
579
580 void SCENE_OT_render_layer_remove(wmOperatorType *ot)
581 {
582         /* identifiers */
583         ot->name= "Remove Render Layer";
584         ot->idname= "SCENE_OT_render_layer_remove";
585         ot->description="Remove the selected render layer";
586         
587         /* api callbacks */
588         ot->exec= render_layer_remove_exec;
589
590         /* flags */
591         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
592 }
593
594 static int texture_slot_move(bContext *C, wmOperator *op)
595 {
596         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
597
598         if(id) {
599                 MTex **mtex_ar, *mtexswap;
600                 short act;
601                 int type= RNA_enum_get(op->ptr, "type");
602                 struct AnimData *adt= BKE_animdata_from_id(id);
603
604                 give_active_mtex(id, &mtex_ar, &act);
605
606                 if(type == -1) { /* Up */
607                         if(act > 0) {
608                                 mtexswap = mtex_ar[act];
609                                 mtex_ar[act] = mtex_ar[act-1];
610                                 mtex_ar[act-1] = mtexswap;
611                                 
612                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, act-1, -1, 0);
613                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, act, act-1, 0);
614                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, -1, act, 0);
615
616                                 if(GS(id->name)==ID_MA) {
617                                         Material *ma= (Material *)id;
618                                         int mtexuse = ma->septex & (1<<act);
619                                         ma->septex &= ~(1<<act);
620                                         ma->septex |= (ma->septex & (1<<(act-1))) << 1;
621                                         ma->septex &= ~(1<<(act-1));
622                                         ma->septex |= mtexuse >> 1;
623                                 }
624                                 
625                                 set_active_mtex(id, act-1);
626                         }
627                 }
628                 else { /* Down */
629                         if(act < MAX_MTEX-1) {
630                                 mtexswap = mtex_ar[act];
631                                 mtex_ar[act] = mtex_ar[act+1];
632                                 mtex_ar[act+1] = mtexswap;
633                                 
634                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, act+1, -1, 0);
635                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, act, act+1, 0);
636                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, -1, act, 0);
637
638                                 if(GS(id->name)==ID_MA) {
639                                         Material *ma= (Material *)id;
640                                         int mtexuse = ma->septex & (1<<act);
641                                         ma->septex &= ~(1<<act);
642                                         ma->septex |= (ma->septex & (1<<(act+1))) >> 1;
643                                         ma->septex &= ~(1<<(act+1));
644                                         ma->septex |= mtexuse << 1;
645                                 }
646                                 
647                                 set_active_mtex(id, act+1);
648                         }
649                 }
650
651                 DAG_id_tag_update(id, 0);
652                 WM_event_add_notifier(C, NC_TEXTURE, CTX_data_scene(C));
653         }
654
655         return OPERATOR_FINISHED;
656 }
657
658 void TEXTURE_OT_slot_move(wmOperatorType *ot)
659 {
660         static EnumPropertyItem slot_move[] = {
661                 {-1, "UP", 0, "Up", ""},
662                 {1, "DOWN", 0, "Down", ""},
663                 {0, NULL, 0, NULL, NULL}
664         };
665
666         /* identifiers */
667         ot->name= "Move Texture Slot";
668         ot->idname= "TEXTURE_OT_slot_move";
669         ot->description="Move texture slots up and down";
670
671         /* api callbacks */
672         ot->exec= texture_slot_move;
673
674         /* flags */
675         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
676
677         RNA_def_enum(ot->srna, "type", slot_move, 0, "Type", "");
678 }
679
680
681
682 /********************** environment map operators *********************/
683
684 static int save_envmap(wmOperator *op, Scene *scene, EnvMap *env, char *path, int imtype)
685 {
686         float layout[12];
687         if ( RNA_struct_find_property(op->ptr, "layout") )
688                 RNA_float_get_array(op->ptr, "layout",layout);
689         else
690                 memcpy(layout, default_envmap_layout, sizeof(layout));
691
692         if (RE_WriteEnvmapResult(op->reports, scene, env, path, imtype, layout)) {
693                 return OPERATOR_FINISHED;
694         }
695         else {
696                 return OPERATOR_CANCELLED;
697         }
698
699 }
700
701 static int envmap_save_exec(bContext *C, wmOperator *op)
702 {
703         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
704         Scene *scene = CTX_data_scene(C);
705         //int imtype = RNA_enum_get(op->ptr, "file_type");
706         int imtype = scene->r.imtype;
707         char path[FILE_MAX];
708         
709         RNA_string_get(op->ptr, "filepath", path);
710         
711         if(scene->r.scemode & R_EXTENSION)  {
712                 BKE_add_image_extension(path, imtype);
713         }
714         
715         WM_cursor_wait(1);
716         
717         save_envmap(op, scene, tex->env, path, imtype);
718         
719         WM_cursor_wait(0);
720         
721         WM_event_add_notifier(C, NC_TEXTURE, tex);
722         
723         return OPERATOR_FINISHED;
724 }
725
726 static int envmap_save_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
727 {
728         //Scene *scene= CTX_data_scene(C);
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