[Bf-blender-cvs] [15af2d709ef] asset-greasepencil: GPencil: Basic structure to create asset (WIP)

Antonio Vazquez noreply at git.blender.org
Wed Jul 7 17:50:27 CEST 2021


Commit: 15af2d709ef7f70fe8caa06c60617aef0cea1fc9
Author: Antonio Vazquez
Date:   Wed Jul 7 17:50:09 2021 +0200
Branches: asset-greasepencil
https://developer.blender.org/rB15af2d709ef7f70fe8caa06c60617aef0cea1fc9

GPencil: Basic structure to create asset (WIP)

This operator creates an asset base on one of the next option:

* Active Layer
* Active FRame
* Active Frame All Layers
* Selected Strokes

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

M	release/scripts/startup/bl_ui/space_view3d.py
M	source/blender/editors/gpencil/CMakeLists.txt
A	source/blender/editors/gpencil/gpencil_asset.c
M	source/blender/editors/gpencil/gpencil_intern.h
M	source/blender/editors/gpencil/gpencil_ops.c

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

diff --git a/release/scripts/startup/bl_ui/space_view3d.py b/release/scripts/startup/bl_ui/space_view3d.py
index 1c21d27a117..879e51c5fe0 100644
--- a/release/scripts/startup/bl_ui/space_view3d.py
+++ b/release/scripts/startup/bl_ui/space_view3d.py
@@ -7127,6 +7127,11 @@ class VIEW3D_MT_gpencil_edit_context_menu(Menu):
 
             col.operator("gpencil.reproject", text="Reproject")
 
+        # Assets
+        if context.preferences.experimental.use_asset_browser:
+            col.separator()
+            col.operator_menu_enum("gpencil.asset_create", "mode", text="Create Asset")
+
 
 def draw_gpencil_layer_active(context, layout):
     gpl = context.active_gpencil_layer
