[Bf-blender-cvs] [350ec8e9b96] asset-lite-greasepencil: GPencil: Asset Manager support (LITE)

Antonio Vazquez noreply at git.blender.org
Wed Jun 22 16:10:47 CEST 2022


Commit: 350ec8e9b9651123ec53837e2b085f46f4bcbd3d
Author: Antonio Vazquez
Date:   Wed Jun 22 15:57:54 2022 +0200
Branches: asset-lite-greasepencil
https://developer.blender.org/rB350ec8e9b9651123ec53837e2b085f46f4bcbd3d

GPencil: Asset Manager support (LITE)

This patch replaces D13193 and remove any transform tool in the import. Now the import must be done in Edit mode and
all transformations must be done with the standard transform tools of Blender.

A new operator has been added to create an asset base on one of the next option:

* Active Layer.
* All Layers.
* All Layers Separated
* Active Frame (Active Layer).
* Active Frame (All Layers).
* Selected Frames.
* Selected Strokes.

The menu uses `Create Asset`instead of `Mark Asset` because the asset is created as new datablock and not reuse existing data.

To import asset, set the grease pencil object where you want import in Edit mode.

Also new asset preview for Grease Pencil has been added.

For testing, be sure to enable `Experimental Feature`and `Developers Extra`

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

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

M	release/scripts/startup/bl_ui/space_view3d.py
M	source/blender/blenkernel/BKE_gpencil.h
M	source/blender/blenkernel/intern/gpencil.c
M	source/blender/blenkernel/intern/icons.cc
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
M	source/blender/editors/render/render_preview.cc
M	source/blender/editors/space_view3d/space_view3d.c
M	source/blender/makesdna/DNA_gpencil_types.h

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

diff --git a/release/scripts/startup/bl_ui/space_view3d.py b/release/scripts/startup/bl_ui/space_view3d.py
index 5e5e7a79035..c6c2e0fb0d7 100644
--- a/release/scripts/startup/bl_ui/space_view3d.py
+++ b/release/scripts/startup/bl_ui/space_view3d.py
@@ -934,6 +934,7 @@ class VIEW3D_MT_editor_menus(Menu):
                 layout.menu("VIEW3D_MT_edit_gpencil")
                 layout.menu("VIEW3D_MT_edit_gpencil_stroke")
                 layout.menu("VIEW3D_MT_edit_gpencil_point")
+                layout.menu("VIEW3D_MT_edit_gpencil_asset")
             elif obj and obj.mode == 'WEIGHT_GPENCIL':
                 layout.menu("VIEW3D_MT_weight_gpencil")
             if obj and obj.mode == 'VERTEX_GPENCIL':
@@ -5167,6 +5168,21 @@ class VIEW3D_MT_edit_gpencil_point(Menu):
         layout.menu("VIEW3D_MT_gpencil_vertex_group")
 
 
+class VIEW3D_MT_edit_gpencil_asset(Menu):
+    bl_label = "Asset"
+
+    def draw(self, _context):
+        layout = self.layout
+
+        layout.operator("gpencil.asset_create", text="Active Layer").mode = 'LAYER'
+        layout.operator("gpencil.asset_create", text="All Layers").mode = 'LAYERS_ALL'
+        layout.operator("gpencil.asset_create", text="All Layers Separated").mode = 'LAYERS_SPLIT'
+        layout.operator("gpencil.asset_create", text="Active Frame (Active Layer)").mode = 'FRAME'
+        layout.operator("gpencil.asset_create", text="Active Frame (All Layers)").mode = 'FRAME_ALL'
+        layout.operator("gpencil.asset_create", text="Selected Frames").mode = 'FRAME_SELECTED'
+        layout.operator("gpencil.asset_create", text="Selected Strokes").mode = 'SELECTED'
+
+
 class VIEW3D_MT_weight_gpencil(Menu):
     bl_label = "Weights"
 
@@ -7299,6 +7315,10 @@ class VIEW3D_MT_gpencil_edit_context_menu(Menu):
 
             col.operator("gpencil.reproject", text="Reproject")
 
