GPv3: Menu to Select Material using U key #114694

Merged
Antonio Vazquez merged 10 commits from antoniov/blender:GPv3_114203_active_mat into main 2023-12-01 13:12:18 +01:00
3 changed files with 109 additions and 1 deletions

View File

@ -4575,7 +4575,8 @@ def km_grease_pencil_paint(_params):
{"properties": [("mode", 'INVERT')]}),
("grease_pencil.brush_stroke", {"type": 'LEFTMOUSE', "value": 'PRESS', "shift": True},
{"properties": [("mode", 'SMOOTH')]}),
# Active material
antoniov marked this conversation as resolved Outdated

Seems like there was some unresolved conflict here. Please resolve.

Seems like there was some unresolved conflict here. Please resolve.
op_menu("VIEW3D_MT_greasepencil_material_active", {"type": 'U', "value": 'PRESS'}),
# Active layer
op_menu("GREASE_PENCIL_MT_layer_active", {"type": 'Y', "value": 'PRESS'}),

View File

@ -8081,6 +8081,32 @@ class VIEW3D_MT_gpencil_edit_context_menu(Menu):
col.operator("gpencil.reproject", text="Reproject")
class VIEW3D_MT_greasepencil_material_active(Menu):
bl_label = "Active Material"
@classmethod
def poll(cls, context):
ob = context.active_object
if ob is None or len(ob.material_slots) == 0:
return False
return True
def draw(self, context):
layout = self.layout
layout.operator_context = 'INVOKE_REGION_WIN'
ob = context.active_object
for slot in ob.material_slots:
mat = slot.material
if not mat:
continue
mat.id_data.preview_ensure()
if mat and mat.id_data and mat.id_data.preview:
icon = mat.id_data.preview.icon_id
layout.operator("grease_pencil.set_material", text=mat.name, icon_value=icon).slot = mat.name
class VIEW3D_MT_greasepencil_edit_context_menu(Menu):
bl_label = ""
@ -8815,6 +8841,7 @@ classes = (
VIEW3D_MT_edit_mesh_merge,
VIEW3D_MT_edit_mesh_split,
VIEW3D_MT_edit_mesh_showhide,
VIEW3D_MT_greasepencil_material_active,
VIEW3D_MT_paint_grease_pencil,
VIEW3D_MT_paint_gpencil,
VIEW3D_MT_draw_gpencil,

View File

@ -14,12 +14,16 @@
#include "BLI_span.hh"
#include "BLI_stack.hh"
#include "DNA_material_types.h"
#include "BKE_context.hh"
#include "BKE_curves_utils.hh"
#include "BKE_grease_pencil.hh"
#include "BKE_material.h"
#include "RNA_access.hh"
#include "RNA_define.hh"
#include "RNA_enum_types.hh"
#include "DEG_depsgraph.hh"
@ -29,6 +33,8 @@
#include "WM_api.hh"
#include "UI_resources.hh"
namespace blender::ed::greasepencil {
bool active_grease_pencil_poll(bContext *C)
@ -1448,6 +1454,79 @@ static void GREASE_PENCIL_OT_caps_set(wmOperatorType *ot)
/** \} */
/* -------------------------------------------------------------------- */
/** \name Set Active Material Operator
* \{ */
/* Retry enum items with object materials. */
static const EnumPropertyItem *material_enum_itemf(bContext *C,
PointerRNA * /*ptr*/,
PropertyRNA * /*prop*/,
bool *r_free)
{
Object *ob = CTX_data_active_object(C);
EnumPropertyItem *item = nullptr, item_tmp = {0};
int totitem = 0;
if (ob == nullptr) {
antoniov marked this conversation as resolved Outdated

We only need to check for ob here: if (ob == nullptr) {

We only need to check for `ob` here: `if (ob == nullptr) {`
return rna_enum_dummy_DEFAULT_items;
}
/* Existing materials */
for (const int i : IndexRange(ob->totcol)) {
if (Material *ma = BKE_object_material_get(ob, i + 1)) {
item_tmp.identifier = ma->id.name + 2;
item_tmp.name = ma->id.name + 2;
item_tmp.value = i + 1;
item_tmp.icon = ma->preview ? ma->preview->icon_id : ICON_NONE;
RNA_enum_item_add(&item, &totitem, &item_tmp);
}
}
RNA_enum_item_end(&item, &totitem);
*r_free = true;
return item;
}
static int grease_pencil_set_material_exec(bContext *C, wmOperator *op)
{
Object *object = CTX_data_active_object(C);
GreasePencil &grease_pencil = *static_cast<GreasePencil *>(object->data);
const int slot = RNA_enum_get(op->ptr, "slot");
antoniov marked this conversation as resolved Outdated

Use const.

Use `const`.
/* Try to get material slot. */
if ((slot < 1) || (slot > object->totcol)) {
return OPERATOR_CANCELLED;
}
/* Set active material. */
object->actcol = slot;
WM_event_add_notifier(C, NC_GEOM | ND_DATA | NA_EDITED, &grease_pencil);
return OPERATOR_FINISHED;
}
void GREASE_PENCIL_OT_set_material(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Set Active Material";
ot->idname = "GREASE_PENCIL_OT_set_material";
ot->description = "Set active material";
/* callbacks */
ot->exec = grease_pencil_set_material_exec;
ot->poll = active_grease_pencil_poll;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* Material to use (dynamic enum) */
ot->prop = RNA_def_enum(ot->srna, "slot", rna_enum_dummy_DEFAULT_items, 0, "Material Slot", "");
RNA_def_enum_funcs(ot->prop, material_enum_itemf);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Duplicate Operator
* \{ */
@ -1644,6 +1723,7 @@ void ED_operatortypes_grease_pencil_edit()
WM_operatortype_append(GREASE_PENCIL_OT_set_uniform_opacity);
WM_operatortype_append(GREASE_PENCIL_OT_caps_set);
WM_operatortype_append(GREASE_PENCIL_OT_duplicate);
WM_operatortype_append(GREASE_PENCIL_OT_set_material);
}
void ED_keymap_grease_pencil(wmKeyConfig *keyconf)