Fix: GPv3: Remap materials

Also fixes a crash when moving the first material of the default Grease Pencil Suzanne down by one.

Pull Request: https://projects.blender.org/blender/blender/pulls/113855
This commit is contained in:
Falk David
2023-10-19 13:19:41 +02:00
committed by Falk David
parent a5b8a04cb0
commit 26816931c4
4 changed files with 29 additions and 1 deletions

View File

@@ -826,6 +826,7 @@ Material *BKE_grease_pencil_object_material_ensure_from_active_input_brush(Main
Brush *brush);
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);
bool BKE_grease_pencil_references_cyclic_check(const GreasePencil *id_reference,
const GreasePencil *grease_pencil);

View File

@@ -1386,6 +1386,29 @@ Material *BKE_grease_pencil_object_material_ensure_active(Object *ob)
return ma;
}
void BKE_grease_pencil_material_remap(GreasePencil *grease_pencil, const uint *remap, int totcol)
{
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();
SpanAttributeWriter<int> material_indices = attributes.lookup_or_add_for_write_span<int>(
"material_index", ATTR_DOMAIN_CURVE);
if (!material_indices) {
return;
}
for (const int i : material_indices.span.index_range()) {
BLI_assert(IndexRange(totcol).contains(remap[material_indices.span[i]]));
material_indices.span[i] = remap[material_indices.span[i]];
}
material_indices.finish();
}
}
/** \} */
/* ------------------------------------------------------------------- */

View File

@@ -53,6 +53,7 @@
#include "BKE_displist.h"
#include "BKE_editmesh.h"
#include "BKE_gpencil_legacy.h"
#include "BKE_grease_pencil.hh"
#include "BKE_icons.h"
#include "BKE_idtype.h"
#include "BKE_image.h"
@@ -1112,6 +1113,9 @@ void BKE_object_material_remap(Object *ob, const uint *remap)
else if (ob->type == OB_GPENCIL_LEGACY) {
BKE_gpencil_material_remap(static_cast<bGPdata *>(ob->data), remap, ob->totcol);
}
else if (ob->type == OB_GREASE_PENCIL) {
BKE_grease_pencil_material_remap(static_cast<GreasePencil *>(ob->data), remap, ob->totcol);
}
else {
/* add support for this object data! */
BLI_assert(matar == nullptr);

View File

@@ -1226,7 +1226,7 @@ void create_suzanne(Main &bmain, Object &object, float4x4 matrix, const int fram
int color_skin_light = add_material_from_template(bmain, object, gp_monkey_material_skin_light);
int color_skin_shadow = add_material_from_template(
bmain, object, gp_monkey_material_skin_shadow);
object.actcol = color_black;
object.actcol = color_black + 1;
const std::array<int, 15> monkey_line_materials({
color_black,