[Bf-blender-cvs] [fe274d91a12] master: Link/Append: Move most of core link/append code from WM to new `BKE_blendflie_link_append` module.

Bastien Montagne noreply at git.blender.org
Mon Nov 22 16:55:20 CET 2021


Commit: fe274d91a1295ea16cd71b25691c0a492e95dbb8
Author: Bastien Montagne
Date:   Tue Nov 2 14:47:21 2021 +0100
Branches: master
https://developer.blender.org/rBfe274d91a1295ea16cd71b25691c0a492e95dbb8

Link/Append: Move most of core link/append code from WM to new `BKE_blendflie_link_append` module.

This will allow to expose all those advanced features of the WM
operators to other parts of the code, like the python library context
manager, copy/paste code, etc.

This is expected to be a strictly no-behavioral-change commit.

Part of T91414: Unify link/append between WM operators and BPY context
manager API, and cleanup usages of `BKE_library_make_local`.

Maniphest Tasks: T91414

Differential Revision: https://developer.blender.org/D13222

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

A	source/blender/blenkernel/BKE_blendfile_link_append.h
M	source/blender/blenkernel/CMakeLists.txt
A	source/blender/blenkernel/intern/blendfile_link_append.c
M	source/blender/windowmanager/intern/wm_files_link.c

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

