7 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
23 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24 * All rights reserved.
26 * The Original Code is: all of this file.
28 * Contributor(s): none yet.
30 * ***** END GPL LICENSE BLOCK *****
33 /** \file blender/blenkernel/intern/material.c
42 #include "MEM_guardedalloc.h"
44 #include "DNA_curve_types.h"
45 #include "DNA_material_types.h"
46 #include "DNA_mesh_types.h"
47 #include "DNA_meshdata_types.h"
48 #include "DNA_customdata_types.h"
50 #include "DNA_meta_types.h"
51 #include "DNA_node_types.h"
52 #include "DNA_object_types.h"
53 #include "DNA_scene_types.h"
56 #include "BLI_listbase.h"
57 #include "BLI_utildefines.h"
59 #include "BKE_animsys.h"
60 #include "BKE_displist.h"
61 #include "BKE_global.h"
62 #include "BKE_icons.h"
63 #include "BKE_image.h"
64 #include "BKE_library.h"
66 #include "BKE_material.h"
69 #include "BKE_curve.h"
71 #include "GPU_material.h"
73 /* used in UI and render */
76 /* called on startup, creator.c */
77 void init_def_material(void)
79 init_material(&defmaterial);
82 /* not material itself */
83 void free_material(Material *ma)
88 for(a=0; a<MAX_MTEX; a++) {
90 if(mtex && mtex->tex) mtex->tex->id.us--;
91 if(mtex) MEM_freeN(mtex);
94 if(ma->ramp_col) MEM_freeN(ma->ramp_col);
95 if(ma->ramp_spec) MEM_freeN(ma->ramp_spec);
97 BKE_free_animdata((ID *)ma);
100 BKE_previewimg_free(&ma->preview);
101 BKE_icon_delete((struct ID*)ma);
104 /* is no lib link block, but material extension */
106 ntreeFreeTree(ma->nodetree);
107 MEM_freeN(ma->nodetree);
110 if(ma->gpumaterial.first)
111 GPU_material_free(ma);
114 void init_material(Material *ma)
116 ma->r= ma->g= ma->b= ma->ref= 0.8;
117 ma->specr= ma->specg= ma->specb= 1.0;
118 ma->mirr= ma->mirg= ma->mirb= 1.0;
122 ma->spec= ma->hasize= 0.5;
124 ma->starc= ma->ringc= 4;
127 ma->flaresize= ma->subsize= 1.0;
140 ma->strand_sta= ma->strand_end= 1.0f;
144 ma->ray_depth_tra= 2;
145 ma->fresnel_mir= 0.0;
146 ma->fresnel_tra= 0.0;
147 ma->fresnel_tra_i= 1.25;
148 ma->fresnel_mir_i= 1.25;
151 ma->shad_alpha= 1.0f;
153 ma->gloss_mir = ma->gloss_tra= 1.0;
154 ma->samp_gloss_mir = ma->samp_gloss_tra= 18;
155 ma->adapt_thresh_mir = ma->adapt_thresh_tra = 0.005;
157 ma->fadeto_mir = MA_RAYMIR_FADETOSKY;
159 ma->rampfac_col= 1.0;
160 ma->rampfac_spec= 1.0;
161 ma->pr_lamp= 3; /* two lamps, is bits */
162 ma->pr_type= MA_SPHERE;
164 ma->sss_radius[0]= 1.0f;
165 ma->sss_radius[1]= 1.0f;
166 ma->sss_radius[2]= 1.0f;
167 ma->sss_col[0]= 1.0f;
168 ma->sss_col[1]= 1.0f;
169 ma->sss_col[2]= 1.0f;
170 ma->sss_error= 0.05f;
173 ma->sss_colfac= 1.0f;
174 ma->sss_texfac= 0.0f;
178 ma->vol.density = 1.0f;
179 ma->vol.emission = 0.0f;
180 ma->vol.scattering = 1.0f;
181 ma->vol.reflection = 1.0f;
182 ma->vol.transmission_col[0] = ma->vol.transmission_col[1] = ma->vol.transmission_col[2] = 1.0f;
183 ma->vol.reflection_col[0] = ma->vol.reflection_col[1] = ma->vol.reflection_col[2] = 1.0f;
184 ma->vol.emission_col[0] = ma->vol.emission_col[1] = ma->vol.emission_col[2] = 1.0f;
185 ma->vol.density_scale = 1.0f;
186 ma->vol.depth_cutoff = 0.01f;
187 ma->vol.stepsize_type = MA_VOL_STEP_RANDOMIZED;
188 ma->vol.stepsize = 0.2f;
189 ma->vol.shade_type = MA_VOL_SHADE_SHADED;
190 ma->vol.shadeflag |= MA_VOL_PRECACHESHADING;
191 ma->vol.precache_resolution = 50;
192 ma->vol.ms_spread = 0.2f;
193 ma->vol.ms_diff = 1.f;
194 ma->vol.ms_intensity = 1.f;
197 ma->game.alpha_blend=0;
198 ma->game.face_orientation=0;
200 ma->mode= MA_TRACEBLE|MA_SHADBUF|MA_SHADOW|MA_RAYBIAS|MA_TANGENT_STR|MA_ZTRANSP;
201 ma->shade_flag= MA_APPROX_OCCLUSION;
205 Material *add_material(const char *name)
209 ma= alloc_libblock(&G.main->mat, ID_MA, name);
216 /* XXX keep synced with next function */
217 Material *copy_material(Material *ma)
222 man= copy_libblock(ma);
224 id_lib_extern((ID *)man->group);
226 for(a=0; a<MAX_MTEX; a++) {
228 man->mtex[a]= MEM_mallocN(sizeof(MTex), "copymaterial");
229 memcpy(man->mtex[a], ma->mtex[a], sizeof(MTex));
230 id_us_plus((ID *)man->mtex[a]->tex);
234 if(ma->ramp_col) man->ramp_col= MEM_dupallocN(ma->ramp_col);
235 if(ma->ramp_spec) man->ramp_spec= MEM_dupallocN(ma->ramp_spec);
237 if (ma->preview) man->preview = BKE_previewimg_copy(ma->preview);
240 man->nodetree= ntreeCopyTree(ma->nodetree); /* 0 == full new tree */
243 man->gpumaterial.first= man->gpumaterial.last= NULL;
248 /* XXX (see above) material copy without adding to main dbase */
249 Material *localize_material(Material *ma)
254 man= copy_libblock(ma);
255 BLI_remlink(&G.main->mat, man);
257 /* no increment for texture ID users, in previewrender.c it prevents decrement */
258 for(a=0; a<MAX_MTEX; a++) {
260 man->mtex[a]= MEM_mallocN(sizeof(MTex), "copymaterial");
261 memcpy(man->mtex[a], ma->mtex[a], sizeof(MTex));
265 if(ma->ramp_col) man->ramp_col= MEM_dupallocN(ma->ramp_col);
266 if(ma->ramp_spec) man->ramp_spec= MEM_dupallocN(ma->ramp_spec);
271 man->nodetree= ntreeLocalize(ma->nodetree);
273 man->gpumaterial.first= man->gpumaterial.last= NULL;
278 static void extern_local_material(Material *ma)
281 for(i=0; i < MAX_MTEX; i++) {
282 if(ma->mtex[i]) id_lib_extern((ID *)ma->mtex[i]->tex);
286 void make_local_material(Material *ma)
294 int a, local=0, lib=0;
296 /* - only lib users: do nothing
297 * - only local users: set flag
301 if(ma->id.lib==NULL) return;
304 ma->id.flag= LIB_LOCAL;
306 new_id(&bmain->mat, (ID *)ma, NULL);
307 extern_local_material(ma);
312 ob= bmain->object.first;
315 for(a=0; a<ob->totcol; a++) {
317 if(ob->id.lib) lib= 1;
325 me= bmain->mesh.first;
328 for(a=0; a<me->totcol; a++) {
330 if(me->id.lib) lib= 1;
338 cu= bmain->curve.first;
341 for(a=0; a<cu->totcol; a++) {
343 if(cu->id.lib) lib= 1;
351 mb= bmain->mball.first;
354 for(a=0; a<mb->totcol; a++) {
356 if(mb->id.lib) lib= 1;
364 if(local && lib==0) {
366 ma->id.flag= LIB_LOCAL;
368 new_id(&bmain->mat, (ID *)ma, NULL);
369 extern_local_material(ma);
371 else if(local && lib) {
373 man= copy_material(ma);
377 ob= bmain->object.first;
380 for(a=0; a<ob->totcol; a++) {
382 if(ob->id.lib==NULL) {
393 me= bmain->mesh.first;
396 for(a=0; a<me->totcol; a++) {
398 if(me->id.lib==NULL) {
409 cu= bmain->curve.first;
412 for(a=0; a<cu->totcol; a++) {
414 if(cu->id.lib==NULL) {
425 mb= bmain->mball.first;
428 for(a=0; a<mb->totcol; a++) {
430 if(mb->id.lib==NULL) {
443 /* for curve, mball, mesh types */
444 void extern_local_matarar(struct Material **matar, short totcol)
447 for(i= 0; i < totcol; i++) {
448 id_lib_extern((ID *)matar[i]);
452 Material ***give_matarar(Object *ob)
458 if(ob->type==OB_MESH) {
462 else if ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF) {
466 else if(ob->type==OB_MBALL) {
473 short *give_totcolp(Object *ob)
479 if(ob->type==OB_MESH) {
481 return &(me->totcol);
483 else if ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF) {
485 return &(cu->totcol);
487 else if(ob->type==OB_MBALL) {
489 return &(mb->totcol);
494 /* same as above but for ID's */
495 Material ***give_matarar_id(ID *id)
497 switch(GS(id->name)) {
499 return &(((Mesh *)id)->mat);
502 return &(((Curve *)id)->mat);
505 return &(((MetaBall *)id)->mat);
511 short *give_totcolp_id(ID *id)
513 switch(GS(id->name)) {
515 return &(((Mesh *)id)->totcol);
518 return &(((Curve *)id)->totcol);
521 return &(((MetaBall *)id)->totcol);
527 static void data_delete_material_index_id(ID *id, int index)
529 switch(GS(id->name)) {
531 mesh_delete_material_index((Mesh *)id, index);
534 curve_delete_material_index((Curve *)id, index);
537 /* meta-elems dont have materials atm */
542 void material_append_id(ID *id, Material *ma)
545 if((matar= give_matarar_id(id))) {
546 short *totcol= give_totcolp_id(id);
547 Material **mat= MEM_callocN(sizeof(void *) * ((*totcol) + 1), "newmatar");
548 if(*totcol) memcpy(mat, *matar, sizeof(void *) * (*totcol));
549 if(*matar) MEM_freeN(*matar);
552 (*matar)[(*totcol)++]= ma;
554 id_us_plus((ID *)ma);
555 test_object_materials(id);
559 Material *material_pop_id(ID *id, int index, int remove_material_slot)
563 if((matar= give_matarar_id(id))) {
564 short *totcol= give_totcolp_id(id);
565 if(index >= 0 && index < (*totcol)) {
566 ret= (*matar)[index];
567 id_us_min((ID *)ret);
569 if (remove_material_slot) {
577 if(index + 1 != (*totcol))
578 memmove((*matar)+index, (*matar)+(index+1), sizeof(void *) * ((*totcol) - (index + 1)));
582 mat= MEM_callocN(sizeof(void *) * (*totcol), "newmatar");
583 memcpy(mat, *matar, sizeof(void *) * (*totcol));
587 test_object_materials(id);
590 /* decrease mat_nr index */
591 data_delete_material_index_id(id, index);
594 /* don't remove material slot, only clear it*/
596 (*matar)[index]= NULL;
603 Material *give_current_material(Object *ob, int act)
605 Material ***matarar, *ma;
608 if(ob==NULL) return NULL;
610 /* if object cannot have material, totcolp==NULL */
611 totcolp= give_totcolp(ob);
612 if(totcolp==NULL || ob->totcol==0) return NULL;
618 if(act>ob->totcol) act= ob->totcol;
619 else if(act<=0) act= 1;
621 if(ob->matbits && ob->matbits[act-1]) { /* in object */
626 /* check for inconsistency */
627 if(*totcolp < ob->totcol)
628 ob->totcol= *totcolp;
629 if(act>ob->totcol) act= ob->totcol;
631 matarar= give_matarar(ob);
633 if(matarar && *matarar) ma= (*matarar)[act-1];
641 ID *material_from(Object *ob, int act)
644 if(ob==NULL) return NULL;
646 if(ob->totcol==0) return ob->data;
649 if(ob->matbits[act-1]) return (ID *)ob;
650 else return ob->data;
653 Material *give_node_material(Material *ma)
655 if(ma && ma->use_nodes && ma->nodetree) {
656 bNode *node= nodeGetActiveID(ma->nodetree, ID_MA);
659 return (Material *)node->id;
665 /* GS reads the memory pointed at in a specific ordering. There are,
666 * however two definitions for it. I have jotted them down here, both,
667 * but I think the first one is actually used. The thing is that
668 * big-endian systems might read this the wrong way round. OTOH, we
669 * constructed the IDs that are read out with this macro explicitly as
670 * well. I expect we'll sort it out soon... */
673 #define GS(a) (*((short *)(a)))
675 /* from misc_util: flip the bytes from x */
676 /* #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
678 void resize_object_material(Object *ob, const short totcol)
686 MEM_freeN(ob->matbits);
691 else if(ob->totcol<totcol) {
692 newmatar= MEM_callocN(sizeof(void *)*totcol, "newmatar");
693 newmatbits= MEM_callocN(sizeof(char)*totcol, "newmatbits");
695 memcpy(newmatar, ob->mat, sizeof(void *)*ob->totcol);
696 memcpy(newmatbits, ob->matbits, sizeof(char)*ob->totcol);
698 MEM_freeN(ob->matbits);
701 ob->matbits= newmatbits;
704 if(ob->totcol && ob->actcol==0) ob->actcol= 1;
705 if(ob->actcol>ob->totcol) ob->actcol= ob->totcol;
708 void test_object_materials(ID *id)
710 /* make the ob mat-array same size as 'ob->data' mat-array */
714 if(id==NULL || (totcol=give_totcolp_id(id))==NULL) {
718 for(ob= G.main->object.first; ob; ob= ob->id.next) {
720 resize_object_material(ob, *totcol);
725 void assign_material(Object *ob, Material *ma, int act)
727 Material *mao, **matar, ***matarar;
731 if(act>MAXMAT) return;
734 /* prevent crashing when using accidentally */
735 BLI_assert(ob->id.lib == NULL);
736 if(ob->id.lib) return;
740 totcolp= give_totcolp(ob);
741 matarar= give_matarar(ob);
743 if(totcolp==NULL || matarar==NULL) return;
746 matar= MEM_callocN(sizeof(void *)*act, "matarray1");
749 memcpy(matar, *matarar, sizeof(void *)*(*totcolp));
757 if(act > ob->totcol) {
758 matar= MEM_callocN(sizeof(void *)*act, "matarray2");
759 matbits= MEM_callocN(sizeof(char)*act, "matbits1");
761 memcpy(matar, ob->mat, sizeof(void *)*( ob->totcol ));
762 memcpy(matbits, ob->matbits, sizeof(char)*(*totcolp));
764 MEM_freeN(ob->matbits);
767 ob->matbits= matbits;
770 /* copy object/mesh linking, or assign based on userpref */
772 ob->matbits[act-1]= ob->matbits[ob->actcol-1];
774 ob->matbits[act-1]= (U.flag & USER_MAT_ON_OB)? 1: 0;
779 if(ob->matbits[act-1]) { /* in object */
781 if(mao) mao->id.us--;
785 mao= (*matarar)[act-1];
786 if(mao) mao->id.us--;
787 (*matarar)[act-1]= ma;
791 id_us_plus((ID *)ma);
792 test_object_materials(ob->data);
795 /* XXX - this calls many more update calls per object then are needed, could be optimized */
796 void assign_matarar(struct Object *ob, struct Material ***matar, int totcol)
798 int i, actcol_orig= ob->actcol;
800 while(object_remove_material_slot(ob)) {};
802 /* now we have the right number of slots */
803 for(i=0; i<totcol; i++)
804 assign_material(ob, (*matar)[i], i+1);
806 if(actcol_orig > ob->totcol)
807 actcol_orig= ob->totcol;
809 ob->actcol= actcol_orig;
813 int find_material_index(Object *ob, Material *ma)
818 if(ma==NULL) return 0;
820 totcolp= give_totcolp(ob);
821 matarar= give_matarar(ob);
823 if(totcolp==NULL || matarar==NULL) return 0;
825 for(a=0; a<*totcolp; a++)
826 if((*matarar)[a]==ma)
833 int object_add_material_slot(Object *ob)
835 if(ob==NULL) return FALSE;
836 if(ob->totcol>=MAXMAT) return FALSE;
838 assign_material(ob, NULL, ob->totcol+1);
839 ob->actcol= ob->totcol;
843 static void do_init_render_material(Material *ma, int r_mode, float *amb)
846 int a, needuv=0, needtang=0;
848 if(ma->flarec==0) ma->flarec= 1;
850 /* add all texcoflags from mtex, texco and mapto were cleared in advance */
851 for(a=0; a<MAX_MTEX; a++) {
853 /* separate tex switching */
854 if(ma->septex & (1<<a)) continue;
857 if(mtex && mtex->tex && (mtex->tex->type | (mtex->tex->use_nodes && mtex->tex->nodetree) )) {
859 ma->texco |= mtex->texco;
860 ma->mapto |= mtex->mapto;
862 /* always get derivatives for these textures */
863 if ELEM3(mtex->tex->type, TEX_IMAGE, TEX_PLUGIN, TEX_ENVMAP) ma->texco |= TEXCO_OSA;
864 else if(mtex->texflag & (MTEX_COMPAT_BUMP|MTEX_3TAP_BUMP|MTEX_5TAP_BUMP)) ma->texco |= TEXCO_OSA;
866 if(ma->texco & (TEXCO_ORCO|TEXCO_REFL|TEXCO_NORM|TEXCO_STRAND|TEXCO_STRESS)) needuv= 1;
867 else if(ma->texco & (TEXCO_GLOB|TEXCO_UV|TEXCO_OBJECT|TEXCO_SPEED)) needuv= 1;
868 else if(ma->texco & (TEXCO_LAVECTOR|TEXCO_VIEW|TEXCO_STICKY)) needuv= 1;
870 if((ma->mapto & MAP_NORM) && (mtex->normapspace == MTEX_NSPACE_TANGENT))
875 if(needtang) ma->mode |= MA_NORMAP_TANG;
876 else ma->mode &= ~MA_NORMAP_TANG;
878 if(ma->mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)) {
880 if(r_mode & R_OSA) ma->texco |= TEXCO_OSA; /* for texfaces */
882 if(needuv) ma->texco |= NEED_UV;
884 /* since the raytracer doesnt recalc O structs for each ray, we have to preset them all */
885 if(r_mode & R_RAYTRACE) {
886 if((ma->mode & (MA_RAYMIRROR|MA_SHADOW_TRA)) || ((ma->mode & MA_TRANSP) && (ma->mode & MA_RAYTRANSP))) {
887 ma->texco |= NEED_UV|TEXCO_ORCO|TEXCO_REFL|TEXCO_NORM;
888 if(r_mode & R_OSA) ma->texco |= TEXCO_OSA;
893 ma->ambr= ma->amb*amb[0];
894 ma->ambg= ma->amb*amb[1];
895 ma->ambb= ma->amb*amb[2];
897 /* will become or-ed result of all node modes */
898 ma->mode_l= ma->mode;
899 ma->mode_l &= ~MA_SHLESS;
901 if(ma->strand_surfnor > 0.0f)
902 ma->mode_l |= MA_STR_SURFDIFF;
904 /* parses the geom+tex nodes */
905 if(ma->nodetree && ma->use_nodes)
906 ntreeShaderGetTexcoMode(ma->nodetree, r_mode, &ma->texco, &ma->mode_l);
909 static void init_render_nodetree(bNodeTree *ntree, Material *basemat, int r_mode, float *amb)
913 for(node=ntree->nodes.first; node; node= node->next) {
915 if(GS(node->id->name)==ID_MA) {
916 Material *ma= (Material *)node->id;
918 do_init_render_material(ma, r_mode, amb);
919 basemat->texco |= ma->texco;
920 basemat->mode_l |= ma->mode_l & ~(MA_TRANSP|MA_ZTRANSP|MA_RAYTRANSP);
923 else if(node->type==NODE_GROUP)
924 init_render_nodetree((bNodeTree *)node->id, basemat, r_mode, amb);
929 void init_render_material(Material *mat, int r_mode, float *amb)
932 do_init_render_material(mat, r_mode, amb);
934 if(mat->nodetree && mat->use_nodes) {
935 init_render_nodetree(mat->nodetree, mat, r_mode, amb);
937 if (!mat->nodetree->execdata)
938 mat->nodetree->execdata = ntreeShaderBeginExecTree(mat->nodetree, 1);
942 void init_render_materials(Main *bmain, int r_mode, float *amb)
946 /* clear these flags before going over materials, to make sure they
947 * are cleared only once, otherwise node materials contained in other
948 * node materials can go wrong */
949 for(ma= bmain->mat.first; ma; ma= ma->id.next) {
956 /* two steps, first initialize, then or the flags for layers */
957 for(ma= bmain->mat.first; ma; ma= ma->id.next) {
958 /* is_used flag comes back in convertblender.c */
959 ma->flag &= ~MA_IS_USED;
961 init_render_material(ma, r_mode, amb);
964 do_init_render_material(&defmaterial, r_mode, amb);
967 /* only needed for nodes now */
968 void end_render_material(Material *mat)
970 if(mat && mat->nodetree && mat->use_nodes) {
971 if (mat->nodetree->execdata)
972 ntreeShaderEndExecTree(mat->nodetree->execdata, 1);
976 void end_render_materials(Main *bmain)
979 for(ma= bmain->mat.first; ma; ma= ma->id.next)
981 end_render_material(ma);
984 static int material_in_nodetree(bNodeTree *ntree, Material *mat)
988 for(node=ntree->nodes.first; node; node= node->next) {
989 if(node->id && GS(node->id->name)==ID_MA) {
990 if(node->id==(ID*)mat)
993 else if(node->type==NODE_GROUP)
994 if(material_in_nodetree((bNodeTree*)node->id, mat))
1001 int material_in_material(Material *parmat, Material *mat)
1005 else if(parmat->nodetree && parmat->use_nodes)
1006 return material_in_nodetree(parmat->nodetree, mat);
1011 /* ****************** */
1013 static char colname_array[125][20]= {
1014 "Black","DarkRed","HalfRed","Red","Red",
1015 "DarkGreen","DarkOlive","Brown","Chocolate","OrangeRed",
1016 "HalfGreen","GreenOlive","DryOlive","Goldenrod","DarkOrange",
1017 "LightGreen","Chartreuse","YellowGreen","Yellow","Gold",
1018 "Green","LawnGreen","GreenYellow","LightOlive","Yellow",
1019 "DarkBlue","DarkPurple","HotPink","VioletPink","RedPink",
1020 "SlateGray","DarkGrey","PalePurple","IndianRed","Tomato",
1021 "SeaGreen","PaleGreen","GreenKhaki","LightBrown","LightSalmon",
1022 "SpringGreen","PaleGreen","MediumOlive","YellowBrown","LightGold",
1023 "LightGreen","LightGreen","LightGreen","GreenYellow","PaleYellow",
1024 "HalfBlue","DarkSky","HalfMagenta","VioletRed","DeepPink",
1025 "SteelBlue","SkyBlue","Orchid","LightHotPink","HotPink",
1026 "SeaGreen","SlateGray","MediumGrey","Burlywood","LightPink",
1027 "SpringGreen","Aquamarine","PaleGreen","Khaki","PaleOrange",
1028 "SpringGreen","SeaGreen","PaleGreen","PaleWhite","YellowWhite",
1029 "LightBlue","Purple","MediumOrchid","Magenta","Magenta",
1030 "RoyalBlue","SlateBlue","MediumOrchid","Orchid","Magenta",
1031 "DeepSkyBlue","LightSteelBlue","LightSkyBlue","Violet","LightPink",
1032 "Cyan","DarkTurquoise","SkyBlue","Grey","Snow",
1033 "Mint","Mint","Aquamarine","MintCream","Ivory",
1034 "Blue","Blue","DarkMagenta","DarkOrchid","Magenta",
1035 "SkyBlue","RoyalBlue","LightSlateBlue","MediumOrchid","Magenta",
1036 "DodgerBlue","SteelBlue","MediumPurple","PalePurple","Plum",
1037 "DeepSkyBlue","PaleBlue","LightSkyBlue","PalePurple","Thistle",
1038 "Cyan","ColdBlue","PaleTurquoise","GhostWhite","White"
1041 void automatname(Material *ma)
1046 if(ma==NULL) return;
1047 if(ma->mode & MA_SHLESS) ref= 1.0;
1050 r= (int)(4.99f*(ref*ma->r));
1051 g= (int)(4.99f*(ref*ma->g));
1052 b= (int)(4.99f*(ref*ma->b));
1055 new_id(&G.main->mat, (ID *)ma, colname_array[nr]);
1060 int object_remove_material_slot(Object *ob)
1062 Material *mao, ***matarar;
1067 if(ob==NULL || ob->totcol==0) return FALSE;
1069 /* take a mesh/curve/mball as starting point, remove 1 index,
1070 * AND with all objects that share the ob->data
1072 * after that check indices in mesh/curve/mball!!!
1075 totcolp= give_totcolp(ob);
1076 matarar= give_matarar(ob);
1078 if(*matarar==NULL) return FALSE;
1080 /* we delete the actcol */
1082 mao= (*matarar)[ob->actcol-1];
1083 if(mao) mao->id.us--;
1086 for(a=ob->actcol; a<ob->totcol; a++)
1087 (*matarar)[a-1]= (*matarar)[a];
1091 MEM_freeN(*matarar);
1096 obt= G.main->object.first;
1099 if(obt->data==ob->data) {
1101 /* WATCH IT: do not use actcol from ob or from obt (can become zero) */
1102 mao= obt->mat[actcol-1];
1103 if(mao) mao->id.us--;
1105 for(a=actcol; a<obt->totcol; a++) {
1106 obt->mat[a-1]= obt->mat[a];
1107 obt->matbits[a-1]= obt->matbits[a];
1110 if(obt->actcol > obt->totcol) obt->actcol= obt->totcol;
1112 if(obt->totcol==0) {
1113 MEM_freeN(obt->mat);
1114 MEM_freeN(obt->matbits);
1122 /* check indices from mesh */
1123 if (ELEM4(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT)) {
1124 data_delete_material_index_id((ID *)ob->data, actcol-1);
1125 freedisplist(&ob->disp);
1132 /* r g b = current value, col = new value, fac==0 is no change */
1133 /* if g==NULL, it only does r channel */
1134 void ramp_blend(int type, float *r, float *g, float *b, float fac, const float col[3])
1136 float tmp, facm= 1.0f-fac;
1140 *r = facm*(*r) + fac*col[0];
1142 *g = facm*(*g) + fac*col[1];
1143 *b = facm*(*b) + fac*col[2];
1154 *r *= (facm + fac*col[0]);
1156 *g *= (facm + fac*col[1]);
1157 *b *= (facm + fac*col[2]);
1160 case MA_RAMP_SCREEN:
1161 *r = 1.0f - (facm + fac*(1.0f - col[0])) * (1.0f - *r);
1163 *g = 1.0f - (facm + fac*(1.0f - col[1])) * (1.0f - *g);
1164 *b = 1.0f - (facm + fac*(1.0f - col[2])) * (1.0f - *b);
1167 case MA_RAMP_OVERLAY:
1169 *r *= (facm + 2.0f*fac*col[0]);
1171 *r = 1.0f - (facm + 2.0f*fac*(1.0f - col[0])) * (1.0f - *r);
1174 *g *= (facm + 2.0f*fac*col[1]);
1176 *g = 1.0f - (facm + 2.0f*fac*(1.0f - col[1])) * (1.0f - *g);
1178 *b *= (facm + 2.0f*fac*col[2]);
1180 *b = 1.0f - (facm + 2.0f*fac*(1.0f - col[2])) * (1.0f - *b);
1192 *r = facm*(*r) + fac*(*r)/col[0];
1195 *g = facm*(*g) + fac*(*g)/col[1];
1197 *b = facm*(*b) + fac*(*b)/col[2];
1201 *r = facm*(*r) + fac*fabsf(*r-col[0]);
1203 *g = facm*(*g) + fac*fabsf(*g-col[1]);
1204 *b = facm*(*b) + fac*fabsf(*b-col[2]);
1208 tmp=col[0]+((1-col[0])*facm);
1209 if(tmp < *r) *r= tmp;
1211 tmp=col[1]+((1-col[1])*facm);
1212 if(tmp < *g) *g= tmp;
1213 tmp=col[2]+((1-col[2])*facm);
1214 if(tmp < *b) *b= tmp;
1219 if(tmp > *r) *r= tmp;
1222 if(tmp > *g) *g= tmp;
1224 if(tmp > *b) *b= tmp;
1231 tmp = 1.0f - fac*col[0];
1234 else if ((tmp = (*r) / tmp)> 1.0f)
1241 tmp = 1.0f - fac*col[1];
1244 else if ((tmp = (*g) / tmp) > 1.0f )
1250 tmp = 1.0f - fac*col[2];
1253 else if ((tmp = (*b) / tmp) > 1.0f )
1263 tmp = facm + fac*col[0];
1267 else if (( tmp = (1.0f - (1.0f - (*r)) / tmp )) < 0.0f)
1269 else if (tmp > 1.0f)
1275 tmp = facm + fac*col[1];
1278 else if (( tmp = (1.0f - (1.0f - (*g)) / tmp )) < 0.0f )
1285 tmp = facm + fac*col[2];
1288 else if (( tmp = (1.0f - (1.0f - (*b)) / tmp )) < 0.0f )
1299 float colH,colS,colV;
1300 float tmpr,tmpg,tmpb;
1301 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1303 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1304 hsv_to_rgb( colH , rS, rV, &tmpr, &tmpg, &tmpb);
1305 *r = facm*(*r) + fac*tmpr;
1306 *g = facm*(*g) + fac*tmpg;
1307 *b = facm*(*b) + fac*tmpb;
1314 float colH,colS,colV;
1315 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1317 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1318 hsv_to_rgb( rH, (facm*rS +fac*colS), rV, r, g, b);
1325 float colH,colS,colV;
1326 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1327 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1328 hsv_to_rgb( rH, rS, (facm*rV +fac*colV), r, g, b);
1334 float colH,colS,colV;
1335 float tmpr,tmpg,tmpb;
1336 rgb_to_hsv(col[0],col[1],col[2],&colH,&colS,&colV);
1338 rgb_to_hsv(*r,*g,*b,&rH,&rS,&rV);
1339 hsv_to_rgb( colH, colS, rV, &tmpr, &tmpg, &tmpb);
1340 *r = facm*(*r) + fac*tmpr;
1341 *g = facm*(*g) + fac*tmpg;
1342 *b = facm*(*b) + fac*tmpb;
1348 float scr, scg, scb;
1350 /* first calculate non-fac based Screen mix */
1351 scr = 1.0f - (1.0f - col[0]) * (1.0f - *r);
1352 scg = 1.0f - (1.0f - col[1]) * (1.0f - *g);
1353 scb = 1.0f - (1.0f - col[2]) * (1.0f - *b);
1355 *r = facm*(*r) + fac*(((1.0f - *r) * col[0] * (*r)) + (*r * scr));
1356 *g = facm*(*g) + fac*(((1.0f - *g) * col[1] * (*g)) + (*g * scg));
1357 *b = facm*(*b) + fac*(((1.0f - *b) * col[2] * (*b)) + (*b * scb));
1360 case MA_RAMP_LINEAR:
1362 *r = *r + fac*(2.0f*(col[0]-0.5f));
1364 *r = *r + fac*(2.0f*(col[0]) - 1.0f);
1367 *g = *g + fac*(2.0f*(col[1]-0.5f));
1369 *g = *g + fac*(2.0f*(col[1]) -1.0f);
1371 *b = *b + fac*(2.0f*(col[2]-0.5f));
1373 *b = *b + fac*(2.0f*(col[2]) - 1.0f);
1379 /* copy/paste buffer, if we had a propper py api that would be better */
1380 static Material matcopybuf;
1381 static short matcopied= 0;
1383 void clear_matcopybuf(void)
1385 memset(&matcopybuf, 0, sizeof(Material));
1389 void free_matcopybuf(void)
1393 for(a=0; a<MAX_MTEX; a++) {
1394 if(matcopybuf.mtex[a]) {
1395 MEM_freeN(matcopybuf.mtex[a]);
1396 matcopybuf.mtex[a]= NULL;
1400 if(matcopybuf.ramp_col) MEM_freeN(matcopybuf.ramp_col);
1401 if(matcopybuf.ramp_spec) MEM_freeN(matcopybuf.ramp_spec);
1403 matcopybuf.ramp_col= NULL;
1404 matcopybuf.ramp_spec= NULL;
1406 if(matcopybuf.nodetree) {
1407 ntreeFreeTree(matcopybuf.nodetree);
1408 MEM_freeN(matcopybuf.nodetree);
1409 matcopybuf.nodetree= NULL;
1415 void copy_matcopybuf(Material *ma)
1423 memcpy(&matcopybuf, ma, sizeof(Material));
1424 if(matcopybuf.ramp_col) matcopybuf.ramp_col= MEM_dupallocN(matcopybuf.ramp_col);
1425 if(matcopybuf.ramp_spec) matcopybuf.ramp_spec= MEM_dupallocN(matcopybuf.ramp_spec);
1427 for(a=0; a<MAX_MTEX; a++) {
1428 mtex= matcopybuf.mtex[a];
1430 matcopybuf.mtex[a]= MEM_dupallocN(mtex);
1433 matcopybuf.nodetree= ntreeCopyTree(ma->nodetree);
1434 matcopybuf.preview= NULL;
1435 matcopybuf.gpumaterial.first= matcopybuf.gpumaterial.last= NULL;
1439 void paste_matcopybuf(Material *ma)
1447 /* free current mat */
1448 if(ma->ramp_col) MEM_freeN(ma->ramp_col);
1449 if(ma->ramp_spec) MEM_freeN(ma->ramp_spec);
1450 for(a=0; a<MAX_MTEX; a++) {
1452 if(mtex && mtex->tex) mtex->tex->id.us--;
1453 if(mtex) MEM_freeN(mtex);
1457 ntreeFreeTree(ma->nodetree);
1458 MEM_freeN(ma->nodetree);
1461 GPU_material_free(ma);
1464 memcpy(ma, &matcopybuf, sizeof(Material));
1467 if(matcopybuf.ramp_col) ma->ramp_col= MEM_dupallocN(matcopybuf.ramp_col);
1468 if(matcopybuf.ramp_spec) ma->ramp_spec= MEM_dupallocN(matcopybuf.ramp_spec);
1470 for(a=0; a<MAX_MTEX; a++) {
1473 ma->mtex[a]= MEM_dupallocN(mtex);
1474 if(mtex->tex) id_us_plus((ID *)mtex->tex);
1478 ma->nodetree= ntreeCopyTree(matcopybuf.nodetree);
1482 /*********************** texface to material convert functions **********************/
1483 /* encode all the TF information into a single int */
1484 static int encode_tfaceflag(MTFace *tf, int convertall)
1486 /* calculate the flag */
1487 int flag = tf->mode;
1489 /* options that change the material offline render */
1494 /* clean flags that are not being converted */
1496 flag &= ~TF_SHAREDVERT;
1497 flag &= ~TF_SHAREDCOL;
1498 flag &= ~TF_CONVERTED;
1500 /* light tface flag is ignored in GLSL mode */
1503 /* 15 is how big the flag can be - hardcoded here and in decode_tfaceflag() */
1504 flag |= tf->transp << 15;
1506 /* increase 1 so flag 0 is different than no flag yet */
1510 /* set the material options based in the tface flag */
1511 static void decode_tfaceflag(Material *ma, int flag, int convertall)
1514 GameSettings *game= &ma->game;
1516 /* flag is shifted in 1 to make 0 != no flag yet (see encode_tfaceflag) */
1519 alphablend = flag >> 15; //encoded in the encode_tfaceflag function
1522 /* General Material Options */
1523 if ((flag & TF_DYNAMIC)==0) (*game).flag |= GEMAT_NOPHYSICS;
1525 /* Material Offline Rendering Properties */
1527 if (flag & TF_OBCOL) ma->shade_flag |= MA_OBCOLOR;
1530 /* Special Face Properties */
1531 if ((flag & TF_TWOSIDE)==0) (*game).flag |= GEMAT_BACKCULL;
1532 if (flag & TF_INVISIBLE)(*game).flag |= GEMAT_INVISIBLE;
1533 if (flag & TF_BMFONT) (*game).flag |= GEMAT_TEXT;
1535 /* Face Orientation */
1536 if (flag & TF_BILLBOARD) (*game).face_orientation |= GEMAT_HALO;
1537 else if (flag & TF_BILLBOARD2) (*game).face_orientation |= GEMAT_BILLBOARD;
1538 else if (flag & TF_SHADOW) (*game).face_orientation |= GEMAT_SHADOW;
1541 if (flag & TF_ALPHASORT && ELEM(alphablend, TF_ALPHA, TF_ADD)) (*game).alpha_blend = GEMAT_ALPHA_SORT;
1542 else if (alphablend & TF_ALPHA) (*game).alpha_blend = GEMAT_ALPHA;
1543 else if (alphablend & TF_ADD) (*game).alpha_blend = GEMAT_ADD;
1544 else if (alphablend & TF_CLIP) (*game).alpha_blend = GEMAT_CLIP;
1547 /* boolean check to see if the mesh needs a material */
1548 static int check_tfaceneedmaterial(int flag)
1550 // check if the flags we have are not deprecated != than default material options
1551 // also if only flags are visible and collision see if all objects using this mesh have this option in physics
1553 /* flag is shifted in 1 to make 0 != no flag yet (see encode_tfaceflag) */
1558 flag &= ~TF_SHAREDVERT;
1559 flag &= ~TF_SHAREDCOL;
1561 /* light tface flag is ignored in GLSL mode */
1564 // automatic detected if tex image has alpha
1565 flag &= ~(TF_ALPHA << 15);
1566 // automatic detected if using texture
1569 // settings for the default NoMaterial
1570 if (flag == TF_DYNAMIC)
1577 /* return number of digits of an integer */
1578 // XXX to be optmized or replaced by an equivalent blender internal function
1579 static int integer_getdigits(int number)
1582 if (number == 0) return 1;
1584 while (number != 0){
1585 number = (int)(number/10);
1591 static void calculate_tface_materialname(char *matname, char *newname, int flag)
1593 // if flag has only light and collision and material matches those values
1594 // you can do strcpy(name, mat_name);
1596 int digits = integer_getdigits(flag);
1597 /* clamp the old name, remove the MA prefix and add the .TF.flag suffix
1598 e.g. matname = "MALoooooooooooooongName"; newname = "Loooooooooooooon.TF.2" */
1599 sprintf(newname, "%.*s.TF.%0*d", MAX_ID_NAME-(digits+5), matname, digits, flag);
1602 /* returns -1 if no match */
1603 static int mesh_getmaterialnumber(Mesh *me, Material *ma) {
1606 for (a=0; a<me->totcol; a++)
1607 if (me->mat[a] == ma)
1613 /* append material */
1614 static int mesh_addmaterial(Mesh *me, Material *ma)
1616 material_append_id(&me->id, NULL);
1617 me->mat[me->totcol-1]= ma;
1619 id_us_plus(&ma->id);
1621 return me->totcol-1;
1624 static void set_facetexture_flags(Material *ma, Image *image)
1627 ma->mode |= MA_FACETEXTURE;
1628 /* we could check if the texture has alpha, but then more meshes sharing the same
1629 * material may need it. Let's make it simple. */
1630 if(BKE_image_has_alpha(image))
1631 ma->mode |= MA_FACETEXTURE_ALPHA;
1635 /* returns material number */
1636 static int convert_tfacenomaterial(Main *main, Mesh *me, MTFace *tf, int flag)
1639 char idname[MAX_ID_NAME];
1642 /* new material, the name uses the flag*/
1643 sprintf(idname, "MAMaterial.TF.%0*d", integer_getdigits(flag), flag);
1645 if ((ma= BLI_findstring(&main->mat, idname+2, offsetof(ID, name)+2))) {
1646 mat_nr= mesh_getmaterialnumber(me, ma);
1647 /* assign the material to the mesh */
1648 if(mat_nr == -1) mat_nr= mesh_addmaterial(me, ma);
1650 /* if needed set "Face Textures [Alpha]" Material options */
1651 set_facetexture_flags(ma, tf->tpage);
1653 /* create a new material */
1655 ma= add_material(idname+2);
1658 printf("TexFace Convert: Material \"%s\" created.\n", idname+2);
1659 mat_nr= mesh_addmaterial(me, ma);
1661 /* if needed set "Face Textures [Alpha]" Material options */
1662 set_facetexture_flags(ma, tf->tpage);
1664 decode_tfaceflag(ma, flag, 1);
1665 // the final decoding will happen after, outside the main loop
1666 // for now store the flag into the material and change light/tex/collision
1667 // store the flag as a negative number
1668 ma->game.flag = -flag;
1669 id_us_min((ID *)ma);
1671 else printf("Error: Unable to create Material \"%s\" for Mesh \"%s\".", idname+2, me->id.name+2);
1674 /* set as converted, no need to go bad to this face */
1675 tf->mode |= TF_CONVERTED;
1679 /* Function to fully convert materials */
1680 static void convert_tfacematerial(Main *main, Material *ma)
1688 CustomDataLayer *cdl;
1689 char idname[MAX_ID_NAME];
1691 for(me=main->mesh.first; me; me=me->id.next){
1692 /* check if this mesh uses this material */
1693 for(a=0;a<me->totcol;a++)
1694 if(me->mat[a] == ma) break;
1696 /* no material found */
1697 if (a == me->totcol) continue;
1699 /* get the active tface layer */
1700 index= CustomData_get_active_layer_index(&me->fdata, CD_MTFACE);
1701 cdl= (index == -1)? NULL: &me->fdata.layers[index];
1704 /* loop over all the faces and stop at the ones that use the material*/
1705 for(a=0, mf=me->mface; a<me->totface; a++, mf++) {
1706 if(me->mat[(int)mf->mat_nr] != ma) continue;
1708 /* texface data for this face */
1709 tf = ((MTFace*)cdl->data) + a;
1710 flag = encode_tfaceflag(tf, 1);
1712 /* the name of the new material */
1713 calculate_tface_materialname(ma->id.name, (char *)&idname, flag);
1715 if ((mat_new= BLI_findstring(&main->mat, idname+2, offsetof(ID, name)+2))) {
1716 /* material already existent, see if the mesh has it */
1717 mat_nr = mesh_getmaterialnumber(me, mat_new);
1718 /* material is not in the mesh, add it */
1719 if(mat_nr == -1) mat_nr= mesh_addmaterial(me, mat_new);
1721 /* create a new material */
1723 mat_new=copy_material(ma);
1725 /* rename the material*/
1726 strcpy(mat_new->id.name, idname);
1727 id_us_min((ID *)mat_new);
1729 mat_nr= mesh_addmaterial(me, mat_new);
1730 decode_tfaceflag(mat_new, flag, 1);
1733 printf("Error: Unable to create Material \"%s\" for Mesh \"%s.", idname+2, me->id.name+2);
1734 mat_nr = mf->mat_nr;
1739 /* if the material has a texture but no texture channel
1740 * set "Face Textures [Alpha]" Material options
1741 * actually we need to run it always, because of old behavior
1742 * of using face texture if any texture channel was present (multitex) */
1743 //if((!mat_new->mtex[0]) && (!mat_new->mtex[0]->tex))
1744 set_facetexture_flags(mat_new, tf->tpage);
1746 /* set the material number to the face*/
1747 mf->mat_nr = mat_nr;
1749 /* remove material from mesh */
1750 for(a=0;a<me->totcol;)
1751 if(me->mat[a] == ma) material_pop_id(&me->id, a, 1);else a++;
1755 int do_version_tface(Main *main, int fileload)
1761 CustomDataLayer *cdl;
1766 /* sometimes mesh has no materials but will need a new one. In those
1767 * cases we need to ignore the mf->mat_nr and only look at the face
1768 * mode because it can be zero as uninitialized or the 1st created material
1770 int nomaterialslots;
1772 /* alert to user to check the console */
1775 /* mark all the materials to conversion with a flag
1776 * if there is tface create a complete flag for that storing in flag
1777 * if there is tface and flag > 0: creates a new flag based on this face
1778 * if flags are different set flag to -1
1781 /* 1st part: marking mesh materials to update */
1782 for(me=main->mesh.first; me; me=me->id.next){
1783 if (me->id.lib) continue;
1785 /* get the active tface layer */
1786 index= CustomData_get_active_layer_index(&me->fdata, CD_MTFACE);
1787 cdl= (index == -1)? NULL: &me->fdata.layers[index];
1790 nomaterialslots = (me->totcol==0?1:0);
1792 /* loop over all the faces*/
1793 for(a=0, mf=me->mface; a<me->totface; a++, mf++) {
1794 /* texface data for this face */
1795 tf = ((MTFace*)cdl->data) + a;
1797 /* conversion should happen only once */
1799 tf->mode &= ~TF_CONVERTED;
1801 if((tf->mode & TF_CONVERTED)) continue;
1802 else tf->mode |= TF_CONVERTED;
1805 /* no material slots */
1806 if(nomaterialslots) {
1807 flag = encode_tfaceflag(tf, 1);
1809 /* create/find a new material and assign to the face */
1810 if (check_tfaceneedmaterial(flag))
1811 mf->mat_nr= convert_tfacenomaterial(main, me, tf, flag);
1813 /* else mark them as no-material to be reverted to 0 later */
1817 else if(mf->mat_nr < me->totcol) {
1818 ma= me->mat[(int)mf->mat_nr];
1820 /* no material create one if necessary */
1822 /* find a new material and assign to the face */
1823 flag = encode_tfaceflag(tf, 1);
1825 /* create/find a new material and assign to the face */
1826 if (check_tfaceneedmaterial(flag))
1827 mf->mat_nr= convert_tfacenomaterial(main, me, tf, flag);
1832 /* we can't read from this if it comes from a library,
1833 * at doversion time: direct_link might not have happened on it,
1834 * so ma->mtex is not pointing to valid memory yet.
1835 * later we could, but it's better not */
1839 /* material already marked as disputed */
1840 else if(ma->game.flag == -99999)
1843 /* found a material */
1845 flag = encode_tfaceflag(tf, ((fileload)?0:1));
1847 /* first time changing this material */
1848 if (ma->game.flag == 0)
1849 ma->game.flag= -flag;
1851 /* mark material as disputed */
1852 else if (ma->game.flag != -flag) {
1853 ma->game.flag = -99999;
1857 /* material ok so far */
1859 ma->game.flag = -flag;
1861 /* some people uses multitexture with TexFace by creating a texture
1862 * channel which not neccessarly the tf->tpage image. But the game engine
1863 * was enabling it. Now it's required to set "Face Texture [Alpha] in the
1864 * material settings. */
1866 set_facetexture_flags(ma, tf->tpage);
1874 /* if we didn't have material slot and now we do, we need to
1875 * make sure the materials are correct */
1876 if(nomaterialslots) {
1878 for(a=0, mf=me->mface; a<me->totface; a++, mf++) {
1879 if (mf->mat_nr == -1) {
1880 /* texface data for this face */
1881 tf = ((MTFace*)cdl->data) + a;
1882 mf->mat_nr= convert_tfacenomaterial(main, me, tf, encode_tfaceflag(tf, 1));
1886 for(a=0, mf=me->mface; a<me->totface; a++, mf++)
1893 /* 2nd part - conversion */
1894 /* skip library files */
1896 /* we shouldn't loop through the materials created in the loop. make the loop stop at its original length) */
1897 for (ma= main->mat.first, a=0; ma; ma= ma->id.next, a++) {
1898 if (ma->id.lib) continue;
1900 /* disputed material */
1901 if (ma->game.flag == -99999) {
1904 printf("Warning: material \"%s\" skipped - to convert old game texface to material go to the Help menu.\n", ma->id.name+2);
1908 convert_tfacematerial(main, ma);
1912 /* no conflicts in this material - 90% of cases
1913 * convert from tface system to material */
1914 else if (ma->game.flag < 0){
1915 decode_tfaceflag(ma, -(ma->game.flag), 1);
1917 /* material is good make sure all faces using
1918 * this material are set to converted */
1920 for(me=main->mesh.first; me; me=me->id.next){
1921 /* check if this mesh uses this material */
1922 for(a=0;a<me->totcol;a++)
1923 if(me->mat[a] == ma) break;
1925 /* no material found */
1926 if (a == me->totcol) continue;
1928 /* get the active tface layer */
1929 index= CustomData_get_active_layer_index(&me->fdata, CD_MTFACE);
1930 cdl= (index == -1)? NULL: &me->fdata.layers[index];
1933 /* loop over all the faces and stop at the ones that use the material*/
1934 for(a=0, mf=me->mface; a<me->totface; a++, mf++) {
1935 if(me->mat[(int)mf->mat_nr] != ma) continue;
1936 else tf->mode |= TF_CONVERTED;