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
This commit is contained in:
Bastien Montagne 2014-07-17 17:12:12 +02:00
parent f46223f29e
commit 1097a3f70d
6 changed files with 84 additions and 4 deletions

View File

@ -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);

View File

@ -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,

View File

@ -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;

View File

@ -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;
}
}
/** \} */
/* -------------------------------------------------------------------- */

View File

@ -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

View File

@ -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