GPv3: Copy Materials to Selected operator #115535

Merged
Falk David merged 3 commits from antoniov/blender:GPv3_mat_copy_to_object into main 2024-01-12 11:20:46 +01:00
7 changed files with 110 additions and 16 deletions

View File

@ -27,6 +27,13 @@ class GPENCIL_MT_material_context_menu(Menu):
layout.operator("grease_pencil.material_unlock_all", icon='UNLOCKED', text="Unlock All")
layout.operator("grease_pencil.material_lock_unselected", text="Lock Unselected")
layout.operator("grease_pencil.material_lock_unused", text="Lock Unused")
layout.separator()
layout.operator("grease_pencil.material_copy_to_object", text="Copy Material to Selected").only_active = True
layout.operator("grease_pencil.material_copy_to_object",
text="Copy All Materials to Selected").only_active = False
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

@ -813,7 +813,6 @@ void BKE_grease_pencil_data_update(Depsgraph *depsgraph, Scene *scene, Object *o
void BKE_grease_pencil_duplicate_drawing_array(const GreasePencil *grease_pencil_src,
GreasePencil *grease_pencil_dst);
int BKE_grease_pencil_object_material_index_get(Object *ob, Material *ma);
int BKE_grease_pencil_object_material_index_get_by_name(Object *ob, const char *name);
Material *BKE_grease_pencil_object_material_new(Main *bmain,
Object *ob,

View File

@ -114,6 +114,9 @@ bool BKE_object_material_slot_add(struct Main *bmain, struct Object *ob);
bool BKE_object_material_slot_remove(struct Main *bmain, struct Object *ob);
bool BKE_object_material_slot_used(struct Object *object, short actcol);
int BKE_object_material_index_get(Object *ob, Material *ma);
int BKE_object_material_ensure(Main *bmain, Object *ob, Material *material);
struct Material *BKE_gpencil_material(struct Object *ob, short act);
struct MaterialGPencilStyle *BKE_gpencil_material_settings(struct Object *ob, short act);

View File

@ -1216,19 +1216,6 @@ void BKE_grease_pencil_duplicate_drawing_array(const GreasePencil *grease_pencil
/** \name Grease Pencil material functions
* \{ */
int BKE_grease_pencil_object_material_index_get(Object *ob, Material *ma)
{
short *totcol = BKE_object_material_len_p(ob);
Material *read_ma = nullptr;
for (short i = 0; i < *totcol; i++) {
read_ma = BKE_object_material_get(ob, i + 1);
if (ma == read_ma) {
return i;
}
}
return -1;
}
int BKE_grease_pencil_object_material_index_get_by_name(Object *ob, const char *name)
{
short *totcol = BKE_object_material_len_p(ob);
@ -1304,7 +1291,7 @@ Material *BKE_grease_pencil_object_material_ensure_from_brush(Main *bmain,
Material *ma = BKE_grease_pencil_brush_material_get(brush);
/* check if the material is already on object material slots and add it if missing */
if (ma && BKE_grease_pencil_object_material_index_get(ob, ma) < 0) {
if (ma && BKE_object_material_index_get(ob, ma) < 0) {
BKE_object_material_slot_add(bmain, ob);
BKE_object_material_assign(bmain, ob, ma, ob->totcol, BKE_MAT_ASSIGN_USERPREF);
}

View File

@ -825,6 +825,33 @@ void BKE_id_material_eval_ensure_default_slot(ID *id)
}
}
int BKE_object_material_index_get(Object *ob, Material *ma)
{
short *totcol = BKE_object_material_len_p(ob);
Material *read_ma = nullptr;
for (short i = 0; i < *totcol; i++) {
read_ma = BKE_object_material_get(ob, i + 1);
if (ma == read_ma) {
return i;
}
}
return -1;
}
int BKE_object_material_ensure(Main *bmain, Object *ob, Material *material)
{
if (!material) {
return -1;
}
int index = BKE_object_material_index_get(ob, material);
if (index < 0) {
BKE_object_material_slot_add(bmain, ob);
BKE_object_material_assign(bmain, ob, material, ob->totcol, BKE_MAT_ASSIGN_USERPREF);
return ob->totcol - 1;
}
return index;
}
Material *BKE_gpencil_material(Object *ob, short act)
{
Material *ma = BKE_object_material_get(ob, act);

View File

@ -333,6 +333,76 @@ static void GREASE_PENCIL_OT_material_lock_unselected(wmOperatorType *ot)
/** \} */
/* -------------------------------------------------------------------- */
/** \name Copy Materials to Selected Objects
* \{ */
static int grease_pencil_material_copy_to_object_exec(bContext *C, wmOperator *op)
{
using namespace blender;
using namespace blender::bke;
Main *bmain = CTX_data_main(C);
const bool only_active = RNA_boolean_get(op->ptr, "only_active");
Object *ob_src = CTX_data_active_object(C);
Material *ma_active = BKE_object_material_get(ob_src, ob_src->actcol);
if (ma_active == nullptr) {
return OPERATOR_CANCELLED;
}
CTX_DATA_BEGIN (C, Object *, ob, selected_objects) {
if ((ob == ob_src) || (ob->type != OB_GREASE_PENCIL)) {
continue;
}
/* Duplicate materials. */
for (const int i : IndexRange(ob_src->totcol)) {
Material *ma_src = BKE_object_material_get(ob_src, i + 1);
if (only_active && ma_src != ma_active) {
continue;
}
if (ma_src == nullptr) {
antoniov marked this conversation as resolved Outdated

Flip the condition and use continue instead

Flip the condition and use `continue` instead
continue;
}
BKE_object_material_ensure(bmain, ob, ma_src);
}
Review

Remove the /* notifiers */ comments, they aren't really helpful

Remove the `/* notifiers */` comments, they aren't really helpful
Review

A couple lines below too

A couple lines below too
DEG_id_tag_update(&ob->id, ID_RECALC_COPY_ON_WRITE);
}
CTX_DATA_END;
WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, nullptr);
return OPERATOR_FINISHED;
}
static void GREASE_PENCIL_OT_material_copy_to_object(wmOperatorType *ot)
{
PropertyRNA *prop;
/* Identifiers. */
ot->name = "Copy Materials to Selected Object";
ot->idname = "GREASE_PENCIL_OT_material_copy_to_object";
ot->description = "Append Materials of the active Grease Pencil to other object";
/* Callbacks. */
ot->exec = grease_pencil_material_copy_to_object_exec;
ot->poll = active_grease_pencil_poll;
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
prop = RNA_def_boolean(ot->srna,
"only_active",
true,
"Only Active",
"Append only active material, uncheck to append all materials");
RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
}
/** \} */
} // namespace blender::ed::greasepencil
void ED_operatortypes_grease_pencil_material()
@ -344,4 +414,5 @@ void ED_operatortypes_grease_pencil_material()
WM_operatortype_append(GREASE_PENCIL_OT_material_unlock_all);
WM_operatortype_append(GREASE_PENCIL_OT_material_lock_unused);
WM_operatortype_append(GREASE_PENCIL_OT_material_lock_unselected);
WM_operatortype_append(GREASE_PENCIL_OT_material_copy_to_object);
}

View File

@ -475,7 +475,7 @@ void PaintOperation::on_stroke_begin(const bContext &C, const InputSample &start
Material *material = BKE_grease_pencil_object_material_ensure_from_active_input_brush(
CTX_data_main(&C), object, brush);
const int material_index = BKE_grease_pencil_object_material_index_get(object, material);
const int material_index = BKE_object_material_index_get(object, material);
PaintOperationExecutor executor{C};
executor.process_start_sample(*this, C, start_sample, material_index);