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