[Bf-blender-cvs] [5e6e5508619] greasepencil-object: GPencil: Use collection names as layer names in conversion from curves

Antonioya noreply at git.blender.org
Mon Jul 8 11:55:32 CEST 2019


Commit: 5e6e55086193e2e7b92c854ed1f5f56aa7369dd3
Author: Antonioya
Date:   Mon Jul 8 11:03:45 2019 +0200
Branches: greasepencil-object
https://developer.blender.org/rB5e6e55086193e2e7b92c854ed1f5f56aa7369dd3

GPencil: Use collection names as layer names in conversion from curves

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

M	source/blender/blenkernel/BKE_gpencil.h
M	source/blender/blenkernel/intern/gpencil.c
M	source/blender/editors/object/object_add.c

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

diff --git a/source/blender/blenkernel/BKE_gpencil.h b/source/blender/blenkernel/BKE_gpencil.h
index 47c35f2b2f8..55fc1eae769 100644
--- a/source/blender/blenkernel/BKE_gpencil.h
+++ b/source/blender/blenkernel/BKE_gpencil.h
@@ -299,10 +299,11 @@ float BKE_gpencil_multiframe_falloff_calc(
     struct bGPDframe *gpf, int actnum, int f_init, int f_end, struct CurveMapping *cur_falloff);
 
 void BKE_gpencil_convert_curve(struct Main *bmain,
-                               const struct Scene *scene,
+                               struct Scene *scene,
                                struct Object *ob_gp,
                                struct Object *ob_cu,
-                               const bool gpencil_lines);
+                               const bool gpencil_lines,
+                               const bool use_collections);
 
 extern void (*BKE_gpencil_batch_cache_dirty_tag_cb)(struct bGPdata *gpd);
 extern void (*BKE_gpencil_batch_cache_free_cb)(struct bGPdata *gpd);
diff --git a/source/blender/blenkernel/intern/gpencil.c b/source/blender/blenkernel/intern/gpencil.c
index 73bbae0c95e..1ba1e9a39c6 100644
--- a/source/blender/blenkernel/intern/gpencil.c
+++ b/source/blender/blenkernel/intern/gpencil.c
@@ -49,14 +49,15 @@
 #include "BKE_action.h"
 #include "BKE_animsys.h"
 #include "BKE_curve.h"
+#include "BKE_collection.h"
+#include "BKE_colortools.h"
 #include "BKE_deform.h"
 #include "BKE_gpencil.h"
-#include "BKE_colortools.h"
 #include "BKE_icons.h"
 #include "BKE_library.h"
 #include "BKE_main.h"
-#include "BKE_object.h"
 #include "BKE_material.h"
+#include "BKE_object.h"
 
 #include "BLI_math_color.h"
 
@@ -2113,14 +2114,35 @@ static void gpencil_add_new_points(bGPDstroke *gps, float *coord_array, int init
   }
 }
 
+/* Helper function to get the first collection that includes the object */
+static Collection *gpencil_get_parent_collection(Scene *scene, Object *ob)
+{
+  FOREACH_SCENE_COLLECTION_BEGIN (scene, collection) {
+    for (CollectionObject *cob = collection->gobject.first; cob; cob = cob->next) {
+      if (cob->ob == ob) {
+        return collection;
+      }
+    }
+  }
+  FOREACH_SCENE_COLLECTION_END;
+
+  return NULL;
+}
+
 /* Convert a curve to grease pencil stroke.
  *
  * \param scene: Original scene.
  * \param ob_gp: Grease pencil object to add strokes.
  * \param ob_cu: Curve to convert.
+ * \param gpencil_lines: Use lines for strokes.
+ * \param use_collections: Create layers using collection names.
  */
