use set as a suffix (matches operators)
[blender-staging.git] / source / blender / blenkernel / intern / material.c
1
2 /*  material.c
3  *
4  * 
5  * $Id$
6  *
7  * ***** BEGIN GPL LICENSE BLOCK *****
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL LICENSE BLOCK *****
31  */
32
33 #include <string.h>
34 #include <math.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_curve_types.h"
39 #include "DNA_material_types.h"
40 #include "DNA_mesh_types.h"
41 #include "DNA_meta_types.h"
42 #include "DNA_node_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_scene_types.h"
45
46 #include "BLI_math.h"           
47
48 #include "BKE_animsys.h"
49 #include "BKE_displist.h"
50 #include "BKE_global.h"
51 #include "BKE_icons.h"
52 #include "BKE_library.h"
53 #include "BKE_main.h"
54 #include "BKE_material.h"
55 #include "BKE_mesh.h"
56 #include "BKE_node.h"
57 #include "BKE_utildefines.h"
58
59 #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 /* same as above but for ID's */
438 Material ***give_matarar_id(ID *id)
439 {
440         switch(GS(id->name)) {
441         case ID_ME:
442                 return &(((Mesh *)id)->mat);
443                 break;
444         case ID_CU:
445                 return &(((Curve *)id)->mat);
446                 break;
447         case ID_MB:
448                 return &(((Curve *)id)->mat);
449                 break;
450         }
451         return NULL;
452 }
453
454 short *give_totcolp_id(ID *id)
455 {
456         switch(GS(id->name)) {
457         case ID_ME:
458                 return &(((Mesh *)id)->totcol);
459                 break;
460         case ID_CU:
461                 return &(((Curve *)id)->totcol);
462                 break;
463         case ID_MB:
464                 return &(((Curve *)id)->totcol);
465                 break;
466         }
467         return NULL;
468 }
469
470 void material_append_id(ID *id, Material *ma)
471 {
472         Material ***matar;
473         if((matar= give_matarar_id(id))) {
474                 short *totcol= give_totcolp_id(id);
475                 Material **mat= MEM_callocN(sizeof(void *) * (*totcol) + 1, "newmatar");
476                 if(*totcol) memcpy(mat, *matar, sizeof(void *) * (*totcol));
477                 if(*matar) MEM_freeN(*matar);
478
479                 *matar= mat;
480                 (*matar)[(*totcol)++]= ma;
481
482                 id_us_plus((ID *)ma);
483                 test_object_materials(id);
484         }
485 }
486
487 Material *material_pop_id(ID *id, int index)
488 {
489         Material *ret= NULL;
490         Material ***matar;
491         if((matar= give_matarar_id(id))) {
492                 short *totcol= give_totcolp_id(id);
493                 if(index >= 0 && index < (*totcol)) {
494                         ret= (*matar)[index];
495                         if(*totcol <= 1) {
496                                 *totcol= 0;
497                                 MEM_freeN(*matar);
498                                 *matar= NULL;
499                         }
500                         else {
501                                 Material **mat;
502
503                                 if(index + 1 != (*totcol))
504                                         memmove((*matar), (*matar) + 1, (*totcol) - (index + 1));
505
506                                 (*totcol)--;
507                                 
508                                 mat= MEM_callocN(sizeof(void *) * (*totcol), "newmatar");
509                                 memcpy(mat, *matar, sizeof(void *) * (*totcol));
510                                 MEM_freeN(*matar);
511
512                                 *matar= mat;
513                                 test_object_materials(id);
514                         }
515                 }
516         }
517         
518         return ret;
519 }
520
521 Material *give_current_material(Object *ob, int act)
522 {
523         Material ***matarar, *ma;
524         short *totcolp;
525         
526         if(ob==NULL) return NULL;
527         
528         /* if object cannot have material, totcolp==NULL */
529         totcolp= give_totcolp(ob);
530         if(totcolp==NULL || ob->totcol==0) return NULL;
531         
532         if(act>ob->totcol) act= ob->totcol;
533         else if(act<=0) act= 1;
534
535         if(ob->matbits && ob->matbits[act-1]) { /* in object */
536                 ma= ob->mat[act-1];
537         }
538         else {                                                          /* in data */
539
540                 /* check for inconsistency */
541                 if(*totcolp < ob->totcol)
542                         ob->totcol= *totcolp;
543                 if(act>ob->totcol) act= ob->totcol;
544
545                 matarar= give_matarar(ob);
546                 
547                 if(matarar && *matarar) ma= (*matarar)[act-1];
548                 else ma= 0;
549                 
550         }
551         
552         return ma;
553 }
554
555 ID *material_from(Object *ob, int act)
556 {
557
558         if(ob==0) return 0;
559
560         if(ob->totcol==0) return ob->data;
561         if(act==0) act= 1;
562
563         if(ob->matbits[act-1]) return (ID *)ob;
564         else return ob->data;
565 }
566
567 Material *give_node_material(Material *ma)
568 {
569         if(ma && ma->use_nodes && ma->nodetree) {
570                 bNode *node= nodeGetActiveID(ma->nodetree, ID_MA);
571
572                 if(node)
573                         return (Material *)node->id;
574         }
575
576         return NULL;
577 }
578
579 /* GS reads the memory pointed at in a specific ordering. There are,
580  * however two definitions for it. I have jotted them down here, both,
581  * but I think the first one is actually used. The thing is that
582  * big-endian systems might read this the wrong way round. OTOH, we
583  * constructed the IDs that are read out with this macro explicitly as
584  * well. I expect we'll sort it out soon... */
585
586 /* from blendef: */
587 #define GS(a)   (*((short *)(a)))
588
589 /* from misc_util: flip the bytes from x  */
590 /*  #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
591
592 void test_object_materials(ID *id)
593 {
594         /* make the ob mat-array same size as 'ob->data' mat-array */
595         Object *ob;
596         Mesh *me;
597         Curve *cu;
598         MetaBall *mb;
599         Material **newmatar;
600         char *newmatbits;
601         int totcol=0;
602
603         if(id==0) return;
604
605         if( GS(id->name)==ID_ME ) {
606                 me= (Mesh *)id;
607                 totcol= me->totcol;
608         }
609         else if( GS(id->name)==ID_CU ) {
610                 cu= (Curve *)id;
611                 totcol= cu->totcol;
612         }
613         else if( GS(id->name)==ID_MB ) {
614                 mb= (MetaBall *)id;
615                 totcol= mb->totcol;
616         }
617         else return;
618
619         ob= G.main->object.first;
620         while(ob) {
621                 
622                 if(ob->data==id) {
623                 
624                         if(totcol==0) {
625                                 if(ob->totcol) {
626                                         MEM_freeN(ob->mat);
627                                         MEM_freeN(ob->matbits);
628                                         ob->mat= NULL;
629                                         ob->matbits= NULL;
630                                 }
631                         }
632                         else if(ob->totcol<totcol) {
633                                 newmatar= MEM_callocN(sizeof(void *)*totcol, "newmatar");
634                                 newmatbits= MEM_callocN(sizeof(char)*totcol, "newmatbits");
635                                 if(ob->totcol) {
636                                         memcpy(newmatar, ob->mat, sizeof(void *)*ob->totcol);
637                                         memcpy(newmatbits, ob->matbits, sizeof(char)*ob->totcol);
638                                         MEM_freeN(ob->mat);
639                                         MEM_freeN(ob->matbits);
640                                 }
641                                 ob->mat= newmatar;
642                                 ob->matbits= newmatbits;
643                         }
644                         ob->totcol= totcol;
645                         if(ob->totcol && ob->actcol==0) ob->actcol= 1;
646                         if(ob->actcol>ob->totcol) ob->actcol= ob->totcol;
647                 }
648                 ob= ob->id.next;
649         }
650 }
651
652
653 void assign_material(Object *ob, Material *ma, int act)
654 {
655         Material *mao, **matar, ***matarar;
656         char *matbits;
657         short *totcolp;
658
659         if(act>MAXMAT) return;
660         if(act<1) act= 1;
661         
662         /* test arraylens */
663         
664         totcolp= give_totcolp(ob);
665         matarar= give_matarar(ob);
666         
667         if(totcolp==0 || matarar==0) return;
668         
669         if(act > *totcolp) {
670                 matar= MEM_callocN(sizeof(void *)*act, "matarray1");
671
672                 if(*totcolp) {
673                         memcpy(matar, *matarar, sizeof(void *)*(*totcolp));
674                         MEM_freeN(*matarar);
675                 }
676
677                 *matarar= matar;
678                 *totcolp= act;
679         }
680         
681         if(act > ob->totcol) {
682                 matar= MEM_callocN(sizeof(void *)*act, "matarray2");
683                 matbits= MEM_callocN(sizeof(char)*act, "matbits1");
684                 if( ob->totcol) {
685                         memcpy(matar, ob->mat, sizeof(void *)*( ob->totcol ));
686                         memcpy(matbits, ob->matbits, sizeof(char)*(*totcolp));
687                         MEM_freeN(ob->mat);
688                         MEM_freeN(ob->matbits);
689                 }
690                 ob->mat= matar;
691                 ob->matbits= matbits;
692                 ob->totcol= act;
693
694                 /* copy object/mesh linking, or assign based on userpref */
695                 if(ob->actcol)
696                         ob->matbits[act-1]= ob->matbits[ob->actcol-1];
697                 else
698                         ob->matbits[act-1]= (U.flag & USER_MAT_ON_OB)? 1: 0;
699         }
700         
701         /* do it */
702
703         if(ob->matbits[act-1]) {        /* in object */
704                 mao= ob->mat[act-1];
705                 if(mao) mao->id.us--;
706                 ob->mat[act-1]= ma;
707         }
708         else {  /* in data */
709                 mao= (*matarar)[act-1];
710                 if(mao) mao->id.us--;
711                 (*matarar)[act-1]= ma;
712         }
713
714         if(ma)
715                 id_us_plus((ID *)ma);
716         test_object_materials(ob->data);
717 }
718
719 /* XXX - this calls many more update calls per object then are needed, could be optimized */
720 void assign_matarar(struct Object *ob, struct Material ***matar, int totcol)
721 {
722         int i, actcol_orig= ob->actcol;
723
724         while(object_remove_material_slot(ob)) {};
725
726         /* now we have the right number of slots */
727         for(i=0; i<totcol; i++)
728                 assign_material(ob, (*matar)[i], i+1);
729
730         if(actcol_orig > ob->totcol)
731                 actcol_orig= ob->totcol;
732
733         ob->actcol= actcol_orig;
734 }
735
736
737 int find_material_index(Object *ob, Material *ma)
738 {
739         Material ***matarar;
740         short a, *totcolp;
741         
742         if(ma==NULL) return 0;
743         
744         totcolp= give_totcolp(ob);
745         matarar= give_matarar(ob);
746         
747         if(totcolp==NULL || matarar==NULL) return 0;
748         
749         for(a=0; a<*totcolp; a++)
750                 if((*matarar)[a]==ma)
751                    break;
752         if(a<*totcolp)
753                 return a+1;
754         return 0;          
755 }
756
757 int object_add_material_slot(Object *ob)
758 {
759         Material *ma;
760         
761         if(ob==0) return FALSE;
762         if(ob->totcol>=MAXMAT) return FALSE;
763         
764         ma= give_current_material(ob, ob->actcol);
765
766         assign_material(ob, ma, ob->totcol+1);
767         ob->actcol= ob->totcol;
768         return TRUE;
769 }
770
771 static void do_init_render_material(Material *ma, int r_mode, float *amb)
772 {
773         MTex *mtex;
774         int a, needuv=0, needtang=0;
775         
776         if(ma->flarec==0) ma->flarec= 1;
777
778         /* add all texcoflags from mtex, texco and mapto were cleared in advance */
779         for(a=0; a<MAX_MTEX; a++) {
780                 
781                 /* separate tex switching */
782                 if(ma->septex & (1<<a)) continue;
783
784                 mtex= ma->mtex[a];
785                 if(mtex && mtex->tex && (mtex->tex->type | (mtex->tex->use_nodes && mtex->tex->nodetree) )) {
786                         
787                         ma->texco |= mtex->texco;
788                         ma->mapto |= mtex->mapto;
789                         if(r_mode & R_OSA) {
790                                 if ELEM3(mtex->tex->type, TEX_IMAGE, TEX_PLUGIN, TEX_ENVMAP) ma->texco |= TEXCO_OSA;
791                                 else if(mtex->texflag & MTEX_NEW_BUMP) ma->texco |= TEXCO_OSA; // NEWBUMP: need texture derivatives for procedurals as well
792                         }
793                         
794                         if(ma->texco & (TEXCO_ORCO|TEXCO_REFL|TEXCO_NORM|TEXCO_STRAND|TEXCO_STRESS)) needuv= 1;
795                         else if(ma->texco & (TEXCO_GLOB|TEXCO_UV|TEXCO_OBJECT|TEXCO_SPEED)) needuv= 1;
796                         else if(ma->texco & (TEXCO_LAVECTOR|TEXCO_VIEW|TEXCO_STICKY)) needuv= 1;
797
798                         if((ma->mapto & MAP_NORM) && (mtex->normapspace == MTEX_NSPACE_TANGENT))
799                                 needtang= 1;
800                 }
801         }
802
803         if(needtang) ma->mode |= MA_NORMAP_TANG;
804         else ma->mode &= ~MA_NORMAP_TANG;
805         
806         if(ma->mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)) {
807                 needuv= 1;
808                 if(r_mode & R_OSA) ma->texco |= TEXCO_OSA;              /* for texfaces */
809         }
810         if(needuv) ma->texco |= NEED_UV;
811         
812         /* since the raytracer doesnt recalc O structs for each ray, we have to preset them all */
813         if(r_mode & R_RAYTRACE) {
814                 if((ma->mode & (MA_RAYMIRROR|MA_SHADOW_TRA)) || ((ma->mode & MA_TRANSP) && (ma->mode & MA_RAYTRANSP))) {
815                         ma->texco |= NEED_UV|TEXCO_ORCO|TEXCO_REFL|TEXCO_NORM;
816                         if(r_mode & R_OSA) ma->texco |= TEXCO_OSA;
817                 }
818         }
819         
820         if(amb) {
821                 ma->ambr= ma->amb*amb[0];
822                 ma->ambg= ma->amb*amb[1];
823                 ma->ambb= ma->amb*amb[2];
824         }       
825         /* will become or-ed result of all node modes */
826         ma->mode_l= ma->mode;
827         ma->mode_l &= ~MA_SHLESS;
828
829         if(ma->strand_surfnor > 0.0f)
830                 ma->mode_l |= MA_STR_SURFDIFF;
831 }
832
833 static void init_render_nodetree(bNodeTree *ntree, Material *basemat, int r_mode, float *amb)
834 {
835         bNode *node;
836         
837         for(node=ntree->nodes.first; node; node= node->next) {
838                 if(node->id) {
839                         if(GS(node->id->name)==ID_MA) {
840                                 Material *ma= (Material *)node->id;
841                                 if(ma!=basemat) {
842                                         do_init_render_material(ma, r_mode, amb);
843                                         basemat->texco |= ma->texco;
844                                         basemat->mode_l |= ma->mode_l;
845                                 }
846                         }
847                         else if(node->type==NODE_GROUP)
848                                 init_render_nodetree((bNodeTree *)node->id, basemat, r_mode, amb);
849                 }
850         }
851         /* parses the geom+tex nodes */
852         ntreeShaderGetTexcoMode(ntree, r_mode, &basemat->texco, &basemat->mode_l);
853 }
854
855 void init_render_material(Material *mat, int r_mode, float *amb)
856 {
857         
858         do_init_render_material(mat, r_mode, amb);
859         
860         if(mat->nodetree && mat->use_nodes) {
861                 init_render_nodetree(mat->nodetree, mat, r_mode, amb);
862                 
863                 ntreeBeginExecTree(mat->nodetree); /* has internal flag to detect it only does it once */
864         }
865 }
866
867 void init_render_materials(Main *bmain, int r_mode, float *amb)
868 {
869         Material *ma;
870         
871         /* clear these flags before going over materials, to make sure they
872          * are cleared only once, otherwise node materials contained in other
873          * node materials can go wrong */
874         for(ma= bmain->mat.first; ma; ma= ma->id.next) {
875                 if(ma->id.us) {
876                         ma->texco= 0;
877                         ma->mapto= 0;
878                 }
879         }
880
881         /* two steps, first initialize, then or the flags for layers */
882         for(ma= bmain->mat.first; ma; ma= ma->id.next) {
883                 /* is_used flag comes back in convertblender.c */
884                 ma->flag &= ~MA_IS_USED;
885                 if(ma->id.us) 
886                         init_render_material(ma, r_mode, amb);
887         }
888         
889         do_init_render_material(&defmaterial, r_mode, amb);
890 }
891
892 /* only needed for nodes now */
893 void end_render_material(Material *mat)
894 {
895         if(mat && mat->nodetree && mat->use_nodes)
896                 ntreeEndExecTree(mat->nodetree); /* has internal flag to detect it only does it once */
897 }
898
899 void end_render_materials(Main *bmain)
900 {
901         Material *ma;
902         for(ma= bmain->mat.first; ma; ma= ma->id.next)
903                 if(ma->id.us) 
904                         end_render_material(ma);
905 }
906
907 static int material_in_nodetree(bNodeTree *ntree, Material *mat)
908 {
909         bNode *node;
910
911         for(node=ntree->nodes.first; node; node= node->next) {
912                 if(node->id && GS(node->id->name)==ID_MA) {
913                         if(node->id==(ID*)mat)
914                                 return 1;
915                 }
916                 else if(node->type==NODE_GROUP)
917                         if(material_in_nodetree((bNodeTree*)node->id, mat))
918                                 return 1;
919         }
920
921         return 0;
922 }
923
924 int material_in_material(Material *parmat, Material *mat)
925 {
926         if(parmat==mat)
927                 return 1;
928         else if(parmat->nodetree && parmat->use_nodes)
929                 return material_in_nodetree(parmat->nodetree, mat);
930         else
931                 return 0;
932 }
933         
934 /* ****************** */
935
936 char colname_array[125][20]= {
937 "Black","DarkRed","HalfRed","Red","Red",
938 "DarkGreen","DarkOlive","Brown","Chocolate","OrangeRed",
939 "HalfGreen","GreenOlive","DryOlive","Goldenrod","DarkOrange",
940 "LightGreen","Chartreuse","YellowGreen","Yellow","Gold",
941 "Green","LawnGreen","GreenYellow","LightOlive","Yellow",
942 "DarkBlue","DarkPurple","HotPink","VioletPink","RedPink",
943 "SlateGray","DarkGrey","PalePurple","IndianRed","Tomato",
944 "SeaGreen","PaleGreen","GreenKhaki","LightBrown","LightSalmon",
945 "SpringGreen","PaleGreen","MediumOlive","YellowBrown","LightGold",
946 "LightGreen","LightGreen","LightGreen","GreenYellow","PaleYellow",
947 "HalfBlue","DarkSky","HalfMagenta","VioletRed","DeepPink",
948 "SteelBlue","SkyBlue","Orchid","LightHotPink","HotPink",
949 "SeaGreen","SlateGray","MediumGrey","Burlywood","LightPink",
950 "SpringGreen","Aquamarine","PaleGreen","Khaki","PaleOrange",
951 "SpringGreen","SeaGreen","PaleGreen","PaleWhite","YellowWhite",
952 "LightBlue","Purple","MediumOrchid","Magenta","Magenta",
953 "RoyalBlue","SlateBlue","MediumOrchid","Orchid","Magenta",
954 "DeepSkyBlue","LightSteelBlue","LightSkyBlue","Violet","LightPink",
955 "Cyan","DarkTurquoise","SkyBlue","Grey","Snow",
956 "Mint","Mint","Aquamarine","MintCream","Ivory",
957 "Blue","Blue","DarkMagenta","DarkOrchid","Magenta",
958 "SkyBlue","RoyalBlue","LightSlateBlue","MediumOrchid","Magenta",
959 "DodgerBlue","SteelBlue","MediumPurple","PalePurple","Plum",
960 "DeepSkyBlue","PaleBlue","LightSkyBlue","PalePurple","Thistle",
961 "Cyan","ColdBlue","PaleTurquoise","GhostWhite","White"
962 };
963
964 void automatname(Material *ma)
965 {
966         int nr, r, g, b;
967         float ref;
968         
969         if(ma==0) return;
970         if(ma->mode & MA_SHLESS) ref= 1.0;
971         else ref= ma->ref;
972         
973         r= (int)(4.99*(ref*ma->r));
974         g= (int)(4.99*(ref*ma->g));
975         b= (int)(4.99*(ref*ma->b));
976         nr= r + 5*g + 25*b;
977         if(nr>124) nr= 124;
978         new_id(&G.main->mat, (ID *)ma, colname_array[nr]);
979         
980 }
981
982
983 int object_remove_material_slot(Object *ob)
984 {
985         Material *mao, ***matarar;
986         Object *obt;
987         Curve *cu;
988         Nurb *nu;
989         short *totcolp;
990         int a, actcol;
991         
992         if(ob==NULL || ob->totcol==0) return FALSE;
993         
994         /* take a mesh/curve/mball as starting point, remove 1 index,
995          * AND with all objects that share the ob->data
996          * 
997          * after that check indices in mesh/curve/mball!!!
998          */
999         
1000         totcolp= give_totcolp(ob);
1001         matarar= give_matarar(ob);
1002
1003         if(*matarar==NULL) return FALSE;
1004
1005         /* we delete the actcol */
1006         if(ob->totcol) {
1007                 mao= (*matarar)[ob->actcol-1];
1008                 if(mao) mao->id.us--;
1009         }
1010         
1011         for(a=ob->actcol; a<ob->totcol; a++)
1012                 (*matarar)[a-1]= (*matarar)[a];
1013         (*totcolp)--;
1014         
1015         if(*totcolp==0) {
1016                 MEM_freeN(*matarar);
1017                 *matarar= 0;
1018         }
1019         
1020         actcol= ob->actcol;
1021         obt= G.main->object.first;
1022         while(obt) {
1023         
1024                 if(obt->data==ob->data) {
1025                         
1026                         /* WATCH IT: do not use actcol from ob or from obt (can become zero) */
1027                         mao= obt->mat[actcol-1];
1028                         if(mao) mao->id.us--;
1029                 
1030                         for(a=actcol; a<obt->totcol; a++) {
1031                                 obt->mat[a-1]= obt->mat[a];
1032                                 obt->matbits[a-1]= obt->matbits[a];
1033                         }
1034                         obt->totcol--;
1035                         if(obt->actcol > obt->totcol) obt->actcol= obt->totcol;
1036                         
1037                         if(obt->totcol==0) {
1038                                 MEM_freeN(obt->mat);
1039                                 MEM_freeN(obt->matbits);
1040                                 obt->mat= 0;
1041                                 obt->matbits= NULL;
1042                         }
1043                 }
1044                 obt= obt->id.next;
1045         }
1046
1047         /* check indices from mesh */
1048
1049         if(ob->type==OB_MESH) {
1050                 Mesh *me= get_mesh(ob);
1051                 mesh_delete_material_index(me, actcol-1);
1052                 freedisplist(&ob->disp);
1053         }
1054         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
1055                 cu= ob->data;
1056                 nu= cu->nurb.first;
1057                 
1058                 while(nu) {
1059                         if(nu->mat_nr && nu->mat_nr>=actcol-1) {
1060                                 nu->mat_nr--;
1061                                 if (ob->type == OB_CURVE) nu->charidx--;
1062                         }
1063                         nu= nu->next;
1064                 }
1065                 freedisplist(&ob->disp);
1066         }
1067
1068         return TRUE;
1069 }
1070
1071
1072 /* r g b = current value, col = new value, fac==0 is no change */
1073 /* if g==NULL, it only does r channel */
1074 void ramp_blend(int type, float *r, float *g, float *b, float fac, float *col)
1075 {
1076         float tmp, facm= 1.0f-fac;
1077         
1078         switch (type) {
1079                 case MA_RAMP_BLEND:
1080                         *r = facm*(*r) + fac*col[0];
1081                         if(g) {
1082                                 *g = facm*(*g) + fac*col[1];
1083                                 *b = facm*(*b) + fac*col[2];
1084                         }
1085                                 break;
1086                 case MA_RAMP_ADD:
1087                         *r += fac*col[0];
1088                         if(g) {
1089                                 *g += fac*col[1];
1090                                 *b += fac*col[2];
1091                         }
1092                                 break;
1093                 case MA_RAMP_MULT:
1094                         *r *= (facm + fac*col[0]);
1095                         if(g) {
1096                                 *g *= (facm + fac*col[1]);
1097                                 *b *= (facm + fac*col[2]);
1098                         }
1099                                 break;
1100                 case MA_RAMP_SCREEN:
1101                         *r = 1.0f - (facm + fac*(1.0f - col[0])) * (1.0f - *r);
1102                         if(g) {
1103                                 *g = 1.0f - (facm + fac*(1.0f - col[1])) * (1.0f - *g);
1104                                 *b = 1.0f - (facm + fac*(1.0f - col[2])) * (1.0f - *b);
1105                         }
1106                                 break;
1107                 case MA_RAMP_OVERLAY:
1108                         if(*r < 0.5f)
1109                                 *r *= (facm + 2.0f*fac*col[0]);
1110                         else
1111                                 *r = 1.0f - (facm + 2.0f*fac*(1.0f - col[0])) * (1.0f - *r);
1112                         if(g) {
1113                                 if(*g < 0.5f)
1114                                         *g *= (facm + 2.0f*fac*col[1]);
1115                                 else
1116                                         *g = 1.0f - (facm + 2.0f*fac*(1.0f - col[1])) * (1.0f - *g);
1117                                 if(*b < 0.5f)
1118                                         *b *= (facm + 2.0f*fac*col[2]);
1119                                 else
1120                                         *b = 1.0f - (facm + 2.0f*fac*(1.0f - col[2])) * (1.0f - *b);
1121                         }
1122                                 break;
1123                 case MA_RAMP_SUB:
1124                         *r -= fac*col[0];
1125                         if(g) {
1126                                 *g -= fac*col[1];
1127                                 *b -= fac*col[2];
1128                         }
1129                                 break;
1130                 case MA_RAMP_DIV:
1131                         if(col[0]!=0.0f)
1132                                 *r = facm*(*r) + fac*(*r)/col[0];
1133                         if(g) {
1134                                 if(col[1]!=0.0f)
1135                                         *g = facm*(*g) + fac*(*g)/col[1];
1136                                 if(col[2]!=0.0f)
1137                                         *b = facm*(*b) + fac*(*b)/col[2];
1138                         }
1139                                 break;
1140                 case MA_RAMP_DIFF:
1141                         *r = facm*(*r) + fac*fabs(*r-col[0]);
1142                         if(g) {
1143                                 *g = facm*(*g) + fac*fabs(*g-col[1]);
1144                                 *b = facm*(*b) + fac*fabs(*b-col[2]);
1145                         }
1146                                 break;
1147                 case MA_RAMP_DARK:
1148                         tmp=col[0]+((1-col[0])*facm); 
1149                         if(tmp < *r) *r= tmp; 
1150                         if(g) { 
1151                                 tmp=col[1]+((1-col[1])*facm); 
1152                                 if(tmp < *g) *g= tmp; 
1153                                 tmp=col[2]+((1-col[2])*facm); 
1154                                 if(tmp < *b) *b= tmp; 
1155                         } 
1156                                 break; 
1157                 case MA_RAMP_LIGHT:
1158                         tmp= fac*col[0];
1159                         if(tmp > *r) *r= tmp; 
1160                                 if(g) {
1161                                         tmp= fac*col[1];
1162                                         if(tmp > *g) *g= tmp; 
1163                                         tmp= fac*col[2];
1164                                         if(tmp > *b) *b= tmp; 
1165                                 }
1166                                         break;  
1167                 case MA_RAMP_DODGE:                     
1168                         
1169                                 
1170                         if(*r !=0.0f){
1171                                 tmp = 1.0f - fac*col[0];
1172                                 if(tmp <= 0.0f)
1173                                         *r = 1.0f;
1174                                 else if ((tmp = (*r) / tmp)> 1.0f)
1175                                         *r = 1.0f;
1176                                 else 
1177                                         *r = tmp;
1178                         }
1179                         if(g) {
1180                                 if(*g !=0.0f){
1181                                         tmp = 1.0f - fac*col[1];
1182                                         if(tmp <= 0.0f )
1183                                                 *g = 1.0f;
1184                                         else if ((tmp = (*g) / tmp) > 1.0f )
1185                                                 *g = 1.0f;
1186                                         else
1187                                                 *g = tmp;
1188                                 }
1189                                 if(*b !=0.0f){
1190                                         tmp = 1.0f - fac*col[2];
1191                                         if(tmp <= 0.0f)
1192                                                 *b = 1.0f;
1193                                         else if ((tmp = (*b) / tmp) > 1.0f )
1194                                                 *b = 1.0f;
1195                                         else
1196                                                 *b = tmp;
1197                                 }
1198
1199                         }
1200                                 break;  
1201                 case MA_RAMP_BURN:
1202                         
1203                         tmp = facm + fac*col[0];
1204                         
1205                         if(tmp <= 0.0f)
1206                                 *r = 0.0f;
1207                         else if (( tmp = (1.0f - (1.0f - (*r)) / tmp )) < 0.0f)
1208                                         *r = 0.0f;
1209                         else if (tmp > 1.0f)
1210                                 *r=1.0f;
1211                         else 
1212                                 *r = tmp; 
1213
1214                         if(g) {
1215                                 tmp = facm + fac*col[1];
1216                                 if(tmp <= 0.0f)
1217                                         *g = 0.0f;
1218                                 else if (( tmp = (1.0f - (1.0f - (*g)) / tmp )) < 0.0f )
1219                                                 *g = 0.0f;
1220                                 else if(tmp >1.0f)
1221                                         *g=1.0f;
1222                                 else
1223                                         *g = tmp;
1224                                         
1225                                         tmp = facm + fac*col[2];
1226                                         if(tmp <= 0.0f)
1227                                         *b = 0.0f;
1228                                 else if (( tmp = (1.0f - (1.0f - (*b)) / tmp )) < 0.0f  )
1229                                                 *b = 0.0f;
1230                                 else if(tmp >1.0f)
1231                                         *b= 1.0f;
1232                                 else
1233                                         *b = tmp;
1234                         }
1235                                 break;
1236                 case MA_RAMP_HUE:               
1237                         if(g){
1238                                 float rH,rS,rV;
1239                                 float colH,colS,colV; 
1240                                 float tmpr,tmpg,tmpb;
1241                                 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1242                                 if(colS!=0 ){
1243                                         rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1244                                         hsv_to_rgb( colH , rS, rV, &tmpr, &tmpg, &tmpb);
1245                                         *r = facm*(*r) + fac*tmpr;  
1246                                         *g = facm*(*g) + fac*tmpg; 
1247                                         *b = facm*(*b) + fac*tmpb;
1248                                 }
1249                         }
1250                                 break;
1251                 case MA_RAMP_SAT:               
1252                         if(g){
1253                                 float rH,rS,rV;
1254                                 float colH,colS,colV;
1255                                 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1256                                 if(rS!=0){
1257                                         rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1258                                         hsv_to_rgb( rH, (facm*rS +fac*colS), rV, r, g, b);
1259                                 }
1260                         }
1261                                 break;
1262                 case MA_RAMP_VAL:               
1263                         if(g){
1264                                 float rH,rS,rV;
1265                                 float colH,colS,colV;
1266                                 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1267                                 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1268                                 hsv_to_rgb( rH, rS, (facm*rV +fac*colV), r, g, b);
1269                         }
1270                                 break;
1271                 case MA_RAMP_COLOR:             
1272                         if(g){
1273                                 float rH,rS,rV;
1274                                 float colH,colS,colV;
1275                                 float tmpr,tmpg,tmpb;
1276                                 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1277                                 if(colS!=0){
1278                                         rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1279                                         hsv_to_rgb( colH, colS, rV, &tmpr, &tmpg, &tmpb);
1280                                         *r = facm*(*r) + fac*tmpr;
1281                                         *g = facm*(*g) + fac*tmpg;
1282                                         *b = facm*(*b) + fac*tmpb;
1283                                 }
1284                         }
1285                                 break;
1286                 case MA_RAMP_SOFT: 
1287                         if (g){ 
1288                                 float scr, scg, scb; 
1289                  
1290                                 /* first calculate non-fac based Screen mix */ 
1291                                 scr = 1.0f - (1.0f - col[0]) * (1.0f - *r); 
1292                                 scg = 1.0f - (1.0f - col[1]) * (1.0f - *g); 
1293                                 scb = 1.0f - (1.0f - col[2]) * (1.0f - *b); 
1294                  
1295                                 *r = facm*(*r) + fac*(((1.0f - *r) * col[0] * (*r)) + (*r * scr)); 
1296                                 *g = facm*(*g) + fac*(((1.0f - *g) * col[1] * (*g)) + (*g * scg)); 
1297                                 *b = facm*(*b) + fac*(((1.0f - *b) * col[2] * (*b)) + (*b * scb)); 
1298                         } 
1299                                 break; 
1300                 case MA_RAMP_LINEAR: 
1301                         if (col[0] > 0.5f)  
1302                                 *r = *r + fac*(2.0f*(col[0]-0.5f)); 
1303                         else  
1304                                 *r = *r + fac*(2.0f*(col[0]) - 1.0f); 
1305                         if (g){ 
1306                                 if (col[1] > 0.5f)  
1307                                         *g = *g + fac*(2.0f*(col[1]-0.5f)); 
1308                                 else  
1309                                         *g = *g + fac*(2.0f*(col[1]) -1.0f); 
1310                                 if (col[2] > 0.5f)  
1311                                         *b = *b + fac*(2.0f*(col[2]-0.5f)); 
1312                                 else  
1313                                         *b = *b + fac*(2.0f*(col[2]) - 1.0f); 
1314                         } 
1315                                 break; 
1316         }       
1317 }
1318
1319 /* copy/paste buffer, if we had a propper py api that would be better */
1320 Material matcopybuf;
1321 // MTex mtexcopybuf;
1322 static short matcopied=0;
1323
1324 void clear_matcopybuf(void)
1325 {
1326         memset(&matcopybuf, 0, sizeof(Material));
1327         matcopied= 0;
1328 }
1329
1330 void free_matcopybuf(void)
1331 {
1332 //      extern MTex mtexcopybuf;        /* buttons.c */
1333         int a;
1334
1335         for(a=0; a<MAX_MTEX; a++) {
1336                 if(matcopybuf.mtex[a]) {
1337                         MEM_freeN(matcopybuf.mtex[a]);
1338                         matcopybuf.mtex[a]= NULL;
1339                 }
1340         }
1341
1342         if(matcopybuf.ramp_col) MEM_freeN(matcopybuf.ramp_col);
1343         if(matcopybuf.ramp_spec) MEM_freeN(matcopybuf.ramp_spec);
1344
1345         matcopybuf.ramp_col= NULL;
1346         matcopybuf.ramp_spec= NULL;
1347
1348         if(matcopybuf.nodetree) {
1349                 ntreeFreeTree(matcopybuf.nodetree);
1350                 MEM_freeN(matcopybuf.nodetree);
1351                 matcopybuf.nodetree= NULL;
1352         }
1353 //      default_mtex(&mtexcopybuf);
1354
1355         matcopied= 0;
1356 }
1357
1358 void copy_matcopybuf(Material *ma)
1359 {
1360         int a;
1361         MTex *mtex;
1362
1363         if(matcopied)
1364                 free_matcopybuf();
1365
1366         memcpy(&matcopybuf, ma, sizeof(Material));
1367         if(matcopybuf.ramp_col) matcopybuf.ramp_col= MEM_dupallocN(matcopybuf.ramp_col);
1368         if(matcopybuf.ramp_spec) matcopybuf.ramp_spec= MEM_dupallocN(matcopybuf.ramp_spec);
1369
1370         for(a=0; a<MAX_MTEX; a++) {
1371                 mtex= matcopybuf.mtex[a];
1372                 if(mtex) {
1373                         matcopybuf.mtex[a]= MEM_dupallocN(mtex);
1374                 }
1375         }
1376         matcopybuf.nodetree= ntreeCopyTree(ma->nodetree, 0);
1377         matcopybuf.preview= NULL;
1378         matcopybuf.gpumaterial.first= matcopybuf.gpumaterial.last= NULL;
1379         matcopied= 1;
1380 }
1381
1382 void paste_matcopybuf(Material *ma)
1383 {
1384         int a;
1385         MTex *mtex;
1386         ID id;
1387
1388         if(matcopied==0)
1389                 return;
1390         /* free current mat */
1391         if(ma->ramp_col) MEM_freeN(ma->ramp_col);
1392         if(ma->ramp_spec) MEM_freeN(ma->ramp_spec);
1393         for(a=0; a<MAX_MTEX; a++) {
1394                 mtex= ma->mtex[a];
1395                 if(mtex && mtex->tex) mtex->tex->id.us--;
1396                 if(mtex) MEM_freeN(mtex);
1397         }
1398
1399         if(ma->nodetree) {
1400                 ntreeFreeTree(ma->nodetree);
1401                 MEM_freeN(ma->nodetree);
1402         }
1403
1404         GPU_materials_free(ma);
1405
1406         id= (ma->id);
1407         memcpy(ma, &matcopybuf, sizeof(Material));
1408         (ma->id)= id;
1409
1410         if(matcopybuf.ramp_col) ma->ramp_col= MEM_dupallocN(matcopybuf.ramp_col);
1411         if(matcopybuf.ramp_spec) ma->ramp_spec= MEM_dupallocN(matcopybuf.ramp_spec);
1412
1413         for(a=0; a<MAX_MTEX; a++) {
1414                 mtex= ma->mtex[a];
1415                 if(mtex) {
1416                         ma->mtex[a]= MEM_dupallocN(mtex);
1417                         if(mtex->tex) id_us_plus((ID *)mtex->tex);
1418                 }
1419         }
1420
1421         ma->nodetree= ntreeCopyTree(matcopybuf.nodetree, 0);
1422
1423         /*
1424         BIF_preview_changed(ID_MA);
1425         BIF_undo_push("Paste material settings");
1426         scrarea_queue_winredraw(curarea);
1427         */
1428 }