[Bf-blender-cvs] [94b43f63687] asset-greasepencil: GPencil: Move to function the layer merge logic

Antonio Vazquez noreply at git.blender.org
Wed Jul 21 20:19:33 CEST 2021


Commit: 94b43f6368761d36dfd437ff6550be8fc5f3c00d
Author: Antonio Vazquez
Date:   Wed Jul 21 20:03:23 2021 +0200
Branches: asset-greasepencil
https://developer.blender.org/rB94b43f6368761d36dfd437ff6550be8fc5f3c00d

GPencil: Move to function the layer merge logic

This will be used by asset creation.

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

M	source/blender/editors/gpencil/gpencil_data.c
M	source/blender/editors/gpencil/gpencil_utils.c
M	source/blender/editors/include/ED_gpencil.h

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

diff --git a/source/blender/editors/gpencil/gpencil_data.c b/source/blender/editors/gpencil/gpencil_data.c
index b1e57079d28..8de0240013a 100644
--- a/source/blender/editors/gpencil/gpencil_data.c
+++ b/source/blender/editors/gpencil/gpencil_data.c
@@ -1333,56 +1333,7 @@ static int gpencil_merge_layer_exec(bContext *C, wmOperator *op)
     return OPERATOR_CANCELLED;
   }
 
-  /* Collect frames of gpl_dst in hash table to avoid O(n^2) lookups. */
-  GHash *gh_frames_dst = BLI_ghash_int_new_ex(__func__, 64);
-  LISTBASE_FOREACH (bGPDframe *, gpf_dst, &gpl_dst->frames) {
-    BLI_ghash_insert(gh_frames_dst, POINTER_FROM_INT(gpf_dst->framenum), gpf_dst);
-  }
-
-  /* Read all frames from merge layer and add any missing in destination layer,
-   * copying all previous strokes to keep the image equals.
-   * Need to do it in a separated loop to avoid strokes accumulation. */
-  LISTBASE_FOREACH (bGPDframe *, gpf_src, &gpl_src->frames) {
-    /* Try to find frame in destination layer hash table. */
-    bGPDframe *gpf_dst = BLI_ghash_lookup(gh_frames_dst, POINTER_FROM_INT(gpf_src->framenum));
-    if (!gpf_dst) {
-      gpf_dst = BKE_gpencil_layer_frame_get(gpl_dst, gpf_src->framenum, GP_GETFRAME_ADD_COPY);
-      BLI_ghash_insert(gh_frames_dst, POINTER_FROM_INT(gpf_src->framenum), gpf_dst);
-    }
-  }
-
-  /* Read all frames from merge layer and add strokes. */
-  LISTBASE_FOREACH (bGPDframe *, gpf_src, &gpl_src->frames) {
-    /* Try to find frame in destination layer hash table. */
-    bGPDframe *gpf_dst = BLI_ghash_lookup(gh_frames_dst, POINTER_FROM_INT(gpf_src->framenum));
-    /* Add to tail all strokes. */
-    if (gpf_dst) {
-      BLI_movelisttolist(&gpf_dst->strokes, &gpf_src->strokes);
-    }
-  }
-
-  /* Add Masks to destination layer. */
-  LISTBASE_FOREACH (bGPDlayer_Mask *, mask, &gpl_src->mask_layers) {
-    /* Don't add merged layers or missing layer names. */
-    if (!BKE_gpencil_layer_named_get(gpd, mask->name) || STREQ(mask->name, gpl_src->info) ||
-        STREQ(mask->name, gpl_dst->info)) {
-      continue;
-    }
-    if (!BKE_gpencil_layer_mask_named_get(gpl_dst, mask->name)) {
-      bGPDlayer_Mask *mask_new = MEM_dupallocN(mask);
-      BLI_addtail(&gpl_dst->mask_layers, mask_new);
-      gpl_dst->act_mask++;
-    }
-  }
-  /* Set destination layer as active. */
-  BKE_gpencil_layer_active_set(gpd, gpl_dst);
-
-  /* Now delete next layer */
-  BKE_gpencil_layer_delete(gpd, gpl_src);
-  BLI_ghash_free(gh_frames_dst, NULL, NULL);
-
-  /* Reorder masking. */
-  BKE_gpencil_layer_mask_sort(gpd, gpl_dst);
+  ED_gpencil_layer_merge(gpd, gpl_src, gpl_dst);
 
   /* notifiers */
   DEG_id_tag_update(&gpd->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY);
