[Bf-blender-cvs] [7021bd52738] master: GPencil: Only brushes with pinned materials have materials

Antonioya noreply at git.blender.org
Mon Mar 25 17:06:10 CET 2019


Commit: 7021bd527380b4d87cf48057f0039509326b03dd
Author: Antonioya
Date:   Mon Mar 25 17:02:42 2019 +0100
Branches: master
https://developer.blender.org/rB7021bd527380b4d87cf48057f0039509326b03dd

GPencil: Only brushes with pinned materials have materials

Using GP_BRUSH_MATERIAL_PINNED to switch between active material and brush material, instead of updating all brushes on active material changes. This will allow brushes to have no material and therefore to not inflate the user count.

This fix T62465.

Patch contributed by @matc
Reviewers: @brecht @antoniov @billreynish @mendio

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

M	release/scripts/startup/bl_ui/properties_grease_pencil_common.py
M	release/scripts/startup/bl_ui/space_topbar.py
M	release/scripts/startup/bl_ui/space_view3d_toolbar.py
M	source/blender/blenkernel/BKE_brush.h
M	source/blender/blenkernel/BKE_gpencil.h
M	source/blender/blenkernel/intern/brush.c
M	source/blender/blenkernel/intern/gpencil.c
M	source/blender/blenkernel/intern/material.c
M	source/blender/blenloader/intern/readfile.c
M	source/blender/draw/engines/gpencil/gpencil_draw_utils.c
M	source/blender/editors/gpencil/gpencil_add_monkey.c
M	source/blender/editors/gpencil/gpencil_add_stroke.c
M	source/blender/editors/gpencil/gpencil_brush.c
M	source/blender/editors/gpencil/gpencil_data.c
M	source/blender/editors/gpencil/gpencil_edit.c
M	source/blender/editors/gpencil/gpencil_fill.c
M	source/blender/editors/gpencil/gpencil_old.c
M	source/blender/editors/gpencil/gpencil_paint.c
M	source/blender/editors/gpencil/gpencil_primitive.c
M	source/blender/editors/gpencil/gpencil_utils.c
M	source/blender/editors/render/render_shading.c
M	source/blender/makesrna/intern/rna_brush.c
M	source/blender/makesrna/intern/rna_object.c

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

diff --git a/release/scripts/startup/bl_ui/properties_grease_pencil_common.py b/release/scripts/startup/bl_ui/properties_grease_pencil_common.py
index 5f2ac7e7123..bb059d6befc 100644
--- a/release/scripts/startup/bl_ui/properties_grease_pencil_common.py
+++ b/release/scripts/startup/bl_ui/properties_grease_pencil_common.py
@@ -832,6 +832,11 @@ class GreasePencilMaterialsPanel:
         layout = self.layout
         show_full_ui = (self.bl_space_type == 'PROPERTIES')
 
+        is_view3d = (self.bl_space_type == 'VIEW_3D')
+        tool_settings = context.scene.tool_settings
+        gpencil_paint = tool_settings.gpencil_paint
+        brush = gpencil_paint.brush
+
         ob = context.object
         row = layout.row()
 
@@ -841,6 +846,12 @@ class GreasePencilMaterialsPanel:
 
             row.template_list("GPENCIL_UL_matslots", "", ob, "material_slots", ob, "active_material_index", rows=rows)
 
+            # if topbar popover and brush pinned, disable
+            if is_view3d and brush is not None:
+                gp_settings = brush.gpencil_settings
+                if gp_settings.use_material_pin:
+                    row.enabled = False
+
             col = row.column(align=True)
             if show_full_ui:
                 col.operator("object.material_slot_add", icon='ADD', text="")
diff --git a/release/scripts/startup/bl_ui/space_topbar.py b/release/scripts/startup/bl_ui/space_topbar.py
index 0e687452e5c..7b9b324066a 100644
--- a/release/scripts/startup/bl_ui/space_topbar.py
+++ b/release/scripts/startup/bl_ui/space_topbar.py
@@ -316,7 +316,8 @@ class _draw_left_context_mode:
             def draw_color_selector():
                 ma = gp_settings.material
                 row = layout.row(align=True)
-
+                if not gp_settings.use_material_pin:
+                    ma = context.object.active_material
                 icon_id = 0
                 if ma:
                     icon_id = ma.id_data.preview.icon_id
diff --git a/release/scripts/startup/bl_ui/space_view3d_toolbar.py b/release/scripts/startup/bl_ui/space_view3d_toolbar.py
index 212427dead9..17dd35d9fc4 100644
--- a/release/scripts/startup/bl_ui/space_view3d_toolbar.py
+++ b/release/scripts/startup/bl_ui/space_view3d_toolbar.py
@@ -1679,7 +1679,15 @@ class VIEW3D_PT_tools_grease_pencil_brush(View3DPanel, Panel):
             gp_settings = brush.gpencil_settings
 
             if brush.gpencil_tool in {'DRAW', 'FILL'}:
-                layout.row(align=True).template_ID(gp_settings, "material")
+                row = layout.row(align=True)
+                row_mat = row.row()
+                if gp_settings.use_material_pin:
+                    row_mat.template_ID(gp_settings, "material", live_icon=True)
+                else:
+                    row_mat.template_ID(context.active_object, "active_material", live_icon=True)
+                    row_mat.enabled = False  # will otherwise allow to change material in active slot
+
+                row.prop(gp_settings, "use_material_pin", text="")
 
             if not self.is_popover:
                 from .properties_paint_common import (
diff --git a/source/blender/blenkernel/BKE_brush.h b/source/blender/blenkernel/BKE_brush.h
index 9360962317c..30746af0b53 100644
--- a/source/blender/blenkernel/BKE_brush.h
+++ b/source/blender/blenkernel/BKE_brush.h
@@ -56,7 +56,6 @@ void BKE_brush_free(struct Brush *brush);
 
 void BKE_brush_sculpt_reset(struct Brush *brush);
 void BKE_brush_gpencil_presets(struct bContext *C);
-void BKE_brush_update_material(struct Main *bmain, struct Material *ma, struct Brush *exclude_brush);
 
 /* image icon function */
 struct ImBuf *get_brush_icon(struct Brush *brush);
diff --git a/source/blender/blenkernel/BKE_gpencil.h b/source/blender/blenkernel/BKE_gpencil.h
index 353ba61b70d..e94ba27a8ea 100644
--- a/source/blender/blenkernel/BKE_gpencil.h
+++ b/source/blender/blenkernel/BKE_gpencil.h
@@ -132,6 +132,21 @@ void BKE_gpencil_layer_setactive(struct bGPdata *gpd, struct bGPDlayer *active);
 void BKE_gpencil_layer_delete(struct bGPdata *gpd, struct bGPDlayer *gpl);
 
 struct Material *BKE_gpencil_get_material_from_brush(struct Brush *brush);
+void BKE_gpencil_brush_set_material(struct Brush *brush, struct Material *material);
+
+struct Material *BKE_gpencil_handle_brush_material(struct Main *bmain, struct Object *ob, struct Brush *brush);
+int BKE_gpencil_handle_material(struct Main *bmain, struct Object *ob, struct Material *material);
+
+struct Material *BKE_gpencil_handle_new_material(struct Main *bmain, struct Object *ob, const char *name, int *r_index);
+
+struct Material *BKE_gpencil_get_material_for_brush(struct Object *ob, struct Brush *brush);
+int BKE_gpencil_get_material_index_for_brush(struct Object *ob, struct Brush *brush);
+
+struct Material *BKE_gpencil_current_input_toolsettings_material(struct Main *bmain, struct Object *ob, struct ToolSettings *ts);
+struct Material *BKE_gpencil_current_input_brush_material(struct Main *bmain, struct Object *ob, struct Brush *brush);
+struct Material *BKE_gpencil_current_input_material(struct Main *bmain, struct Object *ob);
+
+
 struct Material *BKE_gpencil_material_ensure(struct Main *bmain, struct Object *ob);
 
 /* object boundbox */
diff --git a/source/blender/blenkernel/intern/brush.c b/source/blender/blenkernel/intern/brush.c
index c959504f84f..ea5605d8ed1 100644
--- a/source/blender/blenkernel/intern/brush.c
+++ b/source/blender/blenkernel/intern/brush.c
@@ -540,24 +540,6 @@ void BKE_brush_gpencil_presets(bContext *C)
 
 }
 
-void BKE_brush_update_material(Main *bmain, Material *ma, Brush *exclude_brush)
-{
-	for (Brush *brush = bmain->brushes.first; brush; brush = brush->id.next) {
-		if ((exclude_brush != NULL) && (brush == exclude_brush)) {
-			continue;
-		}
-
-		if (brush->gpencil_settings != NULL) {
-			BrushGpencilSettings *gpencil_settings = brush->gpencil_settings;
-			if (((gpencil_settings->flag & GP_BRUSH_MATERIAL_PINNED) == 0) &&
-			    (gpencil_settings->material != ma))
-			{
-				gpencil_settings->material = ma;
-			}
-		}
-	}
-}
-
 struct Brush *BKE_brush_first_search(struct Main *bmain, const eObjectMode ob_mode)
 {
 	Brush *brush;
diff --git a/source/blender/blenkernel/intern/gpencil.c b/source/blender/blenkernel/intern/gpencil.c
index eb6fc9ceeeb..263bf4e57e2 100644
--- a/source/blender/blenkernel/intern/gpencil.c
+++ b/source/blender/blenkernel/intern/gpencil.c
@@ -996,6 +996,136 @@ Material *BKE_gpencil_get_material_from_brush(Brush *brush)
 	return ma;
 }
 
