More F-Modifier Tweaks:
[blender.git] / source / blender / blenkernel / intern / material.c
1
2 /*  material.c
3  *
4  * 
5  * $Id$
6  *
7  * ***** BEGIN GPL LICENSE BLOCK *****
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL LICENSE BLOCK *****
31  */
32
33 #include <string.h>
34 #include <math.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_curve_types.h"
39 #include "DNA_material_types.h"
40 #include "DNA_mesh_types.h"
41 #include "DNA_meta_types.h"
42 #include "DNA_node_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_scene_types.h"
45 #include "DNA_texture_types.h"
46 #include "DNA_userdef_types.h"
47
48 #include "BLI_blenlib.h"
49 #include "BLI_math.h"           
50
51 #include "BKE_animsys.h"
52 #include "BKE_blender.h"
53 #include "BKE_displist.h"
54 #include "BKE_global.h"
55 #include "BKE_icons.h"
56 #include "BKE_library.h"
57 #include "BKE_main.h"
58 #include "BKE_material.h"
59 #include "BKE_mesh.h"
60 #include "BKE_node.h"
61 #include "BKE_utildefines.h"
62
63 #ifndef DISABLE_PYTHON
64 #include "BPY_extern.h"
65 #endif
66
67 #include "GPU_material.h"
68
69 /* used in UI and render */
70 Material defmaterial;
71
72 /* called on startup, creator.c */
73 void init_def_material(void)
74 {
75         init_material(&defmaterial);
76 }
77
78 /* not material itself */
79 void free_material(Material *ma)
80 {
81         MTex *mtex;
82         int a;
83         
84         for(a=0; a<MAX_MTEX; a++) {
85                 mtex= ma->mtex[a];
86                 if(mtex && mtex->tex) mtex->tex->id.us--;
87                 if(mtex) MEM_freeN(mtex);
88         }
89         
90         if(ma->ramp_col) MEM_freeN(ma->ramp_col);
91         if(ma->ramp_spec) MEM_freeN(ma->ramp_spec);
92         
93         BKE_free_animdata((ID *)ma);
94         
95         BKE_previewimg_free(&ma->preview);
96         BKE_icon_delete((struct ID*)ma);
97         ma->id.icon_id = 0;
98         
99         /* is no lib link block, but material extension */
100         if(ma->nodetree) {
101                 ntreeFreeTree(ma->nodetree);
102                 MEM_freeN(ma->nodetree);
103         }
104
105         if(ma->gpumaterial.first)
106                 GPU_material_free(ma);
107 }
108
109 void init_material(Material *ma)
110 {
111         ma->r= ma->g= ma->b= ma->ref= 0.8;
112         ma->specr= ma->specg= ma->specb= 1.0;
113         ma->mirr= ma->mirg= ma->mirb= 1.0;
114         ma->spectra= 1.0;
115         ma->amb= 1.0;
116         ma->alpha= 1.0;
117         ma->spec= ma->hasize= 0.5;
118         ma->har= 50;
119         ma->starc= ma->ringc= 4;
120         ma->linec= 12;
121         ma->flarec= 1;
122         ma->flaresize= ma->subsize= 1.0;
123         ma->flareboost= 1;
124         ma->seed2= 6;
125         ma->friction= 0.5;
126         ma->refrac= 4.0;
127         ma->roughness= 0.5;
128         ma->param[0]= 0.5;
129         ma->param[1]= 0.1;
130         ma->param[2]= 0.5;
131         ma->param[3]= 0.1;
132         ma->rms= 0.1;
133         ma->darkness= 1.0;      
134         
135         ma->strand_sta= ma->strand_end= 1.0f;
136         
137         ma->ang= 1.0;
138         ma->ray_depth= 2;
139         ma->ray_depth_tra= 2;
140         ma->fresnel_mir= 0.0;
141         ma->fresnel_tra= 0.0;
142         ma->fresnel_tra_i= 1.25;
143         ma->fresnel_mir_i= 1.25;
144         ma->tx_limit= 0.0;
145         ma->tx_falloff= 1.0;
146         ma->shad_alpha= 1.0f;
147         
148         ma->gloss_mir = ma->gloss_tra= 1.0;
149         ma->samp_gloss_mir = ma->samp_gloss_tra= 18;
150         ma->adapt_thresh_mir = ma->adapt_thresh_tra = 0.005;
151         ma->dist_mir = 0.0;
152         ma->fadeto_mir = MA_RAYMIR_FADETOSKY;
153         
154         ma->rampfac_col= 1.0;
155         ma->rampfac_spec= 1.0;
156         ma->pr_lamp= 3;                 /* two lamps, is bits */
157         ma->pr_type= MA_SPHERE;
158
159         ma->sss_radius[0]= 1.0f;
160         ma->sss_radius[1]= 1.0f;
161         ma->sss_radius[2]= 1.0f;
162         ma->sss_col[0]= 1.0f;
163         ma->sss_col[1]= 1.0f;
164         ma->sss_col[2]= 1.0f;
165         ma->sss_error= 0.05f;
166         ma->sss_scale= 0.1f;
167         ma->sss_ior= 1.3f;
168         ma->sss_colfac= 1.0f;
169         ma->sss_texfac= 0.0f;
170         ma->sss_front= 1.0f;
171         ma->sss_back= 1.0f;
172
173         ma->vol.density = 1.0f;
174         ma->vol.emission = 0.0f;
175         ma->vol.scattering = 1.0f;
176         ma->vol.reflection = 1.0f;
177         ma->vol.transmission_col[0] = ma->vol.transmission_col[1] = ma->vol.transmission_col[2] = 1.0f;
178         ma->vol.reflection_col[0] = ma->vol.reflection_col[1] = ma->vol.reflection_col[2] = 1.0f;
179         ma->vol.emission_col[0] = ma->vol.emission_col[1] = ma->vol.emission_col[2] = 1.0f;
180         ma->vol.density_scale = 1.0f;
181         ma->vol.depth_cutoff = 0.01f;
182         ma->vol.stepsize_type = MA_VOL_STEP_RANDOMIZED;
183         ma->vol.stepsize = 0.2f;
184         ma->vol.shade_type = MA_VOL_SHADE_SHADED;
185         ma->vol.shadeflag |= MA_VOL_PRECACHESHADING;
186         ma->vol.precache_resolution = 50;
187         ma->vol.ms_spread = 0.2f;
188         ma->vol.ms_diff = 1.f;
189         ma->vol.ms_intensity = 1.f;
190         
191         ma->mode= MA_TRACEBLE|MA_SHADBUF|MA_SHADOW|MA_RAYBIAS|MA_TANGENT_STR|MA_ZTRANSP;
192         ma->shade_flag= MA_APPROX_OCCLUSION;
193         ma->preview = NULL;
194 }
195
196 Material *add_material(char *name)
197 {
198         Material *ma;
199
200         ma= alloc_libblock(&G.main->mat, ID_MA, name);
201         
202         init_material(ma);
203         
204         return ma;      
205 }
206
207 Material *copy_material(Material *ma)
208 {
209         Material *man;
210         int a;
211         
212         man= copy_libblock(ma);
213         
214 #if 0 // XXX old animation system
215         id_us_plus((ID *)man->ipo);
216 #endif // XXX old animation system
217         id_us_plus((ID *)man->group);
218         
219         for(a=0; a<MAX_MTEX; a++) {
220                 if(ma->mtex[a]) {
221                         man->mtex[a]= MEM_mallocN(sizeof(MTex), "copymaterial");
222                         memcpy(man->mtex[a], ma->mtex[a], sizeof(MTex));
223                         id_us_plus((ID *)man->mtex[a]->tex);
224                 }
225         }
226         
227         if(ma->ramp_col) man->ramp_col= MEM_dupallocN(ma->ramp_col);
228         if(ma->ramp_spec) man->ramp_spec= MEM_dupallocN(ma->ramp_spec);
229         
230         if (ma->preview) man->preview = BKE_previewimg_copy(ma->preview);
231
232         if(ma->nodetree) {
233                 man->nodetree= ntreeCopyTree(ma->nodetree, 0);  /* 0 == full new tree */
234         }
235
236         man->gpumaterial.first= man->gpumaterial.last= NULL;
237         
238         return man;
239 }
240
241 void make_local_material(Material *ma)
242 {
243         Object *ob;
244         Mesh *me;
245         Curve *cu;
246         MetaBall *mb;
247         Material *man;
248         int a, local=0, lib=0;
249
250         /* - only lib users: do nothing
251             * - only local users: set flag
252             * - mixed: make copy
253             */
254         
255         if(ma->id.lib==0) return;
256         if(ma->id.us==1) {
257                 ma->id.lib= 0;
258                 ma->id.flag= LIB_LOCAL;
259                 new_id(0, (ID *)ma, 0);
260                 for(a=0; a<MAX_MTEX; a++) {
261                         if(ma->mtex[a]) id_lib_extern((ID *)ma->mtex[a]->tex);
262                 }
263                 
264                 return;
265         }
266         
267         /* test objects */
268         ob= G.main->object.first;
269         while(ob) {
270                 if(ob->mat) {
271                         for(a=0; a<ob->totcol; a++) {
272                                 if(ob->mat[a]==ma) {
273                                         if(ob->id.lib) lib= 1;
274                                         else local= 1;
275                                 }
276                         }
277                 }
278                 ob= ob->id.next;
279         }
280         /* test meshes */
281         me= G.main->mesh.first;
282         while(me) {
283                 if(me->mat) {
284                         for(a=0; a<me->totcol; a++) {
285                                 if(me->mat[a]==ma) {
286                                         if(me->id.lib) lib= 1;
287                                         else local= 1;
288                                 }
289                         }
290                 }
291                 me= me->id.next;
292         }
293         /* test curves */
294         cu= G.main->curve.first;
295         while(cu) {
296                 if(cu->mat) {
297                         for(a=0; a<cu->totcol; a++) {
298                                 if(cu->mat[a]==ma) {
299                                         if(cu->id.lib) lib= 1;
300                                         else local= 1;
301                                 }
302                         }
303                 }
304                 cu= cu->id.next;
305         }
306         /* test mballs */
307         mb= G.main->mball.first;
308         while(mb) {
309                 if(mb->mat) {
310                         for(a=0; a<mb->totcol; a++) {
311                                 if(mb->mat[a]==ma) {
312                                         if(mb->id.lib) lib= 1;
313                                         else local= 1;
314                                 }
315                         }
316                 }
317                 mb= mb->id.next;
318         }
319         
320         if(local && lib==0) {
321                 ma->id.lib= 0;
322                 ma->id.flag= LIB_LOCAL;
323                 
324                 for(a=0; a<MAX_MTEX; a++) {
325                         if(ma->mtex[a]) id_lib_extern((ID *)ma->mtex[a]->tex);
326                 }
327                 
328                 new_id(0, (ID *)ma, 0);
329         }
330         else if(local && lib) {
331                 
332                 man= copy_material(ma);
333                 man->id.us= 0;
334                 
335                 /* do objects */
336                 ob= G.main->object.first;
337                 while(ob) {
338                         if(ob->mat) {
339                                 for(a=0; a<ob->totcol; a++) {
340                                         if(ob->mat[a]==ma) {
341                                                 if(ob->id.lib==0) {
342                                                         ob->mat[a]= man;
343                                                         man->id.us++;
344                                                         ma->id.us--;
345                                                 }
346                                         }
347                                 }
348                         }
349                         ob= ob->id.next;
350                 }
351                 /* do meshes */
352                 me= G.main->mesh.first;
353                 while(me) {
354                         if(me->mat) {
355                                 for(a=0; a<me->totcol; a++) {
356                                         if(me->mat[a]==ma) {
357                                                 if(me->id.lib==0) {
358                                                         me->mat[a]= man;
359                                                         man->id.us++;
360                                                         ma->id.us--;
361                                                 }
362                                         }
363                                 }
364                         }
365                         me= me->id.next;
366                 }
367                 /* do curves */
368                 cu= G.main->curve.first;
369                 while(cu) {
370                         if(cu->mat) {
371                                 for(a=0; a<cu->totcol; a++) {
372                                         if(cu->mat[a]==ma) {
373                                                 if(cu->id.lib==0) {
374                                                         cu->mat[a]= man;
375                                                         man->id.us++;
376                                                         ma->id.us--;
377                                                 }
378                                         }
379                                 }
380                         }
381                         cu= cu->id.next;
382                 }
383                 /* do mballs */
384                 mb= G.main->mball.first;
385                 while(mb) {
386                         if(mb->mat) {
387                                 for(a=0; a<mb->totcol; a++) {
388                                         if(mb->mat[a]==ma) {
389                                                 if(mb->id.lib==0) {
390                                                         mb->mat[a]= man;
391                                                         man->id.us++;
392                                                         ma->id.us--;
393                                                 }
394                                         }
395                                 }
396                         }
397                         mb= mb->id.next;
398                 }
399         }
400 }
401
402 Material ***give_matarar(Object *ob)
403 {
404         Mesh *me;
405         Curve *cu;
406         MetaBall *mb;
407         
408         if(ob->type==OB_MESH) {
409                 me= ob->data;
410                 return &(me->mat);
411         }
412         else if ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF) {
413                 cu= ob->data;
414                 return &(cu->mat);
415         }
416         else if(ob->type==OB_MBALL) {
417                 mb= ob->data;
418                 return &(mb->mat);
419         }
420         return NULL;
421 }
422
423 short *give_totcolp(Object *ob)
424 {
425         Mesh *me;
426         Curve *cu;
427         MetaBall *mb;
428         
429         if(ob->type==OB_MESH) {
430                 me= ob->data;
431                 return &(me->totcol);
432         }
433         else if ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF) {
434                 cu= ob->data;
435                 return &(cu->totcol);
436         }
437         else if(ob->type==OB_MBALL) {
438                 mb= ob->data;
439                 return &(mb->totcol);
440         }
441         return NULL;
442 }
443
444 Material *give_current_material(Object *ob, int act)
445 {
446         Material ***matarar, *ma;
447         short *totcolp;
448         
449         if(ob==NULL) return NULL;
450         
451         /* if object cannot have material, totcolp==NULL */
452         totcolp= give_totcolp(ob);
453         if(totcolp==NULL || ob->totcol==0) return NULL;
454         
455         if(act>ob->totcol) act= ob->totcol;
456         else if(act<=0) act= 1;
457
458         if(ob->matbits[act-1]) {        /* in object */
459                 ma= ob->mat[act-1];
460         }
461         else {                                                          /* in data */
462
463                 /* check for inconsistancy */
464                 if(*totcolp < ob->totcol)
465                         ob->totcol= *totcolp;
466                 if(act>ob->totcol) act= ob->totcol;
467
468                 matarar= give_matarar(ob);
469                 
470                 if(matarar && *matarar) ma= (*matarar)[act-1];
471                 else ma= 0;
472                 
473         }
474         
475         return ma;
476 }
477
478 ID *material_from(Object *ob, int act)
479 {
480
481         if(ob==0) return 0;
482
483         if(ob->totcol==0) return ob->data;
484         if(act==0) act= 1;
485
486         if(ob->matbits[act-1]) return (ID *)ob;
487         else return ob->data;
488 }
489
490 Material *give_node_material(Material *ma)
491 {
492         if(ma && ma->use_nodes && ma->nodetree) {
493                 bNode *node= nodeGetActiveID(ma->nodetree, ID_MA);
494
495                 if(node)
496                         return (Material *)node->id;
497         }
498
499         return NULL;
500 }
501
502 /* GS reads the memory pointed at in a specific ordering. There are,
503  * however two definitions for it. I have jotted them down here, both,
504  * but I think the first one is actually used. The thing is that
505  * big-endian systems might read this the wrong way round. OTOH, we
506  * constructed the IDs that are read out with this macro explicitly as
507  * well. I expect we'll sort it out soon... */
508
509 /* from blendef: */
510 #define GS(a)   (*((short *)(a)))
511
512 /* from misc_util: flip the bytes from x  */
513 /*  #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
514
515 void test_object_materials(ID *id)
516 {
517         /* make the ob mat-array same size as 'ob->data' mat-array */
518         Object *ob;
519         Mesh *me;
520         Curve *cu;
521         MetaBall *mb;
522         Material **newmatar;
523         char *newmatbits;
524         int totcol=0;
525
526         if(id==0) return;
527
528         if( GS(id->name)==ID_ME ) {
529                 me= (Mesh *)id;
530                 totcol= me->totcol;
531         }
532         else if( GS(id->name)==ID_CU ) {
533                 cu= (Curve *)id;
534                 totcol= cu->totcol;
535         }
536         else if( GS(id->name)==ID_MB ) {
537                 mb= (MetaBall *)id;
538                 totcol= mb->totcol;
539         }
540         else return;
541
542         ob= G.main->object.first;
543         while(ob) {
544                 
545                 if(ob->data==id) {
546                 
547                         if(totcol==0) {
548                                 if(ob->totcol) {
549                                         MEM_freeN(ob->mat);
550                                         MEM_freeN(ob->matbits);
551                                         ob->mat= NULL;
552                                         ob->matbits= NULL;
553                                 }
554                         }
555                         else if(ob->totcol<totcol) {
556                                 newmatar= MEM_callocN(sizeof(void *)*totcol, "newmatar");
557                                 newmatbits= MEM_callocN(sizeof(char)*totcol, "newmatbits");
558                                 if(ob->totcol) {
559                                         memcpy(newmatar, ob->mat, sizeof(void *)*ob->totcol);
560                                         memcpy(newmatbits, ob->matbits, sizeof(char)*ob->totcol);
561                                         MEM_freeN(ob->mat);
562                                         MEM_freeN(ob->matbits);
563                                 }
564                                 ob->mat= newmatar;
565                                 ob->matbits= newmatbits;
566                         }
567                         ob->totcol= totcol;
568                         if(ob->totcol && ob->actcol==0) ob->actcol= 1;
569                         if(ob->actcol>ob->totcol) ob->actcol= ob->totcol;
570                 }
571                 ob= ob->id.next;
572         }
573 }
574
575
576 void assign_material(Object *ob, Material *ma, int act)
577 {
578         Material *mao, **matar, ***matarar;
579         char *matbits;
580         short *totcolp;
581
582         if(act>MAXMAT) return;
583         if(act<1) act= 1;
584         
585         /* test arraylens */
586         
587         totcolp= give_totcolp(ob);
588         matarar= give_matarar(ob);
589         
590         if(totcolp==0 || matarar==0) return;
591         
592         if(act > *totcolp) {
593                 matar= MEM_callocN(sizeof(void *)*act, "matarray1");
594
595                 if(*totcolp) {
596                         memcpy(matar, *matarar, sizeof(void *)*(*totcolp));
597                         MEM_freeN(*matarar);
598                 }
599
600                 *matarar= matar;
601                 *totcolp= act;
602         }
603         
604         if(act > ob->totcol) {
605                 matar= MEM_callocN(sizeof(void *)*act, "matarray2");
606                 matbits= MEM_callocN(sizeof(char)*act, "matbits1");
607                 if( ob->totcol) {
608                         memcpy(matar, ob->mat, sizeof(void *)*( ob->totcol ));
609                         memcpy(matbits, ob->matbits, sizeof(char)*(*totcolp));
610                         MEM_freeN(ob->mat);
611                         MEM_freeN(ob->matbits);
612                 }
613                 ob->mat= matar;
614                 ob->matbits= matbits;
615                 ob->totcol= act;
616
617                 /* copy object/mesh linking, or assign based on userpref */
618                 if(ob->actcol)
619                         ob->matbits[act-1]= ob->matbits[ob->actcol-1];
620                 else
621                         ob->matbits[act-1]= (U.flag & USER_MAT_ON_OB)? 1: 0;
622         }
623         
624         /* do it */
625
626         if(ob->matbits[act-1]) {        /* in object */
627                 mao= ob->mat[act-1];
628                 if(mao) mao->id.us--;
629                 ob->mat[act-1]= ma;
630         }
631         else {  /* in data */
632                 mao= (*matarar)[act-1];
633                 if(mao) mao->id.us--;
634                 (*matarar)[act-1]= ma;
635         }
636
637         if(ma)
638                 id_us_plus((ID *)ma);
639         test_object_materials(ob->data);
640 }
641
642 /* XXX - this calls many more update calls per object then are needed, could be optimized */
643 void assign_matarar(struct Object *ob, struct Material ***matar, int totcol)
644 {
645         int i, actcol_orig= ob->actcol;
646
647         while(object_remove_material_slot(ob)) {};
648
649         /* now we have the right number of slots */
650         for(i=0; i<totcol; i++)
651                 assign_material(ob, (*matar)[i], i+1);
652
653         if(actcol_orig > ob->totcol)
654                 actcol_orig= ob->totcol;
655
656         ob->actcol= actcol_orig;
657 }
658
659
660 int find_material_index(Object *ob, Material *ma)
661 {
662         Material ***matarar;
663         short a, *totcolp;
664         
665         if(ma==NULL) return 0;
666         
667         totcolp= give_totcolp(ob);
668         matarar= give_matarar(ob);
669         
670         if(totcolp==NULL || matarar==NULL) return 0;
671         
672         for(a=0; a<*totcolp; a++)
673                 if((*matarar)[a]==ma)
674                    break;
675         if(a<*totcolp)
676                 return a+1;
677         return 0;          
678 }
679
680 int object_add_material_slot(Object *ob)
681 {
682         Material *ma;
683         
684         if(ob==0) return FALSE;
685         if(ob->totcol>=MAXMAT) return FALSE;
686         
687         ma= give_current_material(ob, ob->actcol);
688
689         assign_material(ob, ma, ob->totcol+1);
690         ob->actcol= ob->totcol;
691         return TRUE;
692 }
693
694 static void do_init_render_material(Material *ma, int r_mode, float *amb)
695 {
696         MTex *mtex;
697         int a, needuv=0, needtang=0;
698         
699         if(ma->flarec==0) ma->flarec= 1;
700
701         /* add all texcoflags from mtex, texco and mapto were cleared in advance */
702         for(a=0; a<MAX_MTEX; a++) {
703                 
704                 /* separate tex switching */
705                 if(ma->septex & (1<<a)) continue;
706
707                 mtex= ma->mtex[a];
708                 if(mtex && mtex->tex && (mtex->tex->type | (mtex->tex->use_nodes && mtex->tex->nodetree) )) {
709                         
710                         ma->texco |= mtex->texco;
711                         ma->mapto |= mtex->mapto;
712                         if(r_mode & R_OSA) {
713                                 if ELEM3(mtex->tex->type, TEX_IMAGE, TEX_PLUGIN, TEX_ENVMAP) ma->texco |= TEXCO_OSA;
714                                 else if(mtex->texflag & MTEX_NEW_BUMP) ma->texco |= TEXCO_OSA; // NEWBUMP: need texture derivatives for procedurals as well
715                         }
716                         
717                         if(ma->texco & (TEXCO_ORCO|TEXCO_REFL|TEXCO_NORM|TEXCO_STRAND|TEXCO_STRESS)) needuv= 1;
718                         else if(ma->texco & (TEXCO_GLOB|TEXCO_UV|TEXCO_OBJECT|TEXCO_SPEED)) needuv= 1;
719                         else if(ma->texco & (TEXCO_LAVECTOR|TEXCO_VIEW|TEXCO_STICKY)) needuv= 1;
720
721                         if((ma->mapto & MAP_NORM) && (mtex->normapspace == MTEX_NSPACE_TANGENT))
722                                 needtang= 1;
723                 }
724         }
725
726         if(needtang) ma->mode |= MA_NORMAP_TANG;
727         else ma->mode &= ~MA_NORMAP_TANG;
728         
729         if(ma->mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)) {
730                 needuv= 1;
731                 if(r_mode & R_OSA) ma->texco |= TEXCO_OSA;              /* for texfaces */
732         }
733         if(needuv) ma->texco |= NEED_UV;
734         
735         /* since the raytracer doesnt recalc O structs for each ray, we have to preset them all */
736         if(r_mode & R_RAYTRACE) {
737                 if((ma->mode & (MA_RAYMIRROR|MA_SHADOW_TRA)) || ((ma->mode && MA_TRANSP) && (ma->mode & MA_RAYTRANSP))) { 
738                         ma->texco |= NEED_UV|TEXCO_ORCO|TEXCO_REFL|TEXCO_NORM;
739                         if(r_mode & R_OSA) ma->texco |= TEXCO_OSA;
740                 }
741         }
742         
743         if(amb) {
744                 ma->ambr= ma->amb*amb[0];
745                 ma->ambg= ma->amb*amb[1];
746                 ma->ambb= ma->amb*amb[2];
747         }       
748         /* will become or-ed result of all node modes */
749         ma->mode_l= ma->mode;
750         ma->mode_l &= ~MA_SHLESS;
751
752         if(ma->strand_surfnor > 0.0f)
753                 ma->mode_l |= MA_STR_SURFDIFF;
754 }
755
756 static void init_render_nodetree(bNodeTree *ntree, Material *basemat, int r_mode, float *amb)
757 {
758         bNode *node;
759         
760         for(node=ntree->nodes.first; node; node= node->next) {
761                 if(node->id) {
762                         if(GS(node->id->name)==ID_MA) {
763                                 Material *ma= (Material *)node->id;
764                                 if(ma!=basemat) {
765                                         do_init_render_material(ma, r_mode, amb);
766                                         basemat->texco |= ma->texco;
767                                         basemat->mode_l |= ma->mode_l;
768                                 }
769                         }
770                         else if(node->type==NODE_GROUP)
771                                 init_render_nodetree((bNodeTree *)node->id, basemat, r_mode, amb);
772                 }
773         }
774         /* parses the geom+tex nodes */
775         ntreeShaderGetTexcoMode(ntree, r_mode, &basemat->texco, &basemat->mode_l);
776 }
777
778 void init_render_material(Material *mat, int r_mode, float *amb)
779 {
780         
781         do_init_render_material(mat, r_mode, amb);
782         
783         if(mat->nodetree && mat->use_nodes) {
784                 init_render_nodetree(mat->nodetree, mat, r_mode, amb);
785                 
786                 ntreeBeginExecTree(mat->nodetree); /* has internal flag to detect it only does it once */
787         }
788 }
789
790 void init_render_materials(int r_mode, float *amb)
791 {
792         Material *ma;
793         
794         /* clear these flags before going over materials, to make sure they
795          * are cleared only once, otherwise node materials contained in other
796          * node materials can go wrong */
797         for(ma= G.main->mat.first; ma; ma= ma->id.next) {
798                 if(ma->id.us) {
799                         ma->texco= 0;
800                         ma->mapto= 0;
801                 }
802         }
803
804         /* two steps, first initialize, then or the flags for layers */
805         for(ma= G.main->mat.first; ma; ma= ma->id.next) {
806                 /* is_used flag comes back in convertblender.c */
807                 ma->flag &= ~MA_IS_USED;
808                 if(ma->id.us) 
809                         init_render_material(ma, r_mode, amb);
810         }
811         
812         do_init_render_material(&defmaterial, r_mode, amb);
813 }
814
815 /* only needed for nodes now */
816 void end_render_material(Material *mat)
817 {
818         if(mat && mat->nodetree && mat->use_nodes)
819                 ntreeEndExecTree(mat->nodetree); /* has internal flag to detect it only does it once */
820 }
821
822 void end_render_materials(void)
823 {
824         Material *ma;
825         for(ma= G.main->mat.first; ma; ma= ma->id.next)
826                 if(ma->id.us) 
827                         end_render_material(ma);
828 }
829
830 static int material_in_nodetree(bNodeTree *ntree, Material *mat)
831 {
832         bNode *node;
833
834         for(node=ntree->nodes.first; node; node= node->next) {
835                 if(node->id && GS(node->id->name)==ID_MA) {
836                         if(node->id==(ID*)mat)
837                                 return 1;
838                 }
839                 else if(node->type==NODE_GROUP)
840                         if(material_in_nodetree((bNodeTree*)node->id, mat))
841                                 return 1;
842         }
843
844         return 0;
845 }
846
847 int material_in_material(Material *parmat, Material *mat)
848 {
849         if(parmat==mat)
850                 return 1;
851         else if(parmat->nodetree && parmat->use_nodes)
852                 return material_in_nodetree(parmat->nodetree, mat);
853         else
854                 return 0;
855 }
856         
857 /* ****************** */
858
859 char colname_array[125][20]= {
860 "Black","DarkRed","HalfRed","Red","Red",
861 "DarkGreen","DarkOlive","Brown","Chocolate","OrangeRed",
862 "HalfGreen","GreenOlive","DryOlive","Goldenrod","DarkOrange",
863 "LightGreen","Chartreuse","YellowGreen","Yellow","Gold",
864 "Green","LawnGreen","GreenYellow","LightOlive","Yellow",
865 "DarkBlue","DarkPurple","HotPink","VioletPink","RedPink",
866 "SlateGray","DarkGrey","PalePurple","IndianRed","Tomato",
867 "SeaGreen","PaleGreen","GreenKhaki","LightBrown","LightSalmon",
868 "SpringGreen","PaleGreen","MediumOlive","YellowBrown","LightGold",
869 "LightGreen","LightGreen","LightGreen","GreenYellow","PaleYellow",
870 "HalfBlue","DarkSky","HalfMagenta","VioletRed","DeepPink",
871 "SteelBlue","SkyBlue","Orchid","LightHotPink","HotPink",
872 "SeaGreen","SlateGray","MediumGrey","Burlywood","LightPink",
873 "SpringGreen","Aquamarine","PaleGreen","Khaki","PaleOrange",
874 "SpringGreen","SeaGreen","PaleGreen","PaleWhite","YellowWhite",
875 "LightBlue","Purple","MediumOrchid","Magenta","Magenta",
876 "RoyalBlue","SlateBlue","MediumOrchid","Orchid","Magenta",
877 "DeepSkyBlue","LightSteelBlue","LightSkyBlue","Violet","LightPink",
878 "Cyan","DarkTurquoise","SkyBlue","Grey","Snow",
879 "Mint","Mint","Aquamarine","MintCream","Ivory",
880 "Blue","Blue","DarkMagenta","DarkOrchid","Magenta",
881 "SkyBlue","RoyalBlue","LightSlateBlue","MediumOrchid","Magenta",
882 "DodgerBlue","SteelBlue","MediumPurple","PalePurple","Plum",
883 "DeepSkyBlue","PaleBlue","LightSkyBlue","PalePurple","Thistle",
884 "Cyan","ColdBlue","PaleTurquoise","GhostWhite","White"
885 };
886
887 void automatname(Material *ma)
888 {
889         int nr, r, g, b;
890         float ref;
891         
892         if(ma==0) return;
893         if(ma->mode & MA_SHLESS) ref= 1.0;
894         else ref= ma->ref;
895         
896         r= (int)(4.99*(ref*ma->r));
897         g= (int)(4.99*(ref*ma->g));
898         b= (int)(4.99*(ref*ma->b));
899         nr= r + 5*g + 25*b;
900         if(nr>124) nr= 124;
901         new_id(&G.main->mat, (ID *)ma, colname_array[nr]);
902         
903 }
904
905
906 int object_remove_material_slot(Object *ob)
907 {
908         Material *mao, ***matarar;
909         Object *obt;
910         Curve *cu;
911         Nurb *nu;
912         short *totcolp;
913         int a, actcol;
914         
915         if(ob==NULL || ob->totcol==0) return FALSE;
916         
917         /* take a mesh/curve/mball as starting point, remove 1 index,
918          * AND with all objects that share the ob->data
919          * 
920          * after that check indices in mesh/curve/mball!!!
921          */
922         
923         totcolp= give_totcolp(ob);
924         matarar= give_matarar(ob);
925
926         if(*matarar==NULL) return FALSE;
927
928         /* we delete the actcol */
929         if(ob->totcol) {
930                 mao= (*matarar)[ob->actcol-1];
931                 if(mao) mao->id.us--;
932         }
933         
934         for(a=ob->actcol; a<ob->totcol; a++)
935                 (*matarar)[a-1]= (*matarar)[a];
936         (*totcolp)--;
937         
938         if(*totcolp==0) {
939                 MEM_freeN(*matarar);
940                 *matarar= 0;
941         }
942         
943         actcol= ob->actcol;
944         obt= G.main->object.first;
945         while(obt) {
946         
947                 if(obt->data==ob->data) {
948                         
949                         /* WATCH IT: do not use actcol from ob or from obt (can become zero) */
950                         mao= obt->mat[actcol-1];
951                         if(mao) mao->id.us--;
952                 
953                         for(a=actcol; a<obt->totcol; a++) {
954                                 obt->mat[a-1]= obt->mat[a];
955                                 obt->matbits[a-1]= obt->matbits[a];
956                         }
957                         obt->totcol--;
958                         if(obt->actcol > obt->totcol) obt->actcol= obt->totcol;
959                         
960                         if(obt->totcol==0) {
961                                 MEM_freeN(obt->mat);
962                                 MEM_freeN(obt->matbits);
963                                 obt->mat= 0;
964                                 obt->matbits= NULL;
965                         }
966                 }
967                 obt= obt->id.next;
968         }
969
970         /* check indices from mesh */
971
972         if(ob->type==OB_MESH) {
973                 Mesh *me= get_mesh(ob);
974                 mesh_delete_material_index(me, actcol-1);
975                 freedisplist(&ob->disp);
976         }
977         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
978                 cu= ob->data;
979                 nu= cu->nurb.first;
980                 
981                 while(nu) {
982                         if(nu->mat_nr && nu->mat_nr>=actcol-1) {
983                                 nu->mat_nr--;
984                                 if (ob->type == OB_CURVE) nu->charidx--;
985                         }
986                         nu= nu->next;
987                 }
988                 freedisplist(&ob->disp);
989         }
990
991         return TRUE;
992 }
993
994
995 /* r g b = current value, col = new value, fac==0 is no change */
996 /* if g==NULL, it only does r channel */
997 void ramp_blend(int type, float *r, float *g, float *b, float fac, float *col)
998 {
999         float tmp, facm= 1.0f-fac;
1000         
1001         switch (type) {
1002                 case MA_RAMP_BLEND:
1003                         *r = facm*(*r) + fac*col[0];
1004                         if(g) {
1005                                 *g = facm*(*g) + fac*col[1];
1006                                 *b = facm*(*b) + fac*col[2];
1007                         }
1008                                 break;
1009                 case MA_RAMP_ADD:
1010                         *r += fac*col[0];
1011                         if(g) {
1012                                 *g += fac*col[1];
1013                                 *b += fac*col[2];
1014                         }
1015                                 break;
1016                 case MA_RAMP_MULT:
1017                         *r *= (facm + fac*col[0]);
1018                         if(g) {
1019                                 *g *= (facm + fac*col[1]);
1020                                 *b *= (facm + fac*col[2]);
1021                         }
1022                                 break;
1023                 case MA_RAMP_SCREEN:
1024                         *r = 1.0f - (facm + fac*(1.0f - col[0])) * (1.0f - *r);
1025                         if(g) {
1026                                 *g = 1.0f - (facm + fac*(1.0f - col[1])) * (1.0f - *g);
1027                                 *b = 1.0f - (facm + fac*(1.0f - col[2])) * (1.0f - *b);
1028                         }
1029                                 break;
1030                 case MA_RAMP_OVERLAY:
1031                         if(*r < 0.5f)
1032                                 *r *= (facm + 2.0f*fac*col[0]);
1033                         else
1034                                 *r = 1.0f - (facm + 2.0f*fac*(1.0f - col[0])) * (1.0f - *r);
1035                         if(g) {
1036                                 if(*g < 0.5f)
1037                                         *g *= (facm + 2.0f*fac*col[1]);
1038                                 else
1039                                         *g = 1.0f - (facm + 2.0f*fac*(1.0f - col[1])) * (1.0f - *g);
1040                                 if(*b < 0.5f)
1041                                         *b *= (facm + 2.0f*fac*col[2]);
1042                                 else
1043                                         *b = 1.0f - (facm + 2.0f*fac*(1.0f - col[2])) * (1.0f - *b);
1044                         }
1045                                 break;
1046                 case MA_RAMP_SUB:
1047                         *r -= fac*col[0];
1048                         if(g) {
1049                                 *g -= fac*col[1];
1050                                 *b -= fac*col[2];
1051                         }
1052                                 break;
1053                 case MA_RAMP_DIV:
1054                         if(col[0]!=0.0f)
1055                                 *r = facm*(*r) + fac*(*r)/col[0];
1056                         if(g) {
1057                                 if(col[1]!=0.0f)
1058                                         *g = facm*(*g) + fac*(*g)/col[1];
1059                                 if(col[2]!=0.0f)
1060                                         *b = facm*(*b) + fac*(*b)/col[2];
1061                         }
1062                                 break;
1063                 case MA_RAMP_DIFF:
1064                         *r = facm*(*r) + fac*fabs(*r-col[0]);
1065                         if(g) {
1066                                 *g = facm*(*g) + fac*fabs(*g-col[1]);
1067                                 *b = facm*(*b) + fac*fabs(*b-col[2]);
1068                         }
1069                                 break;
1070                 case MA_RAMP_DARK:
1071             tmp=col[0]+((1-col[0])*facm); 
1072             if(tmp < *r) *r= tmp; 
1073             if(g) { 
1074                 tmp=col[1]+((1-col[1])*facm); 
1075                 if(tmp < *g) *g= tmp; 
1076                 tmp=col[2]+((1-col[2])*facm); 
1077                 if(tmp < *b) *b= tmp; 
1078             } 
1079                 break; 
1080                 case MA_RAMP_LIGHT:
1081                         tmp= fac*col[0];
1082                         if(tmp > *r) *r= tmp; 
1083                                 if(g) {
1084                                         tmp= fac*col[1];
1085                                         if(tmp > *g) *g= tmp; 
1086                                         tmp= fac*col[2];
1087                                         if(tmp > *b) *b= tmp; 
1088                                 }
1089                                         break;  
1090                 case MA_RAMP_DODGE:                     
1091                         
1092                                 
1093                         if(*r !=0.0f){
1094                                 tmp = 1.0f - fac*col[0];
1095                                 if(tmp <= 0.0f)
1096                                         *r = 1.0f;
1097                                 else if ((tmp = (*r) / tmp)> 1.0f)
1098                                         *r = 1.0f;
1099                                 else 
1100                                         *r = tmp;
1101                         }
1102                         if(g) {
1103                                 if(*g !=0.0f){
1104                                         tmp = 1.0f - fac*col[1];
1105                                         if(tmp <= 0.0f )
1106                                                 *g = 1.0f;
1107                                         else if ((tmp = (*g) / tmp) > 1.0f )
1108                                                 *g = 1.0f;
1109                                         else
1110                                                 *g = tmp;
1111                                 }
1112                                 if(*b !=0.0f){
1113                                         tmp = 1.0f - fac*col[2];
1114                                         if(tmp <= 0.0f)
1115                                                 *b = 1.0f;
1116                                         else if ((tmp = (*b) / tmp) > 1.0f )
1117                                                 *b = 1.0f;
1118                                         else
1119                                                 *b = tmp;
1120                                 }
1121
1122                         }
1123                                 break;  
1124                 case MA_RAMP_BURN:
1125                         
1126                         tmp = facm + fac*col[0];
1127                         
1128                         if(tmp <= 0.0f)
1129                                 *r = 0.0f;
1130                         else if (( tmp = (1.0f - (1.0f - (*r)) / tmp )) < 0.0f)
1131                                 *r = 0.0f;
1132                         else if (tmp > 1.0f)
1133                                 *r=1.0f;
1134                         else 
1135                                 *r = tmp; 
1136
1137                         if(g) {
1138                                 tmp = facm + fac*col[1];
1139                                 if(tmp <= 0.0f)
1140                                         *g = 0.0f;
1141                                 else if (( tmp = (1.0f - (1.0f - (*g)) / tmp )) < 0.0f )
1142                                         *g = 0.0f;
1143                                 else if(tmp >1.0f)
1144                                         *g=1.0f;
1145                                 else
1146                                         *g = tmp;
1147                                         
1148                                 tmp = facm + fac*col[2];
1149                                 if(tmp <= 0.0f)
1150                                         *b = 0.0f;
1151                                 else if (( tmp = (1.0f - (1.0f - (*b)) / tmp )) < 0.0f  )
1152                                         *b = 0.0f;
1153                                 else if(tmp >1.0f)
1154                                         *b= 1.0f;
1155                                 else
1156                                         *b = tmp;
1157                         }
1158                                 break;
1159                 case MA_RAMP_HUE:               
1160                         if(g){
1161                                 float rH,rS,rV;
1162                                 float colH,colS,colV; 
1163                                 float tmpr,tmpg,tmpb;
1164                                 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1165                                 if(colS!=0 ){
1166                                         rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1167                                         hsv_to_rgb( colH , rS, rV, &tmpr, &tmpg, &tmpb);
1168                                         *r = facm*(*r) + fac*tmpr;  
1169                                         *g = facm*(*g) + fac*tmpg; 
1170                                         *b = facm*(*b) + fac*tmpb;
1171                                 }
1172                         }
1173                                 break;
1174                 case MA_RAMP_SAT:               
1175                         if(g){
1176                                 float rH,rS,rV;
1177                                 float colH,colS,colV;
1178                                 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1179                                 if(rS!=0){
1180                                         rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1181                                         hsv_to_rgb( rH, (facm*rS +fac*colS), rV, r, g, b);
1182                                 }
1183                         }
1184                                 break;
1185                 case MA_RAMP_VAL:               
1186                         if(g){
1187                                 float rH,rS,rV;
1188                                 float colH,colS,colV;
1189                                 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1190                                 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1191                                 hsv_to_rgb( rH, rS, (facm*rV +fac*colV), r, g, b);
1192                         }
1193                                 break;
1194                 case MA_RAMP_COLOR:             
1195                         if(g){
1196                                 float rH,rS,rV;
1197                                 float colH,colS,colV;
1198                                 float tmpr,tmpg,tmpb;
1199                                 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1200                                 if(colS!=0){
1201                                         rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1202                                         hsv_to_rgb( colH, colS, rV, &tmpr, &tmpg, &tmpb);
1203                                         *r = facm*(*r) + fac*tmpr;
1204                                         *g = facm*(*g) + fac*tmpg;
1205                                         *b = facm*(*b) + fac*tmpb;
1206                                 }
1207                         }
1208                                 break;
1209         case MA_RAMP_SOFT: 
1210             if (g){ 
1211                 float scr, scg, scb; 
1212                  
1213                 /* first calculate non-fac based Screen mix */ 
1214                 scr = 1.0f - (1.0f - col[0]) * (1.0f - *r); 
1215                 scg = 1.0f - (1.0f - col[1]) * (1.0f - *g); 
1216                 scb = 1.0f - (1.0f - col[2]) * (1.0f - *b); 
1217                  
1218                 *r = facm*(*r) + fac*(((1.0f - *r) * col[0] * (*r)) + (*r * scr)); 
1219                 *g = facm*(*g) + fac*(((1.0f - *g) * col[1] * (*g)) + (*g * scg)); 
1220                 *b = facm*(*b) + fac*(((1.0f - *b) * col[2] * (*b)) + (*b * scb)); 
1221             } 
1222                 break; 
1223         case MA_RAMP_LINEAR: 
1224             if (col[0] > 0.5f)  
1225                 *r = *r + fac*(2.0f*(col[0]-0.5f)); 
1226             else  
1227                 *r = *r + fac*(2.0f*(col[0]) - 1.0f); 
1228             if (g){ 
1229                 if (col[1] > 0.5f)  
1230                     *g = *g + fac*(2.0f*(col[1]-0.5f)); 
1231                 else  
1232                     *g = *g + fac*(2.0f*(col[1]) -1.0f); 
1233                 if (col[2] > 0.5f)  
1234                     *b = *b + fac*(2.0f*(col[2]-0.5f)); 
1235                 else  
1236                     *b = *b + fac*(2.0f*(col[2]) - 1.0f); 
1237             } 
1238                 break; 
1239         }       
1240 }
1241
1242 /* copy/paste buffer, if we had a propper py api that would be better */
1243 Material matcopybuf;
1244 // MTex mtexcopybuf;
1245 static short matcopied=0;
1246
1247 void clear_matcopybuf(void)
1248 {
1249         memset(&matcopybuf, 0, sizeof(Material));
1250         matcopied= 0;
1251 }
1252
1253 void free_matcopybuf(void)
1254 {
1255 //      extern MTex mtexcopybuf;        /* buttons.c */
1256         int a;
1257
1258         for(a=0; a<MAX_MTEX; a++) {
1259                 if(matcopybuf.mtex[a]) {
1260                         MEM_freeN(matcopybuf.mtex[a]);
1261                         matcopybuf.mtex[a]= NULL;
1262                 }
1263         }
1264
1265         if(matcopybuf.ramp_col) MEM_freeN(matcopybuf.ramp_col);
1266         if(matcopybuf.ramp_spec) MEM_freeN(matcopybuf.ramp_spec);
1267
1268         matcopybuf.ramp_col= NULL;
1269         matcopybuf.ramp_spec= NULL;
1270
1271         if(matcopybuf.nodetree) {
1272                 ntreeFreeTree(matcopybuf.nodetree);
1273                 MEM_freeN(matcopybuf.nodetree);
1274                 matcopybuf.nodetree= NULL;
1275         }
1276 //      default_mtex(&mtexcopybuf);
1277
1278         matcopied= 0;
1279 }
1280
1281 void copy_matcopybuf(Material *ma)
1282 {
1283         int a;
1284         MTex *mtex;
1285
1286         if(matcopied)
1287                 free_matcopybuf();
1288
1289         memcpy(&matcopybuf, ma, sizeof(Material));
1290         if(matcopybuf.ramp_col) matcopybuf.ramp_col= MEM_dupallocN(matcopybuf.ramp_col);
1291         if(matcopybuf.ramp_spec) matcopybuf.ramp_spec= MEM_dupallocN(matcopybuf.ramp_spec);
1292
1293         for(a=0; a<MAX_MTEX; a++) {
1294                 mtex= matcopybuf.mtex[a];
1295                 if(mtex) {
1296                         matcopybuf.mtex[a]= MEM_dupallocN(mtex);
1297                 }
1298         }
1299         matcopybuf.nodetree= ntreeCopyTree(ma->nodetree, 0);
1300         matcopybuf.preview= NULL;
1301         matcopybuf.gpumaterial.first= matcopybuf.gpumaterial.last= NULL;
1302         matcopied= 1;
1303 }
1304
1305 void paste_matcopybuf(Material *ma)
1306 {
1307         int a;
1308         MTex *mtex;
1309         ID id;
1310
1311         if(matcopied==0)
1312                 return;
1313         /* free current mat */
1314         if(ma->ramp_col) MEM_freeN(ma->ramp_col);
1315         if(ma->ramp_spec) MEM_freeN(ma->ramp_spec);
1316         for(a=0; a<MAX_MTEX; a++) {
1317                 mtex= ma->mtex[a];
1318                 if(mtex && mtex->tex) mtex->tex->id.us--;
1319                 if(mtex) MEM_freeN(mtex);
1320         }
1321
1322         if(ma->nodetree) {
1323                 ntreeFreeTree(ma->nodetree);
1324                 MEM_freeN(ma->nodetree);
1325         }
1326
1327         GPU_materials_free(ma);
1328
1329         id= (ma->id);
1330         memcpy(ma, &matcopybuf, sizeof(Material));
1331         (ma->id)= id;
1332
1333         if(matcopybuf.ramp_col) ma->ramp_col= MEM_dupallocN(matcopybuf.ramp_col);
1334         if(matcopybuf.ramp_spec) ma->ramp_spec= MEM_dupallocN(matcopybuf.ramp_spec);
1335
1336         for(a=0; a<MAX_MTEX; a++) {
1337                 mtex= ma->mtex[a];
1338                 if(mtex) {
1339                         ma->mtex[a]= MEM_dupallocN(mtex);
1340                         if(mtex->tex) id_us_plus((ID *)mtex->tex);
1341                 }
1342         }
1343
1344         ma->nodetree= ntreeCopyTree(matcopybuf.nodetree, 0);
1345
1346         /*
1347         BIF_preview_changed(ID_MA);
1348         BIF_undo_push("Paste material settings");
1349         scrarea_queue_winredraw(curarea);
1350         */
1351 }
1352
1353
1354 static short mtexcopied=0; /* must be reset on file load */
1355 static MTex mtexcopybuf;
1356
1357 void clear_mat_mtex_copybuf(void)
1358 {       /* use for file reload */
1359         mtexcopied= 0;
1360 }
1361
1362 void copy_mat_mtex_copybuf(ID *id)
1363 {
1364         MTex **mtex= NULL;
1365
1366         switch(GS(id->name)) {
1367         case ID_MA:
1368                 mtex= &(((Material *)id)->mtex[(int)((Material *)id)->texact]);
1369                 break;
1370         case ID_LA:
1371                 // la->mtex[(int)la->texact] // TODO
1372                 break;
1373         case ID_WO:
1374                 // mtex= wrld->mtex[(int)wrld->texact]; // TODO
1375                 break;
1376         }
1377
1378         if(mtex && *mtex) {
1379                 memcpy(&mtexcopybuf, *mtex, sizeof(MTex));
1380                 mtexcopied= 1;
1381         }
1382         else {
1383                 mtexcopied= 0;
1384         }
1385 }
1386
1387 void paste_mat_mtex_copybuf(ID *id)
1388 {
1389         MTex **mtex= NULL;
1390
1391         if(mtexcopied == 0 || mtexcopybuf.tex==NULL)
1392                 return;
1393
1394         switch(GS(id->name)) {
1395         case ID_MA:
1396                 mtex= &(((Material *)id)->mtex[(int)((Material *)id)->texact]);
1397                 break;
1398         case ID_LA:
1399                 // la->mtex[(int)la->texact] // TODO
1400                 break;
1401         case ID_WO:
1402                 // mtex= wrld->mtex[(int)wrld->texact]; // TODO
1403                 break;
1404         }
1405
1406         if(mtex) {
1407                 if(*mtex==NULL) {
1408                         *mtex= MEM_mallocN(sizeof(MTex), "mtex copy");
1409                 }
1410                 else if((*mtex)->tex) {
1411                         (*mtex)->tex->id.us--;
1412                 }
1413
1414                 memcpy(*mtex, &mtexcopybuf, sizeof(MTex));
1415
1416                 id_us_plus((ID *)mtexcopybuf.tex);
1417         }
1418 }