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