+        # Assets
+        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
@@ -7826,6 +7846,7 @@ classes = (
     VIEW3D_MT_edit_gpencil,
     VIEW3D_MT_edit_gpencil_stroke,
     VIEW3D_MT_edit_gpencil_point,
+    VIEW3D_MT_edit_gpencil_asset,
     VIEW3D_MT_edit_gpencil_delete,
     VIEW3D_MT_edit_gpencil_showhide,
     VIEW3D_MT_weight_gpencil,
diff --git a/source/blender/blenkernel/BKE_gpencil.h b/source/blender/blenkernel/BKE_gpencil.h
index dc7a5ab003a..b2cc4474eb2 100644
--- a/source/blender/blenkernel/BKE_gpencil.h
+++ b/source/blender/blenkernel/BKE_gpencil.h
@@ -329,6 +329,7 @@ struct bGPDcurve *BKE_gpencil_stroke_editcurve_new(int tot_curve_points);
  * \return True if layer is editable
  */
 bool BKE_gpencil_layer_is_editable(const struct bGPDlayer *gpl);
+void BKE_gpencil_frame_min_max(const struct bGPdata *gpd, int *r_min, int *r_max);
 
 /* How gpencil_layer_getframe() should behave when there
  * is no existing GP-Frame on the frame requested.
diff --git a/source/blender/blenkernel/intern/gpencil.c b/source/blender/blenkernel/intern/gpencil.c
index f86e947910b..88075c3e375 100644
--- a/source/blender/blenkernel/intern/gpencil.c
+++ b/source/blender/blenkernel/intern/gpencil.c
@@ -62,7 +62,7 @@ static CLG_LogRef LOG = {"bke.gpencil"};
 static void greasepencil_copy_data(Main *UNUSED(bmain),
                                    ID *id_dst,
                                    const ID *id_src,
-                                   const int UNUSED(flag))
+                                   const int flag)
 {
   bGPdata *gpd_dst = (bGPdata *)id_dst;
   const bGPdata *gpd_src = (const bGPdata *)id_src;
@@ -110,6 +110,13 @@ static void greasepencil_copy_data(Main *UNUSED(bmain),
 
     BLI_addtail(&gpd_dst->layers, gpl_dst);
   }
+
+  if (flag & LIB_ID_COPY_NO_PREVIEW) {
+    gpd_dst->preview = NULL;
+  }
+  else {
+    BKE_previewimg_id_copy(&gpd_dst->id, &gpd_src->id);
+  }
 }
 
 static void greasepencil_free_data(ID *id)
@@ -179,6 +186,8 @@ static void greasepencil_blend_write(BlendWriter *writer, ID *id, const void *id
         }
       }
     }
+
+    BKE_previewimg_blend_write(writer, gpd->preview);
   }
 }
 
@@ -193,6 +202,10 @@ void BKE_gpencil_blend_read_data(BlendDataReader *reader, bGPdata *gpd)
   BLO_read_data_address(reader, &gpd->adt);
   BKE_animdata_blend_read_data(reader, gpd->adt);
 
+  /* Preview. */
+  BLO_read_data_address(reader, &gpd->preview);
+  BKE_previewimg_blend_read(reader, gpd->preview);
+
   /* Ensure full objectmode for linked grease pencil. */
   if (ID_IS_LINKED(gpd)) {
     gpd->flag &= ~GP_DATA_STROKE_PAINTMODE;
@@ -495,6 +508,8 @@ void BKE_gpencil_free_data(bGPdata *gpd, bool free_all)
     /* clear cache */
     BKE_gpencil_batch_cache_free(gpd);
   }
+  /* Preview. */
+  BKE_previewimg_free(&gpd->preview);
 }
 
 void BKE_gpencil_eval_delete(bGPdata *gpd_eval)
@@ -2197,7 +2212,7 @@ int BKE_gpencil_object_material_index_get_by_name(Object *ob, const char *name)
   for (short i = 0; i < *totcol; i++) {
     read_ma = BKE_object_material_get(ob, i + 1);
     /* Material names are like "MAMaterial.001" */
-    if (STREQ(name, &read_ma->id.name[2])) {
+    if ((read_ma) && (STREQ(name, &read_ma->id.name[2]))) {
       return i;
     }
   }
@@ -3040,4 +3055,20 @@ void BKE_gpencil_update_on_write(bGPdata *gpd_orig, bGPdata *gpd_eval)
   BKE_gpencil_free_update_cache(gpd_orig);
 }
 
+/* Get min and max frame number for all layers. */
+void BKE_gpencil_frame_min_max(const bGPdata *gpd, int *r_min, int *r_max)
+{
+  *r_min = INT_MAX;
+  *r_max = INT_MIN;
+  LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) {
+    LISTBASE_FOREACH (bGPDframe *, gpf, &gpl->frames) {
+      if (gpf->framenum < *r_min) {
+        *r_min = gpf->framenum;
+      }
+      if (gpf->framenum > *r_max) {
+        *r_max = gpf->framenum;
+      }
+    }
+  }
+}
 /** \} */
diff --git a/source/blender/blenkernel/intern/icons.cc b/source/blender/blenkernel/intern/icons.cc
index f59f5352aad..e2a1192e89d 100644
--- a/source/blender/blenkernel/intern/icons.cc
+++ b/source/blender/blenkernel/intern/icons.cc
@@ -355,6 +355,7 @@ PreviewImage **BKE_previewimg_id_get_p(const ID *id)
     ID_PRV_CASE(ID_LA, Light);
     ID_PRV_CASE(ID_IM, Image);
     ID_PRV_CASE(ID_BR, Brush);
+    ID_PRV_CASE(ID_GD, bGPdata);
     ID_PRV_CASE(ID_GR, Collection);
     ID_PRV_CASE(ID_SCE, Scene);
     ID_PRV_CASE(ID_SCR, bScreen);
