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