Cycles: svn merge -r36352:36495 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
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_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 "render_intern.h"      // own include
90
91 /***************************** Updates ***********************************
92  * ED_render_id_flush_update gets called from DAG_id_tag_update, to do *
93  * editor level updates when the ID changes. when these ID blocks are in *
94  * the dependency graph, we can get rid of the manual dependency checks  */
95
96 static int mtex_use_tex(MTex **mtex, int tot, Tex *tex)
97 {
98         int a;
99
100         if(!mtex)
101                 return 0;
102
103         for(a=0; a<tot; a++)
104                 if(mtex[a] && mtex[a]->tex == tex)
105                         return 1;
106         
107         return 0;
108 }
109
110 static int nodes_use_tex(bNodeTree *ntree, Tex *tex)
111 {
112         bNode *node;
113
114         for(node=ntree->nodes.first; node; node= node->next) {
115                 if(node->id) {
116                         if(node->id == (ID*)tex) {
117                                 return 1;
118                         }
119                         else if(node->type==NODE_GROUP) {
120                                 if(nodes_use_tex((bNodeTree *)node->id, tex))
121                                         return 1;
122                         }
123                 }
124         }
125
126         return 0;
127 }
128
129 static void material_changed(Main *UNUSED(bmain), Material *ma)
130 {
131         /* icons */
132         BKE_icon_changed(BKE_icon_getid(&ma->id));
133
134         /* glsl */
135         if(ma->gpumaterial.first)
136                 GPU_material_free(ma);
137 }
138
139 static void texture_changed(Main *bmain, Tex *tex)
140 {
141         Material *ma;
142         Lamp *la;
143         World *wo;
144
145         /* icons */
146         BKE_icon_changed(BKE_icon_getid(&tex->id));
147
148         /* find materials */
149         for(ma=bmain->mat.first; ma; ma=ma->id.next) {
150                 if(mtex_use_tex(ma->mtex, MAX_MTEX, tex));
151                 else if(ma->use_nodes && ma->nodetree && nodes_use_tex(ma->nodetree, tex));
152                 else continue;
153
154                 BKE_icon_changed(BKE_icon_getid(&ma->id));
155
156                 if(ma->gpumaterial.first)
157                         GPU_material_free(ma);
158         }
159
160         /* find lamps */
161         for(la=bmain->lamp.first; la; la=la->id.next) {
162                 if(mtex_use_tex(la->mtex, MAX_MTEX, tex));
163                 else if(la->nodetree && nodes_use_tex(la->nodetree, tex));
164                 else continue;
165
166                 BKE_icon_changed(BKE_icon_getid(&la->id));
167         }
168
169         /* find worlds */
170         for(wo=bmain->world.first; wo; wo=wo->id.next) {
171                 if(mtex_use_tex(wo->mtex, MAX_MTEX, tex));
172                 else if(wo->nodetree && nodes_use_tex(wo->nodetree, tex));
173                 else continue;
174
175                 BKE_icon_changed(BKE_icon_getid(&wo->id));
176         }
177 }
178
179 static void lamp_changed(Main *bmain, Lamp *la)
180 {
181         Object *ob;
182         Material *ma;
183
184         /* icons */
185         BKE_icon_changed(BKE_icon_getid(&la->id));
186
187         /* glsl */
188         for(ob=bmain->object.first; ob; ob=ob->id.next)
189                 if(ob->data == la && ob->gpulamp.first)
190                         GPU_lamp_free(ob);
191
192         for(ma=bmain->mat.first; ma; ma=ma->id.next)
193                 if(ma->gpumaterial.first)
194                         GPU_material_free(ma);
195 }
196
197 static void world_changed(Main *bmain, World *wo)
198 {
199         Material *ma;
200
201         /* icons */
202         BKE_icon_changed(BKE_icon_getid(&wo->id));
203
204         /* glsl */
205         for(ma=bmain->mat.first; ma; ma=ma->id.next)
206                 if(ma->gpumaterial.first)
207                         GPU_material_free(ma);
208 }
209
210 static void image_changed(Main *bmain, Image *ima)
211 {
212         Tex *tex;
213
214         /* icons */
215         BKE_icon_changed(BKE_icon_getid(&ima->id));
216
217         /* textures */
218         for(tex=bmain->tex.first; tex; tex=tex->id.next)
219                 if(tex->ima == ima)
220                         texture_changed(bmain, tex);
221 }
222
223 static void scene_changed(Main *bmain, Scene *UNUSED(scene))
224 {
225         Object *ob;
226         Material *ma;
227
228         /* glsl */
229         for(ob=bmain->object.first; ob; ob=ob->id.next)
230                 if(ob->gpulamp.first)
231                         GPU_lamp_free(ob);
232
233         for(ma=bmain->mat.first; ma; ma=ma->id.next)
234                 if(ma->gpumaterial.first)
235                         GPU_material_free(ma);
236 }
237
238 #include "DNA_screen_types.h"
239 #include "DNA_view3d_types.h"
240
241 #include "RE_engine.h"
242
243 static void update_render_engines(Main *bmain, int tagged_only)
244 {
245         Scene *scene = bmain->scene.first;
246         bScreen *sc;
247         ScrArea *sa;
248         ARegion *ar;
249
250         for(sc=bmain->screen.first; sc; sc=sc->id.next) {
251                 for(sa=sc->areabase.first; sa; sa=sa->next) {
252                         if(sa->spacetype == SPACE_VIEW3D) {
253                                 for(ar=sa->regionbase.first; ar; ar=ar->next) {
254                                         if(ar->regiontype == RGN_TYPE_WINDOW) {
255                                                 RegionView3D *rv3d = ar->regiondata;
256                                                 RenderEngine *engine = rv3d->render_engine;
257
258                                                 if(engine && (!tagged_only || engine->do_update)) {
259                                                         engine->do_update = 0;
260                                                         engine->type->update(engine, scene);
261                                                 }
262                                         }
263                                 }
264                         }
265                 }
266         }
267 }
268
269 void ED_render_engine_update_tagged(Main *bmain)
270 {
271         update_render_engines(bmain, 1);
272 }
273
274 void ED_render_engine_changed(Main *bmain)
275 {
276         bScreen *sc;
277         ScrArea *sa;
278         ARegion *ar;
279
280         for(sc=bmain->screen.first; sc; sc=sc->id.next) {
281                 for(sa=sc->areabase.first; sa; sa=sa->next) {
282                         if(sa->spacetype == SPACE_VIEW3D) {
283                                 for(ar=sa->regionbase.first; ar; ar=ar->next) {
284                                         if(ar->regiontype == RGN_TYPE_WINDOW) {
285                                                 RegionView3D *rv3d = ar->regiondata;
286
287                                                 if(rv3d->render_engine) {
288                                                         RE_engine_free(rv3d->render_engine);
289                                                         rv3d->render_engine= NULL;
290                                                 }
291                                         }
292                                 }
293                         }
294                 }
295         }
296 }
297
298 void ED_render_id_flush_update(Main *bmain, ID *id)
299 {
300         if(!id) {
301                 update_render_engines(bmain, 0);
302                 return;
303         }
304
305         switch(GS(id->name)) {
306                 case ID_MA:
307                         material_changed(bmain, (Material*)id);
308                         break;
309                 case ID_TE:
310                         texture_changed(bmain, (Tex*)id);
311                         break;
312                 case ID_WO:
313                         world_changed(bmain, (World*)id);
314                         break;
315                 case ID_LA:
316                         lamp_changed(bmain, (Lamp*)id);
317                         break;
318                 case ID_IM:
319                         image_changed(bmain, (Image*)id);
320                         break;
321                 case ID_SCE:
322                         scene_changed(bmain, (Scene*)id);
323                         break;
324                 default:
325                         break;
326         }
327 }
328
329 /********************** material slot operators *********************/
330
331 static int material_slot_add_exec(bContext *C, wmOperator *UNUSED(op))
332 {
333         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
334
335         if(!ob)
336                 return OPERATOR_CANCELLED;
337
338         object_add_material_slot(ob);
339         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
340         WM_event_add_notifier(C, NC_OBJECT|ND_OB_SHADING, ob);
341         
342         return OPERATOR_FINISHED;
343 }
344
345 void OBJECT_OT_material_slot_add(wmOperatorType *ot)
346 {
347         /* identifiers */
348         ot->name= "Add Material Slot";
349         ot->idname= "OBJECT_OT_material_slot_add";
350         ot->description="Add a new material slot or duplicate the selected one";
351         
352         /* api callbacks */
353         ot->exec= material_slot_add_exec;
354         ot->poll= ED_operator_object_active_editable;
355
356         /* flags */
357         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
358 }
359
360 static int material_slot_remove_exec(bContext *C, wmOperator *op)
361 {
362         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
363
364         if(!ob)
365                 return OPERATOR_CANCELLED;
366
367         /* Removing material slots in edit mode screws things up, see bug #21822.*/
368         if(ob == CTX_data_edit_object(C)) {
369                 BKE_report(op->reports, RPT_ERROR, "Unable to remove material slot in edit mode.");
370                 return OPERATOR_CANCELLED;
371         }
372
373         object_remove_material_slot(ob);
374         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
375         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
376         WM_event_add_notifier(C, NC_OBJECT|ND_OB_SHADING, ob);
377         
378         return OPERATOR_FINISHED;
379 }
380
381 void OBJECT_OT_material_slot_remove(wmOperatorType *ot)
382 {
383         /* identifiers */
384         ot->name= "Remove Material Slot";
385         ot->idname= "OBJECT_OT_material_slot_remove";
386         ot->description="Remove the selected material slot";
387         
388         /* api callbacks */
389         ot->exec= material_slot_remove_exec;
390         ot->poll= ED_operator_object_active_editable;
391
392         /* flags */
393         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
394 }
395
396 static int material_slot_assign_exec(bContext *C, wmOperator *UNUSED(op))
397 {
398         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
399
400         if(!ob)
401                 return OPERATOR_CANCELLED;
402
403         if(ob && ob->actcol>0) {
404                 if(ob->type == OB_MESH) {
405                         EditMesh *em= ((Mesh*)ob->data)->edit_mesh;
406                         EditFace *efa;
407
408                         if(em) {
409                                 for(efa= em->faces.first; efa; efa=efa->next)
410                                         if(efa->f & SELECT)
411                                                 efa->mat_nr= ob->actcol-1;
412                         }
413                 }
414                 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
415                         Nurb *nu;
416                         ListBase *nurbs= ED_curve_editnurbs((Curve*)ob->data);
417
418                         if(nurbs) {
419                                 for(nu= nurbs->first; nu; nu= nu->next)
420                                         if(isNurbsel(nu))
421                                                 nu->mat_nr= nu->charidx= ob->actcol-1;
422                         }
423                 }
424                 else if(ob->type == OB_FONT) {
425                         EditFont *ef= ((Curve*)ob->data)->editfont;
426                         int i, selstart, selend;
427
428                         if(ef && BKE_font_getselection(ob, &selstart, &selend)) {
429                                 for(i=selstart; i<=selend; i++)
430                                         ef->textbufinfo[i].mat_nr = ob->actcol;
431                         }
432                 }
433         }
434
435         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
436         WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
437         
438         return OPERATOR_FINISHED;
439 }
440
441 void OBJECT_OT_material_slot_assign(wmOperatorType *ot)
442 {
443         /* identifiers */
444         ot->name= "Assign Material Slot";
445         ot->idname= "OBJECT_OT_material_slot_assign";
446         ot->description="Assign the material in the selected material slot to the selected vertices";
447         
448         /* api callbacks */
449         ot->exec= material_slot_assign_exec;
450         ot->poll= ED_operator_object_active_editable;
451
452         /* flags */
453         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
454 }
455
456 static int material_slot_de_select(bContext *C, int select)
457 {
458         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
459
460         if(!ob)
461                 return OPERATOR_CANCELLED;
462
463         if(ob->type == OB_MESH) {
464                 EditMesh *em= ((Mesh*)ob->data)->edit_mesh;
465
466                 if(em) {
467                         if(select)
468                                 EM_select_by_material(em, ob->actcol-1);
469                         else
470                                 EM_deselect_by_material(em, ob->actcol-1);
471                 }
472         }
473         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
474                 ListBase *nurbs= ED_curve_editnurbs((Curve*)ob->data);
475                 Nurb *nu;
476                 BPoint *bp;
477                 BezTriple *bezt;
478                 int a;
479
480                 if(nurbs) {
481                         for(nu= nurbs->first; nu; nu=nu->next) {
482                                 if(nu->mat_nr==ob->actcol-1) {
483                                         if(nu->bezt) {
484                                                 a= nu->pntsu;
485                                                 bezt= nu->bezt;
486                                                 while(a--) {
487                                                         if(bezt->hide==0) {
488                                                                 if(select) {
489                                                                         bezt->f1 |= SELECT;
490                                                                         bezt->f2 |= SELECT;
491                                                                         bezt->f3 |= SELECT;
492                                                                 }
493                                                                 else {
494                                                                         bezt->f1 &= ~SELECT;
495                                                                         bezt->f2 &= ~SELECT;
496                                                                         bezt->f3 &= ~SELECT;
497                                                                 }
498                                                         }
499                                                         bezt++;
500                                                 }
501                                         }
502                                         else if(nu->bp) {
503                                                 a= nu->pntsu*nu->pntsv;
504                                                 bp= nu->bp;
505                                                 while(a--) {
506                                                         if(bp->hide==0) {
507                                                                 if(select) bp->f1 |= SELECT;
508                                                                 else bp->f1 &= ~SELECT;
509                                                         }
510                                                         bp++;
511                                                 }
512                                         }
513                                 }
514                         }
515                 }
516         }
517
518         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, ob->data);
519
520         return OPERATOR_FINISHED;
521 }
522
523 static int material_slot_select_exec(bContext *C, wmOperator *UNUSED(op))
524 {
525         return material_slot_de_select(C, 1);
526 }
527
528 void OBJECT_OT_material_slot_select(wmOperatorType *ot)
529 {
530         /* identifiers */
531         ot->name= "Select Material Slot";
532         ot->idname= "OBJECT_OT_material_slot_select";
533         ot->description="Select vertices assigned to the selected material slot";
534         
535         /* api callbacks */
536         ot->exec= material_slot_select_exec;
537
538         /* flags */
539         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
540 }
541
542 static int material_slot_deselect_exec(bContext *C, wmOperator *UNUSED(op))
543 {
544         return material_slot_de_select(C, 0);
545 }
546
547 void OBJECT_OT_material_slot_deselect(wmOperatorType *ot)
548 {
549         /* identifiers */
550         ot->name= "Deselect Material Slot";
551         ot->idname= "OBJECT_OT_material_slot_deselect";
552         ot->description="Deselect vertices assigned to the selected material slot";
553         
554         /* api callbacks */
555         ot->exec= material_slot_deselect_exec;
556
557         /* flags */
558         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
559 }
560
561
562 static int material_slot_copy_exec(bContext *C, wmOperator *UNUSED(op))
563 {
564         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
565         Material ***matar;
566
567         if(!ob || !(matar= give_matarar(ob)))
568                 return OPERATOR_CANCELLED;
569
570         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
571                 if(ob != ob_iter && give_matarar(ob_iter)) {
572                         if (ob->data != ob_iter->data)
573                                 assign_matarar(ob_iter, matar, ob->totcol);
574                         
575                         if(ob_iter->totcol==ob->totcol) {
576                                 ob_iter->actcol= ob->actcol;
577                                 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob_iter);
578                         }
579                 }
580         }
581         CTX_DATA_END;
582
583         return OPERATOR_FINISHED;
584 }
585
586
587 void OBJECT_OT_material_slot_copy(wmOperatorType *ot)
588 {
589         /* identifiers */
590         ot->name= "Copy Material to Others";
591         ot->idname= "OBJECT_OT_material_slot_copy";
592         ot->description="Copies materials to other selected objects";
593
594         /* api callbacks */
595         ot->exec= material_slot_copy_exec;
596
597         /* flags */
598         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
599 }
600
601 /********************** new material operator *********************/
602
603 static int new_material_exec(bContext *C, wmOperator *UNUSED(op))
604 {
605         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
606         PointerRNA ptr, idptr;
607         PropertyRNA *prop;
608
609         /* add or copy material */
610         if(ma) {
611                 ma= copy_material(ma);
612         }
613         else {
614                 ma= add_material("Material");
615                 ED_node_shader_default(&ma->id);
616                 ma->use_nodes= 1;
617         }
618
619         /* hook into UI */
620         uiIDContextProperty(C, &ptr, &prop);
621
622         if(prop) {
623                 /* when creating new ID blocks, use is already 1, but RNA
624                  * pointer se also increases user, so this compensates it */
625                 ma->id.us--;
626
627                 RNA_id_pointer_create(&ma->id, &idptr);
628                 RNA_property_pointer_set(&ptr, prop, idptr);
629                 RNA_property_update(C, &ptr, prop);
630         }
631
632         WM_event_add_notifier(C, NC_MATERIAL|NA_ADDED, ma);
633         
634         return OPERATOR_FINISHED;
635 }
636
637 void MATERIAL_OT_new(wmOperatorType *ot)
638 {
639         /* identifiers */
640         ot->name= "New Material";
641         ot->idname= "MATERIAL_OT_new";
642         ot->description="Add a new material";
643         
644         /* api callbacks */
645         ot->exec= new_material_exec;
646
647         /* flags */
648         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
649 }
650
651 /********************** new texture operator *********************/
652
653 static int new_texture_exec(bContext *C, wmOperator *UNUSED(op))
654 {
655         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
656         PointerRNA ptr, idptr;
657         PropertyRNA *prop;
658
659         /* add or copy texture */
660         if(tex)
661                 tex= copy_texture(tex);
662         else
663                 tex= add_texture("Texture");
664
665         /* hook into UI */
666         uiIDContextProperty(C, &ptr, &prop);
667
668         if(prop) {
669                 /* when creating new ID blocks, use is already 1, but RNA
670                  * pointer se also increases user, so this compensates it */
671                 tex->id.us--;
672
673                 RNA_id_pointer_create(&tex->id, &idptr);
674                 RNA_property_pointer_set(&ptr, prop, idptr);
675                 RNA_property_update(C, &ptr, prop);
676         }
677
678         WM_event_add_notifier(C, NC_TEXTURE|NA_ADDED, tex);
679         
680         return OPERATOR_FINISHED;
681 }
682
683 void TEXTURE_OT_new(wmOperatorType *ot)
684 {
685         /* identifiers */
686         ot->name= "New Texture";
687         ot->idname= "TEXTURE_OT_new";
688         ot->description="Add a new texture";
689         
690         /* api callbacks */
691         ot->exec= new_texture_exec;
692
693         /* flags */
694         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
695 }
696
697 /********************** new world operator *********************/
698
699 static int new_world_exec(bContext *C, wmOperator *UNUSED(op))
700 {
701         World *wo= CTX_data_pointer_get_type(C, "world", &RNA_World).data;
702         PointerRNA ptr, idptr;
703         PropertyRNA *prop;
704
705         /* add or copy world */
706         if(wo) {
707                 wo= copy_world(wo);
708         }
709         else {
710                 wo= add_world("World");
711                 ED_node_shader_default(&wo->id);
712                 wo->use_nodes= 1;
713         }
714
715         /* hook into UI */
716         uiIDContextProperty(C, &ptr, &prop);
717
718         if(prop) {
719                 /* when creating new ID blocks, use is already 1, but RNA
720                  * pointer se also increases user, so this compensates it */
721                 wo->id.us--;
722
723                 RNA_id_pointer_create(&wo->id, &idptr);
724                 RNA_property_pointer_set(&ptr, prop, idptr);
725                 RNA_property_update(C, &ptr, prop);
726         }
727
728         WM_event_add_notifier(C, NC_WORLD|NA_ADDED, wo);
729         
730         return OPERATOR_FINISHED;
731 }
732
733 void WORLD_OT_new(wmOperatorType *ot)
734 {
735         /* identifiers */
736         ot->name= "New World";
737         ot->idname= "WORLD_OT_new";
738         ot->description= "Add a new world";
739         
740         /* api callbacks */
741         ot->exec= new_world_exec;
742
743         /* flags */
744         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
745 }
746
747 /********************** render layer operators *********************/
748
749 static int render_layer_add_exec(bContext *C, wmOperator *UNUSED(op))
750 {
751         Scene *scene= CTX_data_scene(C);
752
753         scene_add_render_layer(scene);
754         scene->r.actlay= BLI_countlist(&scene->r.layers) - 1;
755
756         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
757         
758         return OPERATOR_FINISHED;
759 }
760
761 void SCENE_OT_render_layer_add(wmOperatorType *ot)
762 {
763         /* identifiers */
764         ot->name= "Add Render Layer";
765         ot->idname= "SCENE_OT_render_layer_add";
766         ot->description="Add a render layer";
767         
768         /* api callbacks */
769         ot->exec= render_layer_add_exec;
770
771         /* flags */
772         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
773 }
774
775 static int render_layer_remove_exec(bContext *C, wmOperator *UNUSED(op))
776 {
777         Scene *scene= CTX_data_scene(C);
778         SceneRenderLayer *rl;
779         int act= scene->r.actlay;
780
781         if(BLI_countlist(&scene->r.layers) <= 1)
782                 return OPERATOR_CANCELLED;
783         
784         rl= BLI_findlink(&scene->r.layers, scene->r.actlay);
785         BLI_remlink(&scene->r.layers, rl);
786         MEM_freeN(rl);
787
788         scene->r.actlay= 0;
789         
790         if(scene->nodetree) {
791                 bNode *node;
792                 for(node= scene->nodetree->nodes.first; node; node= node->next) {
793                         if(node->type==CMP_NODE_R_LAYERS && node->id==NULL) {
794                                 if(node->custom1==act)
795                                         node->custom1= 0;
796                                 else if(node->custom1>act)
797                                         node->custom1--;
798                         }
799                 }
800         }
801
802         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
803         
804         return OPERATOR_FINISHED;
805 }
806
807 void SCENE_OT_render_layer_remove(wmOperatorType *ot)
808 {
809         /* identifiers */
810         ot->name= "Remove Render Layer";
811         ot->idname= "SCENE_OT_render_layer_remove";
812         ot->description="Remove the selected render layer";
813         
814         /* api callbacks */
815         ot->exec= render_layer_remove_exec;
816
817         /* flags */
818         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
819 }
820
821 static int texture_slot_move(bContext *C, wmOperator *op)
822 {
823         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
824
825         if(id) {
826                 MTex **mtex_ar, *mtexswap;
827                 short act;
828                 int type= RNA_enum_get(op->ptr, "type");
829                 struct AnimData *adt= BKE_animdata_from_id(id);
830
831                 give_active_mtex(id, &mtex_ar, &act);
832
833                 if(type == -1) { /* Up */
834                         if(act > 0) {
835                                 mtexswap = mtex_ar[act];
836                                 mtex_ar[act] = mtex_ar[act-1];
837                                 mtex_ar[act-1] = mtexswap;
838                                 
839                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, act-1, -1, 0);
840                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, act, act-1, 0);
841                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, -1, act, 0);
842
843                                 if(GS(id->name)==ID_MA) {
844                                         Material *ma= (Material *)id;
845                                         int mtexuse = ma->septex & (1<<act);
846                                         ma->septex &= ~(1<<act);
847                                         ma->septex |= (ma->septex & (1<<(act-1))) << 1;
848                                         ma->septex &= ~(1<<(act-1));
849                                         ma->septex |= mtexuse >> 1;
850                                 }
851                                 
852                                 set_active_mtex(id, act-1);
853                         }
854                 }
855                 else { /* Down */
856                         if(act < MAX_MTEX-1) {
857                                 mtexswap = mtex_ar[act];
858                                 mtex_ar[act] = mtex_ar[act+1];
859                                 mtex_ar[act+1] = mtexswap;
860                                 
861                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, act+1, -1, 0);
862                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, act, act+1, 0);
863                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, -1, act, 0);
864
865                                 if(GS(id->name)==ID_MA) {
866                                         Material *ma= (Material *)id;
867                                         int mtexuse = ma->septex & (1<<act);
868                                         ma->septex &= ~(1<<act);
869                                         ma->septex |= (ma->septex & (1<<(act+1))) >> 1;
870                                         ma->septex &= ~(1<<(act+1));
871                                         ma->septex |= mtexuse << 1;
872                                 }
873                                 
874                                 set_active_mtex(id, act+1);
875                         }
876                 }
877
878                 DAG_id_tag_update(id, 0);
879                 WM_event_add_notifier(C, NC_TEXTURE, CTX_data_scene(C));
880         }
881
882         return OPERATOR_FINISHED;
883 }
884
885 void TEXTURE_OT_slot_move(wmOperatorType *ot)
886 {
887         static EnumPropertyItem slot_move[] = {
888                 {-1, "UP", 0, "Up", ""},
889                 {1, "DOWN", 0, "Down", ""},
890                 {0, NULL, 0, NULL, NULL}
891         };
892
893         /* identifiers */
894         ot->name= "Move Texture Slot";
895         ot->idname= "TEXTURE_OT_slot_move";
896         ot->description="Move texture slots up and down";
897
898         /* api callbacks */
899         ot->exec= texture_slot_move;
900
901         /* flags */
902         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
903
904         RNA_def_enum(ot->srna, "type", slot_move, 0, "Type", "");
905 }
906
907
908
909 /********************** environment map operators *********************/
910
911 static int save_envmap(wmOperator *op, Scene *scene, EnvMap *env, char *str, int imtype)
912 {
913         ImBuf *ibuf=NULL;
914         int dx;
915         int retval;
916         int relative= (RNA_struct_find_property(op->ptr, "relative_path") && RNA_boolean_get(op->ptr, "relative_path"));
917         
918         if(env->cube[1]==NULL) {
919                 BKE_report(op->reports, RPT_ERROR, "There is no generated environment map available to save");
920                 return OPERATOR_CANCELLED;
921         }
922         
923         dx= env->cube[1]->x;
924         
925         if (env->type == ENV_CUBE) {
926                 ibuf = IMB_allocImBuf(3*dx, 2*dx, 24, IB_rectfloat);
927
928                 IMB_rectcpy(ibuf, env->cube[0], 0, 0, 0, 0, dx, dx);
929                 IMB_rectcpy(ibuf, env->cube[1], dx, 0, 0, 0, dx, dx);
930                 IMB_rectcpy(ibuf, env->cube[2], 2*dx, 0, 0, 0, dx, dx);
931                 IMB_rectcpy(ibuf, env->cube[3], 0, dx, 0, 0, dx, dx);
932                 IMB_rectcpy(ibuf, env->cube[4], dx, dx, 0, 0, dx, dx);
933                 IMB_rectcpy(ibuf, env->cube[5], 2*dx, dx, 0, 0, dx, dx);
934         }
935         else if (env->type == ENV_PLANE) {
936                 ibuf = IMB_allocImBuf(dx, dx, 24, IB_rectfloat);
937                 IMB_rectcpy(ibuf, env->cube[1], 0, 0, 0, 0, dx, dx);            
938         }
939         else {
940                 BKE_report(op->reports, RPT_ERROR, "Invalid environment map type");
941                 return OPERATOR_CANCELLED;
942         }
943         
944         if (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
945                 ibuf->profile = IB_PROFILE_LINEAR_RGB;
946         
947         /* to save, we first get absolute path */
948         BLI_path_abs(str, G.main->name);
949         
950         if (BKE_write_ibuf(ibuf, str, imtype, scene->r.subimtype, scene->r.quality)) {
951                 retval = OPERATOR_FINISHED;
952         }
953         else {
954                 BKE_reportf(op->reports, RPT_ERROR, "Error saving environment map to %s.", str);
955                 retval = OPERATOR_CANCELLED;
956         }
957         /* in case we were saving with relative paths, change back again */
958         if(relative)
959                 BLI_path_rel(str, G.main->name);
960         
961         IMB_freeImBuf(ibuf);
962         ibuf = NULL;
963         
964         return retval;
965 }
966
967 static int envmap_save_exec(bContext *C, wmOperator *op)
968 {
969         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
970         Scene *scene = CTX_data_scene(C);
971         //int imtype = RNA_enum_get(op->ptr, "file_type");
972         int imtype = scene->r.imtype;
973         char path[FILE_MAX];
974         
975         RNA_string_get(op->ptr, "filepath", path);
976         
977         if(scene->r.scemode & R_EXTENSION)  {
978                 BKE_add_image_extension(path, imtype);
979         }
980         
981         WM_cursor_wait(1);
982         
983         save_envmap(op, scene, tex->env, path, imtype);
984         
985         WM_cursor_wait(0);
986         
987         WM_event_add_notifier(C, NC_TEXTURE, tex);
988         
989         return OPERATOR_FINISHED;
990 }
991
992 static int envmap_save_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
993 {
994         //Scene *scene= CTX_data_scene(C);
995         
996         if(!RNA_property_is_set(op->ptr, "relative_path"))
997                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
998         
999         if(RNA_property_is_set(op->ptr, "filepath"))
1000                 return envmap_save_exec(C, op);
1001
1002         //RNA_enum_set(op->ptr, "file_type", scene->r.imtype);
1003         
1004         RNA_string_set(op->ptr, "filepath", G.main->name);
1005         WM_event_add_fileselect(C, op);
1006         
1007         return OPERATOR_RUNNING_MODAL;
1008 }
1009
1010 static int envmap_save_poll(bContext *C)
1011 {
1012         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
1013
1014         if (!tex) 
1015                 return 0;
1016         if (!tex->env || !tex->env->ok)
1017                 return 0;
1018         if (tex->env->cube[1]==NULL)
1019                 return 0;
1020         
1021         return 1;
1022 }
1023
1024 void TEXTURE_OT_envmap_save(wmOperatorType *ot)
1025 {
1026         /* identifiers */
1027         ot->name= "Save Environment Map";
1028         ot->idname= "TEXTURE_OT_envmap_save";
1029         ot->description="Save the current generated Environment map to an image file";
1030         
1031         /* api callbacks */
1032         ot->exec= envmap_save_exec;
1033         ot->invoke= envmap_save_invoke;
1034         ot->poll= envmap_save_poll;
1035         
1036         /* flags */
1037         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1038         
1039         /* properties */
1040         //RNA_def_enum(ot->srna, "file_type", image_file_type_items, R_PNG, "File Type", "File type to save image as.");
1041         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_SAVE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
1042 }
1043
1044 static int envmap_clear_exec(bContext *C, wmOperator *UNUSED(op))
1045 {
1046         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
1047         
1048         BKE_free_envmapdata(tex->env);
1049         
1050         WM_event_add_notifier(C, NC_TEXTURE|NA_EDITED, tex);
1051         
1052         return OPERATOR_FINISHED;
1053 }
1054
1055 static int envmap_clear_poll(bContext *C)
1056 {
1057         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
1058         
1059         if (!tex) 
1060                 return 0;
1061         if (!tex->env || !tex->env->ok)
1062                 return 0;
1063         if (tex->env->cube[1]==NULL)
1064                 return 0;
1065         
1066         return 1;
1067 }
1068
1069 void TEXTURE_OT_envmap_clear(wmOperatorType *ot)
1070 {
1071         /* identifiers */
1072         ot->name= "Clear Environment Map";
1073         ot->idname= "TEXTURE_OT_envmap_clear";
1074         ot->description="Discard the environment map and free it from memory";
1075         
1076         /* api callbacks */
1077         ot->exec= envmap_clear_exec;
1078         ot->poll= envmap_clear_poll;
1079         
1080         /* flags */
1081         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1082 }
1083
1084 static int envmap_clear_all_exec(bContext *C, wmOperator *UNUSED(op))
1085 {
1086         Main *bmain = CTX_data_main(C);
1087         Tex *tex;
1088         
1089         for (tex=bmain->tex.first; tex; tex=tex->id.next)
1090                 if (tex->env)
1091                         BKE_free_envmapdata(tex->env);
1092         
1093         WM_event_add_notifier(C, NC_TEXTURE|NA_EDITED, tex);
1094         
1095         return OPERATOR_FINISHED;
1096 }
1097
1098 void TEXTURE_OT_envmap_clear_all(wmOperatorType *ot)
1099 {
1100         /* identifiers */
1101         ot->name= "Clear All Environment Maps";
1102         ot->idname= "TEXTURE_OT_envmap_clear_all";
1103         ot->description="Discard all environment maps in the .blend file and free them from memory";
1104         
1105         /* api callbacks */
1106         ot->exec= envmap_clear_all_exec;
1107         ot->poll= envmap_clear_poll;
1108         
1109         /* flags */
1110         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1111 }
1112
1113 /********************** material operators *********************/
1114
1115 /* material copy/paste */
1116 static int copy_material_exec(bContext *C, wmOperator *UNUSED(op))
1117 {
1118         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
1119
1120         if(ma==NULL)
1121                 return OPERATOR_CANCELLED;
1122
1123         copy_matcopybuf(ma);
1124
1125         WM_event_add_notifier(C, NC_MATERIAL, ma);
1126
1127         return OPERATOR_FINISHED;
1128 }
1129
1130 void MATERIAL_OT_copy(wmOperatorType *ot)
1131 {
1132         /* identifiers */
1133         ot->name= "Copy Material";
1134         ot->idname= "MATERIAL_OT_copy";
1135         ot->description="Copy the material settings and nodes";
1136
1137         /* api callbacks */
1138         ot->exec= copy_material_exec;
1139
1140         /* flags */
1141         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1142 }
1143
1144 static int paste_material_exec(bContext *C, wmOperator *UNUSED(op))
1145 {
1146         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
1147
1148         if(ma==NULL)
1149                 return OPERATOR_CANCELLED;
1150
1151         paste_matcopybuf(ma);
1152
1153         WM_event_add_notifier(C, NC_MATERIAL|ND_SHADING_DRAW, ma);
1154
1155         return OPERATOR_FINISHED;
1156 }
1157
1158 void MATERIAL_OT_paste(wmOperatorType *ot)
1159 {
1160         /* identifiers */
1161         ot->name= "Paste Material";
1162         ot->idname= "MATERIAL_OT_paste";
1163         ot->description="Paste the material settings and nodes";
1164
1165         /* api callbacks */
1166         ot->exec= paste_material_exec;
1167
1168         /* flags */
1169         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1170 }
1171
1172
1173 static short mtexcopied=0; /* must be reset on file load */
1174 static MTex mtexcopybuf;
1175
1176 void ED_render_clear_mtex_copybuf(void)
1177 {       /* use for file reload */
1178         mtexcopied= 0;
1179 }
1180
1181 static void copy_mtex_copybuf(ID *id)
1182 {
1183         MTex **mtex= NULL;
1184         
1185         switch(GS(id->name)) {
1186                 case ID_MA:
1187                         mtex= &(((Material *)id)->mtex[(int)((Material *)id)->texact]);
1188                         break;
1189                 case ID_LA:
1190                         mtex= &(((Lamp *)id)->mtex[(int)((Lamp *)id)->texact]);
1191                         // la->mtex[(int)la->texact] // TODO
1192                         break;
1193                 case ID_WO:
1194                         mtex= &(((World *)id)->mtex[(int)((World *)id)->texact]);
1195                         // mtex= wrld->mtex[(int)wrld->texact]; // TODO
1196                         break;
1197                 case ID_PA:
1198                         mtex= &(((ParticleSettings *)id)->mtex[(int)((ParticleSettings *)id)->texact]);
1199                         break;
1200         }
1201         
1202         if(mtex && *mtex) {
1203                 memcpy(&mtexcopybuf, *mtex, sizeof(MTex));
1204                 mtexcopied= 1;
1205         }
1206         else {
1207                 mtexcopied= 0;
1208         }
1209 }
1210
1211 static void paste_mtex_copybuf(ID *id)
1212 {
1213         MTex **mtex= NULL;
1214         
1215         if(mtexcopied == 0 || mtexcopybuf.tex==NULL)
1216                 return;
1217         
1218         switch(GS(id->name)) {
1219                 case ID_MA:
1220                         mtex= &(((Material *)id)->mtex[(int)((Material *)id)->texact]);
1221                         break;
1222                 case ID_LA:
1223                         mtex= &(((Lamp *)id)->mtex[(int)((Lamp *)id)->texact]);
1224                         // la->mtex[(int)la->texact] // TODO
1225                         break;
1226                 case ID_WO:
1227                         mtex= &(((World *)id)->mtex[(int)((World *)id)->texact]);
1228                         // mtex= wrld->mtex[(int)wrld->texact]; // TODO
1229                         break;
1230                 case ID_PA:
1231                         mtex= &(((ParticleSettings *)id)->mtex[(int)((ParticleSettings *)id)->texact]);
1232                         break;
1233                 default:
1234                         BLI_assert("invalid id type");
1235                         return;
1236         }
1237         
1238         if(mtex) {
1239                 if(*mtex==NULL) {
1240                         *mtex= MEM_mallocN(sizeof(MTex), "mtex copy");
1241                 }
1242                 else if((*mtex)->tex) {
1243                         (*mtex)->tex->id.us--;
1244                 }
1245                 
1246                 memcpy(*mtex, &mtexcopybuf, sizeof(MTex));
1247                 
1248                 id_us_plus((ID *)mtexcopybuf.tex);
1249         }
1250 }
1251
1252
1253 static int copy_mtex_exec(bContext *C, wmOperator *UNUSED(op))
1254 {
1255         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
1256
1257         if(id==NULL) {
1258                 /* copying empty slot */
1259                 ED_render_clear_mtex_copybuf();
1260                 return OPERATOR_CANCELLED;
1261         }
1262
1263         copy_mtex_copybuf(id);
1264
1265         WM_event_add_notifier(C, NC_TEXTURE, NULL);
1266
1267         return OPERATOR_FINISHED;
1268 }
1269
1270 static int copy_mtex_poll(bContext *C)
1271 {
1272         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
1273         
1274         return (id != NULL);
1275 }
1276
1277 void TEXTURE_OT_slot_copy(wmOperatorType *ot)
1278 {
1279         /* identifiers */
1280         ot->name= "Copy Texture Slot Settings";
1281         ot->idname= "TEXTURE_OT_slot_copy";
1282         ot->description="Copy the material texture settings and nodes";
1283
1284         /* api callbacks */
1285         ot->exec= copy_mtex_exec;
1286         ot->poll= copy_mtex_poll;
1287         
1288         /* flags */
1289         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1290 }
1291
1292 static int paste_mtex_exec(bContext *C, wmOperator *UNUSED(op))
1293 {
1294         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
1295
1296         if(id==NULL) {
1297                 Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
1298                 Lamp *la= CTX_data_pointer_get_type(C, "lamp", &RNA_Lamp).data;
1299                 World *wo= CTX_data_pointer_get_type(C, "world", &RNA_World).data;
1300                 ParticleSystem *psys= CTX_data_pointer_get_type(C, "particle_system", &RNA_ParticleSystem).data;
1301                 
1302                 if (ma)
1303                         id = &ma->id;
1304                 else if (la)
1305                         id = &la->id;
1306                 else if (wo)
1307                         id = &wo->id;
1308                 else if (psys)
1309                         id = &psys->part->id;
1310                 
1311                 if (id==NULL)
1312                         return OPERATOR_CANCELLED;
1313         }
1314
1315         paste_mtex_copybuf(id);
1316
1317         WM_event_add_notifier(C, NC_TEXTURE|ND_SHADING_DRAW, NULL);
1318
1319         return OPERATOR_FINISHED;
1320 }
1321
1322 void TEXTURE_OT_slot_paste(wmOperatorType *ot)
1323 {
1324         /* identifiers */
1325         ot->name= "Paste Texture Slot Settings";
1326         ot->idname= "TEXTURE_OT_slot_paste";
1327         ot->description="Copy the texture settings and nodes";
1328
1329         /* api callbacks */
1330         ot->exec= paste_mtex_exec;
1331
1332         /* flags */
1333         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1334 }