Fix #145287: Don't touch original mesh when converting to grease pencil

Previously `BKE_id_material_clear` is used on the original mesh data for
clearing mesh array when converting mesh objects into grease pencil,
this is not desired because:

1. It damages original mesh data block, if the mesh is shared across
   multiple objects, this can lead to unwanted changes.
2. Converting multiple mesh objects that shares one mesh can crash
   because the now-modified mesh data block is not fully evaluated and
   subsequent conversions aren't able to get a valid mesh.

By removing the `BKE_id_material_clear` call we can make the conversion
work as expected. Since we replace the object data and reset material
count in the object, the material array in the original mesh shouldn't
pose any influence on the result of the conversion.

Pull Request: https://projects.blender.org/blender/blender/pulls/145288
This commit is contained in:
YimingWu
2025-08-28 08:16:49 +02:00
committed by YimingWu
parent 3a39f9575e
commit a623efe42a

View File

@@ -3299,8 +3299,6 @@ static Object *convert_mesh_to_grease_pencil(Base &base,
fill_colors = mesh_to_grease_pencil_get_material_list(*ob_eval, *mesh_eval, material_remap);
}
Mesh *newob_mesh = static_cast<Mesh *>(newob->data);
BKE_id_material_clear(info.bmain, &newob_mesh->id);
BKE_object_free_derived_caches(newob);
BKE_object_free_modifiers(newob, 0);
@@ -3308,12 +3306,11 @@ static Object *convert_mesh_to_grease_pencil(Base &base,
newob->data = grease_pencil;
newob->type = OB_GREASE_PENCIL;
/* Reset `ob->totcol` and `ob->actcol` since currently the generic / grease pencil material
/* Reset object material array and count since currently the generic / grease pencil material
* functions still depend on this value being coherent (The same value as
* `GreasePencil::material_array_num`).
*/
newob->totcol = 0;
newob->actcol = 0;
BKE_object_material_resize(info.bmain, newob, 0, true);
mesh_to_grease_pencil_add_material(
*info.bmain, *newob, DATA_("Stroke"), float4(0.0f, 0.0f, 0.0f, 1.0f), {});