[Bf-blender-cvs] [ecf3287533c] master: Fix T100822: Merging objects does not assign materials correctly

Philipp Oeser noreply at git.blender.org
Thu Sep 8 09:09:23 CEST 2022


Commit: ecf3287533c8adc90250bc13957eddb7b2e22fc6
Author: Philipp Oeser
Date:   Mon Sep 5 12:22:51 2022 +0200
Branches: master
https://developer.blender.org/rBecf3287533c8adc90250bc13957eddb7b2e22fc6

Fix T100822: Merging objects does not assign materials correctly

Caused by {rBf1c0249f34c4}

This is what (I think) went wrong in the above commit:
- `join_mesh_single` was writing material indices to the custom data /
attribute of the source mesh
- the `polyofs` of each mesh that was joined was not taken into account

Now, instead of using the AttributeWriter on a particular mesh, use the
CustomData (`pdata`) - that is constantly changed during joining -
directly for writing.
Otherwise we end up writing into customdata that has not been "extended"
yet (even if we use the destination mesh).
Also note that even on the destination mesh, CustomData would be freed
anyways after all calls to `join_mesh_single` took place, to be replaced
with the mentioned `pdata` which should be the single customdata to
write to here.

When doing this (writing to `pdata`), we also need to take into account
the poly offset of each contributing mesh.

Maniphest Tasks: T100822

Differential Revision: https://developer.blender.org/D15878

===================================================================

M	source/blender/editors/mesh/meshtools.cc

===================================================================

diff --git a/source/blender/editors/mesh/meshtools.cc b/source/blender/editors/mesh/meshtools.cc
index d6713724e15..58702d9e966 100644
--- a/source/blender/editors/mesh/meshtools.cc
+++ b/source/blender/editors/mesh/meshtools.cc
@@ -253,15 +253,18 @@ static void join_mesh_single(Depsgraph *depsgraph,
     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 =
-        me->attributes_for_write().lookup_for_write<int>("material_index");
+    /* Apply matmap. In case we dont have material indices yet, create them if more than one
+     * material is the result of joining. */
+    int *material_indices = static_cast<int *>(
+        CustomData_get_layer_named(pdata, CD_PROP_INT32, "material_index"));
+    if (!material_indices && totcol > 1) {
+      material_indices = (int *)CustomData_add_layer_named(
+          pdata, CD_PROP_INT32, CD_SET_DEFAULT, NULL, totpoly, "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;
+      for (a = 0; a < me->totpoly; a++) {
+        material_indices[a + *polyofs] = matmap ? matmap[material_indices[a + *polyofs]] : 0;
       }
-      material_indices_span.save();
-      material_indices.finish();
     }
 
     for (a = 0; a < me->totpoly; a++, mpoly++) {



More information about the Bf-blender-cvs mailing list