@@ -419,7 +420,7 @@ void BKE_previewimg_id_custom_set(ID *id, const char *filepath)
 
 bool BKE_previewimg_id_supports_jobs(const ID *id)
 {
-  return ELEM(GS(id->name), ID_OB, ID_MA, ID_TE, ID_LA, ID_WO, ID_IM, ID_BR, ID_GR);
+  return ELEM(GS(id->name), ID_OB, ID_MA, ID_TE, ID_LA, ID_WO, ID_IM, ID_BR, ID_GR, ID_GD);
 }
 
 void BKE_previewimg_deferred_release(PreviewImage *prv)
diff --git a/source/blender/editors/gpencil/CMakeLists.txt b/source/blender/editors/gpencil/CMakeLists.txt
index 09a3cac0d48..13624d09a2e 100644
--- a/source/blender/editors/gpencil/CMakeLists.txt
+++ b/source/blender/editors/gpencil/CMakeLists.txt
@@ -28,6 +28,7 @@ set(SRC
   gpencil_add_monkey.c
   gpencil_add_stroke.c
   gpencil_armature.c
+  gpencil_asset.c
   gpencil_bake_animation.cc
   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..c2aabb7c9df
--- /dev/null
+++ b/source/blender/editors/gpencil/gpencil_asset.c
@@ -0,0 +1,768 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ * Copyright 2022 Blender Foundation. */
+
+/** \file
+ * \ingroup edgpencil
+ */
+
+#include "BLI_blenlib.h"
+#include "BLI_math.h"
+#include "BLI_utildefines.h"
+
+#include "BLT_translation.h"
+
+#include "MEM_guardedalloc.h"
+
+#include "DNA_gpencil_types.h"
+#include "DNA_material_types.h"
+
+#include "BKE_asset.h"
+#include "BKE_context.h"
+#include "BKE_gpencil.h"
+#include "BKE_gpencil_geom.h"
+#include "BKE_lib_id.h"
+#include "BKE_main.h"
+#include "BKE_material.h"
+#include "BKE_object.h"
+#include "BKE_report.h"
+
+#include "UI_interface.h"
+#include "UI_resources.h"
+
+#include "WM_api.h"
+#include "WM_types.h"
+
+#include "RNA_access.h"
+#include "RNA_define.h"
+#include "RNA_enum_types.h"
+
+#include "GPU_immediate.h"
+
+#include "ED_asset.h"
+#include "ED_gpencil.h"
+#include "ED_keyframing.h"
+#include "ED_screen.h"
+#include "ED_space_api.h"
+
+#include "DEG_depsgraph.h"
+
+#include "gpencil_intern.h"
+
+#define ROTATION_CONTROL_GAP 15.0f
+
+typedef struct tGPDAssetStroke {
+  bGPDlayer *gpl;
+  bGPDframe *gpf;
+  bGPDstroke *gps;
+  int slot_index;
+  bool is_new_gpl;
+  bool is_new_gpf;
+} tGPDAssetStroke;
+
+/* Temporary Asset import operation data. */
+typedef struct tGPDasset {
+  struct Main *bmain;
+  struct Depsgraph *depsgraph;
+  struct Scene *scene;
+  struct ScrArea *area;
+  struct ARegion *region;
+  /** Current object. */
+  struct Object *ob;
+  /** Current GP data block. */
+  struct bGPdata *gpd;
+  /** Asset GP data block. */
+  struct bGPdata *gpd_asset;
+  /* Space Conversion Data */
+  struct GP_SpaceConversion gsc;
+
+  /** Current frame number. */
+  int cframe;
+
+  /** Drop initial position. */
+  int drop[2];
+
+  /* Data to keep a reference of the asset data inserted in the target object. */
+  /** Number of elements in data. */
+  int data_len;
+  /** Array of data with all strokes append. */
+  tGPDAssetStroke *data;
+
+} tGPDasset;
+
+/* -------------------------------------------------------------------- */
+/** \name Create Grease Pencil data block Asset operator
+ * \{ */
+
+typedef enum eGP_AssetModes {
+  /* Active Layer. */
+  GP_ASSET_MODE_ACTIVE_LAYER = 0,
+  /* All Layers. */
+  GP_ASSET_MODE_ALL_LAYERS,
+  /* All Layers in separated assets. */
+  GP_ASSET_MODE_ALL_LAYERS_SPLIT,
+  /* Active Frame. */
+  GP_ASSET_MODE_ACTIVE_FRAME,
+  /* Active Frame All Layers. */
+  GP_ASSET_MODE_ACTIVE_FRAME_ALL_LAYERS,
+  /* Selected Frames. */
+  GP_ASSET_MODE_SELECTED_FRAMES,
+  /* Selected Strokes. */
+  GP_ASSET_MODE_SELECTED_STROKES,
+} eGP_AssetModes;
+
+/* Helper: Apply layer settings. */
+static void apply_layer_settings(bGPDlayer *gpl)
+{
+  /* Apply layer attributes. */
+  LISTBASE_FOREACH (bGPDframe *, gpf, &gpl->frames) {
+    LISTBASE_FOREACH (bGP

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list