diff --git a/source/blender/editors/gpencil/CMakeLists.txt b/source/blender/editors/gpencil/CMakeLists.txt
index bff7310e9f7..62657797d6c 100644
--- a/source/blender/editors/gpencil/CMakeLists.txt
+++ b/source/blender/editors/gpencil/CMakeLists.txt
@@ -41,6 +41,7 @@ set(SRC
   gpencil_add_monkey.c
   gpencil_add_stroke.c
   gpencil_armature.c
+  gpencil_asset.c
   gpencil_bake_animation.c
   gpencil_convert.c
   gpencil_data.c
diff --git a/source/blender/editors/gpencil/gpencil_asset.c b/source/blender/editors/gpencil/gpencil_asset.c
new file mode 100644
index 00000000000..95580dc6ece
--- /dev/null
+++ b/source/blender/editors/gpencil/gpencil_asset.c
@@ -0,0 +1,165 @@
+/*
+ * 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) 2021, Blender Foundation
+ * This is a new part of Blender
+ * Operators for editing Grease Pencil strokes
+ */
+
+/** \file
+ * \ingroup edgpencil
+ */
+
+#include "BLI_blenlib.h"
+#include "BLI_utildefines.h"
+
+#include "DNA_gpencil_types.h"
+
+#include "BKE_context.h"
+#include "BKE_gpencil.h"
+#include "BKE_lib_id.h"
+#include "BKE_main.h"
+#include "BKE_report.h"
+
+#include "WM_api.h"
+#include "WM_types.h"
+
+#include "RNA_access.h"
+#include "RNA_define.h"
+#include "RNA_enum_types.h"
+
+#include "ED_asset.h"
+#include "ED_gpencil.h"
+#include "ED_screen.h"
+
+#include "DEG_depsgraph.h"
+#include "DEG_depsgraph_build.h"
+
+static bool gpencil_asset_create_poll(bContext *C)
+{
+  if (U.experimental.use_asset_browser == false) {
+    return false;
+  }
+
+  Object *ob = CTX_data_active_object(C);
+  if ((ob == NULL) || (ob->type != OB_GPENCIL)) {
+    return false;
+  }
+
+  return ED_operator_view3d_active(C);
+}
+
+/* -------------------------------------------------------------------- */
+/** \name Create Grease Pencil Asset operator
+ * \{ */
+
+typedef enum eGP_AssetModes {
+  /* Active Layer. */
+  GP_ASSET_MODE_LAYER = 0,
+  /* Active Frame. */
+  GP_ASSET_MODE_FRAME,
+  /* Active Frame All Layers. */
+  GP_ASSET_MODE_FRAME_ALL_LAYERS,
+  /* Selected Strokesd. */
+  GP_ASSET_MODE_SELECTED_STROKES,
+} eGP_AssetModes;
+
+static int gpencil_asset_create_exec(bContext *C, wmOperator *op)
+{
+  Main *bmain = CTX_data_main(C);
+  Object *ob = CTX_data_active_object(C);
+  bGPdata *gpd_src = ob->data;
+
+  eGP_AssetModes mode = RNA_enum_get(op->ptr, "mode");
+
+  /* Create a copy of selected datablock. */
+  bGPdata *gpd = (bGPdata *)BKE_id_copy(bmain, &gpd_src->id);
+  /* Enable fake user by default. */
+  id_fake_user_set(&gpd->id);
+
+  bGPDlayer *gpl_active = BKE_gpencil_layer_active_get(gpd);
+
+  LISTBASE_FOREACH_MUTABLE (bGPDlayer *, gpl, &gpd->layers) {
+    /* If Layer o Active Frame mode, delete non active layers. */
+    if ((ELEM(mode, GP_ASSET_MODE_LAYER, GP_ASSET_MODE_FRAME)) && (gpl != gpl_active)) {
+      BKE_gpencil_layer_delete(gpd, gpl);
+      continue;
+    }
+
+    bGPDframe *gpf_active = gpl->actframe;
+
+    LISTBASE_FOREACH_MUTABLE (bGPDframe *, gpf, &gpl->frames) {
+      /* If Active Frame mode, delete non active frames. */
+      if ((ELEM(mode, GP_ASSET_MODE_FRAME, GP_ASSET_MODE_FRAME_ALL_LAYERS)) &&
+          (gpf != gpf_active)) {
+        BKE_gpencil_layer_frame_delete(gpl, gpf);
+        continue;
+      }
+      /* Remove any unselected stroke if SELECTED mode. */
+      if (mode == GP_ASSET_MODE_SELECTED_STROKES) {
+        LISTBASE_FOREACH_MUTABLE (bGPDstroke *, gps, &gpf->strokes) {
+          if ((gps->flag & GP_STROKE_SELECT) == 0) {
+            BLI_remlink(&gpf->strokes, gps);
+            BKE_gpencil_free_stroke(gps);
+            continue;
+          }
+        }
+      }
+    }
+  }
+
+  if (ED_asset_mark_id(C, &gpd->id)) {
+  }
+
+  DEG_relations_tag_update(bmain);
+  WM_main_add_notifier(NC_ID | NA_EDITED, NULL);
+  WM_main_add_notifier(NC_ASSET | NA_ADDED, NULL);
+
+  WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, NULL);
+  WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
+  WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_SELECTED, NULL);
+
+  return OPERATOR_FINISHED;
+}
+
+void GPENCIL_OT_asset_create(wmOperatorType *ot)
+{
+  static const EnumPropertyItem mode_types[] = {
+      {GP_ASSET_MODE_LAYER, "LAYER", 0, "Active Layer", ""},
+      {GP_ASSET_MODE_FRAME, "FRAME", 0, "Active Frame", ""},
+      {GP_ASSET_MODE_FRAME_ALL_LAYERS, "FRAME_ALL", 0, "Active Frame All Layers", ""},
+      {GP_ASSET_MODE_SELECTED_STROKES, "SELECTED", 0, "Selected Strokes", ""},
+      {0, NULL, 0, NULL, NULL},
+  };
+
+  /* identifiers */
+  ot->name = "Create Grease Pencil Asset";
+  ot->idname = "GPENCIL_OT_asset_create";
+  ot->description = "Create asset from sections of the active object";
+
+  /* callbacks */
+  ot->invoke = WM_menu_invoke;
+  ot->exec = gpencil_asset_create_exec;
+  ot->poll = gpencil_asset_create_poll;
+
+  /* flags */
+  ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+
+  /* properties */
+  ot->prop = RNA_def_enum(
+      ot->srna, "mode", mode_types, GP_ASSET_MODE_SELECTED_STROKES, "Mode", "");
+}
+
+/** \} */
diff --git a/source/blender/editors/gpencil/gpencil_intern.h b/source/blender/editors/gpencil/gpencil_intern.h
index d1a1e417d9e..06e47f2fbf2 100644
--- a/source/blender/editors/gpencil/gpencil_intern.h
+++ b/source/blender/editors/gpencil/gpencil_intern.h
@@ -548,6 +548,9 @@ void GPENCIL_OT_convert_old_files(struct wmOperatorType *ot);
 /* armatures */
 void GPENCIL_OT_generate_weights(struct wmOperatorType *ot);
 
+/* Assets. */
+void GPENCIL_OT_asset_create(struct wmOperatorType *ot);
+
 /* ****************************************************** */
 /* Stroke Iteration Utilities */
 
diff --git a/source/blender/editors/gpencil/gpencil_ops.c b/source/blender/editors/gpencil/gpencil_ops.c
index 35640cf3b66..151ac172c3d 100644
--- a/source/blender/editors/gpencil/gpencil_ops.c
+++ b/source/blender/editors/gpencil/gpencil_ops.c
@@ -700,6 +700,9 @@ void ED_operatortypes_gpencil(void)
 
   /* armatures */
   WM_operatortype_append(GPENCIL_OT_generate_weights);
+
+  /* Assets. */
+  WM_operatortype_append(GPENCIL_OT_asset_create);
 }
 
 void ED_operatormacros_gpencil(void)



More information about the Bf-blender-cvs mailing list