Commit: 4880e2e75a860f4716e3122f5ae14f34f50a9452
Author: Bastien Montagne
Date:   Fri May 4 12:53:20 2018 +0200
Branches: blender2.8
https://developer.blender.org/rB4880e2e75a860f4716e3122f5ae14f34f50a9452

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.

===================================================================

M       source/blender/blenkernel/BKE_library.h
M       source/blender/blenkernel/intern/library.c

===================================================================

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)

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to