[Bf-blender-cvs] [c94558c30a5] greasepencil-object: GPencil: Add option to append only active material

Antonio Vazquez noreply at git.blender.org
Thu May 6 17:26:11 CEST 2021


Commit: c94558c30a58221924fb548d371246448ea686cc
Author: Antonio Vazquez
Date:   Thu May 6 17:25:55 2021 +0200
Branches: greasepencil-object
https://developer.blender.org/rBc94558c30a58221924fb548d371246448ea686cc

GPencil: Add option to append only active material

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

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

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

diff --git a/release/scripts/startup/bl_ui/properties_material_gpencil.py b/release/scripts/startup/bl_ui/properties_material_gpencil.py
index 509a0b45390..ed5863b463a 100644
--- a/release/scripts/startup/bl_ui/properties_material_gpencil.py
+++ b/release/scripts/startup/bl_ui/properties_material_gpencil.py
@@ -54,7 +54,8 @@ class GPENCIL_MT_material_context_menu(Menu):
         layout.operator("gpencil.extract_palette_vertex", text="Extract Palette from Vertex Color")
 
         layout.separator()
-        layout.menu("VIEW3D_MT_gpencil_append_materials")
+        layout.menu("VIEW3D_MT_gpencil_append_active_material")
+        layout.menu("VIEW3D_MT_gpencil_append_all_materials")
 
 
 class GPENCIL_UL_matslots(UIList):
diff --git a/release/scripts/startup/bl_ui/space_view3d.py b/release/scripts/startup/bl_ui/space_view3d.py
index 3ca335c31e3..07fd4688e7c 100644
--- a/release/scripts/startup/bl_ui/space_view3d.py
+++ b/release/scripts/startup/bl_ui/space_view3d.py
@@ -4978,8 +4978,8 @@ 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"
+class VIEW3D_MT_gpencil_append_active_material(Menu):
+    bl_label = "Append Active Material to Object"
 
     def draw(self, context):
         layout = self.layout
@@ -4989,7 +4989,29 @@ class VIEW3D_MT_gpencil_append_materials(Menu):
         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
+                op = layout.operator("gpencil.materials_append_to_object", text=ob.name)
+                op.object = ob.name
+                op.only_selected = True
+                done = True
+
+        if done is False:
+            layout.label(text="No destination object", icon='ERROR')
+
+
+class VIEW3D_MT_gpencil_append_all_materials(Menu):
+    bl_label = "Append All 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:
+                op = layout.operator("gpencil.materials_append_to_object", text=ob.name)
+                op.object = ob.name
+                op.only_selected = False
                 done = True
 
         if done is False:
@@ -7664,7 +7686,8 @@ classes = (
     VIEW3D_MT_gpencil_animation,
     VIEW3D_MT_gpencil_simplify,
     VIEW3D_MT_gpencil_copy_layer,
-    VIEW3D_MT_gpencil_append_materials,
+    VIEW3D_MT_gpencil_append_active_material,
+    VIEW3D_MT_gpencil_append_all_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 9b84feac922..675c33ad01a 100644
--- a/source/blender/editors/gpencil/gpencil_data.c
+++ b/source/blender/editors/gpencil/gpencil_data.c
@@ -3622,9 +3622,11 @@ static int gpencil_materials_append_to_object_exec(bContext *C, wmOperator *op)
   if (name[0] == '\0') {
     return OPERATOR_CANCELLED;
   }
+  const bool only_selected = RNA_boolean_get(op->ptr, "only_selected");
 
   Object *ob_dst = (Object *)BKE_scene_object_find_by_name(scene, name);
   Object *ob_src = CTX_data_active_object(C);
+  Material *ma_active = BKE_gpencil_material(ob_src, ob_src->actcol);
 
   /* Sanity checks. */
   if (ELEM(NULL, ob_src, ob_dst)) {
@@ -3638,6 +3640,10 @@ static int gpencil_materials_append_to_object_exec(bContext *C, wmOperator *op)
   /* Duplicate materials. */
   for (short i = 0; i < ob_src->totcol; i++) {
     Material *ma_src = BKE_object_material_get(ob_src, i + 1);
+    if (only_selected && ma_src != ma_active) {
+      continue;
+    }
+
     if (ma_src != NULL) {
       BKE_gpencil_object_material_ensure(bmain, ob_dst, ma_src);
     }
@@ -3652,6 +3658,8 @@ static int gpencil_materials_append_to_object_exec(bContext *C, wmOperator *op)
 
 void GPENCIL_OT_materials_append_to_object(wmOperatorType *ot)
 {
+  PropertyRNA *prop;
+
   /* identifiers */
   ot->name = "Append Matewrials to New Object";
   ot->idname = "GPENCIL_OT_materials_append_to_object";
@@ -3667,6 +3675,13 @@ void GPENCIL_OT_materials_append_to_object(wmOperatorType *ot)
   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);
+
+  prop = RNA_def_boolean(ot->srna,
+                         "only_selected",
+                         true,
+                         "Only Selected",
+                         "Append only selected material, uncheck to append all materials");
+  RNA_def_property_flag(ot->prop, PROP_HIDDEN | PROP_SKIP_SAVE);
 }
 
 /* Parent GPencil object to Lattice */



More information about the Bf-blender-cvs mailing list