[Bf-blender-cvs] [b3f9ff2056e] asset-browser: "Make Asset" Operator: Get selected data-blocks from context

Julian Eisel noreply at git.blender.org
Fri Nov 20 23:50:12 CET 2020


Commit: b3f9ff2056e12b3dcaf4251d5b8985d7d92f5543
Author: Julian Eisel
Date:   Fri Nov 20 23:39:37 2020 +0100
Branches: asset-browser
https://developer.blender.org/rBb3f9ff2056e12b3dcaf4251d5b8985d7d92f5543

"Make Asset" Operator: Get selected data-blocks from context

Addresses T82738.

Only works in the Outliner right now.
Also breaks the button context menu entry, I'll have to work on that again
anyway (see T82664).

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

M	source/blender/blenkernel/BKE_context.h
M	source/blender/blenkernel/intern/context.c
M	source/blender/editors/asset/CMakeLists.txt
A	source/blender/editors/asset/asset_edit.c
M	source/blender/editors/asset/asset_ops.c
M	source/blender/editors/include/ED_asset.h
M	source/blender/editors/interface/interface_context_menu.c
M	source/blender/editors/space_outliner/CMakeLists.txt
A	source/blender/editors/space_outliner/outliner_context.c
M	source/blender/editors/space_outliner/outliner_intern.h
M	source/blender/editors/space_outliner/outliner_tools.c
M	source/blender/editors/space_outliner/space_outliner.c

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

diff --git a/source/blender/blenkernel/BKE_context.h b/source/blender/blenkernel/BKE_context.h
index 5c534803781..af9a95e1753 100644
--- a/source/blender/blenkernel/BKE_context.h
+++ b/source/blender/blenkernel/BKE_context.h
@@ -288,6 +288,9 @@ enum eContextObjectMode CTX_data_mode_enum(const bContext *C);
 void CTX_data_main_set(bContext *C, struct Main *bmain);
 void CTX_data_scene_set(bContext *C, struct Scene *scene);
 
+/* Only Outliner currently! */
+int CTX_data_selected_ids(const bContext *C, ListBase *list);
+
 int CTX_data_selected_editable_objects(const bContext *C, ListBase *list);
 int CTX_data_selected_editable_bases(const bContext *C, ListBase *list);
 
diff --git a/source/blender/blenkernel/intern/context.c b/source/blender/blenkernel/intern/context.c
index a1edfd1c56d..2e4d3d62925 100644
--- a/source/blender/blenkernel/intern/context.c
+++ b/source/blender/blenkernel/intern/context.c
@@ -1202,6 +1202,11 @@ ToolSettings *CTX_data_tool_settings(const bContext *C)
   return NULL;
 }
 
