[Bf-blender-cvs] [501befe2c4e] greasepencil-object: GPencil: Improve Bake Animation to Linked

Antonio Vazquez noreply at git.blender.org
Mon Apr 6 17:24:18 CEST 2020


Commit: 501befe2c4e9346a78c55a5700bb63095625617e
Author: Antonio Vazquez
Date:   Mon Apr 6 17:23:56 2020 +0200
Branches: greasepencil-object
https://developer.blender.org/rB501befe2c4e9346a78c55a5700bb63095625617e

GPencil: Improve Bake Animation to Linked

Also allows to bake several meshes at the same time.

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

M	release/scripts/startup/bl_operators/gpencil_mesh_bake.py
M	source/blender/editors/gpencil/gpencil_mesh.c

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

diff --git a/release/scripts/startup/bl_operators/gpencil_mesh_bake.py b/release/scripts/startup/bl_operators/gpencil_mesh_bake.py
index e1dacd4254a..849f7ecfc11 100644
--- a/release/scripts/startup/bl_operators/gpencil_mesh_bake.py
+++ b/release/scripts/startup/bl_operators/gpencil_mesh_bake.py
@@ -127,7 +127,6 @@ class GPENCIL_OT_mesh_bake(Operator):
     def poll(self, context):
         ob = context.active_object
         return ((ob is not None) and
-                (ob.type == 'MESH') and
                 (context.mode == 'OBJECT'))
 
     def execute(self, context):
diff --git a/source/blender/editors/gpencil/gpencil_mesh.c b/source/blender/editors/gpencil/gpencil_mesh.c
index 6a60ba530c4..18bf41e4381 100644
--- a/source/blender/editors/gpencil/gpencil_mesh.c
+++ b/source/blender/editors/gpencil/gpencil_mesh.c
@@ -32,6 +32,7 @@
 #include "DNA_screen_types.h"
 
 #include "BKE_context.h"
+#include "BKE_duplilist.h"
 #include "BKE_global.h"
 #include "BKE_gpencil.h"
 #include "BKE_gpencil_geom.h"
@@ -80,6 +81,53 @@ static bool gp_bake_mesh_animation_poll(bContext *C)
   return (area && area->spacetype);
 }
 
