Cleanup: avoid strcpy for initializing ID names

While strcpy is safe in this case, it's use requires extra scrutiny
and can cause problems if the strings are later translated.

Also move the ID code assignment into material_init_data
as the ID-code is more of an internal detail.
This commit is contained in:
Campbell Barton 2023-05-19 14:58:38 +10:00
parent ec8a4cfcb7
commit 712d177aa3
2 changed files with 7 additions and 6 deletions

View File

@ -2221,8 +2221,7 @@ int BKE_gpencil_object_material_index_get_by_name(Object *ob, const char *name)
Material *read_ma = NULL;
for (short i = 0; i < *totcol; i++) {
read_ma = BKE_object_material_get(ob, i + 1);
/* Material names are like "MAMaterial.001" */
if (STREQ(name, &read_ma->id.name[2])) {
if (STREQ(name, read_ma->id.name + 2)) {
return i;
}
}

View File

@ -82,6 +82,8 @@ static void material_init_data(ID *id)
BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(material, id));
MEMCPY_STRUCT_AFTER(material, DNA_struct_default_get(Material), id);
*((short *)id->name) = ID_MA;
}
static void material_copy_data(Main *bmain, ID *id_dst, const ID *id_src, const int flag)
@ -1992,14 +1994,14 @@ static Material *default_materials[] = {&default_material_empty,
static void material_default_gpencil_init(Material *ma)
{
strcpy(ma->id.name, "MADefault GPencil");
BLI_strncpy(ma->id.name + 2, "Default GPencil", MAX_NAME);
BKE_gpencil_material_attr_init(ma);
add_v3_fl(&ma->gp_style->stroke_rgba[0], 0.6f);
}
static void material_default_surface_init(Material *ma)
{
strcpy(ma->id.name, "MADefault Surface");
BLI_strncpy(ma->id.name + 2, "Default Surface", MAX_NAME);
bNodeTree *ntree = blender::bke::ntreeAddTreeEmbedded(
nullptr, &ma->id, "Shader Nodetree", ntreeType_Shader->idname);
@ -2027,7 +2029,7 @@ static void material_default_surface_init(Material *ma)
static void material_default_volume_init(Material *ma)
{
strcpy(ma->id.name, "MADefault Volume");
BLI_strncpy(ma->id.name + 2, "Default Volume", MAX_NAME);
bNodeTree *ntree = blender::bke::ntreeAddTreeEmbedded(
nullptr, &ma->id, "Shader Nodetree", ntreeType_Shader->idname);
@ -2052,7 +2054,7 @@ static void material_default_volume_init(Material *ma)
static void material_default_holdout_init(Material *ma)
{
strcpy(ma->id.name, "MADefault Holdout");
BLI_strncpy(ma->id.name + 2, "Default Holdout", MAX_NAME);
bNodeTree *ntree = blender::bke::ntreeAddTreeEmbedded(
nullptr, &ma->id, "Shader Nodetree", ntreeType_Shader->idname);