From e0a07700bfbe3a4d64e65d0d51adb8c44ef105d7 Mon Sep 17 00:00:00 2001 From: Philipp Oeser Date: Wed, 31 Mar 2021 15:54:41 +0200 Subject: GPencil: Prevent RNA assignment of invalid materials in modifiers Materials used in grease pencil modifiers have the requirement that they are already used on the object. In the UI dropdown, this restriction is ensured by calling uiItemPointerR with appropriate searchptr and searchpropname, so only giving the user the choice of materials already used on the object. From python though, it was still possible to assign materials outside of this this restriction. This led to reports like T86981 [which have been partially solved by clamping the material index in the modifier code to be in the valid range]. Now make sure we dont assign "invalid" materials through RNA by appropriate RNA pointer functions. This also adds a proper warning (red, alert) in case of the LineArt modifier if such a invalid material is still in the file [same as other modifiers already do]. Differential Revision: https://developer.blender.org/D10873 --- .../gpencil_modifiers/intern/MOD_gpencillineart.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'source/blender/gpencil_modifiers') diff --git a/source/blender/gpencil_modifiers/intern/MOD_gpencillineart.c b/source/blender/gpencil_modifiers/intern/MOD_gpencillineart.c index eca82f4cb90..d6e151fd262 100644 --- a/source/blender/gpencil_modifiers/intern/MOD_gpencillineart.c +++ b/source/blender/gpencil_modifiers/intern/MOD_gpencillineart.c @@ -33,6 +33,7 @@ #include "DNA_defaults.h" #include "DNA_gpencil_modifier_types.h" #include "DNA_gpencil_types.h" +#include "DNA_material_types.h" #include "DNA_object_types.h" #include "DNA_scene_types.h" #include "DNA_screen_types.h" @@ -275,8 +276,25 @@ static void panel_draw(const bContext *UNUSED(C), Panel *panel) uiItemR(sub, ptr, "crease_threshold", UI_ITEM_R_SLIDER, " ", ICON_NONE); uiItemPointerR(layout, ptr, "target_layer", &obj_data_ptr, "layers", NULL, ICON_GREASEPENCIL); - uiItemPointerR( - layout, ptr, "target_material", &obj_data_ptr, "materials", NULL, ICON_SHADING_TEXTURE); + + /* Material has to be used by grease pencil object already, it was possible to assign materials + * without this requirement in earlier versions of blender. */ + bool material_valid = false; + PointerRNA material_ptr = RNA_pointer_get(ptr, "target_material"); + if (!RNA_pointer_is_null(&material_ptr)) { + Material *current_material = material_ptr.data; + Object *ob = ob_ptr.data; + material_valid = BKE_gpencil_object_material_index_get(ob, current_material) != -1; + } + uiLayout *row = uiLayoutRow(layout, true); + uiLayoutSetRedAlert(row, !material_valid); + uiItemPointerR(row, + ptr, + "target_material", + &obj_data_ptr, + "materials", + NULL, + material_valid ? ICON_SHADING_TEXTURE : ICON_ERROR); uiItemR(layout, ptr, "use_remove_doubles", 0, NULL, ICON_NONE); uiItemR(layout, ptr, "use_edge_overlap", 0, IFACE_("Overlapping Edges As Contour"), ICON_NONE); -- cgit v1.2.3