[Bf-blender-cvs] [38b7563a4fb] master: Cleanup: de-duplicate gpencil logic to ensure materials

Campbell Barton noreply at git.blender.org
Tue Mar 23 04:02:09 CET 2021


Commit: 38b7563a4fbb0f06775e980b7439c0354dc78641
Author: Campbell Barton
Date:   Tue Mar 23 13:57:13 2021 +1100
Branches: master
https://developer.blender.org/rB38b7563a4fbb0f06775e980b7439c0354dc78641

Cleanup: de-duplicate gpencil logic to ensure materials

- Rename:
  `BKE_gpencil_object_material_get_index_name`, to
  `BKE_gpencil_object_material_index_get_by_name`
  Matching `BKE_gpencil_layer_get_by_name`.
- Move logic to ensure named materials into a new function:
  `BKE_gpencil_object_material_ensure_by_name`

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

M	source/blender/blenkernel/BKE_gpencil.h
M	source/blender/blenkernel/intern/gpencil.c
M	source/blender/editors/gpencil/gpencil_add_lineart.c
M	source/blender/editors/gpencil/gpencil_add_monkey.c
M	source/blender/editors/gpencil/gpencil_add_stroke.c

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

diff --git a/source/blender/blenkernel/BKE_gpencil.h b/source/blender/blenkernel/BKE_gpencil.h
index a0a3f30d6d8..4b4886e8bf3 100644
--- a/source/blender/blenkernel/BKE_gpencil.h
+++ b/source/blender/blenkernel/BKE_gpencil.h
@@ -228,6 +228,10 @@ struct Material *BKE_gpencil_object_material_ensure_from_brush(struct Main *bmai
 int BKE_gpencil_object_material_ensure(struct Main *bmain,
                                        struct Object *ob,
                                        struct Material *material);
+struct Material *BKE_gpencil_object_material_ensure_by_name(struct Main *bmain,
+                                                            struct Object *ob,
+                                                            const char *name,
+                                                            int *r_index);
 
 struct Material *BKE_gpencil_object_material_new(struct Main *bmain,
                                                  struct Object *ob,
@@ -235,7 +239,7 @@ struct Material *BKE_gpencil_object_material_new(struct Main *bmain,
                                                  int *r_index);
 
 int BKE_gpencil_object_material_index_get(struct Object *ob, struct Material *ma);
-int BKE_gpencil_object_material_get_index_name(struct Object *ob, char *name);
+int BKE_gpencil_object_material_index_get_by_name(struct Object *ob, const char *name);
 
 struct Material *BKE_gpencil_object_material_from_brush_get(struct Object *ob,
                                                             struct Brush *brush);
diff --git a/source/blender/blenkernel/intern/gpencil.c b/source/blender/blenkernel/intern/gpencil.c
index 3b46672f9cd..f7b895c7ca7 100644
--- a/source/blender/blenkernel/intern/gpencil.c
+++ b/source/blender/blenkernel/intern/gpencil.c
@@ -2446,7 +2446,7 @@ int BKE_gpencil_object_material_index_get(Object *ob, Material *ma)
   return -1;
 }
 
-int BKE_gpencil_object_material_get_index_name(Object *ob, char *name)
+int BKE_gpencil_object_material_index_get_by_name(Object *ob, const char *name)
 {
   short *totcol = BKE_object_material_len_p(ob);
   Material *read_ma = NULL;
@@ -2461,6 +2461,19 @@ int BKE_gpencil_object_material_get_index_name(Object *ob, char *name)
   return -1;
 }
 
+Material *BKE_gpencil_object_material_ensure_by_name(Main *bmain,
+                                                     Object *ob,
+                                                     const char *name,
+                                                     int *r_index)
+{
+  int index = BKE_gpencil_object_material_index_get_by_name(ob, name);
+  if (index != -1) {
+    *r_index = index;
+    return BKE_object_material_get(ob, index + 1);
+  }
+  return BKE_gpencil_object_material_new(bmain, ob, name, r_index);
+}
+
 /**
  * Create a default palette.
  * \param bmain: Main pointer
diff --git a/source/blender/editors/gpencil/gpencil_add_lineart.c b/source/blender/editors/gpencil/gpencil_add_lineart.c
index cd7e14278a4..6b28c6ec13e 100644
--- a/source/blender/editors/gpencil/gpencil_add_lineart.c
+++ b/source/blender/editors/gpencil/gpencil_add_lineart.c
@@ -55,19 +55,8 @@ static int gpencil_lineart_material(Main *bmain,
                                     const ColorTemplate *pct,
                                     const bool fill)
 {
-  short *totcol = BKE_object_material_len_p(ob);
-  Material *ma = NULL;
-  for (short i = 0; i < *totcol; i++) {
-    ma = BKE_gpencil_material(ob, i + 1);
-    if (STREQ(ma->id.name, pct->name)) {
-      return i;
-    }
-  }
-
-  int idx;
-
-  /* create a new one */
-  ma = BKE_gpencil_object_material_new(bmain, ob, pct->name, &idx);
+  int index;
+  Material *ma = BKE_gpencil_object_material_ensure_by_name(bmain, ob, pct->name, &index);
 
   copy_v4_v4(ma->gp_style->stroke_rgba, pct->line);
   srgb_to_linearrgb_v4(ma->gp_style->stroke_rgba, ma->gp_style->stroke_rgba);
@@ -79,7 +68,7 @@ static int gpencil_lineart_material(Main *bmain,
     ma->gp_style->flag |= GP_MATERIAL_FILL_SHOW;
   }
 
-  return idx;
+  return index;
 }
 
 /* ***************************************************************** */
