diff options
author | Bastien Montagne <montagne29@wanadoo.fr> | 2018-05-04 13:53:20 +0300 |
---|---|---|
committer | Bastien Montagne <montagne29@wanadoo.fr> | 2018-05-04 13:55:36 +0300 |
commit | 4880e2e75a860f4716e3122f5ae14f34f50a9452 (patch) | |
tree | 6ba2c82e0fb1684e0926ce56bd67a61fdbdac1d7 /source/blender | |
parent | 3248eef697dedc777a2748f8f63e9031dfe5b599 (diff) |
ID management: add higher level BKE_id_new... helpers.
Those should be used in priority when you need to create either a new
datablock in Main, or a new temp one. Lower level BKE_libblock_...
should only be used when you need a very specific, uncommon behavior.
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/blenkernel/BKE_library.h | 6 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/library.c | 37 |
2 files changed, 41 insertions, 2 deletions
diff --git a/source/blender/blenkernel/BKE_library.h b/source/blender/blenkernel/BKE_library.h index c34571fdd7a..0cd5e032f68 100644 --- a/source/blender/blenkernel/BKE_library.h +++ b/source/blender/blenkernel/BKE_library.h @@ -52,9 +52,13 @@ struct PropertyRNA; size_t BKE_libblock_get_alloc_info(short type, const char **name); void *BKE_libblock_alloc_notest(short type) ATTR_WARN_UNUSED_RESULT; -void *BKE_libblock_alloc(struct Main *bmain, short type, const char *name, const int flag) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1); +void *BKE_libblock_alloc(struct Main *bmain, short type, const char *name, const int flag) ATTR_WARN_UNUSED_RESULT; void BKE_libblock_init_empty(struct ID *id) ATTR_NONNULL(1); +void *BKE_id_new(struct Main *bmain, const short type, const char *name); +void *BKE_id_new_nomain(const short type, const char *name); + + /** * New ID creation/copying options. */ diff --git a/source/blender/blenkernel/intern/library.c b/source/blender/blenkernel/intern/library.c index d23d48752b6..2e56ed9d247 100644 --- a/source/blender/blenkernel/intern/library.c +++ b/source/blender/blenkernel/intern/library.c @@ -80,8 +80,8 @@ #include "BLI_memarena.h" #include "BLI_mempool.h" #include "BLI_string_utils.h" - #include "BLI_threads.h" + #include "BLT_translation.h" #include "BKE_action.h" @@ -1368,6 +1368,41 @@ void BKE_libblock_init_empty(ID *id) } } +/** Generic helper to create a new empty datablock of given type in given \a bmain database. + * + * \param name can be NULL, in which case we get default name for this ID type. */ +void *BKE_id_new(Main *bmain, const short type, const char *name) +{ + BLI_assert(bmain != NULL); + + if (name == NULL) { + name = DATA_(BKE_idcode_to_name(type)); + } + + ID *id = BKE_libblock_alloc(bmain, type, name, 0); + BKE_libblock_init_empty(id); + + return id; +} + +/** Generic helper to create a new temporary empty datablock of given type, *outside* of any Main database. + * + * \param name can be NULL, in which case we get default name for this ID type. */ +void *BKE_id_new_nomain(const short type, const char *name) +{ + if (name == NULL) { + name = DATA_(BKE_idcode_to_name(type)); + } + + ID *id = BKE_libblock_alloc(NULL, type, name, + LIB_ID_CREATE_NO_MAIN | + LIB_ID_CREATE_NO_USER_REFCOUNT | + LIB_ID_CREATE_NO_DEG_TAG); + BKE_libblock_init_empty(id); + + return id; +} + /* by spec, animdata is first item after ID */ /* and, trust that BKE_animdata_from_id() will only find AnimData for valid ID-types */ static void id_copy_animdata(Main *bmain, ID *id, const bool do_action) |