GPv3: Lock Unused Materials operator #115272

Merged
Antonio Vazquez merged 2 commits from antoniov/blender:GPv3_mat_lock_unused into main 2023-11-23 15:31:55 +01:00
2 changed files with 46 additions and 0 deletions

View File

@ -19,6 +19,7 @@ class GPENCIL_MT_material_context_menu(Menu):
layout = self.layout
if context.preferences.experimental.use_grease_pencil_version3:
layout.operator("grease_pencil.material_reveal", icon='RESTRICT_VIEW_OFF', text="Show All")
layout.operator("grease_pencil.material_lock_unused", text="Lock Unused")
else:
layout.operator("gpencil.material_reveal", icon='RESTRICT_VIEW_OFF', text="Show All")
layout.operator("gpencil.material_hide", icon='RESTRICT_VIEW_ON', text="Hide Others").unselected = True

View File

@ -63,10 +63,55 @@ static void GREASE_PENCIL_OT_material_reveal(wmOperatorType *ot)
/** \} */
/* -------------------------------------------------------------------- */
/** \name Lock Unused Materials Operator
* \{ */
static int grease_pencil_material_lock_unused_exec(bContext *C, wmOperator * /*op*/)
{
Object *object = CTX_data_active_object(C);
GreasePencil &grease_pencil = *static_cast<GreasePencil *>(object->data);
bool changed = false;
for (const int material_index : IndexRange(object->totcol)) {
if (!BKE_object_material_slot_used(object, material_index + 1)) {
if (Material *ma = BKE_object_material_get(object, material_index + 1)) {
antoniov marked this conversation as resolved Outdated

Should be BKE_object_material_get

Should be `BKE_object_material_get`
MaterialGPencilStyle &gp_style = *ma->gp_style;
gp_style.flag |= GP_MATERIAL_HIDE | GP_MATERIAL_LOCKED;
DEG_id_tag_update(&ma->id, ID_RECALC_COPY_ON_WRITE);
changed = true;
}
}
}
if (changed) {
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_material_lock_unused(wmOperatorType *ot)
{
/* Identifiers. */
ot->name = "Lock Unused Materials";
ot->idname = "GREASE_PENCIL_OT_material_lock_unused";
ot->description = "Lock and hide any material not used";
/* Callbacks. */
ot->exec = grease_pencil_material_lock_unused_exec;
ot->poll = active_grease_pencil_poll;
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/** \} */
} // namespace blender::ed::greasepencil
void ED_operatortypes_grease_pencil_material()
{
using namespace blender::ed::greasepencil;
WM_operatortype_append(GREASE_PENCIL_OT_material_reveal);
WM_operatortype_append(GREASE_PENCIL_OT_material_lock_unused);
}