[Bf-blender-cvs] [00f2e431495] greasepencil-object: GPencil: New operator to Append materials into new object

Antonio Vazquez noreply at git.blender.org
Tue May 4 19:33:49 CEST 2021


Commit: 00f2e43149515599df2beb17cc34a30027360ab0
Author: Antonio Vazquez
Date:   Tue May 4 19:33:43 2021 +0200
Branches: greasepencil-object
https://developer.blender.org/rB00f2e43149515599df2beb17cc34a30027360ab0

GPencil: New operator to Append materials into new object

This is a production request to be able to append all materials of one object into another object.

===================================================================

M	release/scripts/startup/bl_ui/properties_material_gpencil.py
M	release/scripts/startup/bl_ui/space_view3d.py
M	source/blender/editors/gpencil/gpencil_data.c
M	source/blender/editors/gpencil/gpencil_intern.h
M	source/blender/editors/gpencil/gpencil_ops.c

===================================================================

diff --git a/release/scripts/startup/bl_ui/properties_material_gpencil.py b/release/scripts/startup/bl_ui/properties_material_gpencil.py
index 6a5c000116f..509a0b45390 100644
--- a/release/scripts/startup/bl_ui/properties_material_gpencil.py
+++ b/release/scripts/startup/bl_ui/properties_material_gpencil.py
@@ -53,6 +53,9 @@ class GPENCIL_MT_material_context_menu(Menu):
         layout.operator("gpencil.material_to_vertex_color", text="Convert Materials to Vertex Color")
         layout.operator("gpencil.extract_palette_vertex", text="Extract Palette from Vertex Color")
 
+        layout.separator()
+        layout.menu("VIEW3D_MT_gpencil_append_materials")
+
 
 class GPENCIL_UL_matslots(UIList):
     def draw_item(self, _context, layout, _data, item, icon, _active_data, _active_propname, _index):
diff --git a/release/scripts/startup/bl_ui/space_view3d.py b/release/scripts/startup/bl_ui/space_view3d.py
index d86e65d9f0d..3ca335c31e3 100644
--- a/release/scripts/startup/bl_ui/space_view3d.py
+++ b/release/scripts/startup/bl_ui/space_view3d.py
@@ -4957,7 +4957,7 @@ class VIEW3D_MT_assign_material(Menu):
 
 
 class VIEW3D_MT_gpencil_copy_layer(Menu):
-    bl_label = "Copy Layer to Object"
+    bl_label = "Append Layer to Object"
 
     def draw(self, context):
         layout = self.layout
@@ -4978,6 +4978,24 @@ class VIEW3D_MT_gpencil_copy_layer(Menu):
             layout.label(text="No layer to copy", icon='ERROR')
 
 
+class VIEW3D_MT_gpencil_append_materials(Menu):
+    bl_label = "Append Materials to Object"
+
+    def draw(self, context):
+        layout = self.layout
+        view_layer = context.view_layer
+        obact = context.active_object
+
+        done = False
+        for ob in view_layer.objects:
+            if ob.type == 'GPENCIL' and ob != obact:
+                layout.operator("gpencil.materials_append_to_object", text=ob.name).object = ob.name
+                done = True
+
+        if done is False:
+            layout.label(text="No destination object", icon='ERROR')
+
+
 class VIEW3D_MT_edit_gpencil(Menu):
     bl_label = "Grease Pencil"
 
@@ -7646,6 +7664,7 @@ classes = (
     VIEW3D_MT_gpencil_animation,
     VIEW3D_MT_gpencil_simplify,
     VIEW3D_MT_gpencil_copy_layer,
+    VIEW3D_MT_gpencil_append_materials,
     VIEW3D_MT_gpencil_autoweights,
     VIEW3D_MT_gpencil_edit_context_menu,
     VIEW3D_MT_edit_curve,
diff --git a/source/blender/editors/gpencil/gpencil_data.c b/source/blender/editors/gpencil/gpencil_data.c
index c93fcb9eb8c..60d5ed411e5 100644
--- a/source/blender/editors/gpencil/gpencil_data.c
+++ b/source/blender/editors/gpencil/gpencil_data.c
@@ -3589,6 +3589,89 @@ void GPENCIL_OT_set_active_material(wmOperatorType *ot)
   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
 }
 
