2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/blenkernel/intern/material.c
37 #include "MEM_guardedalloc.h"
39 #include "DNA_anim_types.h"
40 #include "DNA_curve_types.h"
41 #include "DNA_group_types.h"
42 #include "DNA_material_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_meshdata_types.h"
45 #include "DNA_customdata_types.h"
47 #include "DNA_meta_types.h"
48 #include "DNA_node_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_scene_types.h"
53 #include "BLI_listbase.h"
54 #include "BLI_utildefines.h"
55 #include "BLI_string.h"
56 #include "BLI_array_utils.h"
58 #include "BKE_animsys.h"
59 #include "BKE_displist.h"
60 #include "BKE_global.h"
61 #include "BKE_icons.h"
62 #include "BKE_image.h"
63 #include "BKE_library.h"
64 #include "BKE_library_query.h"
65 #include "BKE_library_remap.h"
67 #include "BKE_material.h"
69 #include "BKE_scene.h"
71 #include "BKE_curve.h"
72 #include "BKE_editmesh.h"
75 #include "DEG_depsgraph_build.h"
77 #include "GPU_material.h"
79 /* used in UI and render */
82 /* called on startup, creator.c */
83 void init_def_material(void)
85 BKE_material_init(&defmaterial);
88 /** Free (or release) any data used by this material (does not free the material itself). */
89 void BKE_material_free(Material *ma)
93 BKE_animdata_free((ID *)ma, false);
95 for (a = 0; a < MAX_MTEX; a++) {
96 MEM_SAFE_FREE(ma->mtex[a]);
99 MEM_SAFE_FREE(ma->ramp_col);
100 MEM_SAFE_FREE(ma->ramp_spec);
102 /* is no lib link block, but material extension */
104 ntreeFreeTree(ma->nodetree);
105 MEM_freeN(ma->nodetree);
109 MEM_SAFE_FREE(ma->texpaintslot);
111 GPU_material_free(&ma->gpumaterial);
113 BKE_icon_id_delete((ID *)ma);
114 BKE_previewimg_free(&ma->preview);
117 void BKE_material_init(Material *ma)
119 BLI_assert(MEMCMP_STRUCT_OFS_IS_ZERO(ma, id));
121 ma->r = ma->g = ma->b = ma->ref = 0.8;
122 ma->specr = ma->specg = ma->specb = 1.0;
123 ma->mirr = ma->mirg = ma->mirb = 1.0;
127 ma->spec = ma->hasize = 0.5;
129 ma->starc = ma->ringc = 4;
132 ma->flaresize = ma->subsize = 1.0;
145 ma->strand_sta = ma->strand_end = 1.0f;
149 ma->ray_depth_tra = 2;
150 ma->fresnel_mir = 0.0;
151 ma->fresnel_tra = 0.0;
152 ma->fresnel_tra_i = 1.25;
153 ma->fresnel_mir_i = 1.25;
155 ma->tx_falloff = 1.0;
156 ma->shad_alpha = 1.0f;
159 ma->gloss_mir = ma->gloss_tra = 1.0;
160 ma->samp_gloss_mir = ma->samp_gloss_tra = 18;
161 ma->adapt_thresh_mir = ma->adapt_thresh_tra = 0.005;
163 ma->fadeto_mir = MA_RAYMIR_FADETOSKY;
165 ma->rampfac_col = 1.0;
166 ma->rampfac_spec = 1.0;
167 ma->pr_lamp = 3; /* two lamps, is bits */
168 ma->pr_type = MA_SPHERE;
170 ma->sss_radius[0] = 1.0f;
171 ma->sss_radius[1] = 1.0f;
172 ma->sss_radius[2] = 1.0f;
173 ma->sss_col[0] = 1.0f;
174 ma->sss_col[1] = 1.0f;
175 ma->sss_col[2] = 1.0f;
176 ma->sss_error = 0.05f;
177 ma->sss_scale = 0.1f;
179 ma->sss_colfac = 1.0f;
180 ma->sss_texfac = 0.0f;
181 ma->sss_front = 1.0f;
184 ma->vol.density = 1.0f;
185 ma->vol.emission = 0.0f;
186 ma->vol.scattering = 1.0f;
187 ma->vol.reflection = 1.0f;
188 ma->vol.transmission_col[0] = ma->vol.transmission_col[1] = ma->vol.transmission_col[2] = 1.0f;
189 ma->vol.reflection_col[0] = ma->vol.reflection_col[1] = ma->vol.reflection_col[2] = 1.0f;
190 ma->vol.emission_col[0] = ma->vol.emission_col[1] = ma->vol.emission_col[2] = 1.0f;
191 ma->vol.density_scale = 1.0f;
192 ma->vol.depth_cutoff = 0.01f;
193 ma->vol.stepsize_type = MA_VOL_STEP_RANDOMIZED;
194 ma->vol.stepsize = 0.2f;
195 ma->vol.shade_type = MA_VOL_SHADE_SHADED;
196 ma->vol.shadeflag |= MA_VOL_PRECACHESHADING;
197 ma->vol.precache_resolution = 50;
198 ma->vol.ms_spread = 0.2f;
199 ma->vol.ms_diff = 1.f;
200 ma->vol.ms_intensity = 1.f;
202 ma->game.flag = GEMAT_BACKCULL;
203 ma->game.alpha_blend = 0;
204 ma->game.face_orientation = 0;
206 ma->mode = MA_TRACEBLE | MA_SHADBUF | MA_SHADOW | MA_RAYBIAS | MA_TANGENT_STR | MA_ZTRANSP;
207 ma->mode2 = MA_CASTSHADOW;
208 ma->shade_flag = MA_APPROX_OCCLUSION;
211 ma->alpha_threshold = 0.5f;
214 Material *BKE_material_add(Main *bmain, const char *name)
218 ma = BKE_libblock_alloc(bmain, ID_MA, name, 0);
220 BKE_material_init(ma);
226 * Only copy internal data of Material ID from source to already allocated/initialized destination.
227 * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs.
229 * WARNING! This function will not handle ID user count!
231 * \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
233 void BKE_material_copy_data(Main *bmain, Material *ma_dst, const Material *ma_src, const int flag)
235 for (int a = 0; a < MAX_MTEX; a++) {
236 if (ma_src->mtex[a]) {
237 ma_dst->mtex[a] = MEM_mallocN(sizeof(*ma_dst->mtex[a]), __func__);
238 *ma_dst->mtex[a] = *ma_src->mtex[a];
242 if (ma_src->ramp_col) {
243 ma_dst->ramp_col = MEM_dupallocN(ma_src->ramp_col);
245 if (ma_src->ramp_spec) {
246 ma_dst->ramp_spec = MEM_dupallocN(ma_src->ramp_spec);
249 if (ma_src->nodetree) {
250 BKE_id_copy_ex(bmain, (ID *)ma_src->nodetree, (ID **)&ma_dst->nodetree, flag, false);
253 if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) {
254 BKE_previewimg_id_copy(&ma_dst->id, &ma_src->id);
257 ma_dst->preview = NULL;
260 BLI_listbase_clear(&ma_dst->gpumaterial);
262 /* TODO Duplicate Engine Settings and set runtime to NULL */
265 Material *BKE_material_copy(Main *bmain, const Material *ma)
268 BKE_id_copy_ex(bmain, &ma->id, (ID **)&ma_copy, 0, false);
272 /* XXX (see above) material copy without adding to main dbase */
273 Material *localize_material(Material *ma)
275 /* TODO replace with something like
277 * BKE_id_copy_ex(bmain, &ma->id, (ID **)&ma_copy, LIB_ID_COPY_NO_MAIN | LIB_ID_COPY_NO_PREVIEW | LIB_ID_COPY_NO_USER_REFCOUNT, false);
280 * ... Once f*** nodes are fully converted to that too :( */
285 man = BKE_libblock_copy_nolib(&ma->id, false);
287 /* no increment for texture ID users, in previewrender.c it prevents decrement */
288 for (a = 0; a < MAX_MTEX; a++) {
290 man->mtex[a] = MEM_mallocN(sizeof(MTex), "copymaterial");
291 memcpy(man->mtex[a], ma->mtex[a], sizeof(MTex));
295 if (ma->ramp_col) man->ramp_col = MEM_dupallocN(ma->ramp_col);
296 if (ma->ramp_spec) man->ramp_spec = MEM_dupallocN(ma->ramp_spec);
298 man->texpaintslot = NULL;
302 man->nodetree = ntreeLocalize(ma->nodetree);
304 BLI_listbase_clear(&man->gpumaterial);
306 /* TODO Duplicate Engine Settings and set runtime to NULL */
311 void BKE_material_make_local(Main *bmain, Material *ma, const bool lib_local)
313 BKE_id_make_local_generic(bmain, &ma->id, true, lib_local);
316 Material ***give_matarar(Object *ob)
322 if (ob->type == OB_MESH) {
326 else if (ELEM(ob->type, OB_CURVE, OB_FONT, OB_SURF)) {
330 else if (ob->type == OB_MBALL) {
337 short *give_totcolp(Object *ob)
343 if (ob->type == OB_MESH) {
345 return &(me->totcol);
347 else if (ELEM(ob->type, OB_CURVE, OB_FONT, OB_SURF)) {
349 return &(cu->totcol);
351 else if (ob->type == OB_MBALL) {
353 return &(mb->totcol);
358 /* same as above but for ID's */
359 Material ***give_matarar_id(ID *id)
361 /* ensure we don't try get materials from non-obdata */
362 BLI_assert(OB_DATA_SUPPORT_ID(GS(id->name)));
364 switch (GS(id->name)) {
366 return &(((Mesh *)id)->mat);
368 return &(((Curve *)id)->mat);
370 return &(((MetaBall *)id)->mat);
377 short *give_totcolp_id(ID *id)
379 /* ensure we don't try get materials from non-obdata */
380 BLI_assert(OB_DATA_SUPPORT_ID(GS(id->name)));
382 switch (GS(id->name)) {
384 return &(((Mesh *)id)->totcol);
386 return &(((Curve *)id)->totcol);
388 return &(((MetaBall *)id)->totcol);
395 static void material_data_index_remove_id(ID *id, short index)
397 /* ensure we don't try get materials from non-obdata */
398 BLI_assert(OB_DATA_SUPPORT_ID(GS(id->name)));
400 switch (GS(id->name)) {
402 BKE_mesh_material_index_remove((Mesh *)id, index);
405 BKE_curve_material_index_remove((Curve *)id, index);
408 /* meta-elems don't have materials atm */
415 static void material_data_index_clear_id(ID *id)
417 /* ensure we don't try get materials from non-obdata */
418 BLI_assert(OB_DATA_SUPPORT_ID(GS(id->name)));
420 switch (GS(id->name)) {
422 BKE_mesh_material_index_clear((Mesh *)id);
425 BKE_curve_material_index_clear((Curve *)id);
428 /* meta-elems don't have materials atm */
435 void BKE_material_resize_id(Main *bmain, ID *id, short totcol, bool do_id_user)
437 Material ***matar = give_matarar_id(id);
438 short *totcolp = give_totcolp_id(id);
444 if (do_id_user && totcol < (*totcolp)) {
446 for (i = totcol; i < (*totcolp); i++) {
447 id_us_min((ID *)(*matar)[i]);
458 *matar = MEM_recallocN(*matar, sizeof(void *) * totcol);
462 DEG_relations_tag_update(bmain);
465 void BKE_material_append_id(Main *bmain, ID *id, Material *ma)
468 if ((matar = give_matarar_id(id))) {
469 short *totcol = give_totcolp_id(id);
470 Material **mat = MEM_callocN(sizeof(void *) * ((*totcol) + 1), "newmatar");
471 if (*totcol) memcpy(mat, *matar, sizeof(void *) * (*totcol));
472 if (*matar) MEM_freeN(*matar);
475 (*matar)[(*totcol)++] = ma;
477 id_us_plus((ID *)ma);
478 test_all_objects_materials(bmain, id);
479 DEG_relations_tag_update(bmain);
483 Material *BKE_material_pop_id(Main *bmain, ID *id, int index_i, bool update_data)
485 short index = (short)index_i;
486 Material *ret = NULL;
488 if ((matar = give_matarar_id(id))) {
489 short *totcol = give_totcolp_id(id);
490 if (index >= 0 && index < (*totcol)) {
491 ret = (*matar)[index];
492 id_us_min((ID *)ret);
500 if (index + 1 != (*totcol))
501 memmove((*matar) + index, (*matar) + (index + 1), sizeof(void *) * ((*totcol) - (index + 1)));
504 *matar = MEM_reallocN(*matar, sizeof(void *) * (*totcol));
505 test_all_objects_materials(G.main, id);
509 /* decrease mat_nr index */
510 material_data_index_remove_id(id, index);
513 DEG_relations_tag_update(bmain);
520 void BKE_material_clear_id(Main *bmain, ID *id, bool update_data)
523 if ((matar = give_matarar_id(id))) {
524 short *totcol = give_totcolp_id(id);
526 while ((*totcol)--) {
527 id_us_min((ID *)((*matar)[*totcol]));
536 /* decrease mat_nr index */
537 material_data_index_clear_id(id);
540 DEG_relations_tag_update(bmain);
544 Material *give_current_material(Object *ob, short act)
546 Material ***matarar, *ma;
547 const short *totcolp;
549 if (ob == NULL) return NULL;
551 /* if object cannot have material, (totcolp == NULL) */
552 totcolp = give_totcolp(ob);
553 if (totcolp == NULL || ob->totcol == 0) return NULL;
555 /* return NULL for invalid 'act', can happen for mesh face indices */
556 if (act > ob->totcol)
560 printf("Negative material index!\n");
565 if (ob->matbits && ob->matbits[act - 1]) { /* in object */
566 ma = ob->mat[act - 1];
570 /* check for inconsistency */
571 if (*totcolp < ob->totcol)
572 ob->totcol = *totcolp;
573 if (act > ob->totcol) act = ob->totcol;
575 matarar = give_matarar(ob);
577 if (matarar && *matarar) ma = (*matarar)[act - 1];
585 Material *give_node_material(Material *ma)
587 if (ma && ma->use_nodes && ma->nodetree) {
588 bNode *node = nodeGetActiveID(ma->nodetree, ID_MA);
591 return (Material *)node->id;
597 void BKE_material_resize_object(Main *bmain, Object *ob, const short totcol, bool do_id_user)
602 if (do_id_user && totcol < ob->totcol) {
604 for (i = totcol; i < ob->totcol; i++) {
605 id_us_min((ID *)ob->mat[i]);
612 MEM_freeN(ob->matbits);
617 else if (ob->totcol < totcol) {
618 newmatar = MEM_callocN(sizeof(void *) * totcol, "newmatar");
619 newmatbits = MEM_callocN(sizeof(char) * totcol, "newmatbits");
621 memcpy(newmatar, ob->mat, sizeof(void *) * ob->totcol);
622 memcpy(newmatbits, ob->matbits, sizeof(char) * ob->totcol);
624 MEM_freeN(ob->matbits);
627 ob->matbits = newmatbits;
629 /* XXX, why not realloc on shrink? - campbell */
632 if (ob->totcol && ob->actcol == 0) ob->actcol = 1;
633 if (ob->actcol > ob->totcol) ob->actcol = ob->totcol;
635 DEG_relations_tag_update(bmain);
638 void test_object_materials(Object *ob, ID *id)
640 /* make the ob mat-array same size as 'ob->data' mat-array */
643 if (id == NULL || (totcol = give_totcolp_id(id)) == NULL) {
647 BKE_material_resize_object(G.main, ob, *totcol, false);
650 void test_all_objects_materials(Main *bmain, ID *id)
652 /* make the ob mat-array same size as 'ob->data' mat-array */
656 if (id == NULL || (totcol = give_totcolp_id(id)) == NULL) {
660 BKE_main_lock(bmain);
661 for (ob = bmain->object.first; ob; ob = ob->id.next) {
662 if (ob->data == id) {
663 BKE_material_resize_object(bmain, ob, *totcol, false);
666 BKE_main_unlock(bmain);
669 void assign_material_id(ID *id, Material *ma, short act)
671 Material *mao, **matar, ***matarar;
674 if (act > MAXMAT) return;
675 if (act < 1) act = 1;
677 /* this is needed for Python overrides,
678 * we just have to take care that the UI can't do this */
680 /* prevent crashing when using accidentally */
681 BLI_assert(id->lib == NULL);
687 totcolp = give_totcolp_id(id);
688 matarar = give_matarar_id(id);
690 if (totcolp == NULL || matarar == NULL) return;
692 if (act > *totcolp) {
693 matar = MEM_callocN(sizeof(void *) * act, "matarray1");
696 memcpy(matar, *matarar, sizeof(void *) * (*totcolp));
705 mao = (*matarar)[act - 1];
708 (*matarar)[act - 1] = ma;
713 test_all_objects_materials(G.main, id);
716 void assign_material(Object *ob, Material *ma, short act, int assign_type)
718 Material *mao, **matar, ***matarar;
722 if (act > MAXMAT) return;
723 if (act < 1) act = 1;
725 /* prevent crashing when using accidentally */
726 BLI_assert(!ID_IS_LINKED_DATABLOCK(ob));
727 if (ID_IS_LINKED_DATABLOCK(ob)) return;
731 totcolp = give_totcolp(ob);
732 matarar = give_matarar(ob);
734 if (totcolp == NULL || matarar == NULL) return;
736 if (act > *totcolp) {
737 matar = MEM_callocN(sizeof(void *) * act, "matarray1");
740 memcpy(matar, *matarar, sizeof(void *) * (*totcolp));
748 if (act > ob->totcol) {
749 /* Need more space in the material arrays */
750 ob->mat = MEM_recallocN_id(ob->mat, sizeof(void *) * act, "matarray2");
751 ob->matbits = MEM_recallocN_id(ob->matbits, sizeof(char) * act, "matbits1");
755 /* Determine the object/mesh linking */
756 if (assign_type == BKE_MAT_ASSIGN_EXISTING) {
757 /* keep existing option (avoid confusion in scripts),
758 * intentionally ignore userpref (default to obdata). */
759 bit = ob->matbits[act - 1];
761 else if (assign_type == BKE_MAT_ASSIGN_USERPREF && ob->totcol && ob->actcol) {
762 /* copy from previous material */
763 bit = ob->matbits[ob->actcol - 1];
766 switch (assign_type) {
767 case BKE_MAT_ASSIGN_OBDATA:
770 case BKE_MAT_ASSIGN_OBJECT:
773 case BKE_MAT_ASSIGN_USERPREF:
775 bit = (U.flag & USER_MAT_ON_OB) ? 1 : 0;
782 ob->matbits[act - 1] = bit;
783 if (bit == 1) { /* in object */
784 mao = ob->mat[act - 1];
787 ob->mat[act - 1] = ma;
788 test_object_materials(ob, ob->data);
791 mao = (*matarar)[act - 1];
794 (*matarar)[act - 1] = ma;
795 test_all_objects_materials(G.main, ob->data); /* Data may be used by several objects... */
803 void BKE_material_remap_object(Object *ob, const unsigned int *remap)
805 Material ***matar = give_matarar(ob);
806 const short *totcol_p = give_totcolp(ob);
808 BLI_array_permute(ob->mat, ob->totcol, remap);
811 BLI_array_permute(ob->matbits, ob->totcol, remap);
815 BLI_array_permute(*matar, *totcol_p, remap);
818 if (ob->type == OB_MESH) {
819 BKE_mesh_material_remap(ob->data, remap, ob->totcol);
821 else if (ELEM(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
822 BKE_curve_material_remap(ob->data, remap, ob->totcol);
825 /* add support for this object data! */
826 BLI_assert(matar == NULL);
831 * Calculate a material remapping from \a ob_src to \a ob_dst.
833 * \param remap_src_to_dst: An array the size of `ob_src->totcol`
834 * where index values are filled in which map to \a ob_dst materials.
836 void BKE_material_remap_object_calc(
837 Object *ob_dst, Object *ob_src,
838 short *remap_src_to_dst)
840 if (ob_src->totcol == 0) {
844 GHash *gh_mat_map = BLI_ghash_ptr_new_ex(__func__, ob_src->totcol);
846 for (int i = 0; i < ob_dst->totcol; i++) {
847 Material *ma_src = give_current_material(ob_dst, i + 1);
848 BLI_ghash_reinsert(gh_mat_map, ma_src, SET_INT_IN_POINTER(i), NULL, NULL);
851 /* setup default mapping (when materials don't match) */
854 if (ob_dst->totcol >= ob_src->totcol) {
855 for (; i < ob_src->totcol; i++) {
856 remap_src_to_dst[i] = i;
860 for (; i < ob_dst->totcol; i++) {
861 remap_src_to_dst[i] = i;
863 for (; i < ob_src->totcol; i++) {
864 remap_src_to_dst[i] = 0;
869 for (int i = 0; i < ob_src->totcol; i++) {
870 Material *ma_src = give_current_material(ob_src, i + 1);
872 if ((i < ob_dst->totcol) && (ma_src == give_current_material(ob_dst, i + 1))) {
873 /* when objects have exact matching materials - keep existing index */
876 void **index_src_p = BLI_ghash_lookup_p(gh_mat_map, ma_src);
878 remap_src_to_dst[i] = GET_INT_FROM_POINTER(*index_src_p);
883 BLI_ghash_free(gh_mat_map, NULL, NULL);
887 /* XXX - this calls many more update calls per object then are needed, could be optimized */
888 void assign_matarar(struct Object *ob, struct Material ***matar, short totcol)
890 int actcol_orig = ob->actcol;
893 while ((ob->totcol > totcol) &&
894 BKE_object_material_slot_remove(ob))
899 /* now we have the right number of slots */
900 for (i = 0; i < totcol; i++)
901 assign_material(ob, (*matar)[i], i + 1, BKE_MAT_ASSIGN_USERPREF);
903 if (actcol_orig > ob->totcol)
904 actcol_orig = ob->totcol;
906 ob->actcol = actcol_orig;
910 short BKE_object_material_slot_find_index(Object *ob, Material *ma)
915 if (ma == NULL) return 0;
917 totcolp = give_totcolp(ob);
918 matarar = give_matarar(ob);
920 if (totcolp == NULL || matarar == NULL) return 0;
922 for (a = 0; a < *totcolp; a++)
923 if ((*matarar)[a] == ma)
930 bool BKE_object_material_slot_add(Object *ob)
932 if (ob == NULL) return false;
933 if (ob->totcol >= MAXMAT) return false;
935 assign_material(ob, NULL, ob->totcol + 1, BKE_MAT_ASSIGN_USERPREF);
936 ob->actcol = ob->totcol;
940 static void do_init_render_material(Material *ma, int r_mode, float *amb)
943 int a, needuv = 0, needtang = 0;
945 if (ma->flarec == 0) ma->flarec = 1;
947 /* add all texcoflags from mtex, texco and mapto were cleared in advance */
948 for (a = 0; a < MAX_MTEX; a++) {
950 /* separate tex switching */
951 if (ma->septex & (1 << a)) continue;
954 if (mtex && mtex->tex && (mtex->tex->type | (mtex->tex->use_nodes && mtex->tex->nodetree) )) {
956 ma->texco |= mtex->texco;
957 ma->mapto |= mtex->mapto;
959 /* always get derivatives for these textures */
960 if (ELEM(mtex->tex->type, TEX_IMAGE, TEX_ENVMAP)) ma->texco |= TEXCO_OSA;
961 else if (mtex->texflag & (MTEX_COMPAT_BUMP | MTEX_3TAP_BUMP | MTEX_5TAP_BUMP | MTEX_BICUBIC_BUMP)) ma->texco |= TEXCO_OSA;
963 if (ma->texco & (TEXCO_ORCO | TEXCO_REFL | TEXCO_NORM | TEXCO_STRAND | TEXCO_STRESS)) needuv = 1;
964 else if (ma->texco & (TEXCO_GLOB | TEXCO_UV | TEXCO_OBJECT | TEXCO_SPEED)) needuv = 1;
965 else if (ma->texco & (TEXCO_LAVECTOR | TEXCO_VIEW)) needuv = 1;
967 if ((ma->mapto & MAP_NORM) && (mtex->normapspace == MTEX_NSPACE_TANGENT))
972 if (needtang) ma->mode |= MA_NORMAP_TANG;
973 else ma->mode &= ~MA_NORMAP_TANG;
975 if (ma->mode & (MA_VERTEXCOL | MA_VERTEXCOLP)) {
977 if (r_mode & R_OSA) ma->texco |= TEXCO_OSA; /* for texfaces */
979 if (needuv) ma->texco |= NEED_UV;
981 /* since the raytracer doesnt recalc O structs for each ray, we have to preset them all */
982 if (r_mode & R_RAYTRACE) {
983 if ((ma->mode & (MA_RAYMIRROR | MA_SHADOW_TRA)) || ((ma->mode & MA_TRANSP) && (ma->mode & MA_RAYTRANSP))) {
984 ma->texco |= NEED_UV | TEXCO_ORCO | TEXCO_REFL | TEXCO_NORM;
985 if (r_mode & R_OSA) ma->texco |= TEXCO_OSA;
990 ma->ambr = ma->amb * amb[0];
991 ma->ambg = ma->amb * amb[1];
992 ma->ambb = ma->amb * amb[2];
995 /* local group override */
996 if ((ma->shade_flag & MA_GROUP_LOCAL) && ma->id.lib && ma->group && ma->group->id.lib) {
999 for (group = G.main->group.first; group; group = group->id.next) {
1000 if (!ID_IS_LINKED_DATABLOCK(group) && STREQ(group->id.name, ma->group->id.name)) {
1007 static void init_render_nodetree(bNodeTree *ntree, Material *basemat, int r_mode, float *amb)
1011 /* parses the geom+tex nodes */
1012 ntreeShaderGetTexcoMode(ntree, r_mode, &basemat->texco, &basemat->mode_l);
1013 for (node = ntree->nodes.first; node; node = node->next) {
1015 if (GS(node->id->name) == ID_MA) {
1016 Material *ma = (Material *)node->id;
1017 if (ma != basemat) {
1018 do_init_render_material(ma, r_mode, amb);
1019 basemat->texco |= ma->texco;
1022 basemat->mode_l |= ma->mode & ~(MA_MODE_PIPELINE | MA_SHLESS);
1023 basemat->mode2_l |= ma->mode2 & ~MA_MODE2_PIPELINE;
1024 /* basemat only considered shadeless if all node materials are too */
1025 if (!(ma->mode & MA_SHLESS))
1026 basemat->mode_l &= ~MA_SHLESS;
1028 if (ma->strand_surfnor > 0.0f)
1029 basemat->mode_l |= MA_STR_SURFDIFF;
1031 else if (node->type == NODE_GROUP)
1032 init_render_nodetree((bNodeTree *)node->id, basemat, r_mode, amb);
1034 else if (node->typeinfo->type == SH_NODE_NORMAL_MAP) {
1035 basemat->mode2_l |= MA_TANGENT_CONCRETE;
1036 NodeShaderNormalMap *nm = node->storage;
1037 bool taken_into_account = false;
1038 for (int i = 0; i < basemat->nmap_tangent_names_count; i++) {
1039 if (STREQ(basemat->nmap_tangent_names[i], nm->uv_map)) {
1040 taken_into_account = true;
1044 if (!taken_into_account) {
1045 BLI_assert(basemat->nmap_tangent_names_count < MAX_MTFACE + 1);
1046 strcpy(basemat->nmap_tangent_names[basemat->nmap_tangent_names_count++], nm->uv_map);
1052 void init_render_material(Material *mat, int r_mode, float *amb)
1055 do_init_render_material(mat, r_mode, amb);
1057 if (mat->nodetree && mat->use_nodes) {
1058 /* mode_l will take the pipeline options from the main material, and the or-ed
1059 * result of non-pipeline options from the nodes. shadeless is an exception,
1060 * mode_l will have it set when all node materials are shadeless. */
1061 mat->mode_l = (mat->mode & MA_MODE_PIPELINE) | MA_SHLESS;
1062 mat->mode2_l = mat->mode2 & MA_MODE2_PIPELINE;
1063 mat->nmap_tangent_names_count = 0;
1064 init_render_nodetree(mat->nodetree, mat, r_mode, amb);
1066 if (!mat->nodetree->execdata)
1067 mat->nodetree->execdata = ntreeShaderBeginExecTree(mat->nodetree);
1070 mat->mode_l = mat->mode;
1071 mat->mode2_l = mat->mode2;
1073 if (mat->strand_surfnor > 0.0f)
1074 mat->mode_l |= MA_STR_SURFDIFF;
1078 void init_render_materials(Main *bmain, int r_mode, float *amb, bool do_default_material)
1082 /* clear these flags before going over materials, to make sure they
1083 * are cleared only once, otherwise node materials contained in other
1084 * node materials can go wrong */
1085 for (ma = bmain->mat.first; ma; ma = ma->id.next) {
1092 /* two steps, first initialize, then or the flags for layers */
1093 for (ma = bmain->mat.first; ma; ma = ma->id.next) {
1094 /* is_used flag comes back in convertblender.c */
1095 ma->flag &= ~MA_IS_USED;
1097 init_render_material(ma, r_mode, amb);
1100 if (do_default_material) {
1101 init_render_material(&defmaterial, r_mode, amb);
1105 /* only needed for nodes now */
1106 void end_render_material(Material *mat)
1108 if (mat && mat->nodetree && mat->use_nodes) {
1109 if (mat->nodetree->execdata)
1110 ntreeShaderEndExecTree(mat->nodetree->execdata);
1114 void end_render_materials(Main *bmain)
1117 for (ma = bmain->mat.first; ma; ma = ma->id.next)
1119 end_render_material(ma);
1122 static bool material_in_nodetree(bNodeTree *ntree, Material *mat)
1126 for (node = ntree->nodes.first; node; node = node->next) {
1128 if (GS(node->id->name) == ID_MA) {
1129 if (node->id == (ID *)mat) {
1133 else if (node->type == NODE_GROUP) {
1134 if (material_in_nodetree((bNodeTree *)node->id, mat)) {
1144 bool material_in_material(Material *parmat, Material *mat)
1148 else if (parmat->nodetree && parmat->use_nodes)
1149 return material_in_nodetree(parmat->nodetree, mat);
1155 /* ****************** */
1157 /* Update drivers for materials in a nodetree */
1158 static void material_node_drivers_update(Scene *scene, bNodeTree *ntree, float ctime)
1162 /* nodetree itself */
1163 if (ntree->adt && ntree->adt->drivers.first) {
1164 BKE_animsys_evaluate_animdata(scene, &ntree->id, ntree->adt, ctime, ADT_RECALC_DRIVERS);
1168 for (node = ntree->nodes.first; node; node = node->next) {
1170 if (GS(node->id->name) == ID_MA) {
1171 material_drivers_update(scene, (Material *)node->id, ctime);
1173 else if (node->type == NODE_GROUP) {
1174 material_node_drivers_update(scene, (bNodeTree *)node->id, ctime);
1180 /* Calculate all drivers for materials
1181 * FIXME: this is really a terrible method which may result in some things being calculated
1182 * multiple times. However, without proper despgraph support for these things, we are forced
1183 * into this sort of thing...
1185 void material_drivers_update(Scene *scene, Material *ma, float ctime)
1187 //if (G.f & G_DEBUG)
1188 // printf("material_drivers_update(%s, %s)\n", scene->id.name, ma->id.name);
1190 /* Prevent infinite recursion by checking (and tagging the material) as having been visited already
1191 * (see BKE_scene_update_tagged()). This assumes ma->id.tag & LIB_TAG_DOIT isn't set by anything else
1192 * in the meantime... [#32017]
1194 if (ma->id.tag & LIB_TAG_DOIT)
1197 ma->id.tag |= LIB_TAG_DOIT;
1199 /* material itself */
1200 if (ma->adt && ma->adt->drivers.first) {
1201 BKE_animsys_evaluate_animdata(scene, &ma->id, ma->adt, ctime, ADT_RECALC_DRIVERS);
1206 material_node_drivers_update(scene, ma->nodetree, ctime);
1209 ma->id.tag &= ~LIB_TAG_DOIT;
1212 bool BKE_object_material_slot_remove(Object *ob)
1214 Material *mao, ***matarar;
1219 if (ob == NULL || ob->totcol == 0) {
1223 /* this should never happen and used to crash */
1224 if (ob->actcol <= 0) {
1225 printf("%s: invalid material index %d, report a bug!\n", __func__, ob->actcol);
1230 /* take a mesh/curve/mball as starting point, remove 1 index,
1231 * AND with all objects that share the ob->data
1233 * after that check indices in mesh/curve/mball!!!
1236 totcolp = give_totcolp(ob);
1237 matarar = give_matarar(ob);
1239 if (ELEM(NULL, matarar, *matarar)) {
1243 /* can happen on face selection in editmode */
1244 if (ob->actcol > ob->totcol) {
1245 ob->actcol = ob->totcol;
1248 /* we delete the actcol */
1249 mao = (*matarar)[ob->actcol - 1];
1251 id_us_min(&mao->id);
1253 for (a = ob->actcol; a < ob->totcol; a++)
1254 (*matarar)[a - 1] = (*matarar)[a];
1257 if (*totcolp == 0) {
1258 MEM_freeN(*matarar);
1262 actcol = ob->actcol;
1263 obt = G.main->object.first;
1266 if (obt->data == ob->data) {
1268 /* WATCH IT: do not use actcol from ob or from obt (can become zero) */
1269 mao = obt->mat[actcol - 1];
1271 id_us_min(&mao->id);
1273 for (a = actcol; a < obt->totcol; a++) {
1274 obt->mat[a - 1] = obt->mat[a];
1275 obt->matbits[a - 1] = obt->matbits[a];
1278 if (obt->actcol > obt->totcol) obt->actcol = obt->totcol;
1280 if (obt->totcol == 0) {
1281 MEM_freeN(obt->mat);
1282 MEM_freeN(obt->matbits);
1284 obt->matbits = NULL;
1290 /* check indices from mesh */
1291 if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT)) {
1292 material_data_index_remove_id((ID *)ob->data, actcol - 1);
1293 if (ob->curve_cache) {
1294 BKE_displist_free(&ob->curve_cache->disp);
1301 static bool get_mtex_slot_valid_texpaint(struct MTex *mtex)
1303 return (mtex && (mtex->texco == TEXCO_UV) &&
1304 mtex->tex && (mtex->tex->type == TEX_IMAGE) &&
1308 static bNode *nodetree_uv_node_recursive(bNode *node)
1313 for (sock = node->inputs.first; sock; sock = sock->next) {
1315 inode = sock->link->fromnode;
1316 if (inode->typeinfo->nclass == NODE_CLASS_INPUT && inode->typeinfo->type == SH_NODE_UVMAP) {
1320 return nodetree_uv_node_recursive(inode);
1328 void BKE_texpaint_slot_refresh_cache(Scene *scene, Material *ma)
1334 bool use_nodes = BKE_scene_use_new_shading_nodes(scene);
1335 bool is_bi = BKE_scene_uses_blender_internal(scene) || BKE_scene_uses_blender_game(scene);
1337 /* XXX, for 2.8 testing & development its useful to have non Cycles/BI engines use material nodes
1338 * In the future we may have some way to check this which each engine can define.
1339 * For now use material slots for Clay/Eevee.
1341 if (!(use_nodes || is_bi)) {
1348 if (ma->texpaintslot) {
1349 MEM_freeN(ma->texpaintslot);
1351 ma->texpaintslot = NULL;
1354 if (scene->toolsettings->imapaint.mode == IMAGEPAINT_MODE_IMAGE) {
1355 ma->paint_active_slot = 0;
1356 ma->paint_clone_slot = 0;
1360 if (use_nodes || ma->use_nodes) {
1361 bNode *node, *active_node;
1363 if (!(ma->nodetree)) {
1364 ma->paint_active_slot = 0;
1365 ma->paint_clone_slot = 0;
1369 for (node = ma->nodetree->nodes.first; node; node = node->next) {
1370 if (node->typeinfo->nclass == NODE_CLASS_TEXTURE && node->typeinfo->type == SH_NODE_TEX_IMAGE && node->id)
1375 ma->paint_active_slot = 0;
1376 ma->paint_clone_slot = 0;
1379 ma->texpaintslot = MEM_callocN(sizeof(*ma->texpaintslot) * count, "texpaint_slots");
1381 active_node = nodeGetActiveTexture(ma->nodetree);
1383 for (node = ma->nodetree->nodes.first; node; node = node->next) {
1384 if (node->typeinfo->nclass == NODE_CLASS_TEXTURE && node->typeinfo->type == SH_NODE_TEX_IMAGE && node->id) {
1385 if (active_node == node)
1386 ma->paint_active_slot = index;
1387 ma->texpaintslot[index].ima = (Image *)node->id;
1389 /* for new renderer, we need to traverse the treeback in search of a UV node */
1391 bNode *uvnode = nodetree_uv_node_recursive(node);
1394 NodeShaderUVMap *storage = (NodeShaderUVMap *)uvnode->storage;
1395 ma->texpaintslot[index].uvname = storage->uv_map;
1396 /* set a value to index so UI knows that we have a valid pointer for the mesh */
1397 ma->texpaintslot[index].index = 0;
1400 /* just invalidate the index here so UV map does not get displayed on the UI */
1401 ma->texpaintslot[index].index = -1;
1405 ma->texpaintslot[index].index = -1;
1412 for (mtex = ma->mtex, i = 0; i < MAX_MTEX; i++, mtex++) {
1413 if (get_mtex_slot_valid_texpaint(*mtex)) {
1419 ma->paint_active_slot = 0;
1420 ma->paint_clone_slot = 0;
1424 ma->texpaintslot = MEM_callocN(sizeof(*ma->texpaintslot) * count, "texpaint_slots");
1426 for (mtex = ma->mtex, i = 0; i < MAX_MTEX; i++, mtex++) {
1427 if (get_mtex_slot_valid_texpaint(*mtex)) {
1428 ma->texpaintslot[index].ima = (*mtex)->tex->ima;
1429 ma->texpaintslot[index].uvname = (*mtex)->uvname;
1430 ma->texpaintslot[index].index = i;
1437 ma->paint_active_slot = 0;
1438 ma->paint_clone_slot = 0;
1443 ma->tot_slots = count;
1446 if (ma->paint_active_slot >= count) {
1447 ma->paint_active_slot = count - 1;
1450 if (ma->paint_clone_slot >= count) {
1451 ma->paint_clone_slot = count - 1;
1457 void BKE_texpaint_slots_refresh_object(Scene *scene, struct Object *ob)
1461 for (i = 1; i < ob->totcol + 1; i++) {
1462 Material *ma = give_current_material(ob, i);
1463 BKE_texpaint_slot_refresh_cache(scene, ma);
1468 /* r_col = current value, col = new value, (fac == 0) is no change */
1469 void ramp_blend(int type, float r_col[3], const float fac, const float col[3])
1471 float tmp, facm = 1.0f - fac;
1475 r_col[0] = facm * (r_col[0]) + fac * col[0];
1476 r_col[1] = facm * (r_col[1]) + fac * col[1];
1477 r_col[2] = facm * (r_col[2]) + fac * col[2];
1480 r_col[0] += fac * col[0];
1481 r_col[1] += fac * col[1];
1482 r_col[2] += fac * col[2];
1485 r_col[0] *= (facm + fac * col[0]);
1486 r_col[1] *= (facm + fac * col[1]);
1487 r_col[2] *= (facm + fac * col[2]);
1489 case MA_RAMP_SCREEN:
1490 r_col[0] = 1.0f - (facm + fac * (1.0f - col[0])) * (1.0f - r_col[0]);
1491 r_col[1] = 1.0f - (facm + fac * (1.0f - col[1])) * (1.0f - r_col[1]);
1492 r_col[2] = 1.0f - (facm + fac * (1.0f - col[2])) * (1.0f - r_col[2]);
1494 case MA_RAMP_OVERLAY:
1495 if (r_col[0] < 0.5f)
1496 r_col[0] *= (facm + 2.0f * fac * col[0]);
1498 r_col[0] = 1.0f - (facm + 2.0f * fac * (1.0f - col[0])) * (1.0f - r_col[0]);
1499 if (r_col[1] < 0.5f)
1500 r_col[1] *= (facm + 2.0f * fac * col[1]);
1502 r_col[1] = 1.0f - (facm + 2.0f * fac * (1.0f - col[1])) * (1.0f - r_col[1]);
1503 if (r_col[2] < 0.5f)
1504 r_col[2] *= (facm + 2.0f * fac * col[2]);
1506 r_col[2] = 1.0f - (facm + 2.0f * fac * (1.0f - col[2])) * (1.0f - r_col[2]);
1509 r_col[0] -= fac * col[0];
1510 r_col[1] -= fac * col[1];
1511 r_col[2] -= fac * col[2];
1515 r_col[0] = facm * (r_col[0]) + fac * (r_col[0]) / col[0];
1517 r_col[1] = facm * (r_col[1]) + fac * (r_col[1]) / col[1];
1519 r_col[2] = facm * (r_col[2]) + fac * (r_col[2]) / col[2];
1522 r_col[0] = facm * (r_col[0]) + fac * fabsf(r_col[0] - col[0]);
1523 r_col[1] = facm * (r_col[1]) + fac * fabsf(r_col[1] - col[1]);
1524 r_col[2] = facm * (r_col[2]) + fac * fabsf(r_col[2] - col[2]);
1527 r_col[0] = min_ff(r_col[0], col[0]) * fac + r_col[0] * facm;
1528 r_col[1] = min_ff(r_col[1], col[1]) * fac + r_col[1] * facm;
1529 r_col[2] = min_ff(r_col[2], col[2]) * fac + r_col[2] * facm;
1533 if (tmp > r_col[0]) r_col[0] = tmp;
1535 if (tmp > r_col[1]) r_col[1] = tmp;
1537 if (tmp > r_col[2]) r_col[2] = tmp;
1540 if (r_col[0] != 0.0f) {
1541 tmp = 1.0f - fac * col[0];
1544 else if ((tmp = (r_col[0]) / tmp) > 1.0f)
1549 if (r_col[1] != 0.0f) {
1550 tmp = 1.0f - fac * col[1];
1553 else if ((tmp = (r_col[1]) / tmp) > 1.0f)
1558 if (r_col[2] != 0.0f) {
1559 tmp = 1.0f - fac * col[2];
1562 else if ((tmp = (r_col[2]) / tmp) > 1.0f)
1569 tmp = facm + fac * col[0];
1573 else if ((tmp = (1.0f - (1.0f - (r_col[0])) / tmp)) < 0.0f)
1575 else if (tmp > 1.0f)
1580 tmp = facm + fac * col[1];
1583 else if ((tmp = (1.0f - (1.0f - (r_col[1])) / tmp)) < 0.0f)
1585 else if (tmp > 1.0f)
1590 tmp = facm + fac * col[2];
1593 else if ((tmp = (1.0f - (1.0f - (r_col[2])) / tmp)) < 0.0f)
1595 else if (tmp > 1.0f)
1603 float colH, colS, colV;
1604 float tmpr, tmpg, tmpb;
1605 rgb_to_hsv(col[0], col[1], col[2], &colH, &colS, &colV);
1607 rgb_to_hsv(r_col[0], r_col[1], r_col[2], &rH, &rS, &rV);
1608 hsv_to_rgb(colH, rS, rV, &tmpr, &tmpg, &tmpb);
1609 r_col[0] = facm * (r_col[0]) + fac * tmpr;
1610 r_col[1] = facm * (r_col[1]) + fac * tmpg;
1611 r_col[2] = facm * (r_col[2]) + fac * tmpb;
1618 float colH, colS, colV;
1619 rgb_to_hsv(r_col[0], r_col[1], r_col[2], &rH, &rS, &rV);
1621 rgb_to_hsv(col[0], col[1], col[2], &colH, &colS, &colV);
1622 hsv_to_rgb(rH, (facm * rS + fac * colS), rV, r_col + 0, r_col + 1, r_col + 2);
1629 float colH, colS, colV;
1630 rgb_to_hsv(r_col[0], r_col[1], r_col[2], &rH, &rS, &rV);
1631 rgb_to_hsv(col[0], col[1], col[2], &colH, &colS, &colV);
1632 hsv_to_rgb(rH, rS, (facm * rV + fac * colV), r_col + 0, r_col + 1, r_col + 2);
1638 float colH, colS, colV;
1639 float tmpr, tmpg, tmpb;
1640 rgb_to_hsv(col[0], col[1], col[2], &colH, &colS, &colV);
1642 rgb_to_hsv(r_col[0], r_col[1], r_col[2], &rH, &rS, &rV);
1643 hsv_to_rgb(colH, colS, rV, &tmpr, &tmpg, &tmpb);
1644 r_col[0] = facm * (r_col[0]) + fac * tmpr;
1645 r_col[1] = facm * (r_col[1]) + fac * tmpg;
1646 r_col[2] = facm * (r_col[2]) + fac * tmpb;
1652 float scr, scg, scb;
1654 /* first calculate non-fac based Screen mix */
1655 scr = 1.0f - (1.0f - col[0]) * (1.0f - r_col[0]);
1656 scg = 1.0f - (1.0f - col[1]) * (1.0f - r_col[1]);
1657 scb = 1.0f - (1.0f - col[2]) * (1.0f - r_col[2]);
1659 r_col[0] = facm * (r_col[0]) + fac * (((1.0f - r_col[0]) * col[0] * (r_col[0])) + (r_col[0] * scr));
1660 r_col[1] = facm * (r_col[1]) + fac * (((1.0f - r_col[1]) * col[1] * (r_col[1])) + (r_col[1] * scg));
1661 r_col[2] = facm * (r_col[2]) + fac * (((1.0f - r_col[2]) * col[2] * (r_col[2])) + (r_col[2] * scb));
1664 case MA_RAMP_LINEAR:
1666 r_col[0] = r_col[0] + fac * (2.0f * (col[0] - 0.5f));
1668 r_col[0] = r_col[0] + fac * (2.0f * (col[0]) - 1.0f);
1670 r_col[1] = r_col[1] + fac * (2.0f * (col[1] - 0.5f));
1672 r_col[1] = r_col[1] + fac * (2.0f * (col[1]) - 1.0f);
1674 r_col[2] = r_col[2] + fac * (2.0f * (col[2] - 0.5f));
1676 r_col[2] = r_col[2] + fac * (2.0f * (col[2]) - 1.0f);
1682 * \brief copy/paste buffer, if we had a proper py api that would be better
1683 * \note matcopybuf.nodetree does _NOT_ use ID's
1684 * \todo matcopybuf.nodetree's node->id's are NOT validated, this will crash!
1686 static Material matcopybuf;
1687 static short matcopied = 0;
1689 void clear_matcopybuf(void)
1691 memset(&matcopybuf, 0, sizeof(Material));
1695 void free_matcopybuf(void)
1699 for (a = 0; a < MAX_MTEX; a++) {
1700 if (matcopybuf.mtex[a]) {
1701 MEM_freeN(matcopybuf.mtex[a]);
1702 matcopybuf.mtex[a] = NULL;
1706 if (matcopybuf.ramp_col) MEM_freeN(matcopybuf.ramp_col);
1707 if (matcopybuf.ramp_spec) MEM_freeN(matcopybuf.ramp_spec);
1709 matcopybuf.ramp_col = NULL;
1710 matcopybuf.ramp_spec = NULL;
1712 if (matcopybuf.nodetree) {
1713 ntreeFreeTree(matcopybuf.nodetree);
1714 MEM_freeN(matcopybuf.nodetree);
1715 matcopybuf.nodetree = NULL;
1721 void copy_matcopybuf(Material *ma)
1729 memcpy(&matcopybuf, ma, sizeof(Material));
1730 if (matcopybuf.ramp_col) matcopybuf.ramp_col = MEM_dupallocN(matcopybuf.ramp_col);
1731 if (matcopybuf.ramp_spec) matcopybuf.ramp_spec = MEM_dupallocN(matcopybuf.ramp_spec);
1733 for (a = 0; a < MAX_MTEX; a++) {
1734 mtex = matcopybuf.mtex[a];
1736 matcopybuf.mtex[a] = MEM_dupallocN(mtex);
1739 matcopybuf.nodetree = ntreeCopyTree_ex(ma->nodetree, G.main, false);
1740 matcopybuf.preview = NULL;
1741 BLI_listbase_clear(&matcopybuf.gpumaterial);
1742 /* TODO Duplicate Engine Settings and set runtime to NULL */
1746 void paste_matcopybuf(Material *ma)
1754 /* free current mat */
1755 if (ma->ramp_col) MEM_freeN(ma->ramp_col);
1756 if (ma->ramp_spec) MEM_freeN(ma->ramp_spec);
1757 for (a = 0; a < MAX_MTEX; a++) {
1759 if (mtex && mtex->tex)
1760 id_us_min(&mtex->tex->id);
1766 ntreeFreeTree(ma->nodetree);
1767 MEM_freeN(ma->nodetree);
1770 GPU_material_free(&ma->gpumaterial);
1773 memcpy(ma, &matcopybuf, sizeof(Material));
1776 if (matcopybuf.ramp_col) ma->ramp_col = MEM_dupallocN(matcopybuf.ramp_col);
1777 if (matcopybuf.ramp_spec) ma->ramp_spec = MEM_dupallocN(matcopybuf.ramp_spec);
1779 for (a = 0; a < MAX_MTEX; a++) {
1782 ma->mtex[a] = MEM_dupallocN(mtex);
1784 /* first check this is in main (we may have loaded another file) [#35500] */
1785 if (BLI_findindex(&G.main->tex, mtex->tex) != -1) {
1786 id_us_plus((ID *)mtex->tex);
1789 ma->mtex[a]->tex = NULL;
1795 ma->nodetree = ntreeCopyTree_ex(matcopybuf.nodetree, G.main, false);
1798 struct Image *BKE_object_material_edit_image_get(Object *ob, short mat_nr)
1800 Material *ma = give_current_material(ob, mat_nr + 1);
1801 return ma ? ma->edit_image : NULL;
1804 struct Image **BKE_object_material_edit_image_get_array(Object *ob)
1806 Image **image_array = MEM_mallocN(sizeof(Material *) * ob->totcol, __func__);
1807 for (int i = 0; i < ob->totcol; i++) {
1808 image_array[i] = BKE_object_material_edit_image_get(ob, i);
1813 bool BKE_object_material_edit_image_set(Object *ob, short mat_nr, Image *image)
1815 Material *ma = give_current_material(ob, mat_nr + 1);
1817 /* both may be NULL */
1818 id_us_min((ID *)ma->edit_image);
1819 ma->edit_image = image;
1820 id_us_plus((ID *)ma->edit_image);
1826 void BKE_material_eval(const struct EvaluationContext *UNUSED(eval_ctx), Material *material)
1828 if (G.debug & G_DEBUG_DEPSGRAPH) {
1829 printf("%s on %s (%p)\n", __func__, material->id.name, material);
1831 if ((BLI_listbase_is_empty(&material->gpumaterial) == false)) {
1832 GPU_material_uniform_buffer_tag_dirty(&material->gpumaterial);