UV/VCol layers creation: add option to not init those new data.

`ED_mesh_uv_texture_add()`/`ED_mesh_color_add()` would always either
copy data from current active one, or (for UVs), generate default
'valid' UVs for every face.

This commit adds an option to not do that, just keeping default values
from raw CDLayer creation. It is only used/exposed from RNA API
currently.

This is especially useful for importer add-ons, since some formats
support multiple layers of those kind, as well as 'partial' dataset not
explicitely defining values for all mesh items.

Preliminary step to fix T62224.
This commit is contained in:
Bastien Montagne 2019-03-05 21:34:48 +01:00
parent 4326f8af08
commit dbdd79fabe
Notes: blender-bot 2023-02-14 19:18:52 +01:00
Referenced by issue blender/blender-addons#62224, FBX importer dosen't import uv-maps correctly when there is 2 or more maps (possible fix suggestion here too)
5 changed files with 24 additions and 19 deletions

View File

@ -316,14 +316,14 @@ void ED_mesh_calc_tessface(struct Mesh *mesh, bool free_mpoly);
void ED_mesh_update(struct Mesh *mesh, struct bContext *C, bool calc_edges, bool calc_edges_loose, bool calc_tessface);
void ED_mesh_uv_texture_ensure(struct Mesh *me, const char *name);
int ED_mesh_uv_texture_add(struct Mesh *me, const char *name, const bool active_set);
int ED_mesh_uv_texture_add(struct Mesh *me, const char *name, const bool active_set, const bool do_init);
bool ED_mesh_uv_texture_remove_index(struct Mesh *me, const int n);
bool ED_mesh_uv_texture_remove_active(struct Mesh *me);
bool ED_mesh_uv_texture_remove_named(struct Mesh *me, const char *name);
void ED_mesh_uv_loop_reset(struct bContext *C, struct Mesh *me);
void ED_mesh_uv_loop_reset_ex(struct Mesh *me, const int layernum);
bool ED_mesh_color_ensure(struct Mesh *me, const char *name);
int ED_mesh_color_add(struct Mesh *me, const char *name, const bool active_set);
int ED_mesh_color_add(struct Mesh *me, const char *name, const bool active_set, const bool do_init);
bool ED_mesh_color_remove_index(struct Mesh *me, const int n);
bool ED_mesh_color_remove_active(struct Mesh *me);
bool ED_mesh_color_remove_named(struct Mesh *me, const char *name);

View File

