[Bf-blender-cvs] [960535ddf34] master: GPencil: New Append operators

Antonio Vazquez noreply at git.blender.org
Fri May 7 18:28:10 CEST 2021


Commit: 960535ddf3446204dbfcc0f23040bdb74f7a9720
Author: Antonio Vazquez
Date:   Fri May 7 18:27:47 2021 +0200
Branches: master
https://developer.blender.org/rB960535ddf3446204dbfcc0f23040bdb74f7a9720

GPencil: New Append operators

Now it's possible to append materials of one grease pencil object into another one. The operator allows active material or all materials.

Also, the Layer Copy To Object has been renamed to Layer Append to Object to keep consistency and now allows to append all layers at once.

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

M	release/scripts/startup/bl_ui/properties_data_gpencil.py
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_data_gpencil.py b/release/scripts/startup/bl_ui/properties_data_gpencil.py
index 69720a6c54b..d88c9a3502d 100644
--- a/release/scripts/startup/bl_ui/properties_data_gpencil.py
+++ b/release/scripts/startup/bl_ui/properties_data_gpencil.py
@@ -113,7 +113,8 @@ class GPENCIL_MT_layer_context_menu(Menu):
         layout.operator("gpencil.layer_merge", icon='SORT_ASC', text="Merge Down")
 
         layout.separator()
-        layout.menu("VIEW3D_MT_gpencil_copy_layer")
+        layout.menu("VIEW3D_MT_gpencil_append_active_layer")
+        layout.menu("VIEW3D_MT_gpencil_append_all_layers")
 
 
 class DATA_PT_gpencil_layers(DataButtonsPanel, Panel):
diff --git a/release/scripts/startup/bl_ui/properties_material_gpencil.py b/release/scripts/startup/bl_ui/properties_material_gpencil.py
index 6a5c000116f..ed5863b463a 100644
--- a/release/scripts/startup/bl_ui/properties_material_gpencil.py
+++ b/release/scripts/startup/bl_ui/properties_material_gpencil.py
@@ -53,6 +53,10 @@ 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_active_material")
+        layout.menu("VIEW3D_MT_gpencil_append_all_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 bdc924e3197..4b06a2df3a0 100644
--- a/release/scripts/startup/bl_ui/space_view3d.py
+++ b/release/scripts/startup/bl_ui/space_view3d.py
@@ -4956,26 +4956,73 @@ class VIEW3D_MT_assign_material(Menu):
                                 icon='LAYER_ACTIVE' if mat == mat_active else 'BLANK1').material = mat.name
 
 
-class VIEW3D_MT_gpencil_copy_layer(Menu):
-    bl_label = "Copy Layer to Object"
+def gpencil_layer_append_menu_items(context, layout, only_active):
+    done = False
+    view_layer = context.view_layer
+    obact = context.active_object
+    gpl = context.active_gpencil_layer
+
+    done = False
+    if gpl is not None:
+        for ob in view_layer.objects:
+            if ob.type == 'GPENCIL' and ob != obact:
+                op = layout.operator("gpencil.layer_duplicate_object", text=ob.name)
+                op.object = ob.name
+                op.only_active = only_active
+                done = True
+
+        if done is False:
+            layout.label(text="No destination object", icon='ERROR')
+    else:
+        layout.label(text="No layer to copy", icon='ERROR')
+
+
+class VIEW3D_MT_gpencil_append_active_layer(Menu):
+    bl_label = "Append Active Layer to Object"
 
     def draw(self, context):
         layout = self.layout