+void BKE_gpencil_brush_set_material(Brush *brush, Material *ma)
+{
+	BLI_assert(brush);
+	BLI_assert(brush->gpencil_settings);
+	if (brush->gpencil_settings->material != ma) {
+		if (brush->gpencil_settings->material) {
+			id_us_min(&brush->gpencil_settings->material->id);
+		}
+		if (ma) {
+			id_us_plus(&ma->id);
+		}
+		brush->gpencil_settings->material = ma;
+	}
+}
+
+/* Adds the pinned material to the object if necessary. */
+Material *BKE_gpencil_handle_brush_material(Main *bmain, Object *ob, Brush *brush)
+{
+	if (brush->gpencil_settings->flag & GP_BRUSH_MATERIAL_PINNED) {
+		Material *ma = BKE_gpencil_get_material_from_brush(brush);
+
+		/* check if the material is already on object material slots and add it if missing */
+		if (ma && BKE_gpencil_get_material_index(ob, ma) < 0) {
+			BKE_object_material_slot_add(bmain, ob);
+			assign_material(bmain, ob, ma, ob->totcol, BKE_MAT_ASSIGN_USERPREF);
+		}
+
+		return ma;
+	}
+	else {
+		/* using active material instead */
+		return give_current_material(ob, ob->actcol);
+	}
+}
+
+/* Assigns the material to object (if not already present) and returns its index (mat_nr). */
+int BKE_gpencil_handle_material(Main *bmain, Object *ob, Material *material)
+{
+	if (!material) {
+		return -1;
+	}
+	int index = BKE_gpencil_get_material_index(ob, material);
+	if (index < 0) {
+		BKE_object_material_slot_add(bmain, ob);
+		assign_material(bmain, ob, material, ob->totcol, BKE_MAT_ASSIGN_USERPREF);
+		return ob->totcol - 1;
+	}
+	return index;
+}
+
+/** Creates a new gpencil material and assigns it to object.
+ *
+ * \param *r_index: value is set to zero based index of the new material if r_index is not NULL
+ */
+Material *BKE_gpencil_handle_new_material(Main *bmain, Object *ob, const char *name, int *r_index)
+{
+	Material *ma = BKE_material_add_gpencil(bmain, name);
+	id_us_min(&ma->id); /* no users yet */
+
+	BKE_object_material_slot_add(bmain, ob);
+	assign_material(bmain, ob, ma, ob->totcol, BKE_MAT_ASSIGN_USERPREF);
+
+	if (r_index) {
+		*r_index = ob->actcol - 1;
+	}
+	return ma;
+}
+
+/* Returns the material for a brush with respect to its pinned state. */
+Material *BKE_gpencil_get_material_for_brush(Object *ob, Brush *brush)
+{
+	if (brush->gpencil_settings->flag & GP_BRUSH_MATERIAL_PINNED) {
+		Material *ma = BKE_gpencil_get_material_from_brush(brush);
+		return ma;
+	}
+	else {
+		return give_current_material(ob, ob->actcol);
+	}
+}
+
+/* Returns the material index for a brush with respect to its pinned state. */
+int BKE_gpencil_get_material_index_for_brush(Object *ob, Brush *brush)
+{
+	if (brush->gpencil_settings->flag & GP_BRUSH_MATERIAL_PINNED) {
+		return BKE_gpencil_get_material_index(ob, brush->gpencil_settings->material);
+	}
+	else {
+		return ob->actcol - 1;
+	}
+}
+
+/* Guaranteed to return a material assigned to object. Returns never NULL. */
+Material *BKE_gpencil_current_input_toolsettings_material(Main *bmain, Object *ob, ToolSettings *ts)
+{
+	if (ts && ts->gp_paint && ts->gp_paint->paint.brush) {
+		return BKE_gpencil_current_input_brush_material(bmain, ob, ts->gp_paint->paint.brush);
+	}
+	else {
+		return BKE_gpencil_current_input_brush_material(bmain, ob, NULL);
+	}
+}
+
+/* Guaranteed to return a material assigned to object. Returns never NULL. */
+Material *BKE_gpencil_current_input_brush_material(Main *bmain, Object *ob, Brush *brush)
+{
+	Material *ma;
+	if (brush) {
+		ma = BKE_gpencil_handle_brush_material(bmain, ob, brush);
+		if (!ma && brush->gpencil_settings->flag & GP_BRUSH_MATERIAL_PINNED) {
+			/* it is easier to just unpin a NULL material, instead of setting a new one */
+			brush->gpencil_settings->flag &= ~GP_BRUSH_MATERIAL_PINNED;
+		}
+	}
+	if (ma) {
+		return

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list