diff --git a/scripts/startup/bl_ui/space_view3d.py b/scripts/startup/bl_ui/space_view3d.py index e452eb92eef..1d640bed296 100644 --- a/scripts/startup/bl_ui/space_view3d.py +++ b/scripts/startup/bl_ui/space_view3d.py @@ -5831,6 +5831,7 @@ class VIEW3D_MT_edit_greasepencil_stroke(Menu): layout.separator() + layout.menu("VIEW3D_MT_grease_pencil_assign_material") layout.operator("grease_pencil.set_active_material") layout.separator() @@ -8139,6 +8140,27 @@ class VIEW3D_MT_greasepencil_material_active(Menu): layout.operator("grease_pencil.set_material", text=mat.name, icon_value=icon).slot = mat.name +class VIEW3D_MT_grease_pencil_assign_material(Menu): + bl_label = "Assign Material" + + def draw(self, context): + layout = self.layout + ob = context.active_object + mat_active = ob.active_material + + if len(ob.material_slots) == 0: + row = layout.row() + row.label(text="No Materials") + row.enabled = False + return + + for slot in ob.material_slots: + mat = slot.material + if mat: + layout.operator("grease_pencil.stroke_material_set", text=mat.name, + icon='LAYER_ACTIVE' if mat == mat_active else 'BLANK1').material = mat.name + + class VIEW3D_MT_greasepencil_edit_context_menu(Menu): bl_label = "" @@ -8198,6 +8220,11 @@ class VIEW3D_MT_greasepencil_edit_context_menu(Menu): col.separator() + col.menu("VIEW3D_MT_grease_pencil_assign_material") + col.operator("grease_pencil.set_active_material", text="Set as Active Material") + + col.separator() + col.menu("VIEW3D_MT_mirror") @@ -8894,6 +8921,7 @@ classes = ( VIEW3D_MT_gpencil_autoweights, VIEW3D_MT_gpencil_edit_context_menu, VIEW3D_MT_greasepencil_edit_context_menu, + VIEW3D_MT_grease_pencil_assign_material, VIEW3D_MT_edit_greasepencil, VIEW3D_MT_edit_greasepencil_delete, VIEW3D_MT_edit_greasepencil_stroke, diff --git a/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc b/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc index 9d3a13da747..0ac2689f64a 100644 --- a/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc +++ b/source/blender/editors/grease_pencil/intern/grease_pencil_edit.cc @@ -13,6 +13,7 @@ #include "BLI_math_vector_types.hh" #include "BLI_span.hh" #include "BLI_stack.hh" +#include "BLT_translation.h" #include "DNA_material_types.h" @@ -20,7 +21,9 @@ #include "BKE_context.hh" #include "BKE_curves_utils.hh" #include "BKE_grease_pencil.hh" +#include "BKE_lib_id.h" #include "BKE_material.h" +#include "BKE_report.h" #include "RNA_access.hh" #include "RNA_define.hh" @@ -978,12 +981,29 @@ static void GREASE_PENCIL_OT_delete_frame(wmOperatorType *ot) /** \name Stroke Material Set Operator * \{ */ -static int grease_pencil_stroke_material_set_exec(bContext *C, wmOperator * /*op*/) +static int grease_pencil_stroke_material_set_exec(bContext *C, wmOperator *op) { + using namespace blender; + Main *bmain = CTX_data_main(C); const Scene *scene = CTX_data_scene(C); Object *object = CTX_data_active_object(C); GreasePencil &grease_pencil = *static_cast(object->data); - const int material_index = object->actcol - 1; + Material *ma = nullptr; + char name[MAX_ID_NAME - 2]; + RNA_string_get(op->ptr, "material", name); + + int material_index = object->actcol - 1; + + if (name[0] != '\0') { + ma = reinterpret_cast(BKE_libblock_find_name(bmain, ID_MA, name)); + if (ma == nullptr) { + BKE_reportf(op->reports, RPT_WARNING, TIP_("Material '%s' could not be found"), name); + return OPERATOR_CANCELLED; + } + + /* Find slot index. */ + material_index = BKE_object_material_index_get(object, ma); + } if (material_index == -1) { return OPERATOR_CANCELLED; @@ -1022,6 +1042,10 @@ static void GREASE_PENCIL_OT_stroke_material_set(wmOperatorType *ot) ot->poll = editable_grease_pencil_poll; ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + ot->prop = RNA_def_string( + ot->srna, "material", nullptr, MAX_ID_NAME - 2, "Material", "Name of the material"); + RNA_def_property_flag(ot->prop, PROP_SKIP_SAVE); } /** \} */