diff --git a/source/blender/blenkernel/BKE_blendfile_link_append.h b/source/blender/blenkernel/BKE_blendfile_link_append.h
new file mode 100644
index 00000000000..184f1ef1511
--- /dev/null
+++ b/source/blender/blenkernel/BKE_blendfile_link_append.h
@@ -0,0 +1,91 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+#pragma once
+
+/** \file
+ * \ingroup bke
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct ID;
+struct Library;
+struct Main;
+struct ReportList;
+struct Scene;
+struct ViewLayer;
+struct View3D;
+
+typedef struct BlendfileLinkAppendContext BlendfileLinkAppendContext;
+typedef struct BlendfileLinkAppendContextItem BlendfileLinkAppendContextItem;
+
+BlendfileLinkAppendContext *BKE_blendfile_link_append_context_new(const int flag);
+void BKE_blendfile_link_append_context_free(struct BlendfileLinkAppendContext *lapp_context);
+void BKE_blendfile_link_append_context_flag_set(struct BlendfileLinkAppendContext *lapp_context,
+                                                const int flag,
+                                                const bool do_set);
+
+void BKE_blendfile_link_append_context_embedded_blendfile_set(
+    struct BlendfileLinkAppendContext *lapp_context,
+    const void *blendfile_mem,
+    int blendfile_memsize);
+void BKE_blendfile_link_append_context_embedded_blendfile_clear(
+    struct BlendfileLinkAppendContext *lapp_context);
+
+void BKE_blendfile_link_append_context_library_add(struct BlendfileLinkAppendContext *lapp_context,
+                                                   const char *libname);
+struct BlendfileLinkAppendContextItem *BKE_blendfile_link_append_context_item_add(
+    struct BlendfileLinkAppendContext *lapp_context,
+    const char *idname,
+    const short idcode,
+    void *userdata);
+void BKE_blendfile_link_append_context_item_library_index_enable(
+    struct BlendfileLinkAppendContext *lapp_context,
+    struct BlendfileLinkAppendContextItem *item,
+    const int library_index);
+bool BKE_blendfile_link_append_context_is_empty(struct BlendfileLinkAppendContext *lapp_context);
+
+void *BKE_blendfile_link_append_context_item_userdata_get(
+    struct BlendfileLinkAppendContext *lapp_context, struct BlendfileLinkAppendContextItem *item);
+struct ID *BKE_blendfile_link_append_context_item_newid_get(
+    struct BlendfileLinkAppendContext *lapp_context, struct BlendfileLinkAppendContextItem *item);
+
+void BKE_blendfile_append(struct BlendfileLinkAppendContext *lapp_context,
+                          struct ReportList *reports,
+                          struct Main *bmain,
+                          struct Scene *scene,
+                          struct ViewLayer *view_layer,
+                          const struct View3D *v3d);
+void BKE_blendfile_link(struct BlendfileLinkAppendContext *lapp_context,
+                        struct ReportList *reports,
+                        struct Main *bmain,
+                        struct Scene *scene,
+                        struct ViewLayer *view_layer,
+                        const struct View3D *v3d);
+
+void BKE_blendfile_library_relocate(struct BlendfileLinkAppendContext *lapp_context,
+                                    struct ReportList *reports,
+                                    struct Library *library,
+                                    const bool do_reload,
+                                    struct Main *bmain,
+                                    struct Scene *scene,
+                                    struct ViewLayer *view_layer);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index b2418d0539c..a08f1b81dbe 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -100,6 +100,7 @@ set(SRC
   intern/blender_undo.c
   intern/blender_user_menu.c
   intern/blendfile.c
+  intern/blendfile_link_append.c
   intern/boids.c
   intern/bpath.c
   intern/brush.c
@@ -326,6 +327,7 @@ set(SRC
   BKE_blender_user_menu.h
   BKE_blender_version.h
   BKE_blendfile.h
+  BKE_blendfile_link_append.h
   BKE_boids.h
   BKE_bpath.h
   BKE_brush.h
diff --git a/source/blender/blenkernel/intern/blendfile_link_append.c b/source/blender/blenkernel/intern/blendfile_link_append.c
new file mode 100644
index 00000000000..57781172738
--- /dev/null
+++ b/source/blender/blenkernel/intern/blendfile_link_append.c
@@ -0,0 +1,1299 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+/** \file
+ * \ingroup bke
+ *
+ * High level `.blend` file link/append code,
+ * including linking/appending several IDs from different libraries, handling instanciations of
+ * collections/objects/obdata in current scene.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "CLG_log.h"
+
+#include "MEM_guardedalloc.h"
+
+#include "DNA_ID.h"
+#include "DNA_collection_types.h"
+#include "DNA_key_types.h"
+#include "DNA_object_types.h"
+#include "DNA_scene_types.h"
+#include "DNA_screen_types.h"
+#include "DNA_space_types.h"
+
+#include "BLI_bitmap.h"
+#include "BLI_blenlib.h"
+#include "BLI_ghash.h"
+#include "BLI_linklist.h"
+#include "BLI_math.h"
+#include "BLI_memarena.h"
+#include "BLI_utildefines.h"
+
+#include "BLT_translation.h"
+
+#include "BKE_idtype.h"
+#include "BKE_key.h"
+#include "BKE_layer.h"
+#include "BKE_lib_id.h"
+#include "BKE_lib_override.h"
+#include "BKE_lib_query.h"
+#include "BKE_lib_remap.h"
+#include "BKE_main.h"
+#include "BKE_material.h"
+#include "BKE_object.h"
+#include "BKE_report.h"
+#include "BKE_rigidbody.h"
+#include "BKE_scene.h"
+
+#include "BKE_blendfile_link_append.h"
+
+#include "BLO_readfile.h"
+#include "BLO_writefile.h"
+
+static CLG_LogRef LOG = {"bke.blendfile_link_append"};
+
+/* -------------------------------------------------------------------- */
+/** \name Link/append context implementation and public management API.
+ * \{ */
+
+typedef struct BlendfileLinkAppendContextItem {
+  /** Name of the ID (without the heading two-chars IDcode). */
+  char *name;
+  /** All libs (from BlendfileLinkAppendContext.libraries) to try to load this ID from. */
+  BLI_bitmap *libraries;
+  /** ID type. */
+  short idcode;
+
+  /** Type of action to perform on this item, and general status tag information.
+   *  NOTE: Mostly used by append post-linking processing. */
+  char action;
+  char tag;
+
+  /** Newly linked ID (NULL until it has been successfully linked). */
+  ID *new_id;
+  /** Library ID from which the #new_id has been linked (NULL until it has been successfully
+   * linked). */
+  Library *source_library;
+  /** Opaque user data pointer. */
+  void *userdata;
+} BlendfileLinkAppendContextItem;
+
+typedef struct BlendfileLinkAppendContext {
+  /** List of library paths to search IDs in. */
+  LinkNodePair libraries;
+  /** List of all ID to try to link from #libraries. */
+  LinkNodePair items;
+  int num_libraries;
+  int num_items;
+  /**
+   * Combines #eFileSel_Params_Flag from DNA_space_types.h & #eBLOLibLinkFlags from BLO_readfile.h
+   */
+  int flag;
+
+  /** Allows to easily find an existing items from an ID pointer. */
+  GHash *new_id_to_item;
+
+  /** Runtime info used by append code to manage re-use of already appended matching IDs. */
+  GHash *library_weak_reference_mapping;
+
+  /** Embedded blendfile and its size, if needed. */
+  const void *blendfile_mem;
+  size_t blendfile_memsize;
+
+  /** Internal 'private' data */
+  MemArena *memarena;
+} BlendfileLinkAppendContext;
+
+typedef struct BlendfileLinkAppendContextCallBack {
+  BlendfileLinkAppendContext *lapp_context;
+  BlendfileLinkAppendContextItem *item;
+  ReportList *reports;
+
+} BlendfileLinkAppendContextCallBack;
+
+/* Actions to apply to an item (i.e. linked ID). */
+enum {
+  LINK_APPEND_ACT_UNSET = 0,
+  LINK_APPEND_ACT_KEEP_LINKED,
+  LINK_APPEND_ACT_REUSE_LOCAL,
+  LINK_APPEND_ACT_MAKE_LOCAL,
+  LINK_APPEND_ACT_COPY_LOCAL,
+};
+
+/* Various status info about an item (i.e. linked ID). */
+enum {
+  /* An indirectly linked ID. */
+  LINK_APPEND_TAG_INDIRECT = 1 << 0,
+};
+
+/** Allocate and initialize a new context to link/append datablocks.
+ *
+ *  \param flag a combination of #eFileSel_Params_Flag from DNA_space_types.h & #eBLOLibLinkFlags
+ * from BLO_readfile.h
+ */
+BlendfileLinkAppendContext *BKE_blendfile_link_append_context_new(const int flag)
+{
+  MemArena *ma = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
+  BlendfileLinkAppendContext *lapp_context = BLI_memarena_calloc(ma, sizeof(*lapp_context));
+
+  lapp_context->flag = flag;
+  lapp_context->memarena = ma;
+
+  return lapp_context;
+}
+
+/** Free a link/append context. */
+void BKE_blendfile_link_append_context_free(BlendfileLinkAppendContext *lapp_context)
+{
+  if (lapp_context->new_id_to_item != NULL) {
+    BLI_ghash_free(lapp_context->new_id_to_item, NULL, NULL);
+  }
+
+  BLI

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list