Blender V5.3
blenkernel/intern/material.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cmath>
11#include <cstring>
12#include <optional>
13
14#include "BLI_utildefines.hh"
15#include "CLG_log.h"
16
17#include "MEM_guardedalloc.h"
18
19/* Allow using deprecated functionality for .blend file I/O. */
20#define DNA_DEPRECATED_ALLOW
21
22#include "DNA_ID.h"
23#include "DNA_curve_types.h"
24#include "DNA_curves_types.h"
27#include "DNA_material_types.h"
28#include "DNA_meta_types.h"
29#include "DNA_node_types.h"
30#include "DNA_object_types.h"
31#include "DNA_particle_types.h"
33#include "DNA_scene_types.h"
34#include "DNA_userdef_types.h"
35#include "DNA_volume_types.h"
36
37#include "BLI_array_utils_c.hh"
38#include "BLI_enum_flags.hh"
39#include "BLI_listbase.hh"
40#include "BLI_math_color_c.hh"
41#include "BLI_math_vector_c.hh"
42#include "BLI_string.hh"
43
44#include "BLT_translation.hh"
45
46#include "BKE_anim_data.hh"
47#include "BKE_attribute.h"
48#include "BKE_attribute.hh"
49#include "BKE_brush.hh"
50#include "BKE_colorband.hh"
51#include "BKE_curve.hh"
52#include "BKE_curves.hh"
53#include "BKE_displist.h"
54#include "BKE_editmesh.hh"
55#include "BKE_grease_pencil.hh"
56#include "BKE_icons.hh"
57#include "BKE_idtype.hh"
58#include "BKE_image.hh"
59#include "BKE_lib_id.hh"
60#include "BKE_lib_query.hh"
61#include "BKE_main.hh"
62#include "BKE_material.hh"
63#include "BKE_mesh.hh"
64#include "BKE_node.hh"
66#include "BKE_node_runtime.hh"
68#include "BKE_object.hh"
69#include "BKE_object_types.hh"
70#include "BKE_pointcloud.hh"
71#include "BKE_preview_image.hh"
72#include "BKE_scene.hh"
73#include "BKE_vfont.hh"
74
75#include "DEG_depsgraph.hh"
78
79#include "GPU_material.hh"
80
81#include "NOD_shader.h"
82
83#include "BLO_read_write.hh"
84
85namespace blender {
86
87static CLG_LogRef LOG = {"material"};
88
89static void material_init_data(ID *id)
90{
91 Material *material = id_cast<Material *>(id);
92 INIT_DEFAULT_STRUCT_AFTER(material, id);
93
94 material->nodetree = bke::node_tree_add_tree_embedded(
95 nullptr, &material->id, "Shader Nodetree", "ShaderNodeTree");
96}
97
98static void material_copy_data(Main *bmain,
99 std::optional<Library *> owner_library,
100 ID *id_dst,
101 const ID *id_src,
102 const int flag)
103{
104 Material *material_dst = id_cast<Material *>(id_dst);
105 const Material *material_src = id_cast<const Material *>(id_src);
106
107 const bool is_localized = (flag & LIB_ID_CREATE_LOCAL) != 0;
108 /* Never handle user-count here for own sub-data. */
109 const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT;
110 /* Always need allocation of the embedded ID data. */
111 const int flag_embedded_id_data = flag_subdata & ~LIB_ID_CREATE_NO_ALLOCATE;
112
113 if (material_src->nodetree != nullptr) {
114 if (is_localized) {
115 material_dst->nodetree = bke::node_tree_localize(material_src->nodetree, &material_dst->id);
116 }
117 else {
118 BKE_id_copy_in_lib(bmain,
119 owner_library,
120 &material_src->nodetree->id,
121 &material_dst->id,
122 reinterpret_cast<ID **>(&material_dst->nodetree),
123 flag_embedded_id_data);
124 }
125 }
126
127 if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) {
128 BKE_previewimg_id_copy(&material_dst->id, &material_src->id);
129 }
130 else {
131 material_dst->preview = nullptr;
132 }
133
134 if (material_src->texpaintslot != nullptr) {
135 /* TODO: Think we can also skip copying this data in the more generic `NO_MAIN` case? */
136 material_dst->texpaintslot = is_localized ? nullptr :
137 static_cast<TexPaintSlot *>(
138 MEM_dupalloc(material_src->texpaintslot));
139 }
140
141 if (material_src->gp_style != nullptr) {
142 material_dst->gp_style = static_cast<MaterialGPencilStyle *>(
143 MEM_dupalloc(material_src->gp_style));
144
145 if (material_src->gp_style->gradient != nullptr) {
146 material_dst->gp_style->gradient = static_cast<ColorBand *>(
147 MEM_dupalloc(material_src->gp_style->gradient));
148 }
149 }
150
151 material_dst->gpumaterial.clear_no_delete();
152
153 /* TODO: Duplicate Engine Settings and set runtime to nullptr. */
154}
155
156static void material_free_data(ID *id)
157{
158 Material *material = id_cast<Material *>(id);
159
160 /* Free gpu material before the ntree */
161 GPU_material_free(&material->gpumaterial);
162
163 /* is no lib link block, but material extension */
164 if (material->nodetree) {
165 bke::node_tree_free_embedded_tree(material->nodetree);
166 MEM_delete(material->nodetree);
167 material->nodetree = nullptr;
168 }
169
170 MEM_SAFE_DELETE(material->texpaintslot);
171
172 if (material->gp_style) {
173 MEM_SAFE_DELETE(material->gp_style->gradient);
174 }
175
176 MEM_SAFE_DELETE(material->gp_style);
177
178 BKE_previewimg_id_free(&material->id);
179
180 BKE_icon_id_delete(id_cast<ID *>(material));
181}
182
184{
185 Material *material = reinterpret_cast<Material *>(id);
186
187 /* Node-trees **are owned by IDs**, treat them as mere sub-data and not real ID! */
189 data, BKE_library_foreach_ID_embedded(data, (ID **)&material->nodetree));
190 if (material->texpaintslot != nullptr) {
191 BKE_LIB_FOREACHID_PROCESS_IDSUPER(data, material->texpaintslot->ima, IDWALK_CB_NOP);
192 }
193 if (material->gp_style != nullptr) {
194 BKE_LIB_FOREACHID_PROCESS_IDSUPER(data, material->gp_style->sima, IDWALK_CB_USER);
195 BKE_LIB_FOREACHID_PROCESS_IDSUPER(data, material->gp_style->ima, IDWALK_CB_USER);
196 }
197}
198
201{
202 Material *material = reinterpret_cast<Material *>(id);
203
204 fn.single(&material->r);
205 fn.single(&material->specr);
206 fn.single(material->line_col);
207
208 if (material->gp_style) {
209 fn.single(material->gp_style->stroke_rgba);
210 fn.single(material->gp_style->fill_rgba);
211 if (material->gp_style->gradient) {
212 BKE_colorband_foreach_working_space_color(material->gp_style->gradient, fn);
213 }
214 }
215}
216
217static void material_blend_write(BlendWriter *writer, ID *id, const void *id_address)
218{
219 Material *ma = id_cast<Material *>(id);
220
221 /* Clean up, important in undo case to reduce false detection of changed datablocks. */
222 ma->texpaintslot = nullptr;
223 ma->gpumaterial.clear_no_delete();
224
225 /* Set deprecated #use_nodes for forward compatibility. */
226 ma->use_nodes = true;
227
228 /* write LibData */
229 writer->write_id_struct(id_address, ma);
230 BKE_id_blend_write(writer, &ma->id);
231
232 /* nodetree is integral part of material, no libdata */
233 if (ma->nodetree) {
234 BLO_Write_IDBuffer temp_embedded_id_buffer{ma->nodetree->id, writer};
235 bNodeTree *temp_ntree = reinterpret_cast<bNodeTree *>(temp_embedded_id_buffer.get());
236 writer->write_embedded_id_struct(ma->nodetree, temp_ntree);
237 bke::node_tree_blend_write(writer, temp_ntree);
238 }
239
240 BKE_previewimg_blend_write(writer, ma->preview);
241
242 /* grease pencil settings */
243 if (ma->gp_style) {
244 writer->write_struct(ma->gp_style);
245
246 if (ma->gp_style->gradient) {
247 writer->write_struct(ma->gp_style->gradient);
248 }
249 }
250}
251
253{
254 Material *ma = id_cast<Material *>(id);
255
256 ma->texpaintslot = nullptr;
257
258 BLO_read_struct(reader, PreviewImage, &ma->preview);
259 BKE_previewimg_blend_read(reader, ma->preview);
260
261 ma->gpumaterial.clear_no_delete();
262
263 BLO_read_struct(reader, MaterialGPencilStyle, &ma->gp_style);
264
265 if (ma->gp_style) {
266 BLO_read_struct(reader, ColorBand, &ma->gp_style->gradient);
267 }
268}
269
271 .id_code = Material::id_type,
272 .id_filter = FILTER_ID_MA,
273 .dependencies_id_types = FILTER_ID_TE | FILTER_ID_GR,
274 .main_listbase_index = INDEX_ID_MA,
275 .struct_size = sizeof(Material),
276 .name = "Material",
277 .name_plural = N_("materials"),
278 .translation_context = BLT_I18NCONTEXT_ID_MATERIAL,
280 .asset_type_info = nullptr,
281
282 .init_data = material_init_data,
283 .copy_data = material_copy_data,
284 .free_data = material_free_data,
285 .make_local = nullptr,
286 .foreach_id = material_foreach_id,
287 .foreach_cache = nullptr,
288 .foreach_path = nullptr,
289 .foreach_working_space_color = material_foreach_working_space_color,
290 .foreach_asset_weak_reference = nullptr,
291 .owner_pointer_get = nullptr,
292
293 .blend_write = material_blend_write,
294 .blend_read_data = material_blend_read_data,
295 .blend_read_after_liblink = nullptr,
296
297 .blend_read_undo_preserve = nullptr,
298
299 .lib_override_apply_post = nullptr,
300};
301
303{
304 if ((ma) && (ma->gp_style == nullptr)) {
305 ma->gp_style = MEM_new<MaterialGPencilStyle>("Grease Pencil Material Settings");
306
307 MaterialGPencilStyle *gp_style = ma->gp_style;
308 /* set basic settings */
309 gp_style->stroke_rgba[3] = 1.0f;
310 gp_style->fill_rgba[3] = 1.0f;
311 ARRAY_SET_ITEMS(gp_style->texture_scale, 1.0f, 1.0f);
312 gp_style->texture_offset[0] = -0.5f;
313 gp_style->texture_pixsize = 100.0f;
314 gp_style->mix_factor = 0.5f;
316 gp_style->placement_count = 1;
317 gp_style->placement_density = 10.0f;
318 gp_style->placement_radius_spacing = 100.0f;
319 gp_style->random_size_factor = 0.0f;
320 gp_style->random_strength_factor = 0.0f;
321 gp_style->random_rotation_factor = 0.0f;
322 gp_style->random_hue_factor = 0.0f;
323 gp_style->random_saturation_factor = 0.0f;
324 gp_style->random_value_factor = 0.0f;
325 gp_style->random_noise_scale = 1.0f;
326 }
327}
328
330{
331 if (tree == nullptr) {
332 return;
333 }
335 for (bNode *node : tree->all_nodes()) {
336 if (node->is_group()) {
337 bNodeTree *nested_tree = reinterpret_cast<bNodeTree *>(node->id);
339 }
340 }
341}
342
344{
345 if (ma && ma->nodetree) {
347 }
348}
349
350Material *BKE_material_add(Main *bmain, const char *name)
351{
352 Material *ma;
353
354 ma = BKE_id_new<Material>(bmain, name);
355
356 return ma;
357}
358
359Material *BKE_gpencil_material_add(Main *bmain, const char *name)
360{
361 Material *ma;
362
363 ma = BKE_material_add(bmain, name);
364
365 /* grease pencil settings */
366 if (ma != nullptr) {
368 }
369 return ma;
370}
371
373{
374 if (ob->type == OB_MESH) {
375 Mesh *mesh = id_cast<Mesh *>(ob->data);
376 return &(mesh->mat);
377 }
379 Curve *cu = id_cast<Curve *>(ob->data);
380 return &(cu->mat);
381 }
382 if (ob->type == OB_MBALL) {
383 MetaBall *mb = id_cast<MetaBall *>(ob->data);
384 return &(mb->mat);
385 }
386 if (ob->type == OB_CURVES) {
387 Curves *curves = id_cast<Curves *>(ob->data);
388 return &(curves->mat);
389 }
390 if (ob->type == OB_POINTCLOUD) {
391 PointCloud *pointcloud = id_cast<PointCloud *>(ob->data);
392 return &(pointcloud->mat);
393 }
394 if (ob->type == OB_VOLUME) {
395 Volume *volume = id_cast<Volume *>(ob->data);
396 return &(volume->mat);
397 }
398 if (ob->type == OB_GREASE_PENCIL) {
399 GreasePencil *grease_pencil = id_cast<GreasePencil *>(ob->data);
400 return &(grease_pencil->material_array);
401 }
402 return nullptr;
403}
404
406{
407 if (ob->type == OB_MESH) {
408 Mesh *mesh = id_cast<Mesh *>(ob->data);
409 return &(mesh->totcol);
410 }
412 Curve *cu = id_cast<Curve *>(ob->data);
413 return &(cu->totcol);
414 }
415 if (ob->type == OB_MBALL) {
416 MetaBall *mb = id_cast<MetaBall *>(ob->data);
417 return &(mb->totcol);
418 }
419 if (ob->type == OB_CURVES) {
420 Curves *curves = id_cast<Curves *>(ob->data);
421 return &(curves->totcol);
422 }
423 if (ob->type == OB_POINTCLOUD) {
424 PointCloud *pointcloud = id_cast<PointCloud *>(ob->data);
425 return &(pointcloud->totcol);
426 }
427 if (ob->type == OB_VOLUME) {
428 Volume *volume = id_cast<Volume *>(ob->data);
429 return &(volume->totcol);
430 }
431 if (ob->type == OB_GREASE_PENCIL) {
432 GreasePencil *grease_pencil = id_cast<GreasePencil *>(ob->data);
433 return &(grease_pencil->material_array_num);
434 }
435 return nullptr;
436}
437
439{
440 /* ensure we don't try get materials from non-obdata */
442
443 switch (GS(id->name)) {
444 case ID_ME:
445 return &((id_cast<Mesh *>(id))->mat);
446 case ID_CU_LEGACY:
447 return &((id_cast<Curve *>(id))->mat);
448 case ID_MB:
449 return &((id_cast<MetaBall *>(id))->mat);
450 case ID_GD_LEGACY:
451 return &((id_cast<bGPdata *>(id))->mat);
452 case ID_CV:
453 return &((id_cast<Curves *>(id))->mat);
454 case ID_PT:
455 return &((id_cast<PointCloud *>(id))->mat);
456 case ID_VO:
457 return &((id_cast<Volume *>(id))->mat);
458 case ID_GP:
459 return &((id_cast<GreasePencil *>(id))->material_array);
460 default:
461 break;
462 }
463 return nullptr;
464}
465
467{
468 /* ensure we don't try get materials from non-obdata */
470
471 switch (GS(id->name)) {
472 case ID_ME:
473 return &((id_cast<Mesh *>(id))->totcol);
474 case ID_CU_LEGACY:
475 return &((id_cast<Curve *>(id))->totcol);
476 case ID_MB:
477 return &((id_cast<MetaBall *>(id))->totcol);
478 case ID_GD_LEGACY:
479 return &((id_cast<bGPdata *>(id))->totcol);
480 case ID_CV:
481 return &((id_cast<Curves *>(id))->totcol);
482 case ID_PT:
483 return &((id_cast<PointCloud *>(id))->totcol);
484 case ID_VO:
485 return &((id_cast<Volume *>(id))->totcol);
486 case ID_GP:
487 return &((id_cast<GreasePencil *>(id))->material_array_num);
488 default:
489 break;
490 }
491 return nullptr;
492}
493
495{
496 /* ensure we don't try get materials from non-obdata */
498
499 switch (GS(id->name)) {
500 case ID_ME:
501 BKE_mesh_material_index_remove(id_cast<Mesh *>(id), index);
502 break;
503 case ID_CU_LEGACY:
504 BKE_curve_material_index_remove(id_cast<Curve *>(id), index);
505 break;
506 case ID_GP:
508 break;
509 case ID_MB:
510 case ID_CV:
511 case ID_PT:
512 case ID_VO:
513 /* No material indices for these object data types. */
514 break;
515 default:
516 break;
517 }
518}
519
520bool BKE_object_material_slot_used(Object *object, short actcol)
521{
523 return false;
524 }
525
526 for (ParticleSystem &psys : object->particlesystem) {
527 if (psys.part->omat == actcol) {
528 return true;
529 }
530 }
531
532 ID *ob_data = object->data;
533 if (ob_data == nullptr || !OB_DATA_SUPPORT_ID(GS(ob_data->name))) {
534 return false;
535 }
536
537 switch (GS(ob_data->name)) {
538 case ID_ME:
539 return BKE_mesh_material_index_used(id_cast<Mesh *>(ob_data), actcol - 1);
540 case ID_CU_LEGACY:
541 return BKE_curve_material_index_used(id_cast<Curve *>(ob_data), actcol - 1);
542 case ID_MB:
543 /* Meta-elements don't support materials at the moment. */
544 return false;
545 case ID_GP:
546 return BKE_grease_pencil_material_index_used(reinterpret_cast<GreasePencil *>(ob_data),
547 actcol - 1);
548 default:
549 return false;
550 }
551}
552
554{
555 /* ensure we don't try get materials from non-obdata */
557
558 switch (GS(id->name)) {
559 case ID_ME:
560 BKE_mesh_material_index_clear(id_cast<Mesh *>(id));
561 break;
562 case ID_CU_LEGACY:
563 BKE_curve_material_index_clear(id_cast<Curve *>(id));
564 break;
565 case ID_MB:
566 case ID_CV:
567 case ID_PT:
568 case ID_VO:
569 /* No material indices for these object data types. */
570 break;
571 default:
572 break;
573 }
574}
575
576void BKE_id_materials_copy(Main *bmain, ID *id_src, ID *id_dst)
577{
578 Material ***matar_src = BKE_id_material_array_p(id_src);
579 const short *materials_len_p_src = BKE_id_material_len_p(id_src);
580
581 Material ***matar_dst = BKE_id_material_array_p(id_dst);
582 short *materials_len_p_dst = BKE_id_material_len_p(id_dst);
583
584 *materials_len_p_dst = *materials_len_p_src;
585 if (*materials_len_p_src != 0) {
586 (*matar_dst) = MEM_dupalloc(*matar_src);
587
588 for (int a = 0; a < *materials_len_p_src; a++) {
589 id_us_plus(id_cast<ID *>((*matar_dst)[a]));
590 }
591
594 }
595}
596
597void BKE_id_material_resize(Main *bmain, ID *id, short totcol, bool do_id_user)
598{
599 Material ***matar = BKE_id_material_array_p(id);
600 short *totcolp = BKE_id_material_len_p(id);
601
602 if (matar == nullptr) {
603 return;
604 }
605 if (totcol == *totcolp) {
606 /* Prevent depsgraph update and relations tag when nothing changed. */
607 return;
608 }
609
610 if (do_id_user && totcol < (*totcolp)) {
611 short i;
612 for (i = totcol; i < (*totcolp); i++) {
613 id_us_min(id_cast<ID *>((*matar)[i]));
614 }
615 }
616
617 if (totcol == 0) {
618 if (*totcolp) {
619 MEM_delete(*matar);
620 *matar = nullptr;
621 }
622 }
623 else {
624 *matar = static_cast<Material **>(MEM_realloc_zeroed(*matar, sizeof(void *) * totcol));
625 }
626 *totcolp = totcol;
627
630}
631
633{
634 Material ***matar = BKE_id_material_array_p(id);
635 if (matar) {
636 short *totcol = BKE_id_material_len_p(id);
637 Material **mat = MEM_new_array_zeroed<Material *>((*totcol) + 1, "newmatar");
638 if (*totcol) {
639 memcpy(mat, *matar, sizeof(void *) * (*totcol));
640 }
641 if (*matar) {
642 MEM_delete(*matar);
643 }
644
645 *matar = mat;
646 (*matar)[(*totcol)++] = ma;
647
648 id_us_plus(id_cast<ID *>(ma));
650
653 }
654}
655
656Material *BKE_id_material_pop(Main *bmain, ID *id, int index_i)
657{
658 short index = short(index_i);
659 Material *ret = nullptr;
660 Material ***matar = BKE_id_material_array_p(id);
661 if (matar) {
662 short *totcol = BKE_id_material_len_p(id);
663 if (index >= 0 && index < (*totcol)) {
664 ret = (*matar)[index];
665 id_us_min(id_cast<ID *>(ret));
666
667 if (*totcol <= 1) {
668 *totcol = 0;
669 MEM_delete(*matar);
670 *matar = nullptr;
671 }
672 else {
673 if (index + 1 != (*totcol)) {
674 memmove((*matar) + index,
675 (*matar) + (index + 1),
676 sizeof(void *) * ((*totcol) - (index + 1)));
677 }
678
679 (*totcol)--;
680 *matar = static_cast<Material **>(
681 MEM_realloc_uninitialized(*matar, sizeof(void *) * (*totcol)));
683 }
684
686
689 }
690 }
691
692 return ret;
693}
694
696{
697 Material ***matar = BKE_id_material_array_p(id);
698 if (matar) {
699 short *totcol = BKE_id_material_len_p(id);
700
701 while ((*totcol)--) {
702 id_us_min(id_cast<ID *>((*matar)[*totcol]));
703 }
704 *totcol = 0;
705 if (*matar) {
706 MEM_delete(*matar);
707 *matar = nullptr;
708 }
709
712
715 }
716}
717
719{
720 Material ***matarar, **ma_p;
721 const short *totcolp;
722
723 if (ob == nullptr) {
724 return nullptr;
725 }
726
727 /* if object cannot have material, (totcolp == nullptr) */
728 totcolp = BKE_object_material_len_p(ob);
729 if (totcolp == nullptr || *totcolp == 0) {
730 return nullptr;
731 }
732
733 /* Clamp to number of slots if index is out of range, same convention as used for rendering. */
734 const int slot_index = clamp_i(act - 1, 0, *totcolp - 1);
735
736 /* Fix inconsistency which may happen when library linked data reduces the number of
737 * slots but object was not updated. Ideally should be fixed elsewhere. */
738 ob->totcol = std::min<int>(*totcolp, ob->totcol);
739
740 if (slot_index < ob->totcol && ob->matbits && ob->matbits[slot_index]) {
741 /* Use object material slot. */
742 ma_p = &ob->mat[slot_index];
743 }
744 else {
745 /* Use data material slot. */
746 matarar = BKE_object_material_array_p(ob);
747
748 if (matarar && *matarar) {
749 ma_p = &(*matarar)[slot_index];
750 }
751 else {
752 ma_p = nullptr;
753 }
754 }
755
756 return ma_p;
757}
758
760{
761 Material **ma_p = BKE_object_material_get_p(ob, act);
762 /* Grease Pencil objects currently make the assumption that the returned material has Grease
763 * Pencil settings. Ensure that this is the case otherwise return `nullptr`. */
764 if (ob->type == OB_GREASE_PENCIL && ma_p != nullptr) {
765 Material *ma = *ma_p;
766 if (ma != nullptr) {
767 return ma->gp_style != nullptr ? ma : nullptr;
768 }
769 }
770 return ma_p ? *ma_p : nullptr;
771}
772
774{
775 const ID *data = ob->data;
776 /* Meshes in edit mode need special handling. */
777 if (ob->type == OB_MESH && ob->mode == OB_MODE_EDIT) {
778 const Mesh *mesh = id_cast<const Mesh *>(ob->data);
779 const Mesh *editmesh_eval_final = BKE_object_get_editmesh_eval_final(ob);
780 if (mesh->runtime->edit_mesh && editmesh_eval_final) {
781 data = &editmesh_eval_final->id;
782 }
783 }
784 return data;
785}
786
788{
790 return const_cast<Material *>(BKE_object_material_get_eval(*ob, *data, act));
791}
792
793const Material *BKE_object_material_get_eval(const Object &ob, const ID &data, const short act)
794{
796
797 const int slots_num = BKE_object_material_count_eval(ob, data);
798
799 if (slots_num == 0) {
800 return nullptr;
801 }
802
803 /* Clamp to number of slots if index is out of range, same convention as used for rendering. */
804 const int slot_index = clamp_i(act - 1, 0, slots_num - 1);
805 const int tot_slots_object = ob.totcol;
806
807 /* Check if slot is overwritten by object. */
808 if (slot_index < tot_slots_object) {
809 if (ob.matbits) {
810 if (ob.matbits[slot_index]) {
811 Material *material = ob.mat[slot_index];
812 if (material != nullptr) {
813 return material;
814 }
815 }
816 }
817 }
818
819 /* Otherwise use data from object-data. */
820 const short *data_slots_num_ptr = BKE_id_material_len_p(const_cast<ID *>(&data));
821 if (!data_slots_num_ptr) {
822 return nullptr;
823 }
824 const int data_slots_num = *data_slots_num_ptr;
825 Material **data_materials = *BKE_id_material_array_p(const_cast<ID *>(&data));
826 if (slot_index < data_slots_num) {
827 Material *material = data_materials[slot_index];
828 return material;
829 }
830 return nullptr;
831}
832
834{
836 if (ob->type == OB_EMPTY) {
837 return 0;
838 }
839 BLI_assert(ob->data != nullptr);
840 const ID *id = get_evaluated_object_data_with_materials(const_cast<Object *>(ob));
841 const short *len_p = BKE_id_material_len_p(const_cast<ID *>(id));
842 return std::max(ob->totcol, len_p ? *len_p : 0);
843}
844
846{
848 if (ob.type == OB_EMPTY) {
849 return 0;
850 }
851 BLI_assert(ob.data != nullptr);
852 const short *len_p = BKE_id_material_len_p(const_cast<ID *>(&data));
853 return std::max(ob.totcol, len_p ? *len_p : 0);
854}
855
856std::optional<int> BKE_id_material_index_max_eval(const ID &id)
857{
858 switch (GS(id.name)) {
859 case ID_ME:
860 return reinterpret_cast<const Mesh &>(id).material_index_max();
861 case ID_CU_LEGACY:
862 return reinterpret_cast<const Curve &>(id).material_index_max();
863 case ID_CV:
864 return reinterpret_cast<const Curves &>(id).geometry.wrap().material_index_max();
865 case ID_PT:
866 return reinterpret_cast<const PointCloud &>(id).material_index_max();
867 case ID_GP:
868 return reinterpret_cast<const GreasePencil &>(id).material_index_max();
869 case ID_VO:
870 case ID_MB:
871 /* Always use the first material. */
872 return 0;
873 case ID_GD_LEGACY:
874 /* Is not rendered anymore. */
876 return 0;
877 default:
878 break;
879 }
880 return 0;
881}
882
884{
885 if (std::optional<int> max_index = BKE_id_material_index_max_eval(id)) {
886 return *max_index + 1;
887 }
888 return 0;
889}
890
892{
893 const int max_material_index = std::max(0, BKE_id_material_index_max_eval(id).value_or(0));
894 return max_material_index + 1;
895}
896
902
903void BKE_id_material_eval_assign(ID *id, int slot, Material *material)
904{
905 BLI_assert(slot >= 1);
906 Material ***materials_ptr = BKE_id_material_array_p(id);
907 short *len_ptr = BKE_id_material_len_p(id);
908 if (ELEM(nullptr, materials_ptr, len_ptr)) {
910 return;
911 }
912
913 const int slot_index = slot - 1;
914 const int old_length = *len_ptr;
915
916 if (slot_index >= old_length) {
917 /* Need to grow slots array. */
918 const int new_length = slot_index + 1;
919 *materials_ptr = static_cast<Material **>(
920 MEM_realloc_uninitialized(*materials_ptr, sizeof(void *) * new_length));
921 *len_ptr = new_length;
922 for (int i = old_length; i < new_length; i++) {
923 (*materials_ptr)[i] = nullptr;
924 }
925 }
926
927 (*materials_ptr)[slot_index] = material;
928}
929
931{
932 short *len_ptr = BKE_id_material_len_p(id);
933 if (len_ptr == nullptr) {
934 return;
935 }
936 if (*len_ptr == 0) {
937 BKE_id_material_eval_assign(id, 1, nullptr);
938 }
939}
940
942{
943 short *totcol = BKE_object_material_len_p(ob);
944 Material *read_ma = nullptr;
945 for (short i = 0; i < *totcol; i++) {
946 read_ma = BKE_object_material_get(ob, i + 1);
947 if (ma == read_ma) {
948 return i;
949 }
950 }
951 return -1;
952}
953
954int BKE_object_material_index_get_with_hint(Object *ob, const Material *ma, const int hint_index)
955{
956 short *totcol = BKE_object_material_len_p(ob);
957 if ((hint_index >= 0) && (hint_index < *totcol)) {
958 if (ma == BKE_object_material_get(ob, hint_index + 1)) {
959 return hint_index;
960 }
961 }
962 return BKE_object_material_index_get(ob, ma);
963}
964
966{
967 if (!material) {
968 return -1;
969 }
970 int index = BKE_object_material_index_get(ob, material);
971 if (index < 0) {
974 return ob->totcol - 1;
975 }
976 return index;
977}
978
980{
981 Material *ma = BKE_object_material_get(ob, act);
982 if (ma != nullptr) {
983 return ma;
984 }
985
986 /* XXX FIXME This is critical abuse of the 'default material' feature, these IDs should never be
987 * used/returned as 'regular' data. */
989}
990
992{
993 Material *ma = BKE_object_material_get(ob, act);
994 if (ma != nullptr) {
995 if (ma->gp_style == nullptr) {
997 }
998
999 return ma->gp_style;
1000 }
1001
1002 return BKE_material_default_gpencil()->gp_style;
1003}
1004
1011{
1012 const int min_index = (ob->totcol) ? 1 : 0;
1013 ob->actcol = std::clamp(ob->actcol, min_index, ob->totcol);
1014}
1015
1016void BKE_object_material_resize(Main *bmain, Object *ob, const short totcol, bool do_id_user)
1017{
1018 if (totcol == ob->totcol) {
1019 /* Prevent depsgraph update and relations tag when nothing changed. */
1020 return;
1021 }
1022
1023 Material **newmatar;
1024 char *newmatbits;
1025
1026 if (do_id_user && totcol < ob->totcol) {
1027 for (int i = totcol; i < ob->totcol; i++) {
1028 id_us_min(id_cast<ID *>(ob->mat[i]));
1029 }
1030 }
1031
1032 if (totcol == 0) {
1033 if (ob->totcol) {
1034 MEM_delete(ob->mat);
1035 MEM_delete(ob->matbits);
1036 ob->mat = nullptr;
1037 ob->matbits = nullptr;
1038 }
1039 }
1040 else if (ob->totcol < totcol) {
1041 newmatar = MEM_new_array_zeroed<Material *>(totcol, "newmatar");
1042 newmatbits = MEM_new_array_zeroed<char>(totcol, "newmatbits");
1043 if (ob->totcol) {
1044 memcpy(newmatar, ob->mat, sizeof(void *) * ob->totcol);
1045 memcpy(newmatbits, ob->matbits, sizeof(char) * ob->totcol);
1046 MEM_delete(ob->mat);
1047 MEM_delete(ob->matbits);
1048 }
1049 ob->mat = newmatar;
1050 ob->matbits = newmatbits;
1051 }
1052 /* XXX(@ideasman42): why not realloc on shrink? */
1053
1054 ob->totcol = totcol;
1055
1058}
1059
1061{
1062 const short *totcol;
1063
1064 if (id == nullptr || (totcol = BKE_id_material_len_p(id)) == nullptr) {
1065 return;
1066 }
1067
1068 if ((ob->id.tag & ID_TAG_MISSING) == 0 && (id->tag & ID_TAG_MISSING) != 0) {
1069 /* Exception: In case the object is a valid data, but its obdata is an empty place-holder,
1070 * use object's material slots amount as reference.
1071 * This avoids losing materials in a local object when its linked obdata goes missing.
1072 * See #92780. */
1073 BKE_id_material_resize(bmain, id, short(ob->totcol), false);
1074 }
1075 else {
1076 /* Normal case: the use the obdata amount of materials slots to update the object's one. */
1077 BKE_object_material_resize(bmain, ob, *totcol, false);
1079 }
1080}
1081
1083{
1084 Object *ob;
1085 const short *totcol;
1086
1087 if (id == nullptr || (totcol = BKE_id_material_len_p(id)) == nullptr) {
1088 return;
1089 }
1090
1091 BKE_main_lock(bmain);
1092 int processed_objects = 0;
1093 for (ob = bmain->objects.first(); ob; ob = static_cast<Object *>(ob->id.next)) {
1094 if (ob->data == id) {
1095 BKE_object_material_resize(bmain, ob, *totcol, false);
1097 processed_objects++;
1098 BLI_assert(processed_objects <= id->us && processed_objects > 0);
1099 if (processed_objects == id->us) {
1100 break;
1101 }
1102 }
1103 }
1104 BKE_main_unlock(bmain);
1105}
1106
1107void BKE_id_material_assign(Main *bmain, ID *id, Material *ma, short act)
1108{
1109 Material *mao, **matar, ***matarar;
1110 short *totcolp;
1111
1112 if (act > MAXMAT) {
1113 return;
1114 }
1115 act = std::max<int>(act, 1);
1116
1117 /* test arraylens */
1118
1119 totcolp = BKE_id_material_len_p(id);
1120 matarar = BKE_id_material_array_p(id);
1121
1122 if (totcolp == nullptr || matarar == nullptr) {
1123 return;
1124 }
1125
1126 if (act > *totcolp) {
1127 matar = MEM_new_array_zeroed<Material *>(act, "matarray1");
1128
1129 if (*totcolp) {
1130 memcpy(matar, *matarar, sizeof(void *) * (*totcolp));
1131 MEM_delete(*matarar);
1132 }
1133
1134 *matarar = matar;
1135 *totcolp = act;
1136 }
1137
1138 /* in data */
1139 mao = (*matarar)[act - 1];
1140 if (mao) {
1141 id_us_min(&mao->id);
1142 }
1143 (*matarar)[act - 1] = ma;
1144
1145 if (ma) {
1146 id_us_plus(&ma->id);
1147 }
1148
1152}
1153
1155 Main *bmain, Object *ob, Material *ma, short act, int assign_type, bool do_test_all)
1156{
1157 Material *mao, **matar, ***matarar;
1158 short *totcolp;
1159 char bit = 0;
1160
1161 if (act > MAXMAT) {
1162 return;
1163 }
1164 act = std::max<int>(act, 1);
1165
1166 /* test arraylens */
1167
1168 totcolp = BKE_object_material_len_p(ob);
1169 matarar = BKE_object_material_array_p(ob);
1170
1171 if (totcolp == nullptr || matarar == nullptr) {
1172 return;
1173 }
1174
1175 if (act > *totcolp) {
1176 matar = MEM_new_array_zeroed<Material *>(act, "matarray1");
1177
1178 if (*totcolp) {
1179 memcpy(matar, *matarar, sizeof(void *) * (*totcolp));
1180 MEM_delete(*matarar);
1181 }
1182
1183 *matarar = matar;
1184 *totcolp = act;
1185 }
1186
1187 if (act > ob->totcol) {
1188 /* Need more space in the material arrays */
1189 ob->mat = static_cast<Material **>(
1190 MEM_realloc_zeroed_id(ob->mat, sizeof(void *) * act, "matarray2"));
1191 ob->matbits = static_cast<char *>(
1192 MEM_realloc_zeroed_id(ob->matbits, sizeof(char) * act, "matbits1"));
1193 ob->totcol = act;
1194 }
1195
1196 /* Determine the object/mesh linking */
1197 if (assign_type == BKE_MAT_ASSIGN_EXISTING) {
1198 /* keep existing option (avoid confusion in scripts),
1199 * intentionally ignore userpref (default to obdata). */
1200 bit = ob->matbits[act - 1];
1201 }
1202 else if (assign_type == BKE_MAT_ASSIGN_USERPREF && ob->actcol >= 1 && ob->actcol <= ob->totcol) {
1203 /* copy from previous material */
1204 bit = ob->matbits[ob->actcol - 1];
1205 }
1206 else {
1207 switch (assign_type) {
1209 bit = 0;
1210 break;
1212 bit = 1;
1213 break;
1215 default:
1216 bit = (U.flag & USER_MAT_ON_OB) ? 1 : 0;
1217 break;
1218 }
1219 }
1220
1221 /* do it */
1222
1223 ob->matbits[act - 1] = bit;
1224 if (bit == 1) { /* in object */
1225 mao = ob->mat[act - 1];
1226 if (mao) {
1227 id_us_min(&mao->id);
1228 }
1229 ob->mat[act - 1] = ma;
1230 BKE_object_materials_sync_length(bmain, ob, ob->data);
1231 }
1232 else { /* in data */
1233 mao = (*matarar)[act - 1];
1234 if (mao) {
1235 id_us_min(&mao->id);
1236 }
1237 (*matarar)[act - 1] = ma;
1238 /* Data may be used by several objects. */
1239 if (do_test_all) {
1241 }
1242 }
1243
1244 if (ma) {
1245 id_us_plus(&ma->id);
1246 }
1247
1250}
1251
1252void BKE_object_material_assign(Main *bmain, Object *ob, Material *ma, short act, int assign_type)
1253{
1254 object_material_assign(bmain, ob, ma, act, assign_type, true);
1255}
1256
1258{
1259 object_material_assign(bmain, ob, ma, act, BKE_MAT_ASSIGN_OBDATA, false);
1260}
1261
1263 const uint *remap,
1264 const int remap_num)
1265{
1266 /* The "material_index" attribute may contain values outside the valid material range
1267 * (it's only clamped on read), so skip indices that don't map into the material array. */
1268 bke::SpanAttributeWriter<int> material_indices = attributes.lookup_for_write_span<int>(
1269 "material_index");
1270 if (!material_indices) {
1271 return;
1272 }
1273 for (const int i : material_indices.span.index_range()) {
1274 const int index = material_indices.span[i];
1275 if (IndexRange(remap_num).contains(index)) {
1276 material_indices.span[i] = remap[index];
1277 }
1278 }
1279 material_indices.finish();
1280}
1281
1283{
1285 const short *totcol_p = BKE_object_material_len_p(ob);
1286
1287 BLI_array_permute(ob->mat, ob->totcol, remap);
1288
1289 if (ob->matbits) {
1290 BLI_array_permute(ob->matbits, ob->totcol, remap);
1291 }
1292
1293 if (matar) {
1294 BLI_array_permute(*matar, *totcol_p, remap);
1295 }
1296
1297 if (ob->type == OB_MESH) {
1298 BKE_mesh_material_remap(id_cast<Mesh *>(ob->data), remap, ob->totcol);
1299 }
1300 else if (ELEM(ob->type, OB_CURVES_LEGACY, OB_SURF, OB_FONT)) {
1301 BKE_curve_material_remap(id_cast<Curve *>(ob->data), remap, ob->totcol);
1302 }
1303 else if (ob->type == OB_GREASE_PENCIL) {
1304 BKE_grease_pencil_material_remap(id_cast<GreasePencil *>(ob->data), remap, ob->totcol);
1305 }
1306 else if (ob->type == OB_CURVES) {
1307 BKE_curves_material_remap(id_cast<Curves *>(ob->data), remap, ob->totcol);
1308 }
1309 else if (ob->type == OB_POINTCLOUD) {
1310 BKE_pointcloud_material_remap(id_cast<PointCloud *>(ob->data), remap, ob->totcol);
1311 }
1312 else if (ob->type == OB_VOLUME) {
1313 /* Material support doesn't store "indices".
1314 * The way "baked" materials are stored means they store ID's and don't need remapping. */
1315 }
1316 else if (ob->type == OB_MBALL) {
1317 /* While meta-balls have a material array, they only use the first material slot
1318 * (no support for mixing materials). */
1319 }
1320 else {
1321 /* add support for this object data! */
1322 BLI_assert(matar == nullptr);
1323 }
1324}
1325
1326void BKE_object_material_remap_calc(Object *ob_dst, Object *ob_src, short *remap_src_to_dst)
1327{
1328 if (ob_src->totcol == 0) {
1329 return;
1330 }
1331
1332 Map<Material *, int> mat_map;
1333 mat_map.reserve(ob_src->totcol);
1334
1335 for (int i = 0; i < ob_dst->totcol; i++) {
1336 Material *ma_src = BKE_object_material_get(ob_dst, i + 1);
1337 mat_map.add(ma_src, i);
1338 }
1339
1340 /* setup default mapping (when materials don't match) */
1341 {
1342 int i = 0;
1343 if (ob_dst->totcol >= ob_src->totcol) {
1344 for (; i < ob_src->totcol; i++) {
1345 remap_src_to_dst[i] = i;
1346 }
1347 }
1348 else {
1349 for (; i < ob_dst->totcol; i++) {
1350 remap_src_to_dst[i] = i;
1351 }
1352 for (; i < ob_src->totcol; i++) {
1353 remap_src_to_dst[i] = 0;
1354 }
1355 }
1356 }
1357
1358 for (int i = 0; i < ob_src->totcol; i++) {
1359 Material *ma_src = BKE_object_material_get(ob_src, i + 1);
1360
1361 if ((i < ob_dst->totcol) && (ma_src == BKE_object_material_get(ob_dst, i + 1))) {
1362 /* when objects have exact matching materials - keep existing index */
1363 }
1364 else {
1365 if (const int *index_src_p = mat_map.lookup_ptr(ma_src)) {
1366 remap_src_to_dst[i] = *index_src_p;
1367 }
1368 }
1369 }
1370}
1371
1372void BKE_object_material_from_eval_data(Main *bmain, Object *ob_orig, const ID *data_eval)
1373{
1374 ID *data_orig = ob_orig->data;
1375
1376 short *orig_totcol = BKE_id_material_len_p(data_orig);
1377 Material ***orig_mat = BKE_id_material_array_p(data_orig);
1378
1379 /* Can cast away const, because the data is not changed. */
1380 const short *eval_totcol = BKE_id_material_len_p(const_cast<ID *>(data_eval));
1381 Material ***eval_mat = BKE_id_material_array_p(const_cast<ID *>(data_eval));
1382
1383 if (ELEM(nullptr, orig_totcol, orig_mat, eval_totcol, eval_mat)) {
1384 return;
1385 }
1386
1387 /* Remove old materials from original geometry. */
1388 for (int i = 0; i < *orig_totcol; i++) {
1389 id_us_min(&(*orig_mat)[i]->id);
1390 }
1391 MEM_SAFE_DELETE(*orig_mat);
1392
1393 /* Create new material slots based on materials on evaluated geometry. */
1394 *orig_totcol = *eval_totcol;
1395 *orig_mat = *eval_totcol > 0 ? MEM_new_array_zeroed<Material *>(*eval_totcol, __func__) :
1396 nullptr;
1397 for (int i = 0; i < *eval_totcol; i++) {
1398 Material *material_eval = (*eval_mat)[i];
1399 if (material_eval != nullptr) {
1400 Material *material_orig = DEG_get_original(material_eval);
1401 (*orig_mat)[i] = material_orig;
1402 id_us_plus(&material_orig->id);
1403 }
1404 }
1405 BKE_object_materials_sync_length(bmain, ob_orig, data_orig);
1406}
1407
1409 Main *bmain, Object *ob, Material ***matar, int totcol, const bool to_object_only)
1410{
1411 int actcol_orig = ob->actcol;
1412
1413 while ((ob->totcol > totcol) && BKE_object_material_slot_remove(bmain, ob)) {
1414 /* pass */
1415 }
1416
1417 /* now we have the right number of slots */
1418 for (int i = 0; i < totcol; i++) {
1419 if (to_object_only && ob->matbits && ob->matbits[i] == 0) {
1420 /* If we only assign to object, and that slot uses obdata material, do nothing. */
1421 continue;
1422 }
1424 ob,
1425 (*matar)[i],
1426 i + 1,
1428 }
1429
1430 actcol_orig = std::min(actcol_orig, ob->totcol);
1431
1432 ob->actcol = actcol_orig;
1433}
1434
1436{
1437 Material ***matarar;
1438 short a, *totcolp;
1439
1440 if (ma == nullptr) {
1441 return 0;
1442 }
1443
1444 totcolp = BKE_object_material_len_p(ob);
1445 matarar = BKE_object_material_array_p(ob);
1446
1447 if (totcolp == nullptr || matarar == nullptr) {
1448 return 0;
1449 }
1450
1451 for (a = 0; a < *totcolp; a++) {
1452 if ((*matarar)[a] == ma) {
1453 break;
1454 }
1455 }
1456 if (a < *totcolp) {
1457 return a + 1;
1458 }
1459 return 0;
1460}
1461
1462bool BKE_object_material_slot_add(Main *bmain, Object *ob, const bool set_active)
1463{
1464 if (ob == nullptr) {
1465 return false;
1466 }
1467 if (ob->totcol >= MAXMAT) {
1468 return false;
1469 }
1470
1471 BKE_object_material_assign(bmain, ob, nullptr, ob->totcol + 1, BKE_MAT_ASSIGN_USERPREF);
1472 if (set_active) {
1473 ob->actcol = ob->totcol;
1474 }
1475 return true;
1476}
1477
1478/* ****************** */
1479
1481{
1482 Material *mao, ***matarar;
1483 short *totcolp;
1484
1485 if (ob == nullptr || ob->totcol == 0) {
1486 return false;
1487 }
1488
1489 /* this should never happen and used to crash */
1490 if (ob->actcol <= 0) {
1491 CLOG_ERROR(&LOG, "invalid material index %d, report a bug!", ob->actcol);
1492 return false;
1493 }
1494
1495 /* Take a mesh/curve/meta-ball as starting point, remove 1 index,
1496 * AND with all objects that share the `ob->data`.
1497 * After that check indices in mesh/curve/meta-ball! */
1498
1499 totcolp = BKE_object_material_len_p(ob);
1500 matarar = BKE_object_material_array_p(ob);
1501
1502 if (ELEM(nullptr, matarar, *matarar)) {
1503 return false;
1504 }
1505
1506 /* can happen on face selection in editmode */
1508
1509 /* we delete the actcol */
1510 mao = (*matarar)[ob->actcol - 1];
1511 if (mao) {
1512 id_us_min(&mao->id);
1513 }
1514
1515 for (int a = ob->actcol; a < ob->totcol; a++) {
1516 (*matarar)[a - 1] = (*matarar)[a];
1517 }
1518 (*totcolp)--;
1519
1520 if (*totcolp == 0) {
1521 MEM_delete(*matarar);
1522 *matarar = nullptr;
1523 }
1524
1525 const int actcol = ob->actcol;
1526
1527 for (Object *obt = bmain->objects.first(); obt; obt = static_cast<Object *>(obt->id.next)) {
1528 if (obt->data == ob->data) {
1529 /* Can happen when object material lists are used, see: #52953 */
1530 if (actcol > obt->totcol) {
1531 continue;
1532 }
1533 /* WATCH IT: do not use actcol from ob or from obt (can become zero) */
1534 mao = obt->mat[actcol - 1];
1535 if (mao) {
1536 id_us_min(&mao->id);
1537 }
1538
1539 for (int a = actcol; a < obt->totcol; a++) {
1540 obt->mat[a - 1] = obt->mat[a];
1541 obt->matbits[a - 1] = obt->matbits[a];
1542 }
1543 obt->totcol--;
1545
1546 if (obt->totcol == 0) {
1547 MEM_delete(obt->mat);
1548 MEM_delete(obt->matbits);
1549 obt->mat = nullptr;
1550 obt->matbits = nullptr;
1551 }
1552 }
1553 }
1554
1555 /* check indices from mesh and grease pencil. */
1557 material_data_index_remove_id(ob->data, actcol - 1);
1558 if (ob->runtime->curve_cache) {
1560 }
1561 }
1562
1563 return true;
1564}
1565
1567{
1568 int actcol = ob->actcol;
1569 int removed = 0;
1570
1571 for (int slot = 1; slot <= ob->totcol; slot++) {
1572 while (slot <= ob->totcol && !BKE_object_material_slot_used(ob, slot)) {
1573 ob->actcol = slot;
1574
1575 if (!BKE_object_material_slot_remove(bmain, ob)) {
1576 break;
1577 }
1578
1579 if (actcol >= slot) {
1580 actcol--;
1581 }
1582
1583 removed++;
1584 }
1585 }
1586
1587 ob->actcol = actcol;
1589
1590 return removed;
1591}
1592
1594{
1595 int actcol = ob->actcol;
1596 int removed = 0;
1597
1598 for (int slot = 1; slot <= ob->totcol; slot++) {
1599 while (slot <= ob->totcol) {
1600 ob->actcol = slot;
1601
1602 if (!BKE_object_material_slot_remove(bmain, ob)) {
1603 break;
1604 }
1605
1606 if (actcol >= slot) {
1607 actcol--;
1608 }
1609
1610 removed++;
1611 }
1612 }
1613 ob->actcol = actcol;
1615
1616 return removed;
1617}
1618
1620{
1621 for (bNodeSocket &sock : node->inputs) {
1622 if (sock.link) {
1623 bNode *inode = sock.link->fromnode;
1624 if (inode->typeinfo->nclass == NODE_CLASS_INPUT &&
1626 {
1627 return inode;
1628 }
1629
1630 return nodetree_uv_node_recursive(inode);
1631 }
1632 }
1633
1634 return nullptr;
1635}
1636
1643
1644using ForEachTexNodeCallback = bool (*)(bNodeTree *nodetree, bNode *node, void *userdata);
1646 ForEachTexNodeCallback callback,
1647 void *userdata,
1648 ePaintSlotFilter slot_filter)
1649{
1650 const bool do_image_nodes = (slot_filter & PAINT_SLOT_IMAGE) != 0;
1651 const bool do_color_attributes = (slot_filter & PAINT_SLOT_COLOR_ATTRIBUTE) != 0;
1652 for (bNode *node : nodetree->all_nodes()) {
1653 if (do_image_nodes && node->typeinfo->nclass == NODE_CLASS_TEXTURE &&
1654 node->typeinfo->type_legacy == SH_NODE_TEX_IMAGE && node->id)
1655 {
1656 if (!callback(nodetree, node, userdata)) {
1657 return false;
1658 }
1659 }
1660 if (do_color_attributes && node->typeinfo->type_legacy == SH_NODE_ATTRIBUTE) {
1661 if (!callback(nodetree, node, userdata)) {
1662 return false;
1663 }
1664 }
1665 else if (node->is_group() && node->id) {
1666 /* recurse into the node group and see if it contains any textures */
1667 if (!ntree_foreach_texnode_recursive(
1668 id_cast<bNodeTree *>(node->id), callback, userdata, slot_filter))
1669 {
1670 return false;
1671 }
1672 }
1673 }
1674 return true;
1675}
1676
1677static bool count_texture_nodes_cb(bNodeTree * /*nodetree*/, bNode * /*node*/, void *userdata)
1678{
1679 (*(static_cast<int *>(userdata)))++;
1680 return true;
1681}
1682
1684{
1685 int tex_nodes = 0;
1686 ntree_foreach_texnode_recursive(nodetree, count_texture_nodes_cb, &tex_nodes, slot_filter);
1687
1688 return tex_nodes;
1689}
1690
1698
1699static bool fill_texpaint_slots_cb(bNodeTree * /*nodetree*/, bNode *node, void *userdata)
1700{
1701 FillTexPaintSlotsData *fill_data = static_cast<FillTexPaintSlotsData *>(userdata);
1702
1703 Material *ma = fill_data->ma;
1704 int index = fill_data->index;
1705 fill_data->index++;
1706
1707 if (fill_data->active_node == node) {
1708 ma->paint_active_slot = index;
1709 }
1710
1711 switch (node->type_legacy) {
1712 case SH_NODE_TEX_IMAGE: {
1713 TexPaintSlot *slot = &ma->texpaintslot[index];
1714 slot->ima = id_cast<Image *>(node->id);
1715 NodeTexImage *storage = static_cast<NodeTexImage *>(node->storage);
1716 slot->interp = storage->interpolation;
1717 slot->image_user = &storage->iuser;
1718 /* For new renderer, we need to traverse the tree back in search of a UV node. */
1720
1721 if (uvnode) {
1722 NodeShaderUVMap *uv_storage = static_cast<NodeShaderUVMap *>(uvnode->storage);
1723 slot->uvname = uv_storage->uv_map;
1724 /* set a value to index so UI knows that we have a valid pointer for the mesh */
1725 slot->valid = true;
1726 }
1727 else {
1728 /* just invalidate the index here so UV map does not get displayed on the UI */
1729 slot->valid = false;
1730 }
1731 break;
1732 }
1733
1734 case SH_NODE_ATTRIBUTE: {
1735 TexPaintSlot *slot = &ma->texpaintslot[index];
1736 NodeShaderAttribute *storage = static_cast<NodeShaderAttribute *>(node->storage);
1737 slot->attribute_name = storage->name;
1738 if (storage->type == SHD_ATTRIBUTE_GEOMETRY) {
1739 const Mesh *mesh = id_cast<const Mesh *>(fill_data->ob->data);
1740 if (const BMesh *bm = BKE_editmesh_bmesh_get(mesh)) {
1741 const BMDataLayerLookup attr = BM_data_layer_lookup(*bm, storage->name);
1742 slot->valid = attr && bke::mesh::is_color_attribute({attr.domain, attr.type});
1743 }
1744 else {
1745 const bke::AttributeAccessor attributes = mesh->attributes();
1747 }
1748 }
1749
1750 /* Do not show unsupported attributes. */
1751 if (!slot->valid) {
1752 slot->attribute_name = nullptr;
1753 fill_data->index--;
1754 }
1755
1756 break;
1757 }
1758 }
1759
1760 return fill_data->index != fill_data->slot_len;
1761}
1762
1764 bNode *active_node,
1765 const Object *ob,
1766 Material *ma,
1767 int slot_len,
1768 ePaintSlotFilter slot_filter)
1769{
1770 FillTexPaintSlotsData fill_data = {active_node, ob, ma, 0, slot_len};
1771 ntree_foreach_texnode_recursive(nodetree, fill_texpaint_slots_cb, &fill_data, slot_filter);
1772}
1773
1775{
1776 if (!ma) {
1777 return;
1778 }
1779
1780 const ePaintSlotFilter slot_filter = PAINT_SLOT_IMAGE;
1781
1782 const TexPaintSlot *prev_texpaintslot = ma->texpaintslot;
1783 const int prev_paint_active_slot = ma->paint_active_slot;
1784 const int prev_paint_clone_slot = ma->paint_clone_slot;
1785 const int prev_tot_slots = ma->tot_slots;
1786
1787 ma->texpaintslot = nullptr;
1788 ma->tot_slots = 0;
1789
1791 ma->paint_active_slot = 0;
1792 ma->paint_clone_slot = 0;
1793 }
1794 else if (!(ma->nodetree)) {
1795 ma->paint_active_slot = 0;
1796 ma->paint_clone_slot = 0;
1797 }
1798 else {
1799 int count = count_texture_nodes_recursive(ma->nodetree, slot_filter);
1800
1801 if (count == 0) {
1802 ma->paint_active_slot = 0;
1803 ma->paint_clone_slot = 0;
1804 }
1805 else {
1806 ma->texpaintslot = MEM_new_array<TexPaintSlot>(count, "texpaint_slots");
1807
1808 bNode *active_node = bke::node_get_active_paint_canvas(*ma->nodetree);
1809
1810 fill_texpaint_slots_recursive(ma->nodetree, active_node, ob, ma, count, slot_filter);
1811
1812 ma->tot_slots = count;
1813
1814 if (ma->paint_active_slot >= count) {
1815 ma->paint_active_slot = count - 1;
1816 }
1817
1818 if (ma->paint_clone_slot >= count) {
1819 ma->paint_clone_slot = count - 1;
1820 }
1821 }
1822 }
1823
1824 /* Copy-on-eval needed when adding texture slot on an object with no materials.
1825 * But do it only when slots actually change to avoid continuous depsgraph updates. */
1826 if (ma->tot_slots != prev_tot_slots || ma->paint_active_slot != prev_paint_active_slot ||
1827 ma->paint_clone_slot != prev_paint_clone_slot ||
1828 (ma->texpaintslot && prev_texpaintslot &&
1829 memcmp(ma->texpaintslot, prev_texpaintslot, sizeof(*ma->texpaintslot) * ma->tot_slots) !=
1830 0))
1831 {
1833 }
1834
1835 MEM_SAFE_DELETE(prev_texpaintslot);
1836}
1837
1839{
1840 for (int i = 1; i < ob->totcol + 1; i++) {
1841 Material *ma = BKE_object_material_get(ob, i);
1842 BKE_texpaint_slot_refresh_cache(scene, ma, ob);
1843 }
1844}
1845
1851
1852static bool texpaint_slot_node_find_cb(bNodeTree *nodetree, bNode *node, void *userdata)
1853{
1854 FindTexPaintNodeData *find_data = static_cast<FindTexPaintNodeData *>(userdata);
1855 if (find_data->slot->ima && node->type_legacy == SH_NODE_TEX_IMAGE) {
1856 Image *node_ima = id_cast<Image *>(node->id);
1857 if (find_data->slot->ima == node_ima) {
1858 find_data->r_nodetree = nodetree;
1859 find_data->r_node = node;
1860 return false;
1861 }
1862 }
1863
1864 if (find_data->slot->attribute_name && node->type_legacy == SH_NODE_ATTRIBUTE) {
1865 NodeShaderAttribute *storage = static_cast<NodeShaderAttribute *>(node->storage);
1866 if (STREQLEN(find_data->slot->attribute_name, storage->name, sizeof(storage->name))) {
1867 find_data->r_nodetree = nodetree;
1868 find_data->r_node = node;
1869 return false;
1870 }
1871 }
1872
1873 return true;
1874}
1875
1876std::pair<bNodeTree *, bNode *> BKE_texpaint_slot_material_find_node(Material *ma,
1877 short texpaint_slot)
1878{
1879 if (ma->texpaintslot == nullptr) {
1880 return {};
1881 }
1882
1883 if (texpaint_slot >= ma->tot_slots) {
1884 return {};
1885 }
1886
1887 TexPaintSlot *slot = &ma->texpaintslot[texpaint_slot];
1888 FindTexPaintNodeData find_data = {slot, nullptr};
1891 &find_data,
1893
1894 return std::pair<bNodeTree *, bNode *>(find_data.r_nodetree, find_data.r_node);
1895}
1896
1897void ramp_blend(int type, float r_col[4], const float fac, const float col[4])
1898{
1899 float tmp, facm = 1.0f - fac;
1900
1901 switch (type) {
1902 case MA_RAMP_BLEND:
1903 r_col[0] = facm * (r_col[0]) + fac * col[0];
1904 r_col[1] = facm * (r_col[1]) + fac * col[1];
1905 r_col[2] = facm * (r_col[2]) + fac * col[2];
1906 r_col[3] = facm * (r_col[3]) + fac * col[3];
1907 break;
1908 case MA_RAMP_ADD:
1909 r_col[0] += fac * col[0];
1910 r_col[1] += fac * col[1];
1911 r_col[2] += fac * col[2];
1912 break;
1913 case MA_RAMP_MULT:
1914 r_col[0] *= (facm + fac * col[0]);
1915 r_col[1] *= (facm + fac * col[1]);
1916 r_col[2] *= (facm + fac * col[2]);
1917 break;
1918 case MA_RAMP_SCREEN:
1919 r_col[0] = 1.0f - (facm + fac * (1.0f - col[0])) * (1.0f - r_col[0]);
1920 r_col[1] = 1.0f - (facm + fac * (1.0f - col[1])) * (1.0f - r_col[1]);
1921 r_col[2] = 1.0f - (facm + fac * (1.0f - col[2])) * (1.0f - r_col[2]);
1922 break;
1923 case MA_RAMP_OVERLAY:
1924 if (r_col[0] < 0.5f) {
1925 r_col[0] *= (facm + 2.0f * fac * col[0]);
1926 }
1927 else {
1928 r_col[0] = 1.0f - (facm + 2.0f * fac * (1.0f - col[0])) * (1.0f - r_col[0]);
1929 }
1930 if (r_col[1] < 0.5f) {
1931 r_col[1] *= (facm + 2.0f * fac * col[1]);
1932 }
1933 else {
1934 r_col[1] = 1.0f - (facm + 2.0f * fac * (1.0f - col[1])) * (1.0f - r_col[1]);
1935 }
1936 if (r_col[2] < 0.5f) {
1937 r_col[2] *= (facm + 2.0f * fac * col[2]);
1938 }
1939 else {
1940 r_col[2] = 1.0f - (facm + 2.0f * fac * (1.0f - col[2])) * (1.0f - r_col[2]);
1941 }
1942 break;
1943 case MA_RAMP_SUB:
1944 r_col[0] -= fac * col[0];
1945 r_col[1] -= fac * col[1];
1946 r_col[2] -= fac * col[2];
1947 break;
1948 case MA_RAMP_DIV:
1949 if (col[0] != 0.0f) {
1950 r_col[0] = facm * (r_col[0]) + fac * (r_col[0]) / col[0];
1951 }
1952 if (col[1] != 0.0f) {
1953 r_col[1] = facm * (r_col[1]) + fac * (r_col[1]) / col[1];
1954 }
1955 if (col[2] != 0.0f) {
1956 r_col[2] = facm * (r_col[2]) + fac * (r_col[2]) / col[2];
1957 }
1958 break;
1959 case MA_RAMP_DIFF:
1960 r_col[0] = facm * (r_col[0]) + fac * fabsf(r_col[0] - col[0]);
1961 r_col[1] = facm * (r_col[1]) + fac * fabsf(r_col[1] - col[1]);
1962 r_col[2] = facm * (r_col[2]) + fac * fabsf(r_col[2] - col[2]);
1963 break;
1964 case MA_RAMP_EXCLUSION:
1965 r_col[0] = max_ff(facm * (r_col[0]) + fac * (r_col[0] + col[0] - 2.0f * r_col[0] * col[0]),
1966 0.0f);
1967 r_col[1] = max_ff(facm * (r_col[1]) + fac * (r_col[1] + col[1] - 2.0f * r_col[1] * col[1]),
1968 0.0f);
1969 r_col[2] = max_ff(facm * (r_col[2]) + fac * (r_col[2] + col[2] - 2.0f * r_col[2] * col[2]),
1970 0.0f);
1971 break;
1972 case MA_RAMP_DARK:
1973 r_col[0] = min_ff(r_col[0], col[0]) * fac + r_col[0] * facm;
1974 r_col[1] = min_ff(r_col[1], col[1]) * fac + r_col[1] * facm;
1975 r_col[2] = min_ff(r_col[2], col[2]) * fac + r_col[2] * facm;
1976 break;
1977 case MA_RAMP_LIGHT:
1978 r_col[0] = max_ff(r_col[0], col[0]) * fac + r_col[0] * facm;
1979 r_col[1] = max_ff(r_col[1], col[1]) * fac + r_col[1] * facm;
1980 r_col[2] = max_ff(r_col[2], col[2]) * fac + r_col[2] * facm;
1981 break;
1982 case MA_RAMP_DODGE:
1983 if (r_col[0] != 0.0f) {
1984 tmp = 1.0f - fac * col[0];
1985 if (tmp <= 0.0f) {
1986 r_col[0] = 1.0f;
1987 }
1988 else if ((tmp = (r_col[0]) / tmp) > 1.0f) {
1989 r_col[0] = 1.0f;
1990 }
1991 else {
1992 r_col[0] = tmp;
1993 }
1994 }
1995 if (r_col[1] != 0.0f) {
1996 tmp = 1.0f - fac * col[1];
1997 if (tmp <= 0.0f) {
1998 r_col[1] = 1.0f;
1999 }
2000 else if ((tmp = (r_col[1]) / tmp) > 1.0f) {
2001 r_col[1] = 1.0f;
2002 }
2003 else {
2004 r_col[1] = tmp;
2005 }
2006 }
2007 if (r_col[2] != 0.0f) {
2008 tmp = 1.0f - fac * col[2];
2009 if (tmp <= 0.0f) {
2010 r_col[2] = 1.0f;
2011 }
2012 else if ((tmp = (r_col[2]) / tmp) > 1.0f) {
2013 r_col[2] = 1.0f;
2014 }
2015 else {
2016 r_col[2] = tmp;
2017 }
2018 }
2019 break;
2020 case MA_RAMP_BURN:
2021 tmp = facm + fac * col[0];
2022
2023 if (tmp <= 0.0f) {
2024 r_col[0] = 0.0f;
2025 }
2026 else if ((tmp = (1.0f - (1.0f - (r_col[0])) / tmp)) < 0.0f) {
2027 r_col[0] = 0.0f;
2028 }
2029 else if (tmp > 1.0f) {
2030 r_col[0] = 1.0f;
2031 }
2032 else {
2033 r_col[0] = tmp;
2034 }
2035
2036 tmp = facm + fac * col[1];
2037 if (tmp <= 0.0f) {
2038 r_col[1] = 0.0f;
2039 }
2040 else if ((tmp = (1.0f - (1.0f - (r_col[1])) / tmp)) < 0.0f) {
2041 r_col[1] = 0.0f;
2042 }
2043 else if (tmp > 1.0f) {
2044 r_col[1] = 1.0f;
2045 }
2046 else {
2047 r_col[1] = tmp;
2048 }
2049
2050 tmp = facm + fac * col[2];
2051 if (tmp <= 0.0f) {
2052 r_col[2] = 0.0f;
2053 }
2054 else if ((tmp = (1.0f - (1.0f - (r_col[2])) / tmp)) < 0.0f) {
2055 r_col[2] = 0.0f;
2056 }
2057 else if (tmp > 1.0f) {
2058 r_col[2] = 1.0f;
2059 }
2060 else {
2061 r_col[2] = tmp;
2062 }
2063 break;
2064 case MA_RAMP_HUE: {
2065 float rH, rS, rV;
2066 float colH, colS, colV;
2067 float tmpr, tmpg, tmpb;
2068 rgb_to_hsv(col[0], col[1], col[2], &colH, &colS, &colV);
2069 if (colS != 0) {
2070 rgb_to_hsv(r_col[0], r_col[1], r_col[2], &rH, &rS, &rV);
2071 hsv_to_rgb(colH, rS, rV, &tmpr, &tmpg, &tmpb);
2072 r_col[0] = facm * (r_col[0]) + fac * tmpr;
2073 r_col[1] = facm * (r_col[1]) + fac * tmpg;
2074 r_col[2] = facm * (r_col[2]) + fac * tmpb;
2075 }
2076 break;
2077 }
2078 case MA_RAMP_SAT: {
2079 float rH, rS, rV;
2080 float colH, colS, colV;
2081 rgb_to_hsv(r_col[0], r_col[1], r_col[2], &rH, &rS, &rV);
2082 if (rS != 0) {
2083 rgb_to_hsv(col[0], col[1], col[2], &colH, &colS, &colV);
2084 hsv_to_rgb(rH, (facm * rS + fac * colS), rV, r_col + 0, r_col + 1, r_col + 2);
2085 }
2086 break;
2087 }
2088 case MA_RAMP_VAL: {
2089 float rH, rS, rV;
2090 float colH, colS, colV;
2091 rgb_to_hsv(r_col[0], r_col[1], r_col[2], &rH, &rS, &rV);
2092 rgb_to_hsv(col[0], col[1], col[2], &colH, &colS, &colV);
2093 hsv_to_rgb(rH, rS, (facm * rV + fac * colV), r_col + 0, r_col + 1, r_col + 2);
2094 break;
2095 }
2096 case MA_RAMP_COLOR: {
2097 float rH, rS, rV;
2098 float colH, colS, colV;
2099 float tmpr, tmpg, tmpb;
2100 rgb_to_hsv(col[0], col[1], col[2], &colH, &colS, &colV);
2101 if (colS != 0) {
2102 rgb_to_hsv(r_col[0], r_col[1], r_col[2], &rH, &rS, &rV);
2103 hsv_to_rgb(colH, colS, rV, &tmpr, &tmpg, &tmpb);
2104 r_col[0] = facm * (r_col[0]) + fac * tmpr;
2105 r_col[1] = facm * (r_col[1]) + fac * tmpg;
2106 r_col[2] = facm * (r_col[2]) + fac * tmpb;
2107 }
2108 break;
2109 }
2110 case MA_RAMP_SOFT: {
2111 float scr, scg, scb;
2112
2113 /* first calculate non-fac based Screen mix */
2114 scr = 1.0f - (1.0f - col[0]) * (1.0f - r_col[0]);
2115 scg = 1.0f - (1.0f - col[1]) * (1.0f - r_col[1]);
2116 scb = 1.0f - (1.0f - col[2]) * (1.0f - r_col[2]);
2117
2118 r_col[0] = facm * (r_col[0]) +
2119 fac * (((1.0f - r_col[0]) * col[0] * (r_col[0])) + (r_col[0] * scr));
2120 r_col[1] = facm * (r_col[1]) +
2121 fac * (((1.0f - r_col[1]) * col[1] * (r_col[1])) + (r_col[1] * scg));
2122 r_col[2] = facm * (r_col[2]) +
2123 fac * (((1.0f - r_col[2]) * col[2] * (r_col[2])) + (r_col[2] * scb));
2124 break;
2125 }
2126 case MA_RAMP_LINEAR:
2127 if (col[0] > 0.5f) {
2128 r_col[0] = r_col[0] + fac * (2.0f * (col[0] - 0.5f));
2129 }
2130 else {
2131 r_col[0] = r_col[0] + fac * (2.0f * (col[0]) - 1.0f);
2132 }
2133 if (col[1] > 0.5f) {
2134 r_col[1] = r_col[1] + fac * (2.0f * (col[1] - 0.5f));
2135 }
2136 else {
2137 r_col[1] = r_col[1] + fac * (2.0f * (col[1]) - 1.0f);
2138 }
2139 if (col[2] > 0.5f) {
2140 r_col[2] = r_col[2] + fac * (2.0f * (col[2] - 0.5f));
2141 }
2142 else {
2143 r_col[2] = r_col[2] + fac * (2.0f * (col[2]) - 1.0f);
2144 }
2145 break;
2146 }
2147}
2148
2149void BKE_material_eval(Depsgraph *depsgraph, Material *material)
2150{
2151 DEG_debug_print_eval(depsgraph, __func__, material->id.name, material);
2152 GPU_material_free(&material->gpumaterial);
2153}
2154
2155/* Default Materials
2156 *
2157 * Used for rendering when objects have no materials assigned, and initializing
2158 * default shader nodes. */
2159
2166
2174
2175static Material *material_default_create(Material **ma_p, const char *name)
2176{
2177 *ma_p = BKE_id_new_nomain<Material>(name);
2178 return *ma_p;
2179}
2180
2182{
2183 Material *ma = material_default_create(ma_p, "Default GPencil");
2184
2186 add_v3_fl(&ma->gp_style->stroke_rgba[0], 0.6f);
2187}
2188
2190{
2191 Material *ma = material_default_create(ma_p, "Default Surface");
2192 bNodeTree *ntree = ma->nodetree;
2193
2194 bNode *principled = bke::node_add_static_node(nullptr, *ntree, SH_NODE_BSDF_PRINCIPLED);
2195 bNodeSocket *base_color = bke::node_find_socket(*principled, SOCK_IN, "Base Color"_ustr);
2196 copy_v3_v3((static_cast<bNodeSocketValueRGBA *>(base_color->default_value))->value, &ma->r);
2197
2199
2200 bke::node_add_link(*ntree,
2201 *principled,
2202 *bke::node_find_socket(*principled, SOCK_OUT, "BSDF"_ustr),
2203 *output,
2204 *bke::node_find_socket(*output, SOCK_IN, "Surface"_ustr));
2205
2206 principled->location[0] = -200.0f;
2207 principled->location[1] = 100.0f;
2208 output->location[0] = 200.0f;
2209 output->location[1] = 100.0f;
2210
2211 bke::node_set_active(*ntree, *output);
2213}
2214
2216{
2217 Material *ma = material_default_create(ma_p, "Default Volume");
2218 bNodeTree *ntree = ma->nodetree;
2219
2220 bNode *principled = bke::node_add_static_node(nullptr, *ntree, SH_NODE_VOLUME_PRINCIPLED);
2222
2223 bke::node_add_link(*ntree,
2224 *principled,
2225 *bke::node_find_socket(*principled, SOCK_OUT, "Volume"_ustr),
2226 *output,
2227 *bke::node_find_socket(*output, SOCK_IN, "Volume"_ustr));
2228
2229 principled->location[0] = -200.0f;
2230 principled->location[1] = 100.0f;
2231 output->location[0] = 200.0f;
2232 output->location[1] = 100.0f;
2233
2234 bke::node_set_active(*ntree, *output);
2236}
2237
2239{
2240 Material *ma = material_default_create(ma_p, "Default Holdout");
2241 bNodeTree *ntree = ma->nodetree;
2242
2243 bNode *holdout = bke::node_add_static_node(nullptr, *ntree, SH_NODE_HOLDOUT);
2245
2246 bke::node_add_link(*ntree,
2247 *holdout,
2248 *bke::node_find_socket(*holdout, SOCK_OUT, "Holdout"_ustr),
2249 *output,
2250 *bke::node_find_socket(*output, SOCK_IN, "Surface"_ustr));
2251
2252 holdout->location[0] = 10.0f;
2253 holdout->location[1] = 300.0f;
2254 output->location[0] = 300.0f;
2255 output->location[1] = 300.0f;
2256
2257 bke::node_set_active(*ntree, *output);
2259}
2260
2262{
2263 Material *ma = material_default_create(ma_p, "Default Gaussian Splat");
2264 bNodeTree *ntree = ma->nodetree;
2265
2267 NodeShaderAttribute *storage = static_cast<NodeShaderAttribute *>(attribute->storage);
2268 STRNCPY(storage->name, "radiance");
2269
2270 bNode *emission = bke::node_add_static_node(nullptr, *ntree, SH_NODE_EMISSION);
2271 bNodeSocket *color = bke::node_find_socket(*emission, SOCK_IN, "Color"_ustr);
2272 copy_v3_v3((static_cast<bNodeSocketValueRGBA *>(color->default_value))->value, &ma->r);
2273
2275
2276 bke::node_add_link(*ntree,
2277 *attribute,
2278 *bke::node_find_socket(*attribute, SOCK_OUT, "Color"_ustr),
2279 *emission,
2280 *color);
2281
2282 bke::node_add_link(*ntree,
2283 *emission,
2284 *bke::node_find_socket(*emission, SOCK_OUT, "Emission"_ustr),
2285 *output,
2286 *bke::node_find_socket(*output, SOCK_IN, "Surface"_ustr));
2287
2288 attribute->location[0] = -300.0f;
2289 attribute->location[1] = 100.0f;
2290 emission->location[0] = -50.0f;
2291 emission->location[1] = 100.0f;
2292 output->location[0] = 200.0f;
2293 output->location[1] = 100.0f;
2294
2295 bke::node_set_active(*ntree, *output);
2297}
2298
2303
2308
2313
2318
2323
2328
2330{
2331 for (int i = 0; default_materials[i]; i++) {
2332 Material *ma = *default_materials[i];
2333 if (ma && ma->gpumaterial.first()) {
2334 GPU_material_free(&ma->gpumaterial);
2335 }
2336 }
2337}
2338
2339/* Module functions called on startup and exit. */
2340
2342{
2343 for (int i = 0; default_materials[i]; i++) {
2344 BLI_assert_msg(*default_materials[i] == nullptr,
2345 "Default material pointers should always be null when initializing them, maybe "
2346 "missing a call to `BKE_materials_exit` first?");
2347 }
2348
2355}
2356
2358{
2359 for (int i = 0; default_materials[i]; i++) {
2360 Material *ma = *default_materials[i];
2361 *default_materials[i] = nullptr;
2362 if (ma) {
2363 BKE_id_free(nullptr, &ma->id);
2364 }
2365 }
2366}
2367
2368} // namespace blender
Generic geometry attributes built on CustomData.
Low-level operations for curves.
display list (or rather multi purpose list) stuff.
Low-level operations for grease pencil.
#define BKE_LIB_FOREACHID_PROCESS_FUNCTION_CALL(data_, func_call_)
#define BKE_LIB_FOREACHID_PROCESS_IDSUPER(data_, id_super_, cb_flag_)
General operations, lookup, etc. for materials.
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define SH_NODE_HOLDOUT
#define SH_NODE_UVMAP
#define SH_NODE_TEX_IMAGE
#define SH_NODE_BSDF_PRINCIPLED
#define SH_NODE_EMISSION
#define SH_NODE_OUTPUT_MATERIAL
#define SH_NODE_VOLUME_PRINCIPLED
#define SH_NODE_ATTRIBUTE
General operations, lookup, etc. for blender objects.
General operations for point clouds.
Generic array manipulation API.
#define BLI_array_permute(arr, arr_len, order)
#define ENUM_OPERATORS(_enum_type)
#define BLO_read_struct(reader, struct_name, ptr_p)
#define BLT_I18NCONTEXT_ID_MATERIAL
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
ID and Library types, which are fundamental for SDNA.
#define FILTER_ID_MA
Definition DNA_ID.h:1210
#define FILTER_ID_GR
Definition DNA_ID.h:1205
#define FILTER_ID_TE
Definition DNA_ID.h:1222
#define MAXMAT
Object is a sort of wrapper for general info.
#define OB_DATA_SUPPORT_ID(_id_type)
#define IMAGEPAINT_MODE_IMAGE
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define ARRAY_SET_ITEMS(...)
#define STREQLEN(a, b, n)
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define MEM_realloc_zeroed(vmemh, len)
#define MEM_realloc_uninitialized(vmemh, len)
#define output
constexpr IndexRange()=default
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
void reserve(int64_t n)
Definition BLI_map.hh:1028
std::optional< AttributeMetaData > lookup_meta_data(StringRef name) const
GSpanAttributeWriter lookup_for_write_span(StringRef name)
#define storage(slot, qualifiers)
#define attribute(slot)
bool is_color_attribute(const AttributeMetaData &meta_data)
bNodeTree * node_tree_add_tree_embedded(Main *bmain, ID *owner_id, StringRefNull name, StringRefNull idname)
Definition node.cc:5005
void node_tree_blend_write(BlendWriter *writer, bNodeTree *ntree)
Definition node.cc:1361
bNodeTree * node_tree_localize(bNodeTree *ntree, std::optional< ID * > new_owner_id)
Definition node.cc:5355
bNode * node_get_active_paint_canvas(bNodeTree &ntree)
bNode * node_add_static_node(const bContext *C, bNodeTree &ntree, int type)
Definition node.cc:4283
bNodeLink & node_add_link(bNodeTree &ntree, bNode &fromnode, bNodeSocket &fromsock, bNode &tonode, bNodeSocket &tosock)
Definition node.cc:4638
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, UString identifier)
Definition node.cc:3036
void node_set_active(bNodeTree &ntree, bNode &node)
Definition node.cc:5484
void node_tree_free_embedded_tree(bNodeTree *ntree)
Definition node.cc:5231
BMesh Iterators.
static Material ** default_materials[]
MINLINE float min_ff(float a, float b)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void material_default_holdout_init(Material **ma_p)
void BKE_id_material_eval_assign(ID *id, int slot, Material *material)
void BKE_icon_id_delete(ID *id)
Definition icons.cc:524
static void material_free_data(ID *id)
int BKE_object_material_count_eval(const Object *ob)
void BKE_previewimg_id_free(ID *id)
void ramp_blend(int type, float r_col[4], float fac, const float col[4])
char * STRNCPY(char(&dst)[N], const char *src)
int BKE_object_material_used_with_fallback_eval(const Object &ob)
void BKE_object_material_remap(Object *ob, const unsigned int *remap)
static int count_texture_nodes_recursive(bNodeTree *nodetree, ePaintSlotFilter slot_filter)
static Material * default_material_surface
Material * BKE_gpencil_material(Object *ob, short act)
static void material_default_gpencil_init(Material **ma_p)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
int BKE_object_material_remove_unused(Main *bmain, Object *ob)
void BKE_previewimg_blend_read(BlendDataReader *reader, PreviewImage *prv)
bool BKE_object_material_slot_remove(Main *bmain, Object *ob)
short * BKE_object_material_len_p(Object *ob)
void hsv_to_rgb(float h, float s, float v, float *r_r, float *r_g, float *r_b)
Definition math_color.cc:25
Material * BKE_material_default_surface()
@ GP_MATERIAL_PLACEMENT_RADIUS
void BKE_pointcloud_material_remap(PointCloud *pointcloud, const unsigned int *remap, int remap_num)
Material *** BKE_object_material_array_p(Object *ob)
void id_us_plus(ID *id)
Definition lib_id.cc:433
static void fill_texpaint_slots_recursive(bNodeTree *nodetree, bNode *active_node, const Object *ob, Material *ma, int slot_len, ePaintSlotFilter slot_filter)
Material * BKE_gpencil_material_add(Main *bmain, const char *name)
bool BKE_mesh_material_index_used(Mesh *mesh, short index)
void BKE_displist_free(ListBaseT< DispList > *lb)
Definition displist.cc:68
static void material_default_gsplat_init(Material **ma_p)
static bool fill_texpaint_slots_cb(bNodeTree *, bNode *node, void *userdata)
bool BKE_object_material_slot_add(Main *bmain, Object *ob, bool set_active=true)
Material * BKE_object_material_get(Object *ob, short act)
void BKE_previewimg_blend_write(BlendWriter *writer, const PreviewImage *prv)
@ SHD_ATTRIBUTE_GEOMETRY
static Material * default_material_holdout
short * BKE_id_material_len_p(ID *id)
static bNode * nodetree_uv_node_recursive(bNode *node)
const Mesh * BKE_object_get_editmesh_eval_final(const Object *object)
Definition object.cc:4477
void DEG_debug_print_eval(Depsgraph *depsgraph, const char *function_name, const char *object_name, const void *object_address)
Material *** BKE_id_material_array_p(ID *id)
int BKE_id_material_used_eval(const ID &id)
int BKE_object_material_index_get_with_hint(Object *ob, const Material *ma, int hint_index)
void BKE_curves_material_remap(struct Curves *curves, const unsigned int *remap, int remap_num)
static void material_blend_read_data(BlendDataReader *reader, ID *id)
MINLINE void add_v3_fl(float r[3], float f)
Material * BKE_material_default_empty()
void BKE_object_material_active_index_sanitize(Object *ob)
void BKE_object_material_assign(Main *bmain, Object *ob, Material *ma, short act, int assign_type)
void rgb_to_hsv(float r, float g, float b, float *r_h, float *r_s, float *r_v)
void BKE_id_materials_copy(Main *bmain, ID *id_src, ID *id_dst)
void BKE_curve_material_remap(Curve *cu, const unsigned int *remap, unsigned int remap_len)
Definition curve.cc:5399
Material * BKE_material_default_gpencil()
static void material_foreach_working_space_color(ID *id, const IDTypeForeachColorFunctionCallback &fn)
Material * BKE_material_default_volume()
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2890
static bool texpaint_slot_node_find_cb(bNodeTree *nodetree, bNode *node, void *userdata)
int BKE_object_material_ensure(Main *bmain, Object *ob, Material *material)
@ LIB_ID_CREATE_NO_USER_REFCOUNT
@ LIB_ID_COPY_NO_PREVIEW
@ LIB_ID_CREATE_LOCAL
@ LIB_ID_CREATE_NO_ALLOCATE
static void material_init_data(ID *id)
bool BKE_grease_pencil_material_index_used(GreasePencil *grease_pencil, int index)
void BKE_colorband_foreach_working_space_color(ColorBand *coba, const IDTypeForeachColorFunctionCallback &fn)
Definition colorband.cc:655
bool(*)(bNodeTree *nodetree, bNode *node, void *userdata) ForEachTexNodeCallback
MaterialGPencilStyle * BKE_gpencil_material_settings(Object *ob, short act)
int BKE_id_material_used_with_fallback_eval(const ID &id)
void GPU_material_free(ListBaseT< LinkData > *gpumaterial)
void BKE_gpencil_material_attr_init(Material *ma)
void BKE_id_material_assign(Main *bmain, ID *id, Material *ma, short act)
bool BKE_object_material_slot_used(Object *object, short actcol)
static Material * default_material_gpencil
void BKE_texpaint_slot_refresh_cache(Scene *scene, Material *ma, const Object *ob)
void BKE_mesh_material_index_remove(Mesh *mesh, short index)
void DEG_id_tag_update(ID *id, unsigned int flags)
BMesh const char void * data
void BKE_object_materials_sync_length(Main *bmain, Object *ob, ID *id)
void BKE_object_material_remap_calc(Object *ob_dst, Object *ob_src, short *remap_src_to_dst)
void BKE_id_material_resize(Main *bmain, ID *id, short totcol, bool do_id_user)
static void material_default_volume_init(Material **ma_p)
void BKE_library_foreach_ID_embedded(LibraryForeachIDData *data, ID **id_pp)
Definition lib_query.cc:176
void BKE_id_material_append(Main *bmain, ID *id, Material *ma)
@ BKE_MAT_ASSIGN_EXISTING
@ BKE_MAT_ASSIGN_USERPREF
@ BKE_MAT_ASSIGN_OBJECT
@ BKE_MAT_ASSIGN_OBDATA
void BKE_grease_pencil_material_index_remove(GreasePencil *grease_pencil, int index)
static void material_blend_write(BlendWriter *writer, ID *id, const void *id_address)
bool BKE_curve_material_index_used(const Curve *cu, int index)
Definition curve.cc:5331
void BKE_id_material_eval_ensure_default_slot(ID *id)
static void material_default_surface_init(Material **ma_p)
Material * BKE_id_material_pop(Main *bmain, ID *id, int index)
static bool count_texture_nodes_cb(bNodeTree *, bNode *, void *userdata)
void BKE_material_make_node_previews_dirty(Material *ma)
BMDataLayerLookup BM_data_layer_lookup(const BMesh &bm, const StringRef name)
static Material * material_default_create(Material **ma_p, const char *name)
int BKE_object_material_index_get(Object *ob, const Material *ma)
void BKE_curve_material_index_remove(Curve *cu, int index)
Definition curve.cc:5312
void BKE_objects_materials_sync_length_all(Main *bmain, ID *id)
void BKE_id_free(Main *bmain, void *idv)
void BKE_object_material_array_assign(Main *bmain, Object *ob, Material ***matar, int totcol, bool to_object_only)
void BKE_material_eval(Depsgraph *depsgraph, Material *material)
void BKE_id_material_clear(Main *bmain, ID *id)
void BKE_object_material_resize(Main *bmain, Object *ob, short totcol, bool do_id_user)
static Material * default_material_empty
static void material_foreach_id(ID *id, LibraryForeachIDData *data)
void BKE_ntree_update_without_main(bNodeTree &tree)
static void material_data_index_clear_id(ID *id)
static Material * default_material_volume
void * BKE_id_new_nomain(short type, const char *name)
Definition lib_id.cc:1617
std::optional< int > BKE_id_material_index_max_eval(const ID &id)
void BKE_previewimg_id_copy(ID *new_id, const ID *old_id)
int BKE_object_material_remove_all(Main *bmain, Object *ob)
T * DEG_get_original(T *id)
@ INDEX_ID_MA
Definition DNA_ID.h:1314
Material ** BKE_object_material_get_p(Object *ob, short act)
MINLINE float max_ff(float a, float b)
void BKE_main_unlock(Main *bmain)
Definition main.cc:666
Material * BKE_object_material_get_eval(Object *ob, short act)
short BKE_object_material_slot_find_index(Object *ob, Material *ma)
@ ID_RECALC_SHADING
Definition DNA_ID.h:1094
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
ID_Type GS(const char *name)
Definition DNA_ID.h:410
static void nodetree_mark_previews_dirty_reccursive(bNodeTree *tree)
@ ID_TAG_MISSING
Definition DNA_ID.h:870
bool BKE_object_supports_material_slots(Object *ob)
Definition object.cc:5182
const BMesh * BKE_editmesh_bmesh_get(const Object *ob)
Definition editmesh.cc:65
static CLG_LogRef LOG
Definition logging.cpp:12
void BKE_main_lock(Main *bmain)
Definition main.cc:661
void BKE_mesh_material_index_clear(Mesh *mesh)
void BKE_object_material_assign_single_obdata(Main *bmain, Object *ob, Material *ma, short act)
static bool ntree_foreach_texnode_recursive(bNodeTree *nodetree, ForEachTexNodeCallback callback, void *userdata, ePaintSlotFilter slot_filter)
static void material_data_index_remove_id(ID *id, short index)
void DEG_relations_tag_update(Main *bmain)
void BKE_mesh_material_remap(Mesh *mesh, const unsigned int *remap, unsigned int remap_len)
void BKE_texpaint_slots_refresh_object(Scene *scene, Object *ob)
void BKE_material_attr_indices_remap(bke::MutableAttributeAccessor attributes, const unsigned int *remap, int remap_num)
std::pair< bNodeTree *, bNode * > BKE_texpaint_slot_material_find_node(Material *ma, short texpaint_slot)
Material * BKE_material_add(Main *bmain, const char *name)
Material * BKE_material_default_gsplat()
Material * BKE_material_default_holdout()
bool DEG_is_evaluated(const T *id)
static const ID * get_evaluated_object_data_with_materials(const Object *ob)
void BKE_grease_pencil_material_remap(GreasePencil *grease_pencil, const uint *remap, int totcol)
void *(*) MEM_realloc_zeroed_id(void *vmemh, size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(2)
MINLINE int clamp_i(int value, int min, int max)
void id_us_min(ID *id)
Definition lib_id.cc:441
void BKE_curve_material_index_clear(Curve *cu)
Definition curve.cc:5352
void BKE_object_material_from_eval_data(Main *bmain, Object *ob_orig, const ID *data_eval)
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
static void material_copy_data(Main *bmain, std::optional< Library * > owner_library, ID *id_dst, const ID *id_src, const int flag)
struct ID * BKE_id_copy_in_lib(Main *bmain, std::optional< Library * > owner_library, const ID *id, std::optional< const ID * > new_owner_id, ID **new_id_p, int flag)
Definition lib_id.cc:749
static Material * default_material_gsplat
static void object_material_assign(Main *bmain, Object *ob, Material *ma, short act, int assign_type, bool do_test_all)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:50
#define fabsf
void write_embedded_id_struct(const void *id_address, const T *id, BlendStructWriterTypedFn< T > fn=nullptr)
void write_id_struct(const void *id_address, const T *id, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
ListBaseT< DispList > disp
Definition BKE_curve.hh:44
struct Material ** mat
char name[258]
Definition DNA_ID.h:433
void * next
Definition DNA_ID.h:418
ListBaseT< Object > objects
Definition BKE_main.hh:388
eMaterialGPencilPlacementMode placement_mode
struct Material ** mat
bke::ObjectRuntime * runtime
struct Material ** mat
struct Material ** mat
struct ToolSettings * toolsettings
struct ImageUser * image_user
struct ImagePaintSettings imapaint
struct Material ** mat
struct bNodeLink * link
bke::bNodeTreeRuntime * runtime
bke::bNodeType * typeinfo
#define N_(msgid)