+int CTX_data_selected_ids(const bContext *C, ListBase *list)
+{
+  return ctx_data_collection_get(C, "selected_ids", list);
+}
+
 int CTX_data_selected_nodes(const bContext *C, ListBase *list)
 {
   return ctx_data_collection_get(C, "selected_nodes", list);
diff --git a/source/blender/editors/asset/CMakeLists.txt b/source/blender/editors/asset/CMakeLists.txt
index 76f02ceae61..8ba15208afc 100644
--- a/source/blender/editors/asset/CMakeLists.txt
+++ b/source/blender/editors/asset/CMakeLists.txt
@@ -28,6 +28,7 @@ set(INC_SYS
 )
 
 set(SRC
+  asset_edit.c
   asset_ops.c
 )
 
diff --git a/source/blender/editors/include/ED_asset.h b/source/blender/editors/asset/asset_edit.c
similarity index 51%
copy from source/blender/editors/include/ED_asset.h
copy to source/blender/editors/asset/asset_edit.c
index 99fea9aba51..583fc66ef40 100644
--- a/source/blender/editors/include/ED_asset.h
+++ b/source/blender/editors/asset/asset_edit.c
@@ -15,20 +15,37 @@
  */
 
 /** \file
- * \ingroup editors
+ * \ingroup edasset
  */
 
-#ifndef __ED_ASSET_H__
-#define __ED_ASSET_H__
+#include "BKE_asset.h"
+#include "BKE_context.h"
+#include "BKE_lib_id.h"
 
-#ifdef __cplusplus
-extern "C" {
-#endif
+#include "DNA_ID.h"
+#include "DNA_asset_types.h"
 
-void ED_operatortypes_asset(void);
+#include "UI_interface_icons.h"
 
-#ifdef __cplusplus
-}
+#include "ED_asset.h"
+
+bool ED_asset_make_for_id(const bContext *C, ID *id)
+{
+  if (id->asset_data) {
+    return false;
+  }
+
+  id_fake_user_set(id);
+
+#ifdef WITH_ASSET_REPO_INFO
+  BKE_asset_repository_info_global_ensure();
 #endif
+  id->asset_data = BKE_asset_data_create();
+
+  UI_icon_render_id(C, NULL, id, true, true);
+  /* Store reference to the ID's preview. */
+  /* XXX get rid of this? File read will be a hassle and no real need for it right now. */
+  id->asset_data->preview = BKE_assetdata_preview_get_from_id(id->asset_data, id);
 
-#endif /* __ED_ASSET_H__ */
+  return true;
+}
diff --git a/source/blender/editors/asset/asset_ops.c b/source/blender/editors/asset/asset_ops.c
index d10335bbf7e..9d14fa72f49 100644
--- a/source/blender/editors/asset/asset_ops.c
+++ b/source/blender/editors/asset/asset_ops.c
@@ -20,8 +20,6 @@
 
 #include "BKE_asset.h"
 #include "BKE_context.h"
-#include "BKE_icons.h"
-#include "BKE_lib_id.h"
 #include "BKE_report.h"
 
 #include "BLI_listbase.h"
@@ -34,40 +32,66 @@
 #include "RNA_access.h"
 #include "RNA_define.h"
 
-#include "UI_interface_icons.h"
-
 #include "WM_api.h"
 #include "WM_types.h"
 
-static int asset_make_exec(bContext *C, wmOperator *op)
+static bool asset_make_poll(bContext *C)
 {
-  PointerRNA idptr = RNA_pointer_get(op->ptr, "id");
-  ID *id = idptr.data;
-
-  if (!id || !RNA_struct_is_ID(idptr.type)) {
-    return OPERATOR_CANCELLED;
+  int tot_selected = 0;
+  bool can_make_asset = false;
+
+  /* Note that this isn't entirely cheap. Iterates over entire Outliner tree and allocates a link
+   * for each selected item. The button only shows in the context menu though, so acceptable. */
+  CTX_DATA_BEGIN (C, ID *, id, selected_ids) {
+    tot_selected++;
+    if (!id->asset_data) {
+      can_make_asset = true;
+      break;
+    }
   }
-
-  if (id->asset_data) {
-    BKE_reportf(op->reports, RPT_ERROR, "Data-block '%s' already is an asset", id->name + 2);
-    return OPERATOR_CANCELLED;
+  CTX_DATA_END;
+
+  if (!can_make_asset) {
+    if (tot_selected > 0) {
+      CTX_wm_operator_poll_msg_set(C, "Selected data-blocks are already assets.");
+    }
+    else {
+      CTX_wm_operator_poll_msg_set(C, "No data-blocks selected");
+    }
+    return false;
   }
 
-  /* TODO this should probably be somewhere in BKE and/or ED. */
+  return true;
+}
 
-  id_fake_user_set(id);
+static int asset_make_exec(bContext *C, wmOperator *op)
+{
+  ID *last_id = NULL;
+  int tot_created = 0;
 
-#ifdef WITH_ASSET_REPO_INFO
-  BKE_asset_repository_info_global_ensure();
-#endif
-  id->asset_data = BKE_asset_data_create();
+  CTX_DATA_BEGIN (C, ID *, id, selected_ids) {
+    if (id->asset_data) {
+      continue;
+    }
 
-  UI_icon_render_id(C, NULL, id, true, true);
-  /* Store reference to the ID's preview. */
-  /* XXX get rid of this? File read will be a hassle and no real need for it right now. */
-  id->asset_data->preview = BKE_assetdata_preview_get_from_id(id->asset_data, id);
+    ED_asset_make_for_id(C, id);
+    last_id = id;
+    tot_created++;
+  }
+  CTX_DATA_END;
 
-  BKE_reportf(op->reports, RPT_INFO, "Data-block '%s' is now an asset", id->name + 2);
+  /* User feedback. */
+  if (tot_created < 1) {
+    BKE_report(op->reports, RPT_ERROR, "No data-blocks to create assets for found");
+    return OPERATOR_CANCELLED;
+  }
+  if (tot_created == 1) {
+    /* If only one data-block: Give more useful message by printing asset name. */
+    BKE_reportf(op->reports, RPT_INFO, "Data-block '%s' is now an asset", last_id->name + 2);
+  }
+  else {
+    BKE_reportf(op->reports, RPT_INFO, "%i data-blocks are now assets", tot_created);
+  }
 
   WM_main_add_notifier(NC_ID | NA_EDITED, NULL);
   WM_main_add_notifier(NC_ASSET | NA_ADDED, NULL);
@@ -81,10 +105,10 @@ static void ASSET_OT_make(wmOperatorType *ot)
   ot->description = "Enable asset management for a data-block";
   ot->idname = "ASSET_OT_make";
 
+  ot->poll = asset_make_poll;
   ot->exec = asset_make_exec;
 
-  RNA_def_pointer_runtime(
-      ot->srna, "id", &RNA_ID, "Data-block", "Data-block to enable asset management for");
+  ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
 }
 
 static int asset_catalog_add_exec(bContext *C, wmOperator *op)
diff --git a/source/blender/editors/include/ED_asset.h b/source/blender/editors/include/ED_asset.h
index 99fea9aba51..7b9f5087da8 100644
--- a/source/blender/editors/include/ED_asset.h
+++ b/source/blender/editors/include/ED_asset.h
@@ -25,6 +25,8 @@
 extern "C" {
 #endif
 
+bool ED_asset_make_for_id(const struct bContext *C, struct ID *id);
+
 void ED_operatortypes_asset(void);
 
 #ifdef __cplusplus
diff --git a/source/blender/editors/interface/interface_context_menu.c b/source/blender/editors/interface/interface_context_menu.c
index 1a70dbd6879..82b5d0a174a 100644
--- a/source/blender/editors/interface/interface_context_menu.c
+++ b/source/blender/editors/interface/interface_context_menu.c
@@ -959,11 +959,7 @@ bool ui_popup_context_menu_for_button(bContext *C, uiBut *but)
     }
 
     if (RNA_struct_is_ID(but->rnapoin.type)) {
-      PointerRNA op_ptr;
-
-      uiItemFullO(
-          layout, "ASSET_OT_make", NULL, ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, 0, &op_ptr);
-      RNA_pointer_set(&op_ptr, "id", but->rnapoin);
+      uiItemO(layout, NULL, ICON_NONE, "ASSET_OT_make");
       uiItemS(layout);
     }
   }
diff --git a/source/blender/editors/space_outliner/CMakeLists.txt b/source/blender/editors/space_outliner/CMakeLists.txt
index 996570fae25..15f8a5746ef 100644
--- a/source/blender/editors/space_outliner/CMakeLists.txt
+++ b/source/blender/editors/space_outliner/CMakeLists.txt
@@ -34,6 +34,7 @@ set(INC
 
 set(SRC
   outliner_collections.c
+  outliner_context.c
   outliner_dragdrop.c
   outliner_draw.c
   outliner_edit.c
diff --git a/source/blender/editors/space_outliner/outliner_context.c b/source/blender/editors/space_outliner/outliner_context.c
new file mode 100644
index 00000000000..d6b467f0c86
--- /dev/null
+++ b/source/blender/editors/space_outliner/outliner_context.c
@@ -0,0 +1,73 @@
+/*
+ * 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.
+ *
+ * The Original Code is Copyright (C) 2017 Blender Foundation.
+ * All rights reserved.
+ */
+
+/** \file
+ * \ingroup spoutliner
+ */
+
+#include "BLI_listbase.h"
+
+#include "BKE_context.h"
+
+#include "DNA_space_types.h"
+
+#include "RNA_access.h"
+
+#include "outliner_intern.h"
+
+static void outliner_context_selected_ids_recursive(const ListBase *subtree,
+                                                    bContextDataResult *result)
+{
+  LISTBASE_FOREACH (const TreeElement *, te, subtree) {
+    const TreeStoreElem *tse = TREESTORE(te);
+    if ((tse->flag & TSE_SELECTED) && (tse->type == 0)) {
+      CTX_data_id_list_add(result, tse->id);
+    }
+    outliner_context_selected_ids_recursive(&te->subtree, result);
+  }
+}
+
+static void outliner_context_selected_ids(const SpaceOutliner *space_outliner,
+                                          bContextDataResult *result)
+{
+  outliner_context_selected_ids_recursive(&space_outliner->tree, result);
+  CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
+}
+
+const char *outliner_context_dir[] = {"selected_ids", NULL};
+
+

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list