diff options
Diffstat (limited to 'source/blender/blenkernel/intern/gpencil.c')
-rw-r--r-- | source/blender/blenkernel/intern/gpencil.c | 164 |
1 files changed, 160 insertions, 4 deletions
diff --git a/source/blender/blenkernel/intern/gpencil.c b/source/blender/blenkernel/intern/gpencil.c index 1e37ae3892b..03ac7e622e1 100644 --- a/source/blender/blenkernel/intern/gpencil.c +++ b/source/blender/blenkernel/intern/gpencil.c @@ -40,12 +40,16 @@ #include "IMB_imbuf.h" #include "IMB_imbuf_types.h" +/* Allow using deprecated functionality for .blend file I/O. */ +#define DNA_DEPRECATED_ALLOW + #include "DNA_gpencil_types.h" #include "DNA_material_types.h" #include "DNA_meshdata_types.h" #include "DNA_space_types.h" #include "BKE_action.h" +#include "BKE_anim_data.h" #include "BKE_collection.h" #include "BKE_colortools.h" #include "BKE_deform.h" @@ -64,6 +68,10 @@ #include "DEG_depsgraph_query.h" +#include "BLO_read_write.h" + +#include "BKE_gpencil.h" + static CLG_LogRef LOG = {"bke.gpencil"}; static void greasepencil_copy_data(Main *UNUSED(bmain), @@ -111,6 +119,154 @@ static void greasepencil_foreach_id(ID *id, LibraryForeachIDData *data) } } +static void greasepencil_blend_write(BlendWriter *writer, ID *id, const void *id_address) +{ + bGPdata *gpd = (bGPdata *)id; + if (gpd->id.us > 0 || BLO_write_is_undo(writer)) { + /* Clean up, important in undo case to reduce false detection of changed data-blocks. */ + /* XXX not sure why the whole run-time data is not cleared in reading code, + * for now mimicking it here. */ + gpd->runtime.sbuffer = NULL; + gpd->runtime.sbuffer_used = 0; + gpd->runtime.sbuffer_size = 0; + gpd->runtime.tot_cp_points = 0; + + /* write gpd data block to file */ + BLO_write_id_struct(writer, bGPdata, id_address, &gpd->id); + BKE_id_blend_write(writer, &gpd->id); + + if (gpd->adt) { + BKE_animdata_blend_write(writer, gpd->adt); + } + + BLO_write_pointer_array(writer, gpd->totcol, gpd->mat); + + /* write grease-pencil layers to file */ + BLO_write_struct_list(writer, bGPDlayer, &gpd->layers); + LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) { + /* Write mask list. */ + BLO_write_struct_list(writer, bGPDlayer_Mask, &gpl->mask_layers); + /* write this layer's frames to file */ + BLO_write_struct_list(writer, bGPDframe, &gpl->frames); + LISTBASE_FOREACH (bGPDframe *, gpf, &gpl->frames) { + /* write strokes */ + BLO_write_struct_list(writer, bGPDstroke, &gpf->strokes); + LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) { + BLO_write_struct_array(writer, bGPDspoint, gps->totpoints, gps->points); + BLO_write_struct_array(writer, bGPDtriangle, gps->tot_triangles, gps->triangles); + BKE_defvert_blend_write(writer, gps->totpoints, gps->dvert); + } + } + } + } +} + +void BKE_gpencil_blend_read_data(BlendDataReader *reader, bGPdata *gpd) +{ + /* we must firstly have some grease-pencil data to link! */ + if (gpd == NULL) { + return; + } + + /* relink animdata */ + BLO_read_data_address(reader, &gpd->adt); + BKE_animdata_blend_read_data(reader, gpd->adt); + + /* Ensure full objectmode for linked grease pencil. */ + if (gpd->id.lib != NULL) { + gpd->flag &= ~GP_DATA_STROKE_PAINTMODE; + gpd->flag &= ~GP_DATA_STROKE_EDITMODE; + gpd->flag &= ~GP_DATA_STROKE_SCULPTMODE; + gpd->flag &= ~GP_DATA_STROKE_WEIGHTMODE; + gpd->flag &= ~GP_DATA_STROKE_VERTEXMODE; + } + + /* init stroke buffer */ + gpd->runtime.sbuffer = NULL; + gpd->runtime.sbuffer_used = 0; + gpd->runtime.sbuffer_size = 0; + gpd->runtime.tot_cp_points = 0; + + /* relink palettes (old palettes deprecated, only to convert old files) */ + BLO_read_list(reader, &gpd->palettes); + if (gpd->palettes.first != NULL) { + LISTBASE_FOREACH (Palette *, palette, &gpd->palettes) { + BLO_read_list(reader, &palette->colors); + } + } + + /* materials */ + BLO_read_pointer_array(reader, (void **)&gpd->mat); + + /* relink layers */ + BLO_read_list(reader, &gpd->layers); + + LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) { + /* relink frames */ + BLO_read_list(reader, &gpl->frames); + + BLO_read_data_address(reader, &gpl->actframe); + + gpl->runtime.icon_id = 0; + + /* Relink masks. */ + BLO_read_list(reader, &gpl->mask_layers); + + LISTBASE_FOREACH (bGPDframe *, gpf, &gpl->frames) { + /* relink strokes (and their points) */ + BLO_read_list(reader, &gpf->strokes); + + LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) { + /* relink stroke points array */ + BLO_read_data_address(reader, &gps->points); + /* Relink geometry*/ + BLO_read_data_address(reader, &gps->triangles); + + /* relink weight data */ + if (gps->dvert) { + BLO_read_data_address(reader, &gps->dvert); + BKE_defvert_blend_read(reader, gps->totpoints, gps->dvert); + } + } + } + } +} + +static void greasepencil_blend_read_data(BlendDataReader *reader, ID *id) +{ + bGPdata *gpd = (bGPdata *)id; + BKE_gpencil_blend_read_data(reader, gpd); +} + +static void greasepencil_blend_read_lib(BlendLibReader *reader, ID *id) +{ + bGPdata *gpd = (bGPdata *)id; + + /* Relink all data-lock linked by GP data-lock */ + /* Layers */ + LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) { + /* Layer -> Parent References */ + BLO_read_id_address(reader, gpd->id.lib, &gpl->parent); + } + + /* materials */ + for (int a = 0; a < gpd->totcol; a++) { + BLO_read_id_address(reader, gpd->id.lib, &gpd->mat[a]); + } +} + +static void greasepencil_blend_read_expand(BlendExpander *expander, ID *id) +{ + bGPdata *gpd = (bGPdata *)id; + LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) { + BLO_expand(expander, gpl->parent); + } + + for (int a = 0; a < gpd->totcol; a++) { + BLO_expand(expander, gpd->mat[a]); + } +} + IDTypeInfo IDType_ID_GD = { .id_code = ID_GD, .id_filter = FILTER_ID_GD, @@ -128,10 +284,10 @@ IDTypeInfo IDType_ID_GD = { .foreach_id = greasepencil_foreach_id, .foreach_cache = NULL, - .blend_write = NULL, - .blend_read_data = NULL, - .blend_read_lib = NULL, - .blend_read_expand = NULL, + .blend_write = greasepencil_blend_write, + .blend_read_data = greasepencil_blend_read_data, + .blend_read_lib = greasepencil_blend_read_lib, + .blend_read_expand = greasepencil_blend_read_expand, }; /* ************************************************** */ |