+/* ********************* Append Materials in a new object ************************** */
+static bool gpencil_materials_append_to_object_poll(bContext *C)
+{
+  ViewLayer *view_layer = CTX_data_view_layer(C);
+  Object *ob = CTX_data_active_object(C);
+  if ((ob == NULL) || (ob->type != OB_GPENCIL)) {
+    return false;
+  }
+
+  bGPdata *gpd = (bGPdata *)ob->data;
+  bGPDlayer *gpl = BKE_gpencil_layer_active_get(gpd);
+
+  if (gpl == NULL) {
+    return false;
+  }
+
+  /* check there are more grease pencil objects */
+  LISTBASE_FOREACH (Base *, base, &view_layer->object_bases) {
+    if ((base->object != ob) && (base->object->type == OB_GPENCIL)) {
+      return true;
+    }
+  }
+
+  return false;
+}
+
+static int gpencil_materials_append_to_object_exec(bContext *C, wmOperator *op)
+{
+  Main *bmain = CTX_data_main(C);
+  Scene *scene = CTX_data_scene(C);
+  char name[MAX_ID_NAME - 2];
+  RNA_string_get(op->ptr, "object", name);
+
+  if (name[0] == '\0') {
+    return OPERATOR_CANCELLED;
+  }
+
+  Object *ob_dst = (Object *)BKE_scene_object_find_by_name(scene, name);
+  Object *ob_src = CTX_data_active_object(C);
+
+  /* Sanity checks. */
+  if (ELEM(NULL, ob_src, ob_dst)) {
+    return OPERATOR_CANCELLED;
+  }
+  /* Cannot copy itself and check destination type. */
+  if ((ob_src == ob_dst) || (ob_dst->type != OB_GPENCIL)) {
+    return OPERATOR_CANCELLED;
+  }
+
+  /* Duplicate materials. */
+  for (short i = 0; i < ob_src->totcol; i++) {
+    Material *ma_src = BKE_object_material_get(ob_src, i + 1);
+    if (ma_src != NULL) {
+      int idx = BKE_gpencil_object_material_ensure(bmain, ob_dst, ma_src);
+    }
+  }
+
+  /* notifiers */
+  DEG_id_tag_update(&ob_dst->id, ID_RECALC_COPY_ON_WRITE);
+  WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
+
+  return OPERATOR_FINISHED;
+}
+
+void GPENCIL_OT_materials_append_to_object(wmOperatorType *ot)
+{
+  /* identifiers */
+  ot->name = "Append Matewrials to New Object";
+  ot->idname = "GPENCIL_OT_materials_append_to_object";
+  ot->description = "Append Materials of the active Grease Pencil to other object";
+
+  /* callbacks */
+  ot->exec = gpencil_materials_append_to_object_exec;
+  ot->poll = gpencil_materials_append_to_object_poll;
+
+  /* flags */
+  ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+
+  ot->prop = RNA_def_string(
+      ot->srna, "object", NULL, MAX_ID_NAME - 2, "Object", "Name of the destination object");
+  RNA_def_property_flag(ot->prop, PROP_HIDDEN | PROP_SKIP_SAVE);
+}
+
 /* Parent GPencil object to Lattice */
 bool ED_gpencil_add_lattice_modifier(const bContext *C,
                                      ReportList *reports,
diff --git a/source/blender/editors/gpencil/gpencil_intern.h b/source/blender/editors/gpencil/gpencil_intern.h
index 09200125cb7..cce56acd3e4 100644
--- a/source/blender/editors/gpencil/gpencil_intern.h
+++ b/source/blender/editors/gpencil/gpencil_intern.h
@@ -538,6 +538,7 @@ void GPENCIL_OT_material_lock_unused(struct wmOperatorType *ot);
 void GPENCIL_OT_material_select(struct wmOperatorType *ot);
 void GPENCIL_OT_material_set(struct wmOperatorType *ot);
 void GPENCIL_OT_set_active_material(struct wmOperatorType *ot);
+void GPENCIL_OT_materials_append_to_object(struct wmOperatorType *ot);
 
 /* convert old 2.7 files to 2.8 */
 void GPENCIL_OT_convert_old_files(struct wmOperatorType *ot);
diff --git a/source/blender/editors/gpencil/gpencil_ops.c b/source/blender/editors/gpencil/gpencil_ops.c
index 7d454eb3be1..6ac1161bff8 100644
--- a/source/blender/editors/gpencil/gpencil_ops.c
+++ b/source/blender/editors/gpencil/gpencil_ops.c
@@ -651,6 +651,7 @@ void ED_operatortypes_gpencil(void)
 
   WM_operatortype_append(GPENCIL_OT_material_to_vertex_color);
   WM_operatortype_append(GPENCIL_OT_extract_palette_vertex);
+  WM_operatortype_append(GPENCIL_OT_materials_append_to_object);
 
   WM_operatortype_append(GPENCIL_OT_transform_fill);
   WM_operatortype_append(GPENCIL_OT_reset_transform_fill);



More information about the Bf-blender-cvs mailing list