+typedef struct GpBakeOb {
+  struct GPBakelist *next, *prev;
+  Object *ob;
+} GpBakeOb;
+
+static void gp_bake_ob_list(bContext *C, Depsgraph *depsgraph, Scene *scene, ListBase *list)
+{
+  GpBakeOb *elem = NULL;
+  Object *ob = NULL;
+
+  /* Add selected objects. */
+  CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases) {
+    ob = base->object;
+    /* Add selected meshes.*/
+    if (ob->type == OB_MESH) {
+      elem = MEM_callocN(sizeof(GpBakeOb), __func__);
+      elem->ob = base->object;
+      BLI_addtail(list, elem);
+    }
+
+    /* Add duplilist. */
+    if (ob->type == OB_EMPTY) {
+      ListBase *lb;
+      DupliObject *dob;
+      lb = object_duplilist(depsgraph, scene, ob);
+      for (dob = lb->first; dob; dob = dob->next) {
+        if (dob->ob->type != OB_MESH) {
+          continue;
+        }
+        elem = MEM_callocN(sizeof(GpBakeOb), __func__);
+        elem->ob = dob->ob;
+        BLI_addtail(list, elem);
+      }
+
+      free_object_duplilist(lb);
+    }
+  }
+  CTX_DATA_END;
+}
+
+static void gp_bake_free_ob_list(ListBase *list)
+{
+  LISTBASE_FOREACH_MUTABLE (GpBakeOb *, elem, list) {
+    MEM_SAFE_FREE(elem);
+  }
+}
+
 static int gp_bake_mesh_animation_exec(bContext *C, wmOperator *op)
 {
   Main *bmain = CTX_data_main(C);
@@ -90,17 +138,16 @@ static int gp_bake_mesh_animation_exec(bContext *C, wmOperator *op)
   Object *ob = CTX_data_active_object(C);
   Object *ob_gpencil = NULL;
 
+  ListBase list = {NULL, NULL};
+  gp_bake_ob_list(C, depsgraph, scene, &list);
+
   /* Cannot check this in poll because the active object changes. */
-  if ((ob == NULL) || (ob->type != OB_MESH)) {
-    BKE_report(op->reports, RPT_ERROR, "No Mesh object selected");
+  if (list.first == NULL) {
+    BKE_report(op->reports, RPT_ERROR, "No valid object selected");
+    gp_bake_free_ob_list(&list);
     return OPERATOR_CANCELLED;
   }
 
-  /* Set cursor to indicate working. */
-  WM_cursor_wait(1);
-
-  Object *ob_eval = (Object *)DEG_get_evaluated_object(depsgraph, ob);
-
   /* Grab all relevant settings. */
   const int step = RNA_int_get(op->ptr, "step");
 
@@ -131,14 +178,19 @@ static int gp_bake_mesh_animation_exec(bContext *C, wmOperator *op)
   else {
     ob_gpencil = BLI_findstring(&bmain->objects, target, offsetof(ID, name) + 2);
   }
+
   if ((ob_gpencil == NULL) || (ob_gpencil->type != OB_GPENCIL)) {
     BKE_report(op->reports, RPT_ERROR, "Target grease pencil object not valid");
+    gp_bake_free_ob_list(&list);
     return OPERATOR_CANCELLED;
   }
 
   bGPdata *gpd = (bGPdata *)ob_gpencil->data;
   gpd->draw_mode = (project_type == GP_REPROJECT_KEEP) ? GP_DRAWMODE_3D : GP_DRAWMODE_2D;
 
+  /* Set cursor to indicate working. */
+  WM_cursor_wait(1);
+
   GP_SpaceConversion gsc = {NULL};
   SnapObjectContext *sctx = NULL;
   if (project_type != GP_REPROJECT_KEEP) {
@@ -172,30 +224,35 @@ static int gp_bake_mesh_animation_exec(bContext *C, wmOperator *op)
     CFRA = i;
     BKE_scene_graph_update_for_newframe(depsgraph, bmain);
 
-    /* Generate strokes. */
-    BKE_gpencil_convert_mesh(bmain,
-                             depsgraph,
-                             scene,
-                             ob_gpencil,
-                             ob,
-                             angle,
-                             thickness,
-                             offset,
-                             ob_eval->obmat,
-                             frame_offset,
-                             use_seams,
-                             use_faces);
-
-    /* Reproject all untaged created strokes. */
-    if (project_type != GP_REPROJECT_KEEP) {
-      LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) {
-        bGPDframe *gpf = gpl->actframe;
-        if (gpf != NULL) {
-          LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) {
-            if ((gps->flag & GP_STROKE_TAG) == 0) {
-              ED_gpencil_stroke_reproject(
-                  depsgraph, &gsc, sctx, gpl, gpf, gps, project_type, false);
-              gps->flag |= GP_STROKE_TAG;
+    /* Loop all objects in the list. */
+    LISTBASE_FOREACH (GpBakeOb *, elem, &list) {
+      Object *ob_eval = (Object *)DEG_get_evaluated_object(depsgraph, elem->ob);
+
+      /* Generate strokes. */
+      BKE_gpencil_convert_mesh(bmain,
+                               depsgraph,
+                               scene,
+                               ob_gpencil,
+                               elem->ob,
+                               angle,
+                               thickness,
+                               offset,
+                               ob_eval->obmat,
+                               frame_offset,
+                               use_seams,
+                               use_faces);
+
+      /* Reproject all untaged created strokes. */
+      if (project_type != GP_REPROJECT_KEEP) {
+        LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) {
+          bGPDframe *gpf = gpl->actframe;
+          if (gpf != NULL) {
+            LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) {
+              if ((gps->flag & GP_STROKE_TAG) == 0) {
+                ED_gpencil_stroke_reproject(
+                    depsgraph, &gsc, sctx, gpl, gpf, gps, project_type, false);
+                gps->flag |= GP_STROKE_TAG;
+              }
             }
           }
         }
@@ -233,6 +290,7 @@ static int gp_bake_mesh_animation_exec(bContext *C, wmOperator *op)
   }
 
   /* Free memory. */
+  gp_bake_free_ob_list(&list);
   if (sctx != NULL) {
     ED_transform_snap_object_context_destroy(sctx);
   }



More information about the Bf-blender-cvs mailing list