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