-void BKE_gpencil_convert_curve(
-    Main *bmain, const Scene *scene, Object *ob_gp, Object *ob_cu, const bool gpencil_lines)
+void BKE_gpencil_convert_curve(Main *bmain,
+                               Scene *scene,
+                               Object *ob_gp,
+                               Object *ob_cu,
+                               const bool gpencil_lines,
+                               const bool use_collections)
 {
   if (ELEM(NULL, ob_gp, ob_cu) || (ob_gp->type != OB_GPENCIL)) {
     return;
@@ -2130,12 +2152,26 @@ void BKE_gpencil_convert_curve(
   Curve *cu = (Curve *)ob_cu->data;
   Nurb *nu = NULL;
   bool cyclic = true;
+  bGPDlayer *gpl = NULL;
 
   /* Check if there is an active layer. */
-  bGPDlayer *gpl = BKE_gpencil_layer_getactive(gpd);
+  if (use_collections) {
+    Collection *collection = gpencil_get_parent_collection(scene, ob_cu);
+    if (collection != NULL) {
+      gpl = BLI_findstring(&gpd->layers, collection->id.name + 2, offsetof(bGPDlayer, info));
+      if (gpl == NULL) {
+        gpl = BKE_gpencil_layer_addnew(gpd, collection->id.name + 2, true);
+      }
+    }
+  }
+
   if (gpl == NULL) {
-    gpl = BKE_gpencil_layer_addnew(gpd, DATA_("GP_Layer"), true);
+    bGPDlayer *gpl = BKE_gpencil_layer_getactive(gpd);
+    if (gpl == NULL) {
+      gpl = BKE_gpencil_layer_addnew(gpd, DATA_("GP_Layer"), true);
+    }
   }
+
   /* Check if there is an active frame. */
   bGPDframe *gpf = BKE_gpencil_layer_getframe(gpl, CFRA, GP_GETFRAME_ADD_COPY);
   /* Create Stroke. */
@@ -2232,8 +2268,8 @@ void BKE_gpencil_convert_curve(
         /* Free memory. */
         MEM_SAFE_FREE(coord_array);
 
-        /* As the last point of segment is the first point of next segment, back one array element
-         * to avoid duplicated points on the same location.
+        /* As the last point of segment is the first point of next segment, back one array
+         * element to avoid duplicated points on the same location.
          */
         init += resolu - 1;
       }
diff --git a/source/blender/editors/object/object_add.c b/source/blender/editors/object/object_add.c
index 66fe88a9c2b..22550e18dba 100644
--- a/source/blender/editors/object/object_add.c
+++ b/source/blender/editors/object/object_add.c
@@ -2140,6 +2140,7 @@ static int convert_exec(bContext *C, wmOperator *op)
   const short target = RNA_enum_get(op->ptr, "target");
   bool keep_original = RNA_boolean_get(op->ptr, "keep_original");
   const bool gpencil_lines = RNA_boolean_get(op->ptr, "gpencil_lines");
+  const bool use_collections = RNA_boolean_get(op->ptr, "use_collections");
   int a, mballConverted = 0;
 
   /* don't forget multiple users! */
@@ -2384,7 +2385,7 @@ static int convert_exec(bContext *C, wmOperator *op)
           ushort local_view_bits = (v3d && v3d->localvd) ? v3d->local_view_uuid : 0;
           gpencil_ob = ED_gpencil_add_object(C, scene, cur, local_view_bits);
         }
-        BKE_gpencil_convert_curve(bmain, scene, gpencil_ob, ob, gpencil_lines);
+        BKE_gpencil_convert_curve(bmain, scene, gpencil_ob, ob, gpencil_lines, use_collections);
       }
     }
     else if (ob->type == OB_MBALL && target == OB_MESH) {
@@ -2533,7 +2534,12 @@ void OBJECT_OT_convert(wmOperatorType *ot)
                   "gpencil_lines",
                   1,
                   "GPencil Lines",
-                  "Use black color lines for grease pencil conversion");
+                  "Use lines for grease pencil conversion");
+  RNA_def_boolean(ot->srna,
+                  "use_collections",
+                  1,
+                  "Use Collections",
+                  "Use name of collections as name for grease pencil layers");
 }
 
 /** \} */



More information about the Bf-blender-cvs mailing list