[Bf-blender-cvs] [3ecb2134624] refactor-mesh-material-index-generic: Merge branch 'master' into refactor-mesh-material-index-generic
Hans Goudey
noreply at git.blender.org
Wed Aug 31 15:15:18 CEST 2022
Commit: 3ecb21346248014aff621ec3b97ccbc1e7774653
Author: Hans Goudey
Date: Wed Aug 31 08:07:51 2022 -0500
Branches: refactor-mesh-material-index-generic
https://developer.blender.org/rB3ecb21346248014aff621ec3b97ccbc1e7774653
Merge branch 'master' into refactor-mesh-material-index-generic
===================================================================
===================================================================
diff --cc source/blender/blenkernel/BKE_mesh.h
index 70f549a6d23,8cf973b785c..ec6799ee995
--- a/source/blender/blenkernel/BKE_mesh.h
+++ b/source/blender/blenkernel/BKE_mesh.h
@@@ -1022,30 -1019,6 +1022,30 @@@ char *BKE_mesh_debug_info(const struct
void BKE_mesh_debug_print(const struct Mesh *me) ATTR_NONNULL(1);
#endif
+/**
+ * \return The material index for each polygon. May be null.
+ * \note In C++ code, prefer using the attribute API (#MutableAttributeAccessor)/
+ */
+BLI_INLINE const int *BKE_mesh_material_indices(const Mesh *mesh)
+{
+ return (const int *)CustomData_get_layer_named(&mesh->pdata, CD_PROP_INT32, "material_index");
+}
+
+/**
+ * \return The material index for each polygon. Create the layer if it doesn't exist.
+ * \note In C++ code, prefer using the attribute API (#MutableAttributeAccessor)/
+ */
+BLI_INLINE int *BKE_mesh_material_indices_for_write(Mesh *mesh)
+{
+ int *indices = (int *)CustomData_duplicate_referenced_layer_named(
+ &mesh->pdata, CD_PROP_INT32, "material_index", mesh->totpoly);
+ if (indices) {
+ return indices;
+ }
+ return (int *)CustomData_add_layer_named(
- &mesh->pdata, CD_PROP_INT32, CD_CALLOC, NULL, mesh->totpoly, "material_index");
++ &mesh->pdata, CD_PROP_INT32, CD_SET_DEFAULT, NULL, mesh->totpoly, "material_index");
+}
+
#ifdef __cplusplus
}
#endif
diff --cc source/blender/blenkernel/intern/mesh_convert.cc
index 0f05a3635c4,cb72e09af16..393d54bb03e
--- a/source/blender/blenkernel/intern/mesh_convert.cc
+++ b/source/blender/blenkernel/intern/mesh_convert.cc
@@@ -195,11 -194,8 +195,11 @@@ static Mesh *mesh_nurbs_displist_to_mes
MEdge *medge = edges.data();
MPoly *mpoly = polys.data();
MLoop *mloop = loops.data();
+ MutableAttributeAccessor attributes = mesh_attributes_for_write(*mesh);
+ SpanAttributeWriter<int> material_indices = attributes.lookup_or_add_for_write_only_span<int>(
+ "material_index", ATTR_DOMAIN_FACE);
MLoopUV *mloopuv = static_cast<MLoopUV *>(CustomData_add_layer_named(
- &mesh->ldata, CD_MLOOPUV, CD_CALLOC, nullptr, mesh->totloop, "UVMap"));
+ &mesh->ldata, CD_MLOOPUV, CD_SET_DEFAULT, nullptr, mesh->totloop, "UVMap"));
/* verts and faces */
vertcount = 0;
diff --cc source/blender/editors/mesh/meshtools.cc
index 7db777c2134,e9a34cf95cb..330560be026
--- a/source/blender/editors/mesh/meshtools.cc
+++ b/source/blender/editors/mesh/meshtools.cc
@@@ -247,22 -244,12 +247,22 @@@ static void join_mesh_single(Depsgraph
}
}
- CustomData_merge(&me->pdata, pdata, CD_MASK_MESH.pmask, CD_DEFAULT, totpoly);
+ CustomData_merge(&me->pdata, pdata, CD_MASK_MESH.pmask, CD_SET_DEFAULT, totpoly);
CustomData_copy_data_named(&me->pdata, pdata, 0, *polyofs, me->totpoly);
+ blender::bke::AttributeWriter<int> material_indices =
+ blender::bke::mesh_attributes_for_write(*me).lookup_for_write<int>("material_index");
+ if (material_indices) {
+ blender::MutableVArraySpan<int> material_indices_span(material_indices.varray);
+ for (const int i : material_indices_span.index_range()) {
+ material_indices_span[i] = matmap ? matmap[material_indices_span[i]] : 0;
+ }
+ material_indices_span.save();
+ material_indices.finish();
+ }
+
for (a = 0; a < me->totpoly; a++, mpoly++) {
mpoly->loopstart += *loopofs;
- mpoly->mat_nr = matmap ? matmap[mpoly->mat_nr] : 0;
}
/* Face maps. */
diff --cc source/blender/freestyle/intern/blender_interface/BlenderStrokeRenderer.cpp
index 7c46937bed2,3df0d723aec..6365dfe26a7
--- a/source/blender/freestyle/intern/blender_interface/BlenderStrokeRenderer.cpp
+++ b/source/blender/freestyle/intern/blender_interface/BlenderStrokeRenderer.cpp
@@@ -577,15 -577,14 +577,15 @@@ void BlenderStrokeRenderer::GenerateStr
mesh->totcol = group->materials.size();
mesh->mvert = (MVert *)CustomData_add_layer(
- &mesh->vdata, CD_MVERT, CD_CALLOC, nullptr, mesh->totvert);
+ &mesh->vdata, CD_MVERT, CD_SET_DEFAULT, nullptr, mesh->totvert);
mesh->medge = (MEdge *)CustomData_add_layer(
- &mesh->edata, CD_MEDGE, CD_CALLOC, nullptr, mesh->totedge);
+ &mesh->edata, CD_MEDGE, CD_SET_DEFAULT, nullptr, mesh->totedge);
mesh->mpoly = (MPoly *)CustomData_add_layer(
- &mesh->pdata, CD_MPOLY, CD_CALLOC, nullptr, mesh->totpoly);
+ &mesh->pdata, CD_MPOLY, CD_SET_DEFAULT, nullptr, mesh->totpoly);
mesh->mloop = (MLoop *)CustomData_add_layer(
- &mesh->ldata, CD_MLOOP, CD_CALLOC, nullptr, mesh->totloop);
+ &mesh->ldata, CD_MLOOP, CD_SET_DEFAULT, nullptr, mesh->totloop);
-
+ int *material_indices = (int *)CustomData_add_layer_named(
- &mesh->pdata, CD_PROP_INT32, CD_DEFAULT, nullptr, mesh->totpoly, "material_index");
++ &mesh->pdata, CD_PROP_INT32, CD_SET_DEFAULT, nullptr, mesh->totpoly, "material_index");
MVert *vertices = mesh->mvert;
MEdge *edges = mesh->medge;
MPoly *polys = mesh->mpoly;
diff --cc source/blender/io/collada/MeshImporter.cpp
index b40546dfd97,34792fd6bb4..fab53908d5a
--- a/source/blender/io/collada/MeshImporter.cpp
+++ b/source/blender/io/collada/MeshImporter.cpp
@@@ -614,9 -615,6 +615,9 @@@ void MeshImporter::read_polys(COLLADAFW
MaterialIdPrimitiveArrayMap mat_prim_map;
+ int *material_indices = (int *)CustomData_add_layer_named(
- &me->pdata, CD_PROP_INT32, CD_DEFAULT, nullptr, me->totpoly, "material_index");
++ &me->pdata, CD_PROP_INT32, CD_SET_DEFAULT, nullptr, me->totpoly, "material_index");
+
COLLADAFW::MeshPrimitiveArray &prim_arr = collada_mesh->getMeshPrimitives();
COLLADAFW::MeshVertexData &nor = collada_mesh->getNormals();
diff --cc source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc
index 4c3d52131b7,9bcc061caf7..902f801ee5b
--- a/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc
+++ b/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc
@@@ -381,17 -374,13 +374,17 @@@ void OBJWriter::write_poly_elements(For
}
}
+ const bke::AttributeAccessor attributes = bke::mesh_attributes(*obj_mesh_data.get_mesh());
+ const VArray<int> material_indices = attributes.lookup_or_default<int>(
+ "material_index", ATTR_DOMAIN_FACE, 0);
+
/* Write material name and material group if different from previous. */
if (export_params_.export_materials && obj_mesh_data.tot_materials() > 0) {
- const int16_t prev_mat = idx == 0 ? NEGATIVE_INIT : obj_mesh_data.ith_poly_matnr(prev_i);
- const int16_t mat = obj_mesh_data.ith_poly_matnr(i);
+ const int16_t prev_mat = idx == 0 ? NEGATIVE_INIT : std::max(0, material_indices[prev_i]);
+ const int16_t mat = std::max(0, material_indices[i]);
if (mat != prev_mat) {
if (mat == NOT_FOUND) {
- buf.write<eOBJSyntaxElement::poly_usemtl>(MATERIAL_GROUP_DISABLED);
+ buf.write_obj_usemtl(MATERIAL_GROUP_DISABLED);
}
else {
const char *mat_name = matname_fn(mat);
diff --cc source/blender/io/wavefront_obj/importer/obj_import_mesh.cc
index 4e6983bc0d5,2a0676b72ff..7f7fda8d8f1
--- a/source/blender/io/wavefront_obj/importer/obj_import_mesh.cc
+++ b/source/blender/io/wavefront_obj/importer/obj_import_mesh.cc
@@@ -181,13 -181,9 +181,13 @@@ void MeshFromGeometry::create_polys_loo
const int64_t total_verts = mesh_geometry_.get_vertex_count();
if (use_vertex_groups && total_verts && mesh_geometry_.has_vertex_groups_) {
mesh->dvert = static_cast<MDeformVert *>(
- CustomData_add_layer(&mesh->vdata, CD_MDEFORMVERT, CD_CALLOC, nullptr, total_verts));
+ CustomData_add_layer(&mesh->vdata, CD_MDEFORMVERT, CD_SET_DEFAULT, nullptr, total_verts));
}
+ bke::SpanAttributeWriter<int> material_indices =
+ bke::mesh_attributes_for_write(*mesh).lookup_or_add_for_write_only_span<int>(
+ "material_index", ATTR_DOMAIN_FACE);
+
const int64_t tot_face_elems{mesh->totpoly};
int tot_loop_idx = 0;
More information about the Bf-blender-cvs
mailing list