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