-        view_layer = context.view_layer
-        obact = context.active_object
-        gpl = context.active_gpencil_layer
-
-        done = False
-        if gpl is not None:
-            for ob in view_layer.objects:
-                if ob.type == 'GPENCIL' and ob != obact:
-                    layout.operator("gpencil.layer_duplicate_object", text=ob.name).object = ob.name
-                    done = True
-
-            if done is False:
-                layout.label(text="No destination object", icon='ERROR')
-        else:
-            layout.label(text="No layer to copy", icon='ERROR')
+        gpencil_layer_append_menu_items(context, layout, True)
+
+
+class VIEW3D_MT_gpencil_append_all_layers(Menu):
+    bl_label = "Append All Layers to Object"
+
+    def draw(self, context):
+        layout = self.layout
+        gpencil_layer_append_menu_items(context, layout, False)
+
+
+def gpencil_material_menu_items(context, layout, only_selected):
+    done = False
+    view_layer = context.view_layer
+    obact = context.active_object
+
+    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 = only_selected
+            done = True
+
+    if done is False:
+        layout.label(text="No destination object", icon='ERROR')
+
+
+class VIEW3D_MT_gpencil_append_active_material(Menu):
+    bl_label = "Append Active Material to Object"
+
+    def draw(self, context):
+        layout = self.layout
+        gpencil_material_menu_items(context, layout, True)
+
+
+class VIEW3D_MT_gpencil_append_all_materials(Menu):
+    bl_label = "Append All Materials to Object"
+
+    def draw(self, context):
+        layout = self.layout
+        gpencil_material_menu_items(context, layout, False)
 
 
 class VIEW3D_MT_edit_gpencil(Menu):
@@ -7642,7 +7689,10 @@ classes = (
     VIEW3D_MT_weight_gpencil,
     VIEW3D_MT_gpencil_animation,
     VIEW3D_MT_gpencil_simplify,
-    VIEW3D_MT_gpencil_copy_layer,
+    VIEW3D_MT_gpencil_append_active_layer,
+    VIEW3D_MT_gpencil_append_all_layers,
+    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 c93fcb9eb8c..c5f094e030a 100644
--- a/source/blender/editors/gpencil/gpencil_data.c
+++ b/source/blender/editors/gpencil/gpencil_data.c
@@ -557,6 +557,7 @@ static int gpencil_layer_duplicate_object_exec(bContext *C, wmOperator *op)
   if (name[0] == '\0') {
     return OPERATOR_CANCELLED;
   }
+  const bool only_active = RNA_boolean_get(op->ptr, "only_active");
 
   Object *ob_dst = (Object *)BKE_scene_object_find_by_name(scene, name);
 
@@ -564,10 +565,10 @@ static int gpencil_layer_duplicate_object_exec(bContext *C, wmOperator *op)
 
   Object *ob_src = CTX_data_active_object(C);
   bGPdata *gpd_src = (bGPdata *)ob_src->data;
-  bGPDlayer *gpl_src = BKE_gpencil_layer_active_get(gpd_src);
+  bGPDlayer *gpl_active = BKE_gpencil_layer_active_get(gpd_src);
 
   /* Sanity checks. */
-  if (ELEM(NULL, gpd_src, gpl_src, ob_dst)) {
+  if (ELEM(NULL, gpd_src, ob_dst)) {
     return OPERATOR_CANCELLED;
   }
   /* Cannot copy itself and check destination type. */
@@ -576,47 +577,55 @@ static int gpencil_layer_duplicate_object_exec(bContext *C, wmOperator *op)
   }
 
   bGPdata *gpd_dst = (bGPdata *)ob_dst->data;
+  /* Disable destination active layer to keep order. */
+  LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd_dst->layers) {
+    gpl->flag &= ~GP_LAYER_ACTIVE;
+  }
 
