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
This commit is contained in:
Philipp Oeser 2022-09-05 12:22:51 +02:00
parent a3ddcc6b4d
commit ecf3287533
Notes: blender-bot 2023-02-14 04:46:12 +01:00
Referenced by issue #100986, Materials are not locked to faces during edits
Referenced by issue #100950, Mesh Combination Error
Referenced by issue #100822, Regression: Merging objects does not assign materials correctly
1 changed files with 10 additions and 7 deletions

View File

@ -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++) {