Merged changes in the trunk up to revision 26856.
[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 #include <stdlib.h>
28 #include <string.h>
29
30 #include "MEM_guardedalloc.h"
31
32 #include "DNA_curve_types.h"
33 #include "DNA_lamp_types.h"
34 #include "DNA_material_types.h"
35 #include "DNA_meshdata_types.h"
36 #include "DNA_node_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_texture_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_world_types.h"
41
42 #include "BKE_context.h"
43 #include "BKE_depsgraph.h"
44 #include "BKE_font.h"
45 #include "BKE_icons.h"
46 #include "BKE_library.h"
47 #include "BKE_main.h"
48 #include "BKE_material.h"
49 #include "BKE_node.h"
50 #include "BKE_scene.h"
51 #include "BKE_texture.h"
52 #include "BKE_utildefines.h"
53 #include "BKE_world.h"
54
55 #include "BLI_math.h"
56 #include "BLI_editVert.h"
57 #include "BLI_listbase.h"
58
59 #include "GPU_material.h"
60
61 #include "FRS_freestyle.h"
62
63 #include "RNA_access.h"
64 #include "RNA_enum_types.h"
65
66 #include "WM_api.h"
67 #include "WM_types.h"
68
69 #include "ED_curve.h"
70 #include "ED_mesh.h"
71 #include "ED_render.h"
72
73 #include "RNA_access.h"
74 #include "RNA_define.h"
75
76 #include "UI_interface.h"
77 #include "UI_resources.h"
78
79 #include "render_intern.h"      // own include
80
81 /***************************** Updates ***********************************
82  * ED_render_id_flush_update gets called from DAG_id_flush_update, to do *
83  * editor level updates when the ID changes. when these ID blocks are in *
84  * the dependency graph, we can get rid of the manual dependency checks  */
85
86 static int mtex_use_tex(MTex **mtex, int tot, Tex *tex)
87 {
88         int a;
89
90         if(!mtex)
91                 return 0;
92
93         for(a=0; a<tot; a++)
94                 if(mtex[a] && mtex[a]->tex == tex)
95                         return 1;
96         
97         return 0;
98 }
99
100 static int nodes_use_tex(bNodeTree *ntree, Tex *tex)
101 {
102         bNode *node;
103
104         for(node=ntree->nodes.first; node; node= node->next) {
105                 if(node->id) {
106                         if(node->id == (ID*)tex) {
107                                 return 1;
108                         }
109                         else if(node->type==NODE_GROUP) {
110                                 if(nodes_use_tex((bNodeTree *)node->id, tex))
111                                         return 1;
112                         }
113                 }
114         }
115
116         return 0;
117 }
118
119 static void material_changed(Main *bmain, Material *ma)
120 {
121         /* icons */
122         BKE_icon_changed(BKE_icon_getid(&ma->id));
123
124         /* glsl */
125         if(ma->gpumaterial.first)
126                 GPU_material_free(ma);
127 }
128
129 static void texture_changed(Main *bmain, Tex *tex)
130 {
131         Material *ma;
132         Lamp *la;
133         World *wo;
134
135         /* icons */
136         BKE_icon_changed(BKE_icon_getid(&tex->id));
137
138         /* find materials */
139         for(ma=bmain->mat.first; ma; ma=ma->id.next) {
140                 if(mtex_use_tex(ma->mtex, MAX_MTEX, tex));
141                 else if(ma->use_nodes && ma->nodetree && nodes_use_tex(ma->nodetree, tex));
142                 else continue;
143
144                 BKE_icon_changed(BKE_icon_getid(&ma->id));
145
146                 if(ma->gpumaterial.first)
147                         GPU_material_free(ma);
148         }
149
150         /* find lamps */
151         for(la=bmain->lamp.first; la; la=la->id.next) {
152                 if(mtex_use_tex(la->mtex, MAX_MTEX, tex));
153                 else continue;
154
155                 BKE_icon_changed(BKE_icon_getid(&la->id));
156         }
157
158         /* find worlds */
159         for(wo=bmain->world.first; wo; wo=wo->id.next) {
160                 if(mtex_use_tex(wo->mtex, MAX_MTEX, tex));
161                 else continue;
162
163                 BKE_icon_changed(BKE_icon_getid(&wo->id));
164         }
165 }
166
167 static void lamp_changed(Main *bmain, Lamp *la)
168 {
169         Object *ob;
170         Material *ma;
171
172         /* icons */
173         BKE_icon_changed(BKE_icon_getid(&la->id));
174
175         /* glsl */
176         for(ob=bmain->object.first; ob; ob=ob->id.next)
177                 if(ob->data == la && ob->gpulamp.first)
178                         GPU_lamp_free(ob);
179
180         for(ma=bmain->mat.first; ma; ma=ma->id.next)
181                 if(ma->gpumaterial.first)
182                         GPU_material_free(ma);
183 }
184
185 static void world_changed(Main *bmain, World *wo)
186 {
187         Material *ma;
188
189         /* icons */
190         BKE_icon_changed(BKE_icon_getid(&wo->id));
191
192         /* glsl */
193         for(ma=bmain->mat.first; ma; ma=ma->id.next)
194                 if(ma->gpumaterial.first)
195                         GPU_material_free(ma);
196 }
197
198 void ED_render_id_flush_update(Main *bmain, ID *id)
199 {
200         switch(GS(id->name)) {
201                 case ID_MA:
202                         material_changed(bmain, (Material*)id);
203                         break;
204                 case ID_TE:
205                         texture_changed(bmain, (Tex*)id);
206                         break;
207                 case ID_WO:
208                         world_changed(bmain, (World*)id);
209                         break;
210                 case ID_LA:
211                         lamp_changed(bmain, (Lamp*)id);
212                         break;
213                 default:
214                         break;
215         }
216 }
217
218 /********************** material slot operators *********************/
219
220 static int material_slot_add_exec(bContext *C, wmOperator *op)
221 {
222         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
223
224         if(!ob)
225                 return OPERATOR_CANCELLED;
226
227         object_add_material_slot(ob);
228         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
229         
230         return OPERATOR_FINISHED;
231 }
232
233 void OBJECT_OT_material_slot_add(wmOperatorType *ot)
234 {
235         /* identifiers */
236         ot->name= "Add Material Slot";
237         ot->idname= "OBJECT_OT_material_slot_add";
238         ot->description="Add a new material slot or duplicate the selected one";
239         
240         /* api callbacks */
241         ot->exec= material_slot_add_exec;
242
243         /* flags */
244         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
245 }
246
247 static int material_slot_remove_exec(bContext *C, wmOperator *op)
248 {
249         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
250
251         if(!ob)
252                 return OPERATOR_CANCELLED;
253
254         object_remove_material_slot(ob);
255         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
256         
257         return OPERATOR_FINISHED;
258 }
259
260 void OBJECT_OT_material_slot_remove(wmOperatorType *ot)
261 {
262         /* identifiers */
263         ot->name= "Remove Material Slot";
264         ot->idname= "OBJECT_OT_material_slot_remove";
265         ot->description="Remove the selected material slot";
266         
267         /* api callbacks */
268         ot->exec= material_slot_remove_exec;
269
270         /* flags */
271         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
272 }
273
274 static int material_slot_assign_exec(bContext *C, wmOperator *op)
275 {
276         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
277
278         if(!ob)
279                 return OPERATOR_CANCELLED;
280
281         if(ob && ob->actcol>0) {
282                 if(ob->type == OB_MESH) {
283                         EditMesh *em= ((Mesh*)ob->data)->edit_mesh;
284                         EditFace *efa;
285
286                         if(em) {
287                                 for(efa= em->faces.first; efa; efa=efa->next)
288                                         if(efa->f & SELECT)
289                                                 efa->mat_nr= ob->actcol-1;
290                         }
291                 }
292                 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
293                         ListBase *editnurb= ((Curve*)ob->data)->editnurb;
294                         Nurb *nu;
295
296                         if(editnurb) {
297                                 for(nu= editnurb->first; nu; nu= nu->next)
298                                         if(isNurbsel(nu))
299                                                 nu->mat_nr= nu->charidx= ob->actcol-1;
300                         }
301                 }
302                 else if(ob->type == OB_FONT) {
303                         EditFont *ef= ((Curve*)ob->data)->editfont;
304                 int i, selstart, selend;
305
306                         if(ef && BKE_font_getselection(ob, &selstart, &selend)) {
307                                 for(i=selstart; i<=selend; i++)
308                                         ef->textbufinfo[i].mat_nr = ob->actcol-1;
309                         }
310                 }
311         }
312
313     DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
314     WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
315         
316         return OPERATOR_FINISHED;
317 }
318
319 void OBJECT_OT_material_slot_assign(wmOperatorType *ot)
320 {
321         /* identifiers */
322         ot->name= "Assign Material Slot";
323         ot->idname= "OBJECT_OT_material_slot_assign";
324         ot->description="Assign the material in the selected material slot to the selected vertices";
325         
326         /* api callbacks */
327         ot->exec= material_slot_assign_exec;
328
329         /* flags */
330         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
331 }
332
333 static int material_slot_de_select(bContext *C, int select)
334 {
335         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
336
337         if(!ob)
338                 return OPERATOR_CANCELLED;
339
340         if(ob->type == OB_MESH) {
341                 EditMesh *em= ((Mesh*)ob->data)->edit_mesh;
342
343                 if(em) {
344                         if(select)
345                                 EM_select_by_material(em, ob->actcol-1);
346                         else
347                                 EM_deselect_by_material(em, ob->actcol-1);
348                 }
349         }
350         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
351                 ListBase *editnurb= ((Curve*)ob->data)->editnurb;
352                 Nurb *nu;
353                 BPoint *bp;
354                 BezTriple *bezt;
355                 int a;
356
357                 for(nu= editnurb->first; nu; nu=nu->next) {
358                         if(nu->mat_nr==ob->actcol-1) {
359                                 if(nu->bezt) {
360                                         a= nu->pntsu;
361                                         bezt= nu->bezt;
362                                         while(a--) {
363                                                 if(bezt->hide==0) {
364                                                         if(select) {
365                                                                 bezt->f1 |= SELECT;
366                                                                 bezt->f2 |= SELECT;
367                                                                 bezt->f3 |= SELECT;
368                                                         }
369                                                         else {
370                                                                 bezt->f1 &= ~SELECT;
371                                                                 bezt->f2 &= ~SELECT;
372                                                                 bezt->f3 &= ~SELECT;
373                                                         }
374                                                 }
375                                                 bezt++;
376                                         }
377                                 }
378                                 else if(nu->bp) {
379                                         a= nu->pntsu*nu->pntsv;
380                                         bp= nu->bp;
381                                         while(a--) {
382                                                 if(bp->hide==0) {
383                                                         if(select) bp->f1 |= SELECT;
384                                                         else bp->f1 &= ~SELECT;
385                                                 }
386                                                 bp++;
387                                         }
388                                 }
389                         }
390                 }
391         }
392
393     WM_event_add_notifier(C, NC_GEOM|ND_SELECT, ob->data);
394
395         return OPERATOR_FINISHED;
396 }
397
398 static int material_slot_select_exec(bContext *C, wmOperator *op)
399 {
400         return material_slot_de_select(C, 1);
401 }
402
403 void OBJECT_OT_material_slot_select(wmOperatorType *ot)
404 {
405         /* identifiers */
406         ot->name= "Select Material Slot";
407         ot->idname= "OBJECT_OT_material_slot_select";
408         ot->description="Select vertices assigned to the selected material slot";
409         
410         /* api callbacks */
411         ot->exec= material_slot_select_exec;
412
413         /* flags */
414         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
415 }
416
417 static int material_slot_deselect_exec(bContext *C, wmOperator *op)
418 {
419         return material_slot_de_select(C, 0);
420 }
421
422 void OBJECT_OT_material_slot_deselect(wmOperatorType *ot)
423 {
424         /* identifiers */
425         ot->name= "Deselect Material Slot";
426         ot->idname= "OBJECT_OT_material_slot_deselect";
427         ot->description="Deselect vertices assigned to the selected material slot";
428         
429         /* api callbacks */
430         ot->exec= material_slot_deselect_exec;
431
432         /* flags */
433         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
434 }
435
436
437 static int material_slot_copy_exec(bContext *C, wmOperator *op)
438 {
439         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
440         Material ***matar;
441
442         if(!ob || !(matar= give_matarar(ob)))
443                 return OPERATOR_CANCELLED;
444
445         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
446                 if(ob != ob_iter && give_matarar(ob_iter)) {
447                         if (ob->data != ob_iter->data)
448                                 assign_matarar(ob_iter, matar, ob->totcol);
449                         
450                         if(ob_iter->totcol==ob->totcol) {
451                                 ob_iter->actcol= ob->actcol;
452                                 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob_iter);
453                         }
454                 }
455         }
456         CTX_DATA_END;
457
458         return OPERATOR_FINISHED;
459 }
460
461
462 void OBJECT_OT_material_slot_copy(wmOperatorType *ot)
463 {
464         /* identifiers */
465         ot->name= "Copy Material to Others";
466         ot->idname= "OBJECT_OT_material_slot_copy";
467         ot->description="Copies materials to other selected objects";
468
469         /* api callbacks */
470         ot->exec= material_slot_copy_exec;
471
472         /* flags */
473         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
474 }
475
476 /********************** new material operator *********************/
477
478 static int new_material_exec(bContext *C, wmOperator *op)
479 {
480         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
481         PointerRNA ptr, idptr;
482         PropertyRNA *prop;
483
484         /* add or copy material */
485         if(ma)
486                 ma= copy_material(ma);
487         else
488                 ma= add_material("Material");
489
490         /* hook into UI */
491         uiIDContextProperty(C, &ptr, &prop);
492
493         if(prop) {
494                 /* when creating new ID blocks, use is already 1, but RNA
495                  * pointer se also increases user, so this compensates it */
496                 ma->id.us--;
497
498                 RNA_id_pointer_create(&ma->id, &idptr);
499                 RNA_property_pointer_set(&ptr, prop, idptr);
500                 RNA_property_update(C, &ptr, prop);
501         }
502
503         WM_event_add_notifier(C, NC_MATERIAL|NA_ADDED, ma);
504         
505         return OPERATOR_FINISHED;
506 }
507
508 void MATERIAL_OT_new(wmOperatorType *ot)
509 {
510         /* identifiers */
511         ot->name= "New Material";
512         ot->idname= "MATERIAL_OT_new";
513         ot->description="Add a new material";
514         
515         /* api callbacks */
516         ot->exec= new_material_exec;
517
518         /* flags */
519         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
520 }
521
522 /********************** new texture operator *********************/
523
524 static int new_texture_exec(bContext *C, wmOperator *op)
525 {
526         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
527         PointerRNA ptr, idptr;
528         PropertyRNA *prop;
529
530         /* add or copy texture */
531         if(tex)
532                 tex= copy_texture(tex);
533         else
534                 tex= add_texture("Texture");
535
536         /* hook into UI */
537         uiIDContextProperty(C, &ptr, &prop);
538
539         if(prop) {
540                 /* when creating new ID blocks, use is already 1, but RNA
541                  * pointer se also increases user, so this compensates it */
542                 tex->id.us--;
543
544                 RNA_id_pointer_create(&tex->id, &idptr);
545                 RNA_property_pointer_set(&ptr, prop, idptr);
546                 RNA_property_update(C, &ptr, prop);
547         }
548
549         WM_event_add_notifier(C, NC_TEXTURE|NA_ADDED, tex);
550         
551         return OPERATOR_FINISHED;
552 }
553
554 void TEXTURE_OT_new(wmOperatorType *ot)
555 {
556         /* identifiers */
557         ot->name= "New Texture";
558         ot->idname= "TEXTURE_OT_new";
559         ot->description="Add a new texture";
560         
561         /* api callbacks */
562         ot->exec= new_texture_exec;
563
564         /* flags */
565         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
566 }
567
568 /********************** new world operator *********************/
569
570 static int new_world_exec(bContext *C, wmOperator *op)
571 {
572         World *wo= CTX_data_pointer_get_type(C, "world", &RNA_World).data;
573         PointerRNA ptr, idptr;
574         PropertyRNA *prop;
575
576         /* add or copy world */
577         if(wo)
578                 wo= copy_world(wo);
579         else
580                 wo= add_world("World");
581
582         /* hook into UI */
583         uiIDContextProperty(C, &ptr, &prop);
584
585         if(prop) {
586                 /* when creating new ID blocks, use is already 1, but RNA
587                  * pointer se also increases user, so this compensates it */
588                 wo->id.us--;
589
590                 RNA_id_pointer_create(&wo->id, &idptr);
591                 RNA_property_pointer_set(&ptr, prop, idptr);
592                 RNA_property_update(C, &ptr, prop);
593         }
594
595         WM_event_add_notifier(C, NC_WORLD|NA_ADDED, wo);
596         
597         return OPERATOR_FINISHED;
598 }
599
600 void WORLD_OT_new(wmOperatorType *ot)
601 {
602         /* identifiers */
603         ot->name= "New World";
604         ot->idname= "WORLD_OT_new";
605         ot->description= "Add a new world";
606         
607         /* api callbacks */
608         ot->exec= new_world_exec;
609
610         /* flags */
611         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
612 }
613
614 /********************** render layer operators *********************/
615
616 static int render_layer_add_exec(bContext *C, wmOperator *op)
617 {
618         Scene *scene= CTX_data_scene(C);
619
620         scene_add_render_layer(scene);
621         scene->r.actlay= BLI_countlist(&scene->r.layers) - 1;
622
623         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
624         
625         return OPERATOR_FINISHED;
626 }
627
628 void SCENE_OT_render_layer_add(wmOperatorType *ot)
629 {
630         /* identifiers */
631         ot->name= "Add Render Layer";
632         ot->idname= "SCENE_OT_render_layer_add";
633         ot->description="Add a render layer";
634         
635         /* api callbacks */
636         ot->exec= render_layer_add_exec;
637
638         /* flags */
639         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
640 }
641
642 static int render_layer_remove_exec(bContext *C, wmOperator *op)
643 {
644         Scene *scene= CTX_data_scene(C);
645         SceneRenderLayer *rl;
646         int act= scene->r.actlay;
647
648         if(BLI_countlist(&scene->r.layers) <= 1)
649                 return OPERATOR_CANCELLED;
650         
651         rl= BLI_findlink(&scene->r.layers, scene->r.actlay);
652         BLI_remlink(&scene->r.layers, rl);
653         MEM_freeN(rl);
654
655         scene->r.actlay= 0;
656         
657         if(scene->nodetree) {
658                 bNode *node;
659                 for(node= scene->nodetree->nodes.first; node; node= node->next) {
660                         if(node->type==CMP_NODE_R_LAYERS && node->id==NULL) {
661                                 if(node->custom1==act)
662                                         node->custom1= 0;
663                                 else if(node->custom1>act)
664                                         node->custom1--;
665                         }
666                 }
667         }
668
669         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
670         
671         return OPERATOR_FINISHED;
672 }
673
674 void SCENE_OT_render_layer_remove(wmOperatorType *ot)
675 {
676         /* identifiers */
677         ot->name= "Remove Render Layer";
678         ot->idname= "SCENE_OT_render_layer_remove";
679         ot->description="Remove the selected render layer";
680         
681         /* api callbacks */
682         ot->exec= render_layer_remove_exec;
683
684         /* flags */
685         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
686 }
687
688 static int freestyle_module_add_exec(bContext *C, wmOperator *op)
689 {
690         Scene *scene= CTX_data_scene(C);
691         SceneRenderLayer *srl = (SceneRenderLayer*) BLI_findlink(&scene->r.layers, scene->r.actlay);
692
693         FRS_add_module(&srl->freestyleConfig);
694
695         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
696         
697         return OPERATOR_FINISHED;
698 }
699
700 void SCENE_OT_freestyle_module_add(wmOperatorType *ot)
701 {
702         /* identifiers */
703         ot->name= "Add Freestyle Module";
704         ot->idname= "SCENE_OT_freestyle_module_add";
705         ot->description="Add a style module into the list of modules.";
706         
707         /* api callbacks */
708         ot->exec= freestyle_module_add_exec;
709
710         /* flags */
711         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
712 }
713
714 static int freestyle_module_remove_exec(bContext *C, wmOperator *op)
715 {
716         Scene *scene= CTX_data_scene(C);
717         SceneRenderLayer *srl = (SceneRenderLayer*) BLI_findlink(&scene->r.layers, scene->r.actlay);
718         PointerRNA ptr= CTX_data_pointer_get_type(C, "freestyle_module", &RNA_FreestyleModuleSettings);
719         FreestyleModuleConfig *module= ptr.data;
720
721         FRS_delete_module(&srl->freestyleConfig, module);
722
723         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
724         
725         return OPERATOR_FINISHED;
726 }
727
728 void SCENE_OT_freestyle_module_remove(wmOperatorType *ot)
729 {
730         /* identifiers */
731         ot->name= "Remove Freestyle Module";
732         ot->idname= "SCENE_OT_freestyle_module_remove";
733         ot->description="Remove the style module from the stack.";
734         
735         /* api callbacks */
736         ot->exec= freestyle_module_remove_exec;
737
738         /* flags */
739         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
740 }
741
742 static int freestyle_module_move_up_exec(bContext *C, wmOperator *op)
743 {
744         Scene *scene= CTX_data_scene(C);
745         SceneRenderLayer *srl = (SceneRenderLayer*) BLI_findlink(&scene->r.layers, scene->r.actlay);
746         PointerRNA ptr= CTX_data_pointer_get_type(C, "freestyle_module", &RNA_FreestyleModuleSettings);
747         FreestyleModuleConfig *module= ptr.data;
748         int active = RNA_boolean_get(op->ptr, "active");
749
750         if(active)
751                 FRS_move_up_module(&srl->freestyleConfig, module);
752
753         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
754         
755         return OPERATOR_FINISHED;
756 }
757
758 void SCENE_OT_freestyle_module_move_up(wmOperatorType *ot)
759 {
760         /* identifiers */
761         ot->name= "Move Up Freestyle Module";
762         ot->idname= "SCENE_OT_freestyle_module_move_up";
763         ot->description="Move the style module up in the stack.";
764         
765         /* api callbacks */
766         ot->exec= freestyle_module_move_up_exec;
767
768         /* flags */
769         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
770
771         /* props */
772         RNA_def_boolean(ot->srna, "active", 0, "Active", "True if the operator is enabled.");
773 }
774
775 static int freestyle_module_move_down_exec(bContext *C, wmOperator *op)
776 {
777         Scene *scene= CTX_data_scene(C);
778         SceneRenderLayer *srl = (SceneRenderLayer*) BLI_findlink(&scene->r.layers, scene->r.actlay);
779         PointerRNA ptr= CTX_data_pointer_get_type(C, "freestyle_module", &RNA_FreestyleModuleSettings);
780         FreestyleModuleConfig *module= ptr.data;
781         int active = RNA_boolean_get(op->ptr, "active");
782
783         if(active)
784                 FRS_move_down_module(&srl->freestyleConfig, module);
785
786         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
787         
788         return OPERATOR_FINISHED;
789 }
790
791 void SCENE_OT_freestyle_module_move_down(wmOperatorType *ot)
792 {
793         /* identifiers */
794         ot->name= "Move Down Freestyle Module";
795         ot->idname= "SCENE_OT_freestyle_module_move_down";
796         ot->description="Move the style module down in the stack.";
797         
798         /* api callbacks */
799         ot->exec= freestyle_module_move_down_exec;
800
801         /* flags */
802         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
803
804         /* props */
805         RNA_def_boolean(ot->srna, "active", 0, "Active", "True if the operator is enabled.");
806 }
807
808 static int texture_slot_move(bContext *C, wmOperator *op)
809 {
810         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
811
812         if(id) {
813                 MTex **mtex_ar, *mtexswap;
814                 short act;
815                 int type= RNA_enum_get(op->ptr, "type");
816
817                 give_active_mtex(id, &mtex_ar, &act);
818
819                 if(type == -1) { /* Up */
820                         if(act > 0) {
821                                 mtexswap = mtex_ar[act];
822                                 mtex_ar[act] = mtex_ar[act-1];
823                                 mtex_ar[act-1] = mtexswap;
824
825                                 if(GS(id->name)==ID_MA) {
826                                         Material *ma= (Material *)id;
827                                         int mtexuse = ma->septex & (1<<act);
828                                         ma->septex &= ~(1<<act);
829                                         ma->septex |= (ma->septex & (1<<(act-1))) << 1;
830                                         ma->septex &= ~(1<<(act-1));
831                                         ma->septex |= mtexuse >> 1;
832                                 }
833
834                                 set_active_mtex(id, act-1);
835                         }
836                 }
837                 else { /* Down */
838                         if(act < MAX_MTEX-1) {
839                                 mtexswap = mtex_ar[act];
840                                 mtex_ar[act] = mtex_ar[act+1];
841                                 mtex_ar[act+1] = mtexswap;
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
856                 WM_event_add_notifier(C, NC_TEXTURE, CTX_data_scene(C));
857         }
858
859         return OPERATOR_FINISHED;
860 }
861
862 void TEXTURE_OT_slot_move(wmOperatorType *ot)
863 {
864         static EnumPropertyItem slot_move[] = {
865                 {-1, "UP", 0, "Up", ""},
866                 {1, "DOWN", 0, "Down", ""},
867                 {0, NULL, 0, NULL, NULL}
868         };
869
870         /* identifiers */
871         ot->name= "Move Texture Slot";
872         ot->idname= "TEXTURE_OT_slot_move";
873         ot->description="Move texture slots up and down";
874
875         /* api callbacks */
876         ot->exec= texture_slot_move;
877
878         /* flags */
879         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
880
881         RNA_def_enum(ot->srna, "type", slot_move, 0, "Type", "");
882 }
883
884
885
886 /* material copy/paste */
887 static int copy_material_exec(bContext *C, wmOperator *op)
888 {
889         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
890
891         if(ma==NULL)
892                 return OPERATOR_CANCELLED;
893
894         copy_matcopybuf(ma);
895
896         WM_event_add_notifier(C, NC_MATERIAL, ma);
897
898         return OPERATOR_FINISHED;
899 }
900
901 void MATERIAL_OT_copy(wmOperatorType *ot)
902 {
903         /* identifiers */
904         ot->name= "Copy Material";
905         ot->idname= "MATERIAL_OT_copy";
906         ot->description="Copy the material settings and nodes";
907
908         /* api callbacks */
909         ot->exec= copy_material_exec;
910
911         /* flags */
912         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
913 }
914
915 static int paste_material_exec(bContext *C, wmOperator *op)
916 {
917         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
918
919         if(ma==NULL)
920                 return OPERATOR_CANCELLED;
921
922         paste_matcopybuf(ma);
923
924         WM_event_add_notifier(C, NC_MATERIAL|ND_SHADING_DRAW, ma);
925
926         return OPERATOR_FINISHED;
927 }
928
929 void MATERIAL_OT_paste(wmOperatorType *ot)
930 {
931         /* identifiers */
932         ot->name= "Paste Material";
933         ot->idname= "MATERIAL_OT_paste";
934         ot->description="Copy the material settings and nodes";
935
936         /* api callbacks */
937         ot->exec= paste_material_exec;
938
939         /* flags */
940         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
941 }