Cleanup/refactor: `BKE_libblock_alloc` -> `BKE_id_new` in ID creation code.

Better use higher-level code from common ID management when possible.
Helps to de-duplicate logic, and reduces outside usages of more
'dangerous' functions.

Note that we could get rid of many of those `BKE_<id_type>_add`
functions now, but on the other hand several of those take extra
parameters and perform additional actions, so think we can keep them all
for now as 'non-standard ID specific creation functions'.
This commit is contained in:
Bastien Montagne 2020-10-08 12:50:04 +02:00
parent df5fe9718b
commit 1cf5ce5e25
28 changed files with 30 additions and 63 deletions

View File

@ -303,7 +303,7 @@ bAction *BKE_action_add(Main *bmain, const char name[])
{
bAction *act;
act = BKE_libblock_alloc(bmain, ID_AC, name, 0);
act = BKE_id_new(bmain, ID_AC, name);
return act;
}

View File

@ -331,7 +331,7 @@ bArmature *BKE_armature_add(Main *bmain, const char *name)
{
bArmature *arm;
arm = BKE_libblock_alloc(bmain, ID_AR, name, 0);
arm = BKE_id_new(bmain, ID_AR, name);
arm->deformflag = ARM_DEF_VGROUP | ARM_DEF_ENVELOPE;
arm->flag = ARM_COL_CUSTOM; /* custom bone-group colors */
arm->layer = 1;

View File

@ -455,9 +455,7 @@ Brush *BKE_brush_add(Main *bmain, const char *name, const eObjectMode ob_mode)
{
Brush *brush;
brush = BKE_libblock_alloc(bmain, ID_BR, name, 0);
brush_init_data(&brush->id);
brush = BKE_id_new(bmain, ID_BR, name);
brush->ob_mode = ob_mode;

View File

@ -247,9 +247,7 @@ static void cachefile_handle_free(CacheFile *cache_file)
void *BKE_cachefile_add(Main *bmain, const char *name)
{
CacheFile *cache_file = BKE_libblock_alloc(bmain, ID_CF, name, 0);
cache_file_init_data(&cache_file->id);
CacheFile *cache_file = BKE_id_new(bmain, ID_CF, name);
return cache_file;
}

View File

@ -213,9 +213,7 @@ void *BKE_camera_add(Main *bmain, const char *name)
{
Camera *cam;
cam = BKE_libblock_alloc(bmain, ID_CA, name, 0);
camera_init_data(&cam->id);
cam = BKE_id_new(bmain, ID_CA, name);
return cam;
}

View File

@ -203,7 +203,7 @@ static Collection *collection_add(Main *bmain,
}
/* Create new collection. */
Collection *collection = BKE_libblock_alloc(bmain, ID_GR, name, 0);
Collection *collection = BKE_id_new(bmain, ID_GR, name);
collection->color_tag = COLLECTION_COLOR_NONE;
/* We increase collection user count when linking to Collections. */

View File

@ -418,6 +418,7 @@ Curve *BKE_curve_add(Main *bmain, const char *name, int type)
{
Curve *cu;
/* We cannot use #BKE_id_new here as we need some custom initialization code. */
cu = BKE_libblock_alloc(bmain, ID_CU, name, 0);
BKE_curve_init(cu, type);

View File

@ -244,9 +244,7 @@ static void hair_random(Hair *hair)
void *BKE_hair_add(Main *bmain, const char *name)
{
Hair *hair = BKE_libblock_alloc(bmain, ID_HA, name, 0);
hair_init_data(&hair->id);
Hair *hair = BKE_id_new(bmain, ID_HA, name);
return hair;
}

View File

@ -249,7 +249,7 @@ Key *BKE_key_add(Main *bmain, ID *id) /* common function */
Key *key;
char *el;
key = BKE_libblock_alloc(bmain, ID_KE, "Key", 0);
key = BKE_id_new(bmain, ID_KE, "Key");
key->type = KEY_NORMAL;
key->from = id;

View File

@ -394,9 +394,7 @@ Lattice *BKE_lattice_add(Main *bmain, const char *name)
{
Lattice *lt;
lt = BKE_libblock_alloc(bmain, ID_LT, name, 0);
lattice_init_data(&lt->id);
lt = BKE_id_new(bmain, ID_LT, name);
return lt;
}

View File

@ -213,9 +213,7 @@ Light *BKE_light_add(Main *bmain, const char *name)
{
Light *la;
la = BKE_libblock_alloc(bmain, ID_LA, name, 0);
light_init_data(&la->id);
la = BKE_id_new(bmain, ID_LA, name);
return la;
}

View File

@ -135,9 +135,7 @@ void *BKE_lightprobe_add(Main *bmain, const char *name)
{
LightProbe *probe;
probe = BKE_libblock_alloc(bmain, ID_LP, name, 0);
lightprobe_init_data(&probe->id);
probe = BKE_id_new(bmain, ID_LP, name);
return probe;
}

View File

@ -297,9 +297,7 @@ Material *BKE_material_add(Main *bmain, const char *name)
{
Material *ma;
ma = BKE_libblock_alloc(bmain, ID_MA, name, 0);
material_init_data(&ma->id);
ma = BKE_id_new(bmain, ID_MA, name);
return ma;
}

View File

@ -210,9 +210,7 @@ MetaBall *BKE_mball_add(Main *bmain, const char *name)
{
MetaBall *mb;
mb = BKE_libblock_alloc(bmain, ID_MB, name, 0);
metaball_init_data(&mb->id);
mb = BKE_id_new(bmain, ID_MB, name);
return mb;
}

View File

@ -860,9 +860,7 @@ Mesh *BKE_mesh_add(Main *bmain, const char *name)
{
Mesh *me;
me = BKE_libblock_alloc(bmain, ID_ME, name, 0);
mesh_init_data(&me->id);
me = BKE_id_new(bmain, ID_ME, name);
return me;
}

View File

@ -921,7 +921,7 @@ static MovieClip *movieclip_alloc(Main *bmain, const char *name)
{
MovieClip *clip;
clip = BKE_libblock_alloc(bmain, ID_MC, name, 0);
clip = BKE_id_new(bmain, ID_MC, name);
clip->aspx = clip->aspy = 1.0f;

View File

@ -1346,6 +1346,7 @@ Object *BKE_object_add_only_object(Main *bmain, int type, const char *name)
name = get_obdata_defname(type);
}
/* We cannot use #BKE_id_new here as we need some custom initialization code. */
ob = BKE_libblock_alloc(bmain, ID_OB, name, 0);
/* We increase object user count when linking to Collections. */

View File

@ -672,7 +672,7 @@ PaintCurve *BKE_paint_curve_add(Main *bmain, const char *name)
{
PaintCurve *pc;
pc = BKE_libblock_alloc(bmain, ID_PC, name, 0);
pc = BKE_id_new(bmain, ID_PC, name);
return pc;
}

View File

@ -3858,7 +3858,7 @@ ParticleSettings *BKE_particlesettings_add(Main *bmain, const char *name)
{
ParticleSettings *part;
part = BKE_libblock_alloc(bmain, ID_PA, name, 0);
part = BKE_id_new(bmain, ID_PA, name);
default_particle_settings(part);
@ -5009,7 +5009,8 @@ void psys_apply_hair_lattice(Depsgraph *depsgraph, Scene *scene, Object *ob, Par
}
/* Draw Engine */
void (*BKE_particle_batch_cache_dirty_tag_cb)(ParticleSystem *psys, eMeshBatchDirtyMode mode) = NULL;
void (*BKE_particle_batch_cache_dirty_tag_cb)(ParticleSystem *psys,
eMeshBatchDirtyMode mode) = NULL;
void (*BKE_particle_batch_cache_free_cb)(ParticleSystem *psys) = NULL;
void BKE_particle_batch_cache_dirty_tag(ParticleSystem *psys, eMeshBatchDirtyMode mode)

View File

@ -206,9 +206,7 @@ static void pointcloud_random(PointCloud *pointcloud)
void *BKE_pointcloud_add(Main *bmain, const char *name)
{
PointCloud *pointcloud = BKE_libblock_alloc(bmain, ID_PT, name, 0);
pointcloud_init_data(&pointcloud->id);
PointCloud *pointcloud = BKE_id_new(bmain, ID_PT, name);
return pointcloud;
}

View File

@ -955,12 +955,10 @@ Scene *BKE_scene_add(Main *bmain, const char *name)
{
Scene *sce;
sce = BKE_libblock_alloc(bmain, ID_SCE, name, 0);
sce = BKE_id_new(bmain, ID_SCE, name);
id_us_min(&sce->id);
id_us_ensure_real(&sce->id);
scene_init_data(&sce->id);
return sce;
}

View File

@ -261,9 +261,7 @@ IDTypeInfo IDType_ID_SIM = {
void *BKE_simulation_add(Main *bmain, const char *name)
{
Simulation *simulation = (Simulation *)BKE_libblock_alloc(bmain, ID_SIM, name, 0);
simulation_init_data(&simulation->id);
Simulation *simulation = (Simulation *)BKE_id_new(bmain, ID_SIM, name);
return simulation;
}

View File

@ -118,9 +118,7 @@ void *BKE_speaker_add(Main *bmain, const char *name)
{
Speaker *spk;
spk = BKE_libblock_alloc(bmain, ID_SPK, name, 0);
speaker_init_data(&spk->id);
spk = BKE_id_new(bmain, ID_SPK, name);
return spk;
}

View File

@ -287,12 +287,10 @@ Text *BKE_text_add(Main *bmain, const char *name)
{
Text *ta;
ta = BKE_libblock_alloc(bmain, ID_TXT, name, 0);
ta = BKE_id_new(bmain, ID_TXT, name);
/* Texts always have 'real' user (see also read code). */
id_us_ensure_real(&ta->id);
text_init_data(&ta->id);
return ta;
}

View File

@ -383,9 +383,7 @@ Tex *BKE_texture_add(Main *bmain, const char *name)
{
Tex *tex;
tex = BKE_libblock_alloc(bmain, ID_TE, name, 0);
texture_init_data(&tex->id);
tex = BKE_id_new(bmain, ID_TE, name);
return tex;
}

View File

@ -603,9 +603,7 @@ void BKE_volume_init_grids(Volume *volume)
void *BKE_volume_add(Main *bmain, const char *name)
{
Volume *volume = (Volume *)BKE_libblock_alloc(bmain, ID_VO, name, 0);
volume_init_data(&volume->id);
Volume *volume = (Volume *)BKE_id_new(bmain, ID_VO, name);
return volume;
}

View File

@ -205,7 +205,7 @@ static bool UNUSED_FUNCTION(workspaces_is_screen_used)
WorkSpace *BKE_workspace_add(Main *bmain, const char *name)
{
WorkSpace *new_workspace = BKE_libblock_alloc(bmain, ID_WS, name, 0);
WorkSpace *new_workspace = BKE_id_new(bmain, ID_WS, name);
id_us_ensure_real(&new_workspace->id);
return new_workspace;
}

View File

@ -210,9 +210,7 @@ World *BKE_world_add(Main *bmain, const char *name)
{
World *wrld;
wrld = BKE_libblock_alloc(bmain, ID_WO, name, 0);
world_init_data(&wrld->id);
wrld = BKE_id_new(bmain, ID_WO, name);
return wrld;
}