De-duplicate tool settings copy and make tool settings freeing reusable
authorSergey Sharybin <sergey.vfx@gmail.com>
Wed, 28 Feb 2018 13:52:17 +0000 (14:52 +0100)
committerSergey Sharybin <sergey.vfx@gmail.com>
Wed, 28 Feb 2018 13:52:17 +0000 (14:52 +0100)
source/blender/blenkernel/BKE_scene.h
source/blender/blenkernel/intern/scene.c

index 481aff3cfa656ed62db562c32300a44b89b239a2..0e1472b706289b66ea3596135369dd1cf8959524 100644 (file)
@@ -96,6 +96,9 @@ void BKE_scene_base_flag_from_objects(struct Scene *scene);
 void BKE_scene_set_background(struct Main *bmain, struct Scene *sce);
 struct Scene *BKE_scene_set_name(struct Main *bmain, const char *name);
 
+struct ToolSettings *BKE_toolsettings_copy(struct ToolSettings *toolsettings, const int flag);
+void BKE_toolsettings_free(struct ToolSettings *toolsettings);
+
 void BKE_scene_copy_data(struct Main *bmain, struct Scene *sce_dst, const struct Scene *sce_src, const int flag);
 struct Scene *BKE_scene_copy(struct Main *bmain, struct Scene *sce, int type);
 void BKE_scene_groups_relink(struct Scene *sce);
index 3f27e136e8c794e587d19013807f39e42d9e58de..9b3299bdbc5bd9594f4913ed290329142a77ec3e 100644 (file)
@@ -143,6 +143,83 @@ static void remove_sequencer_fcurves(Scene *sce)
        }
 }
 
+/* flag -- copying options (see BKE_library.h's LIB_ID_COPY_... flags for more). */
+ToolSettings *BKE_toolsettings_copy(ToolSettings *toolsettings, const int flag)
+{
+       if (toolsettings == NULL) {
+               return NULL;
+       }
+       ToolSettings *ts = MEM_dupallocN(toolsettings);
+       if (ts->vpaint) {
+               ts->vpaint = MEM_dupallocN(ts->vpaint);
+               BKE_paint_copy(&ts->vpaint->paint, &ts->vpaint->paint, flag);
+       }
+       if (ts->wpaint) {
+               ts->wpaint = MEM_dupallocN(ts->wpaint);
+               BKE_paint_copy(&ts->wpaint->paint, &ts->wpaint->paint, flag);
+       }
+       if (ts->sculpt) {
+               ts->sculpt = MEM_dupallocN(ts->sculpt);
+               BKE_paint_copy(&ts->sculpt->paint, &ts->sculpt->paint, flag);
+       }
+       if (ts->uvsculpt) {
+               ts->uvsculpt = MEM_dupallocN(ts->uvsculpt);
+               BKE_paint_copy(&ts->uvsculpt->paint, &ts->uvsculpt->paint, flag);
+       }
+
+       BKE_paint_copy(&ts->imapaint.paint, &ts->imapaint.paint, flag);
+       ts->imapaint.paintcursor = NULL;
+       ts->particle.paintcursor = NULL;
+       ts->particle.scene = NULL;
+       ts->particle.object = NULL;
+
+       /* duplicate Grease Pencil Drawing Brushes */
+       BLI_listbase_clear(&ts->gp_brushes);
+       for (bGPDbrush *brush = toolsettings->gp_brushes.first; brush; brush = brush->next) {
+               bGPDbrush *newbrush = BKE_gpencil_brush_duplicate(brush);
+               BLI_addtail(&ts->gp_brushes, newbrush);
+       }
+
+       /* duplicate Grease Pencil interpolation curve */
+       ts->gp_interpolate.custom_ipo = curvemapping_copy(ts->gp_interpolate.custom_ipo);
+       return ts;
+}
+
+void BKE_toolsettings_free(ToolSettings *toolsettings)
+{
+       if (toolsettings == NULL) {
+               return;
+       }
+       if (toolsettings->vpaint) {
+               BKE_paint_free(&toolsettings->vpaint->paint);
+               MEM_freeN(toolsettings->vpaint);
+       }
+       if (toolsettings->wpaint) {
+               BKE_paint_free(&toolsettings->wpaint->paint);
+               MEM_freeN(toolsettings->wpaint);
+       }
+       if (toolsettings->sculpt) {
+               BKE_paint_free(&toolsettings->sculpt->paint);
+               MEM_freeN(toolsettings->sculpt);
+       }
+       if (toolsettings->uvsculpt) {
+               BKE_paint_free(&toolsettings->uvsculpt->paint);
+               MEM_freeN(toolsettings->uvsculpt);
+       }
+       BKE_paint_free(&toolsettings->imapaint.paint);
+
+       /* free Grease Pencil Drawing Brushes */
+       BKE_gpencil_free_brushes(&toolsettings->gp_brushes);
+       BLI_freelistN(&toolsettings->gp_brushes);
+
+       /* free Grease Pencil interpolation curve */
+       if (toolsettings->gp_interpolate.custom_ipo) {
+               curvemapping_free(toolsettings->gp_interpolate.custom_ipo);
+       }
+
+       MEM_freeN(toolsettings);
+}
+
 /**
  * Only copy internal data of Scene ID from source to already allocated/initialized destination.
  * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs.
@@ -215,41 +292,7 @@ void BKE_scene_copy_data(Main *bmain, Scene *sce_dst, const Scene *sce_src, cons
        curvemapping_copy_data(&sce_dst->r.mblur_shutter_curve, &sce_src->r.mblur_shutter_curve);
 
        /* tool settings */
