[Bf-blender-cvs] [317a2e65401] greasepencil-object: GPencil: Append Layer for Active or All layers

Antonio Vazquez noreply at git.blender.org
Thu May 6 19:12:45 CEST 2021


Commit: 317a2e6540123e338e8b266d7f3664840c1aa959
Author: Antonio Vazquez
Date:   Thu May 6 19:12:41 2021 +0200
Branches: greasepencil-object
https://developer.blender.org/rB317a2e6540123e338e8b266d7f3664840c1aa959

GPencil: Append Layer for Active or All layers

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

M	release/scripts/startup/bl_ui/properties_data_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_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/space_view3d.py b/release/scripts/startup/bl_ui/space_view3d.py
index 8e4f1075a17..ad3768b522d 100644
--- a/release/scripts/startup/bl_ui/space_view3d.py
+++ b/release/scripts/startup/bl_ui/space_view3d.py
@@ -4956,26 +4956,41 @@ 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 = "Append 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):
@@ -7677,7 +7692,8 @@ 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,
diff --git a/source/blender/editors/gpencil/gpencil_data.c b/source/blender/editors/gpencil/gpencil_data.c
index dcd72e71c64..91b11985823 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. */
@@ -577,46 +578,50 @@ static int gpencil_layer_duplicate_object_exec(bContext *C, wmOperator *op)
 
   bGPdata *gpd_dst = (bGPdata *)ob_dst->data;
 
-  /* 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);
+
+          /* 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);
+        /* 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 +633,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", ""},
       {GP_LAYER_COPY_OBJECT_ACT_FRAME, "ACTIVE", 0, "Active Frame", ""},
@@ -651,6 +658,13 @@ void GPENCIL_OT_layer_duplicate_object(wmOperatorType *ot)
   RNA_def_property_flag(ot->prop, PROP_HIDDEN | PROP_SKIP_SAVE);
 
   RNA_def_enum(ot->srna, "mode", copy_mode, GP_LAYER_COPY_OBJECT_ALL_FRAME, "Mode", "");
+
+  prop = RNA_def_boolean(ot->srna,
+                         "only_active",
+                         true,
+                         "Only Active",
+                         "Append only active Layer, uncheck to append all layers");
+  RNA_def_property_flag(ot->prop, PROP_HIDDEN | PROP_SKIP_SAVE);
 }
 
 /* ********************* Duplicate Frame ************************** */



More information about the Bf-blender-cvs mailing list