diff --git a/source/blender/editors/gpencil/gpencil_add_monkey.c b/source/blender/editors/gpencil/gpencil_add_monkey.c
index d86bad7ef3c..4497d963c6d 100644
--- a/source/blender/editors/gpencil/gpencil_add_monkey.c
+++ b/source/blender/editors/gpencil/gpencil_add_monkey.c
@@ -50,19 +50,8 @@ typedef struct ColorTemplate {
 static int gpencil_monkey_color(
     Main *bmain, Object *ob, const ColorTemplate *pct, bool stroke, bool fill)
 {
-  short *totcol = BKE_object_material_len_p(ob);
-  Material *ma = NULL;
-  for (short i = 0; i < *totcol; i++) {
-    ma = BKE_gpencil_material(ob, i + 1);
-    if (STREQ(ma->id.name, pct->name)) {
-      return i;
-    }
-  }
-
-  int idx;
-
-  /* create a new one */
-  ma = BKE_gpencil_object_material_new(bmain, ob, pct->name, &idx);
+  int index;
+  Material *ma = BKE_gpencil_object_material_ensure_by_name(bmain, ob, pct->name, &index);
 
   copy_v4_v4(ma->gp_style->stroke_rgba, pct->line);
   srgb_to_linearrgb_v4(ma->gp_style->stroke_rgba, ma->gp_style->stroke_rgba);
@@ -81,7 +70,7 @@ static int gpencil_monkey_color(
     ma->gp_style->flag |= GP_MATERIAL_FILL_SHOW;
   }
 
-  return idx;
+  return index;
 }
 
 /* ***************************************************************** */
diff --git a/source/blender/editors/gpencil/gpencil_add_stroke.c b/source/blender/editors/gpencil/gpencil_add_stroke.c
index 1e1a70f9c1d..26237636526 100644
--- a/source/blender/editors/gpencil/gpencil_add_stroke.c
+++ b/source/blender/editors/gpencil/gpencil_add_stroke.c
@@ -52,19 +52,8 @@ static int gpencil_stroke_material(Main *bmain,
                                    const ColorTemplate *pct,
                                    const bool fill)
 {
-  short *totcol = BKE_object_material_len_p(ob);
-  Material *ma = NULL;
-  for (short i = 0; i < *totcol; i++) {
-    ma = BKE_gpencil_material(ob, i + 1);
-    if (STREQ(ma->id.name, pct->name)) {
-      return i;
-    }
-  }
-
-  int idx;
-
-  /* create a new one */
-  ma = BKE_gpencil_object_material_new(bmain, ob, pct->name, &idx);
+  int index;
+  Material *ma = BKE_gpencil_object_material_ensure_by_name(bmain, ob, pct->name, &index);
 
   copy_v4_v4(ma->gp_style->stroke_rgba, pct->line);
   srgb_to_linearrgb_v4(ma->gp_style->stroke_rgba, ma->gp_style->stroke_rgba);
@@ -76,7 +65,7 @@ static int gpencil_stroke_material(Main *bmain,
     ma->gp_style->flag |= GP_MATERIAL_FILL_SHOW;
   }
 
-  return idx;
+  return index;
 }
 
 /* ***************************************************************** */



More information about the Bf-blender-cvs mailing list