diff --git a/source/blender/editors/gpencil/gpencil_utils.c b/source/blender/editors/gpencil/gpencil_utils.c
index ba3d3b584d7..0c4a7bc4d14 100644
--- a/source/blender/editors/gpencil/gpencil_utils.c
+++ b/source/blender/editors/gpencil/gpencil_utils.c
@@ -3388,3 +3388,58 @@ void ED_gpencil_stroke_close_by_distance(bGPDstroke *gps, const float threshold)
     BKE_gpencil_stroke_close(gps);
   }
 }
+
+/* Merge two layers. */
+void ED_gpencil_layer_merge(bGPdata *gpd, bGPDlayer *gpl_src, bGPDlayer *gpl_dst)
+{
+  /* Collect frames of gpl_dst in hash table to avoid O(n^2) lookups. */
+  GHash *gh_frames_dst = BLI_ghash_int_new_ex(__func__, 64);
+  LISTBASE_FOREACH (bGPDframe *, gpf_dst, &gpl_dst->frames) {
+    BLI_ghash_insert(gh_frames_dst, POINTER_FROM_INT(gpf_dst->framenum), gpf_dst);
+  }
+
+  /* Read all frames from merge layer and add any missing in destination layer,
+   * copying all previous strokes to keep the image equals.
+   * Need to do it in a separated loop to avoid strokes accumulation. */
+  LISTBASE_FOREACH (bGPDframe *, gpf_src, &gpl_src->frames) {
+    /* Try to find frame in destination layer hash table. */
+    bGPDframe *gpf_dst = BLI_ghash_lookup(gh_frames_dst, POINTER_FROM_INT(gpf_src->framenum));
+    if (!gpf_dst) {
+      gpf_dst = BKE_gpencil_layer_frame_get(gpl_dst, gpf_src->framenum, GP_GETFRAME_ADD_COPY);
+      BLI_ghash_insert(gh_frames_dst, POINTER_FROM_INT(gpf_src->framenum), gpf_dst);
+    }
+  }
+
+  /* Read all frames from merge layer and add strokes. */
+  LISTBASE_FOREACH (bGPDframe *, gpf_src, &gpl_src->frames) {
+    /* Try to find frame in destination layer hash table. */
+    bGPDframe *gpf_dst = BLI_ghash_lookup(gh_frames_dst, POINTER_FROM_INT(gpf_src->framenum));
+    /* Add to tail all strokes. */
+    if (gpf_dst) {
+      BLI_movelisttolist(&gpf_dst->strokes, &gpf_src->strokes);
+    }
+  }
+
+  /* Add Masks to destination layer. */
+  LISTBASE_FOREACH (bGPDlayer_Mask *, mask, &gpl_src->mask_layers) {
+    /* Don't add merged layers or missing layer names. */
+    if (!BKE_gpencil_layer_named_get(gpd, mask->name) || STREQ(mask->name, gpl_src->info) ||
+        STREQ(mask->name, gpl_dst->info)) {
+      continue;
+    }
+    if (!BKE_gpencil_layer_mask_named_get(gpl_dst, mask->name)) {
+      bGPDlayer_Mask *mask_new = MEM_dupallocN(mask);
+      BLI_addtail(&gpl_dst->mask_layers, mask_new);
+      gpl_dst->act_mask++;
+    }
+  }
+  /* Set destination layer as active. */
+  BKE_gpencil_layer_active_set(gpd, gpl_dst);
+
+  /* Now delete next layer */
+  BKE_gpencil_layer_delete(gpd, gpl_src);
+  BLI_ghash_free(gh_frames_dst, NULL, NULL);
+
+  /* Reorder masking. */
+  BKE_gpencil_layer_mask_sort(gpd, gpl_dst);
+}
diff --git a/source/blender/editors/include/ED_gpencil.h b/source/blender/editors/include/ED_gpencil.h
index 8a8d91a570c..9603c99a7e3 100644
--- a/source/blender/editors/include/ED_gpencil.h
+++ b/source/blender/editors/include/ED_gpencil.h
@@ -205,6 +205,10 @@ void ED_gpencil_select_frame(struct bGPDlayer *gpl, int selx, short select_mode)
 bool ED_gpencil_layer_frames_delete(struct bGPDlayer *gpl);
 void ED_gpencil_layer_frames_duplicate(struct bGPDlayer *gpl);
 
+void ED_gpencil_layer_merge(struct bGPdata *gpd,
+                            struct bGPDlayer *gpl_src,
+                            struct bGPDlayer *gpl_dst);
+
 void ED_gpencil_layer_frames_keytype_set(struct bGPDlayer *gpl, short type);
 
 void ED_gpencil_layer_snap_frames(struct bGPDlayer *gpl, struct Scene *scene, short mode);



More information about the Bf-blender-cvs mailing list