-       if (sce_dst->toolsettings != NULL) {
-               ToolSettings *ts = sce_dst->toolsettings = MEM_dupallocN(sce_dst->toolsettings);
-               if (ts->vpaint) {
-                       ts->vpaint = MEM_dupallocN(ts->vpaint);
-                       BKE_paint_copy(&ts->vpaint->paint, &ts->vpaint->paint, flag_subdata);
-               }
-               if (ts->wpaint) {
-                       ts->wpaint = MEM_dupallocN(ts->wpaint);
-                       BKE_paint_copy(&ts->wpaint->paint, &ts->wpaint->paint, flag_subdata);
-               }
-               if (ts->sculpt) {
-                       ts->sculpt = MEM_dupallocN(ts->sculpt);
-                       BKE_paint_copy(&ts->sculpt->paint, &ts->sculpt->paint, flag_subdata);
-               }
-               if (ts->uvsculpt) {
-                       ts->uvsculpt = MEM_dupallocN(ts->uvsculpt);
-                       BKE_paint_copy(&ts->uvsculpt->paint, &ts->uvsculpt->paint, flag_subdata);
-               }
-
-               BKE_paint_copy(&ts->imapaint.paint, &ts->imapaint.paint, flag_subdata);
-               ts->imapaint.paintcursor = NULL;
-               ts->particle.paintcursor = NULL;
-               ts->particle.scene = NULL;
-               ts->particle.object = NULL;
-
-               /* duplicate Grease Pencil Drawing Brushes */
-               BLI_listbase_clear(&ts->gp_brushes);
-               for (bGPDbrush *brush = sce_src->toolsettings->gp_brushes.first; brush; brush = brush->next) {
-                       bGPDbrush *newbrush = BKE_gpencil_brush_duplicate(brush);
-                       BLI_addtail(&ts->gp_brushes, newbrush);
-               }
-
-               /* duplicate Grease Pencil interpolation curve */
-               ts->gp_interpolate.custom_ipo = curvemapping_copy(ts->gp_interpolate.custom_ipo);
-       }
+       sce_dst->toolsettings = BKE_toolsettings_copy(sce_dst->toolsettings, flag_subdata);
 
        /* make a private copy of the avicodecdata */
        if (sce_src->r.avicodecdata) {
@@ -288,7 +331,6 @@ Scene *BKE_scene_copy(Main *bmain, Scene *sce, int type)
        /* TODO this should/could most likely be replaced by call to more generic code at some point...
         * But for now, let's keep it well isolated here. */
        if (type == SCE_COPY_EMPTY) {
-               ToolSettings *ts;
                ListBase rl, rv;
 
                sce_copy = BKE_scene_add(bmain, sce->id.name + 2);
@@ -325,46 +367,7 @@ Scene *BKE_scene_copy(Main *bmain, Scene *sce, int type)
                curvemapping_copy_data(&sce_copy->r.mblur_shutter_curve, &sce->r.mblur_shutter_curve);
 
                /* tool settings */
-               sce_copy->toolsettings = MEM_dupallocN(sce->toolsettings);
-
-               ts = sce_copy->toolsettings;
-               if (ts) {
-                       if (ts->vpaint) {
-                               ts->vpaint = MEM_dupallocN(ts->vpaint);
-                               BKE_paint_copy(&ts->vpaint->paint, &ts->vpaint->paint, 0);
-                       }
-                       if (ts->wpaint) {
-                               ts->wpaint = MEM_dupallocN(ts->wpaint);
-                               BKE_paint_copy(&ts->wpaint->paint, &ts->wpaint->paint, 0);
-                       }
-                       if (ts->sculpt) {
-                               ts->sculpt = MEM_dupallocN(ts->sculpt);
-                               BKE_paint_copy(&ts->sculpt->paint, &ts->sculpt->paint, 0);
-                       }
-                       if (ts->uvsculpt) {
-                               ts->uvsculpt = MEM_dupallocN(ts->uvsculpt);
-                               BKE_paint_copy(&ts->uvsculpt->paint, &ts->uvsculpt->paint, 0);
-                       }
-
-                       BKE_paint_copy(&ts->imapaint.paint, &ts->imapaint.paint, 0);
-                       ts->imapaint.paintcursor = NULL;
-                       id_us_plus((ID *)ts->imapaint.stencil);
-                       id_us_plus((ID *)ts->imapaint.clone);
-                       id_us_plus((ID *)ts->imapaint.canvas);
-                       ts->particle.paintcursor = NULL;
-                       ts->particle.scene = NULL;
-                       ts->particle.object = NULL;
-
-                       /* duplicate Grease Pencil Drawing Brushes */
-                       BLI_listbase_clear(&ts->gp_brushes);
-                       for (bGPDbrush *brush = sce->toolsettings->gp_brushes.first; brush; brush = brush->next) {
-                               bGPDbrush *newbrush = BKE_gpencil_brush_duplicate(brush);
-                               BLI_addtail(&ts->gp_brushes, newbrush);
-                       }
-
-                       /* duplicate Grease Pencil interpolation curve */
-                       ts->gp_interpolate.custom_ipo = curvemapping_copy(ts->gp_interpolate.custom_ipo);
-               }
+               sce_copy->toolsettings = BKE_toolsettings_copy(sce->toolsettings, 0);
 
                /* make a private copy of the avicodecdata */
                if (sce->r.avicodecdata) {
@@ -500,37 +503,8 @@ void BKE_scene_free(Scene *sce)
        BLI_freelistN(&sce->r.layers);
        BLI_freelistN(&sce->r.views);
        
-       if (sce->toolsettings) {
-               if (sce->toolsettings->vpaint) {
-                       BKE_paint_free(&sce->toolsettings->vpaint->paint);
-                       MEM_freeN(sce->toolsettings->vpaint);
-               }
-               if (sce->toolsettings->wpaint) {
-                       BKE_paint_free(&sce->toolsettings->wpaint->paint);
-                       MEM_freeN(sce->toolsettings->wpaint);
-               }
-               if (sce->toolsettings->sculpt) {
-                       BKE_paint_free(&sce->toolsettings->sculpt->paint);
-                       MEM_freeN(sce->toolsettings->sculpt);
-               }
-               if (sce->toolsettings->uvsculpt) {
-                       BKE_paint_free(&sce->toolsettings->uvsculpt->paint);
-                       MEM_freeN(sce->toolsettings->uvsculpt);
-               }
-               BKE_paint_free(&sce->toolsettings->imapaint.paint);
-               
-               /* free Grease Pencil Drawing Brushes */
-               BKE_gpencil_free_brushes(&sce->toolsettings->gp_brushes);
-               BLI_freelistN(&sce->toolsettings->gp_brushes);
-               
-               /* free Grease Pencil interpolation curve */
-               if (sce->toolsettings->gp_interpolate.custom_ipo) {
-                       curvemapping_free(sce->toolsettings->gp_interpolate.custom_ipo);
-               }
-               
-               MEM_freeN(sce->toolsettings);
-               sce->toolsettings = NULL;
-       }
+       BKE_toolsettings_free(sce->toolsettings);
+       sce->toolsettings = NULL;
        
        DAG_scene_free(sce);
        if (sce->depsgraph)