-  /* Create new layer. */
-  bGPDlayer *gpl_dst = BKE_gpencil_layer_addnew(gpd_dst, gpl_src->info, true);
-  /* Need to copy some variables (not all). */
-  gpl_dst->onion_flag = gpl_src->onion_flag;
-  gpl_dst->thickness = gpl_src->thickness;
-  gpl_dst->line_change = gpl_src->line_change;
-  copy_v4_v4(gpl_dst->tintcolor, gpl_src->tintcolor);
-  gpl_dst->opacity = gpl_src->opacity;
-
-  /* Create all frames. */
-  LISTBASE_FOREACH (bGPDframe *, gpf_src, &gpl_src->frames) {
-
-    if ((mode == GP_LAYER_COPY_OBJECT_ACT_FRAME) && (gpf_src != gpl_src->actframe)) {
+  LISTBASE_FOREACH (bGPDlayer *, gpl_src, &gpd_src->layers) {
+    if ((only_active) && (gpl_src != gpl_active)) {
       continue;
     }
+    /* Create new layer. */
+    bGPDlayer *gpl_dst = BKE_gpencil_layer_addnew(gpd_dst, gpl_src->info, true);
+    /* Need to copy some variables (not all). */
+    gpl_dst->onion_flag = gpl_src->onion_flag;
+    gpl_dst->thickness = gpl_src->thickness;
+    gpl_dst->line_change = gpl_src->line_change;
+    copy_v4_v4(gpl_dst->tintcolor, gpl_src->tintcolor);
+    gpl_dst->opacity = gpl_src->opacity;
 
-    /* Create new frame. */
-    bGPDframe *gpf_dst = BKE_gpencil_frame_addnew(gpl_dst, gpf_src->framenum);
+    /* Create all frames. */
+    LISTBASE_FOREACH (bGPDframe *, gpf_src, &gpl_src->frames) {
 
-    /* Copy strokes. */
-    LISTBASE_FOREACH (bGPDstroke *, gps_src, &gpf_src->strokes) {
+      if ((mode == GP_LAYER_COPY_OBJECT_ACT_FRAME) && (gpf_src != gpl_src->actframe)) {
+        continue;
+      }
 
-      /* Make copy of source stroke. */
-      bGPDstroke *gps_dst = BKE_gpencil_stroke_duplicate(gps_src, true, true);
+      /* Create new frame. */
+      bGPDframe *gpf_dst = BKE_gpencil_frame_addnew(gpl_dst, gpf_src->framenum);
 
-      /* Check if material is in destination object,
-       * otherwise add the slot with the material. */
-      Material *ma_src = BKE_object_material_get(ob_src, gps_src->mat_nr + 1);
-      if (ma_src != NULL) {
-        int idx = BKE_gpencil_object_material_ensure(bmain, ob_dst, ma_src);
+      /* Copy strokes. */
+      LISTBASE_FOREACH (bGPDstroke *, gps_src, &gpf_src->strokes) {
 
-        /* Reassign the stroke material to the right slot in destination object. */
-        gps_dst->mat_nr = idx;
-      }
+        /* Make copy of source stroke. */
+        bGPDstroke *gps_dst = BKE_gpencil_stroke_duplicate(gps_src, true, true);
+
+        /* Check if material is in destination object,
+         * otherwise add the slot with the material. */
+        Material *ma_src = BKE_object_material_get(ob_src, gps_src->mat_nr + 1);
+        if (ma_src != NULL) {
+          int idx = BKE_gpencil_object_material_ensure(bmain, ob_dst, ma_src);
 
-      /* Add new stroke to frame. */
-      BLI_addtail(&gpf_dst->strokes, gps_dst);
+          /* Reassign the stroke material to the right slot in destination object. */
+          gps_dst->mat_nr = idx;
+        }
+
+        /* Add new stroke to frame. */
+        BLI_addtail(&gpf_dst->strokes, gps_dst);
+      }
     }
   }
-
   /* notifiers */
   DEG_id_tag_update(&gpd_dst->id,
                     ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_COPY_ON_WRITE);
@@ -628,6 +637,8 @@ static int gpencil_layer_duplicate_object_exec(bContext *C, wmOperator *op)
 
 void GPENCIL_OT_layer_duplicate_object(wmOperatorType *ot)
 {
+  PropertyRNA *prop;
+
   static const EnumPropertyItem copy_mode[] = {
       {GP_LAYER_COPY_OBJECT_ALL_FRAME, "ALL", 0, "All Frames", ""},
       {G

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list