GPv3: Assign material #113573

Merged
Pratik Borhade merged 2 commits from PratikPB2123/blender:gpv3-assign-material into main 2023-10-18 12:58:05 +02:00
4 changed files with 57 additions and 2 deletions

View File

@ -554,7 +554,10 @@ class GreasePencilMaterialsPanel:
icon_link = 'MESH_DATA' if slot.link == 'DATA' else 'OBJECT_DATA'
row.prop(slot, "link", icon=icon_link, icon_only=True)
if not is_grease_pencil_version3 and ob.data.use_stroke_edit_mode:
if is_grease_pencil_version3 and ob.mode == 'EDIT':
row = layout.row(align=True)
row.operator("grease_pencil.stroke_change_color", text="Assign")
elif not is_grease_pencil_version3 and ob.data.use_stroke_edit_mode:
row = layout.row(align=True)
row.operator("gpencil.stroke_change_color", text="Assign")
row.operator("gpencil.material_select", text="Select").deselect = False

View File

@ -23,7 +23,7 @@
namespace blender::ed::curves {
static IndexMask retrieve_selected_curves(const bke::CurvesGeometry &curves,
IndexMask retrieve_selected_curves(const bke::CurvesGeometry &curves,
IndexMaskMemory &memory)
{
const IndexRange curves_range = curves.curves_range();

View File

@ -799,6 +799,56 @@ static void GREASE_PENCIL_OT_delete_frame(wmOperatorType *ot)
RNA_def_property_flag(prop, PROP_SKIP_SAVE);
}
static int grease_pencil_stroke_change_color_exec(bContext *C, wmOperator *op)
{
using namespace blender;
const Scene *scene = CTX_data_scene(C);
Object *object = CTX_data_active_object(C);
GreasePencil &grease_pencil = *static_cast<GreasePencil *>(object->data);
const int material_index = object->actcol - 1;
if (material_index == -1) {
return OPERATOR_CANCELLED;
}
grease_pencil.foreach_editable_drawing(
scene->r.cfra, [&](int /*drawing_index*/, bke::greasepencil::Drawing &drawing) {
bke::CurvesGeometry &curves = drawing.strokes_for_write();
if (curves.points_num() == 0) {
return;
}
bke::SpanAttributeWriter<int> materials = curves.attributes_for_write().lookup_or_add_for_write_span<int>(
"material_index", ATTR_DOMAIN_CURVE);
filedescriptor marked this conversation as resolved
Review

Use

IndexMaskMemory memory;
IndexMask selected_curves = ed::curves::retrieve_selected_curves(curves, memory);

(you'll have to make retrieve_selected_curves non-static and declare it in the ED_curves.hh header).

Use ``` IndexMaskMemory memory; IndexMask selected_curves = ed::curves::retrieve_selected_curves(curves, memory); ``` (you'll have to make `retrieve_selected_curves` non-static and declare it in the `ED_curves.hh` header).
IndexMaskMemory memory;
IndexMask selected_curves = ed::curves::retrieve_selected_curves(curves, memory);
selected_curves.foreach_index(
[&](const int curve_index) { materials.span[curve_index] = material_index;
filedescriptor marked this conversation as resolved Outdated

With the change above, you can now do:

selected_curves.foreach_index([&](GrainSize(512), [&](const int curve_index) {
   materials.span[curve_index] = material_index;
});
With the change above, you can now do: ``` selected_curves.foreach_index([&](GrainSize(512), [&](const int curve_index) { materials.span[curve_index] = material_index; }); ```
});
materials.finish();
});
DEG_id_tag_update(&grease_pencil.id, ID_RECALC_GEOMETRY);
WM_event_add_notifier(C, NC_GEOM | ND_DATA | NA_EDITED, &grease_pencil);
return OPERATOR_FINISHED;
}
static void GREASE_PENCIL_OT_stroke_change_color(wmOperatorType* ot)
{
ot->name = "Change Stroke color";
ot->idname = "GREASE_PENCIL_OT_stroke_change_color";
ot->description = "Change Stroke color with selected material";
ot->exec = grease_pencil_stroke_change_color_exec;
ot->poll = editable_grease_pencil_poll;
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/** \} */
} // namespace blender::ed::greasepencil
@ -810,6 +860,7 @@ void ED_operatortypes_grease_pencil_edit()
WM_operatortype_append(GREASE_PENCIL_OT_stroke_simplify);
WM_operatortype_append(GREASE_PENCIL_OT_dissolve);
WM_operatortype_append(GREASE_PENCIL_OT_delete_frame);
WM_operatortype_append(GREASE_PENCIL_OT_stroke_change_color);
}
void ED_keymap_grease_pencil(wmKeyConfig *keyconf)

View File

@ -134,6 +134,7 @@ bool has_anything_selected(const VArray<bool> &varray, IndexRange range_to_check
* Find curves that have any point selected (a selection factor greater than zero),
* or curves that have their own selection factor greater than zero.
*/
IndexMask retrieve_selected_curves(const bke::CurvesGeometry &curves, IndexMaskMemory &memory);
IndexMask retrieve_selected_curves(const Curves &curves_id, IndexMaskMemory &memory);
/**