paint: fix duplicate name

This commit is contained in:
luboslenco
2026-08-24 21:19:48 +02:00
parent 4aa3f7aa4c
commit cb80a1a898
3 changed files with 40 additions and 23 deletions
+32 -9
View File
@@ -173,6 +173,34 @@ char *_import_mesh_number_ext(i32 i) {
return string_tmp(".%s", i32_to_string(i));
}
static i32 _import_mesh_split_number_ext(char *name, char **base) {
*base = name;
i32 dot = string_last_index_of(name, ".");
i32 len = string_length(name);
if (dot <= 0 || len - dot - 1 < 3) {
return 0;
}
for (i32 i = dot + 1; i < len; ++i) {
i32 c = char_code_at(name, i);
if (c < '0' || c > '9') {
return 0;
}
}
*base = string_tmp("%.*s", dot, name);
return parse_int(name + dot + 1);
}
char *_import_mesh_unique_name(char *name) {
// Returns the name or the next free .00X variant
char *base;
i32 i = _import_mesh_split_number_ext(name, &base);
char *res = i == 0 ? base : string_tmp("%s%s", base, _import_mesh_number_ext(i));
while (!_import_mesh_is_unique_name(res)) {
res = string_tmp("%s%s", base, _import_mesh_number_ext(++i));
}
return res;
}
void import_mesh_make_mesh(raw_mesh_t *mesh) {
if (mesh == NULL || mesh->posa == NULL || mesh->nora == NULL || mesh->inda == NULL || mesh->posa->length == 0) {
console_error(strings_failed_to_read_mesh_data());
@@ -272,15 +300,10 @@ void import_mesh_add_mesh(raw_mesh_t *mesh) {
object->skip_context = "paint";
// Ensure unique names
char *oname = object->base->name;
char *ext = "";
i32 i = 0;
while (!_import_mesh_is_unique_name(string_tmp("%s%s", oname, ext))) {
ext = _import_mesh_number_ext(++i);
}
if (!string_equals(ext, "")) {
object->base->name = string("%s%s", oname, ext);
raw->name = string("%s%s", oname, ext);
char *uname = _import_mesh_unique_name(object->base->name);
if (!string_equals(uname, object->base->name)) {
object->base->name = string_copy(uname);
raw->name = string_copy(uname);
}
any_array_push(g_project->_->paint_objects, object);
+7 -8
View File
@@ -66,13 +66,7 @@ mesh_object_t *sim_duplicate_object(mesh_object_t *so) {
sim_shift_object_masks(at + 1);
// Ensure unique name
char *oname = so->base->name;
char *ext = "";
i32 i = 0;
while (!_import_mesh_is_unique_name(string("%s%s", oname, ext))) {
ext = string_copy(_import_mesh_number_ext(++i));
}
dup->base->name = string("%s%s", oname, ext);
dup->base->name = string_copy(_import_mesh_unique_name(so->base->name));
tab_stages_add_object(dup->base->name);
// Material override
@@ -95,7 +89,12 @@ mesh_object_t *sim_duplicate_object(mesh_object_t *so) {
}
void sim_duplicate() {
sim_duplicate_object(g_context->paint_object);
mesh_object_t *dup = sim_duplicate_object(g_context->paint_object);
if (dup != NULL) {
g_context->paint_object = dup;
ui_header_handle->redraws = 2;
ui_base_hwnds->buffer[TAB_AREA_SIDEBAR0]->redraws = 2;
}
util_mesh_merge(NULL);
g_context->ddirty = 2;
}
+1 -6
View File
@@ -802,12 +802,7 @@ mesh_object_t *tab_meshes_append_shape(char *mesh_name) {
g_project->mesh_parents = i32_array_create(0);
// Ensure unique name
char *ext = "";
i32 n = 0;
while (!_import_mesh_is_unique_name(string("%s%s", md->name, ext))) {
ext = string_copy(_import_mesh_number_ext(++n));
}
mo->base->name = string("%s%s", md->name, ext);
mo->base->name = string_copy(_import_mesh_unique_name(md->name));
obj_t *o = ALLOC_INIT(obj_t, {0});
o->_ = ALLOC_INIT(obj_runtime_t, {._gc = scene_raw});