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