From 1097a3f70d92f888f62a02bb7fb53a25b1059c7b Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Thu, 17 Jul 2014 17:12:12 +0200 Subject: Add helper to validate (and fix) material indices of meshes' polygons, curves' splines and texts' letters. Useful especially for importer addons. Reviewers: campbellbarton Reviewed By: campbellbarton Differential Revision: https://developer.blender.org/D650 --- source/blender/blenkernel/BKE_curve.h | 1 + source/blender/blenkernel/BKE_mesh.h | 1 + source/blender/blenkernel/intern/curve.c | 40 ++++++++++++++++++++++++ source/blender/blenkernel/intern/mesh_validate.c | 32 +++++++++++++++++-- source/blender/makesrna/intern/rna_curve_api.c | 6 ++++ source/blender/makesrna/intern/rna_mesh_api.c | 8 ++++- 6 files changed, 84 insertions(+), 4 deletions(-) (limited to 'source') diff --git a/source/blender/blenkernel/BKE_curve.h b/source/blender/blenkernel/BKE_curve.h index 0f9f8cb2f08..c6aaeb8cc64 100644 --- a/source/blender/blenkernel/BKE_curve.h +++ b/source/blender/blenkernel/BKE_curve.h @@ -87,6 +87,7 @@ bool BKE_curve_center_bounds(struct Curve *cu, float cent[3]); void BKE_curve_translate(struct Curve *cu, float offset[3], const bool do_keys); void BKE_curve_material_index_remove(struct Curve *cu, int index); void BKE_curve_material_index_clear(struct Curve *cu); +int BKE_curve_material_index_validate(struct Curve *cu); ListBase *BKE_curve_nurbs_get(struct Curve *cu); diff --git a/source/blender/blenkernel/BKE_mesh.h b/source/blender/blenkernel/BKE_mesh.h index 975a8fd63df..d0c268df93c 100644 --- a/source/blender/blenkernel/BKE_mesh.h +++ b/source/blender/blenkernel/BKE_mesh.h @@ -287,6 +287,7 @@ void BKE_mesh_calc_relative_deform( int BKE_mesh_validate(struct Mesh *me, const int do_verbose); void BKE_mesh_cd_validate(struct Mesh *me); +int BKE_mesh_validate_material_indices(struct Mesh *me); bool BKE_mesh_validate_arrays( struct Mesh *me, diff --git a/source/blender/blenkernel/intern/curve.c b/source/blender/blenkernel/intern/curve.c index 0abe956b599..eaeffdf6fef 100644 --- a/source/blender/blenkernel/intern/curve.c +++ b/source/blender/blenkernel/intern/curve.c @@ -52,6 +52,7 @@ #include "BKE_animsys.h" #include "BKE_curve.h" +#include "BKE_depsgraph.h" #include "BKE_displist.h" #include "BKE_font.h" #include "BKE_global.h" @@ -4230,6 +4231,45 @@ void BKE_curve_material_index_clear(Curve *cu) } } +int BKE_curve_material_index_validate(Curve *cu) +{ + const int curvetype = BKE_curve_type_get(cu); + bool is_valid = true; + + if (curvetype == OB_FONT) { + CharInfo *info = cu->strinfo; + const int max_idx = max_ii(0, cu->totcol); /* OB_FONT use 1 as first mat index, not 0!!! */ + int i; + for (i = cu->len_wchar - 1; i >= 0; i--, info++) { + if (info->mat_nr > max_idx) { + info->mat_nr = 0; + is_valid = false; + } + } + } + else { + Nurb *nu; + const int max_idx = max_ii(0, cu->totcol - 1); + for (nu = cu->nurb.first; nu; nu = nu->next) { + if (nu->mat_nr > max_idx) { + nu->mat_nr = 0; + if (curvetype == OB_CURVE) { + nu->charidx = 0; + } + is_valid = false; + } + } + } + + if (!is_valid) { + DAG_id_tag_update(&cu->id, OB_RECALC_DATA); + return true; + } + else { + return false; + } +} + void BKE_curve_rect_from_textbox(const struct Curve *cu, const struct TextBox *tb, struct rctf *r_rect) { r_rect->xmin = cu->xof + tb->x; diff --git a/source/blender/blenkernel/intern/mesh_validate.c b/source/blender/blenkernel/intern/mesh_validate.c index 858fe83b43f..e14bb9377da 100644 --- a/source/blender/blenkernel/intern/mesh_validate.c +++ b/source/blender/blenkernel/intern/mesh_validate.c @@ -909,8 +909,6 @@ static bool mesh_validate_customdata(CustomData *data, CustomDataMask mask, return is_valid; } -#undef PRINT - /** * \returns is_valid. */ @@ -1052,8 +1050,36 @@ void BKE_mesh_cd_validate(Mesh *me) } } } -/** \} */ +/** + * Check all material indices of polygons are valid, invalid ones are set to 0. + * \returns is_valid. + */ +int BKE_mesh_validate_material_indices(Mesh *me) +{ + MPoly *mp; + const int max_idx = max_ii(0, me->totcol - 1); + const int totpoly = me->totpoly; + int i; + bool is_valid = true; + + for (mp = me->mpoly, i = 0; i < totpoly; i++, mp++) { + if (mp->mat_nr > max_idx) { + mp->mat_nr = 0; + is_valid = false; + } + } + + if (!is_valid) { + DAG_id_tag_update(&me->id, OB_RECALC_DATA); + return true; + } + else { + return false; + } +} + +/** \} */ /* -------------------------------------------------------------------- */ diff --git a/source/blender/makesrna/intern/rna_curve_api.c b/source/blender/makesrna/intern/rna_curve_api.c index b689242f68f..560cb633a19 100644 --- a/source/blender/makesrna/intern/rna_curve_api.c +++ b/source/blender/makesrna/intern/rna_curve_api.c @@ -58,6 +58,12 @@ void RNA_api_curve(StructRNA *srna) RNA_def_function_ui_description(func, "Transform curve by a matrix"); parm = RNA_def_float_matrix(func, "matrix", 4, 4, NULL, 0.0f, 0.0f, "", "Matrix", 0.0f, 0.0f); RNA_def_property_flag(parm, PROP_REQUIRED); + + func = RNA_def_function(srna, "validate_material_indices", "BKE_curve_material_index_validate"); + RNA_def_function_ui_description(func, "Validate material indices of splines or letters, return True when the curve " + "has had invalid indices corrected (to default 0)"); + parm = RNA_def_boolean(func, "result", 0, "Result", ""); + RNA_def_function_return(func, parm); } #endif diff --git a/source/blender/makesrna/intern/rna_mesh_api.c b/source/blender/makesrna/intern/rna_mesh_api.c index 212859cfea4..3b063515625 100644 --- a/source/blender/makesrna/intern/rna_mesh_api.c +++ b/source/blender/makesrna/intern/rna_mesh_api.c @@ -206,11 +206,17 @@ void RNA_api_mesh(StructRNA *srna) RNA_def_function_return(func, parm); func = RNA_def_function(srna, "validate", "BKE_mesh_validate"); - RNA_def_function_ui_description(func, "validate geometry, return True when the mesh has had " + RNA_def_function_ui_description(func, "Validate geometry, return True when the mesh has had " "invalid geometry corrected/removed"); RNA_def_boolean(func, "verbose", 0, "Verbose", "Output information about the errors found"); parm = RNA_def_boolean(func, "result", 0, "Result", ""); RNA_def_function_return(func, parm); + + func = RNA_def_function(srna, "validate_material_indices", "BKE_mesh_validate_material_indices"); + RNA_def_function_ui_description(func, "Validate material indices of polygons, return True when the mesh has had " + "invalid indices corrected (to default 0)"); + parm = RNA_def_boolean(func, "result", 0, "Result", ""); + RNA_def_function_return(func, parm); } #endif -- cgit v1.2.3