GPv3: Fix Remove Material Slot and Unused Slots

There was two problems because these operators were not supported in GPv3:

* If you remove the slot, the material was not reassigned and the material index attribute could get a wrong index. This also affected the unused slots.
* The Unused Materials operator was executed, but it was doing nothing because there was not support for new GPv3 object.

This PR adds support for new Grease Pencil object.

Pull Request: https://projects.blender.org/blender/blender/pulls/114850
This commit is contained in:
Antonio Vazquez
2023-11-17 10:37:56 +01:00
committed by Falk David
parent 4dfed520a3
commit 00b04e1030
3 changed files with 60 additions and 3 deletions

View File

@@ -837,6 +837,8 @@ Material *BKE_grease_pencil_object_material_ensure_from_active_input_brush(Main
Material *BKE_grease_pencil_object_material_ensure_from_active_input_material(Object *ob);
Material *BKE_grease_pencil_object_material_ensure_active(Object *ob);
void BKE_grease_pencil_material_remap(GreasePencil *grease_pencil, const uint *remap, int totcol);
void BKE_grease_pencil_material_index_remove(GreasePencil *grease_pencil, int index);
bool BKE_grease_pencil_references_cyclic_check(const GreasePencil *id_reference,
const GreasePencil *grease_pencil);
bool BKE_grease_pencil_material_index_used(GreasePencil *grease_pencil, int index);

View File

@@ -1391,6 +1391,55 @@ void BKE_grease_pencil_material_remap(GreasePencil *grease_pencil, const uint *r
}
}
void BKE_grease_pencil_material_index_remove(GreasePencil *grease_pencil, int index)
{
using namespace blender;
using namespace blender::bke;
for (GreasePencilDrawingBase *base : grease_pencil->drawings()) {
if (base->type != GP_DRAWING) {
continue;
}
greasepencil::Drawing &drawing = reinterpret_cast<GreasePencilDrawing *>(base)->wrap();
MutableAttributeAccessor attributes = drawing.strokes_for_write().attributes_for_write();
AttributeWriter<int> material_indices = attributes.lookup_for_write<int>("material_index");
if (!material_indices) {
return;
}
MutableVArraySpan<int> indices_span(material_indices.varray);
for (const int i : indices_span.index_range()) {
if (indices_span[i] > 0 && indices_span[i] >= index) {
indices_span[i]--;
}
}
indices_span.save();
material_indices.finish();
}
}
bool BKE_grease_pencil_material_index_used(GreasePencil *grease_pencil, int index)
{
using namespace blender;
using namespace blender::bke;
for (GreasePencilDrawingBase *base : grease_pencil->drawings()) {
if (base->type != GP_DRAWING) {
continue;
}
greasepencil::Drawing &drawing = reinterpret_cast<GreasePencilDrawing *>(base)->wrap();
AttributeAccessor attributes = drawing.strokes().attributes();
const VArraySpan<int> material_indices = *attributes.lookup_or_default<int>(
"material_index", ATTR_DOMAIN_CURVE, 0);
if (material_indices.contains(index)) {
return true;
}
}
return false;
}
/** \} */
/* ------------------------------------------------------------------- */

View File

@@ -464,6 +464,9 @@ static void material_data_index_remove_id(ID *id, short index)
case ID_CU_LEGACY:
BKE_curve_material_index_remove((Curve *)id, index);
break;
case ID_GP:
BKE_grease_pencil_material_index_remove(reinterpret_cast<GreasePencil *>(id), index);
break;
case ID_MB:
case ID_CV:
case ID_PT:
@@ -502,6 +505,9 @@ bool BKE_object_material_slot_used(Object *object, short actcol)
return false;
case ID_GD_LEGACY:
return BKE_gpencil_material_index_used((bGPdata *)ob_data, actcol - 1);
case ID_GP:
return BKE_grease_pencil_material_index_used(reinterpret_cast<GreasePencil *>(ob_data),
actcol - 1);
default:
return false;
}
@@ -1359,14 +1365,14 @@ bool BKE_object_material_slot_remove(Main *bmain, Object *ob)
}
}
/* check indices from mesh */
if (ELEM(ob->type, OB_MESH, OB_CURVES_LEGACY, OB_SURF, OB_FONT)) {
/* check indices from mesh and grease pencil. */
if (ELEM(ob->type, OB_MESH, OB_CURVES_LEGACY, OB_SURF, OB_FONT, OB_GREASE_PENCIL)) {
material_data_index_remove_id((ID *)ob->data, actcol - 1);
if (ob->runtime->curve_cache) {
BKE_displist_free(&ob->runtime->curve_cache->disp);
}
}
/* check indices from gpencil */
/* check indices from gpencil legacy. */
else if (ob->type == OB_GPENCIL_LEGACY) {
BKE_gpencil_material_index_reassign((bGPdata *)ob->data, ob->totcol, actcol - 1);
}