@ -250,7 +250,7 @@ void ED_mesh_uv_loop_reset(struct bContext *C, struct Mesh *me)
}
/* note: keep in sync with ED_mesh_color_add */
int ED_mesh_uv_texture_add(Mesh *me, const char *name, const bool active_set)
int ED_mesh_uv_texture_add(Mesh *me, const char *name, const bool active_set, const bool do_init)
{
BMEditMesh *em;
int layernum_dst;
@ -267,7 +267,7 @@ int ED_mesh_uv_texture_add(Mesh *me, const char *name, const bool active_set)
/* CD_MLOOPUV */
BM_data_layer_add_named(em->bm, &em->bm->ldata, CD_MLOOPUV, name);
/* copy data from active UV */
if (layernum_dst) {
if (layernum_dst && do_init) {
const int layernum_src = CustomData_get_active_layer(&em->bm->ldata, CD_MLOOPUV);
BM_data_layer_copy(em->bm, &em->bm->ldata, CD_MLOOPUV, layernum_src, layernum_dst);
@ -282,7 +282,7 @@ int ED_mesh_uv_texture_add(Mesh *me, const char *name, const bool active_set)
if (layernum_dst >= MAX_MTFACE)
return -1;
if (me->mloopuv) {
if (me->mloopuv && do_init) {
CustomData_add_layer_named(&me->ldata, CD_MLOOPUV, CD_DUPLICATE, me->mloopuv, me->totloop, name);
CustomData_add_layer_named(&me->fdata, CD_MTFACE, CD_DUPLICATE, me->mtface, me->totface, name);
is_init = true;
@ -301,7 +301,7 @@ int ED_mesh_uv_texture_add(Mesh *me, const char *name, const bool active_set)
}
/* don't overwrite our copied coords */
if (is_init == false) {
if (!is_init && do_init) {
ED_mesh_uv_loop_reset_ex(me, layernum_dst);
}
@ -321,12 +321,12 @@ void ED_mesh_uv_texture_ensure(struct Mesh *me, const char *name)
layernum_dst = CustomData_number_of_layers(&em->bm->ldata, CD_MLOOPUV);
if (layernum_dst == 0)
ED_mesh_uv_texture_add(me, name, true);
ED_mesh_uv_texture_add(me, name, true, true);
}
else {
layernum_dst = CustomData_number_of_layers(&me->ldata, CD_MLOOPUV);
if (layernum_dst == 0)
ED_mesh_uv_texture_add(me, name, true);
ED_mesh_uv_texture_add(me, name, true, true);
}
}
@ -377,7 +377,7 @@ bool ED_mesh_uv_texture_remove_named(Mesh *me, const char *name)
}
/* note: keep in sync with ED_mesh_uv_texture_add */
int ED_mesh_color_add(Mesh *me, const char *name, const bool active_set)
int ED_mesh_color_add(Mesh *me, const char *name, const bool active_set, const bool do_init)
{
BMEditMesh *em;
int layernum;
@ -393,7 +393,7 @@ int ED_mesh_color_add(Mesh *me, const char *name, const bool active_set)
/* CD_MLOOPCOL */
BM_data_layer_add_named(em->bm, &em->bm->ldata, CD_MLOOPCOL, name);
/* copy data from active vertex color layer */
if (layernum) {
if (layernum && do_init) {
const int layernum_dst = CustomData_get_active_layer(&em->bm->ldata, CD_MLOOPCOL);
BM_data_layer_copy(em->bm, &em->bm->ldata, CD_MLOOPCOL, layernum_dst, layernum);
}
@ -407,7 +407,7 @@ int ED_mesh_color_add(Mesh *me, const char *name, const bool active_set)
return -1;
}
if (me->mloopcol) {
if (me->mloopcol && do_init) {
CustomData_add_layer_named(&me->ldata, CD_MLOOPCOL, CD_DUPLICATE, me->mloopcol, me->totloop, name);
CustomData_add_layer_named(&me->fdata, CD_MCOL, CD_DUPLICATE, me->mcol, me->totface, name);
}
@ -499,7 +499,7 @@ static int mesh_uv_texture_add_exec(bContext *C, wmOperator *UNUSED(op))
Object *ob = ED_object_context(C);
Mesh *me = ob->data;
if (ED_mesh_uv_texture_add(me, NULL, true) == -1)
if (ED_mesh_uv_texture_add(me, NULL, true, true) == -1)
return OPERATOR_CANCELLED;
if (ob->mode & OB_MODE_TEXTURE_PAINT) {
@ -565,7 +565,7 @@ static int mesh_vertex_color_add_exec(bContext *C, wmOperator *UNUSED(op))
Object *ob = ED_object_context(C);
Mesh *me = ob->data;
if (ED_mesh_color_add(me, NULL, true) == -1)
if (ED_mesh_color_add(me, NULL, true, true) == -1)
return OPERATOR_CANCELLED;
return OPERATOR_FINISHED;

View File

@ -230,7 +230,7 @@ static int output_toggle_exec(bContext *C, wmOperator *op)
/* Vertex Color Layer */
if (surface->type == MOD_DPAINT_SURFACE_T_PAINT) {
if (!exists)
ED_mesh_color_add(ob->data, name, true);
ED_mesh_color_add(ob->data, name, true, true);
else
ED_mesh_color_remove_named(ob->data, name);
}

View File

@ -111,7 +111,7 @@ static bool ED_uvedit_ensure_uvs(bContext *C, Scene *UNUSED(scene), Object *obed
return 1;
if (em && em->bm->totface && !CustomData_has_layer(&em->bm->ldata, CD_MLOOPUV))
ED_mesh_uv_texture_add(obedit->data, NULL, true);
ED_mesh_uv_texture_add(obedit->data, NULL, true, true);
if (!ED_uvedit_test(obedit))
return 0;

View File

@ -1368,12 +1368,12 @@ static int rna_Mesh_tot_face_get(PointerRNA *ptr)
return me->edit_mesh ? me->edit_mesh->bm->totfacesel : 0;
}
static PointerRNA rna_Mesh_vertex_color_new(struct Mesh *me, const char *name)
static PointerRNA rna_Mesh_vertex_color_new(struct Mesh *me, const char *name, const bool do_init)
{
PointerRNA ptr;
CustomData *ldata;
CustomDataLayer *cdl = NULL;
int index = ED_mesh_color_add(me, name, false);
int index = ED_mesh_color_add(me, name, false, do_init);
if (index != -1) {
ldata = rna_mesh_ldata_helper(me);
@ -1415,12 +1415,12 @@ DEFINE_CUSTOMDATA_PROPERTY_API(polygon, int, CD_PROP_INT, pdata, totpoly, MeshPo
DEFINE_CUSTOMDATA_PROPERTY_API(polygon, string, CD_PROP_STR, pdata, totpoly, MeshPolygonStringPropertyLayer)
#undef DEFINE_CUSTOMDATA_PROPERTY_API
static PointerRNA rna_Mesh_uv_layers_new(struct Mesh *me, const char *name)
static PointerRNA rna_Mesh_uv_layers_new(struct Mesh *me, const char *name, const bool do_init)
{
PointerRNA ptr;
CustomData *ldata;
CustomDataLayer *cdl = NULL;
int index = ED_mesh_uv_texture_add(me, name, false);
int index = ED_mesh_uv_texture_add(me, name, false, do_init);
if (index != -1) {
ldata = rna_mesh_ldata_helper(me);
@ -2213,6 +2213,8 @@ static void rna_def_loop_colors(BlenderRNA *brna, PropertyRNA *cprop)
func = RNA_def_function(srna, "new", "rna_Mesh_vertex_color_new");
RNA_def_function_ui_description(func, "Add a vertex color layer to Mesh");
RNA_def_string(func, "name", "Col", 0, "", "Vertex color name");
RNA_def_boolean(func, "do_init", true, "",
"Whether new layer's data should be initialized by copying current active one");
parm = RNA_def_pointer(func, "layer", "MeshLoopColorLayer", "", "The newly created layer");
RNA_def_parameter_flags(parm, 0, PARM_RNAPTR);
RNA_def_function_return(func, parm);
@ -2255,6 +2257,9 @@ static void rna_def_uv_layers(BlenderRNA *brna, PropertyRNA *cprop)
func = RNA_def_function(srna, "new", "rna_Mesh_uv_layers_new");
RNA_def_function_ui_description(func, "Add a UV map layer to Mesh");
RNA_def_string(func, "name", "UVMap", 0, "", "UV map name");
RNA_def_boolean(func, "do_init", true, "",
"Whether new layer's data should be initialized by copying current active one, "
"or if none is active, with a default UVmap");
parm = RNA_def_pointer(func, "layer", "MeshUVLoopLayer", "", "The newly created layer");
RNA_def_parameter_flags(parm, 0, PARM_RNAPTR);
RNA_def_function_return(func, parm);