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