doxygen: add newline after \file
[blender.git] / source / blender / editors / gpencil / gpencil_old.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2018, Blender Foundation,
17  * This is a new part of Blender
18  * Use deprecated data to convert old 2.7x files
19  */
20
21 /** \file
22  * \ingroup edgpencil
23  */
24
25  /* allow to use deprecated functionality */
26 #define DNA_DEPRECATED_ALLOW
27
28 #include <stdio.h>
29
30 #include "MEM_guardedalloc.h"
31
32 #include "BLI_listbase.h"
33 #include "BLI_math.h"
34
35 #include "DNA_gpencil_types.h"
36 #include "DNA_meshdata_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_scene_types.h"
39
40 #include "BKE_main.h"
41 #include "BKE_brush.h"
42 #include "BKE_context.h"
43 #include "BKE_deform.h"
44 #include "BKE_gpencil.h"
45 #include "BKE_object.h"
46 #include "BKE_material.h"
47
48 #include "WM_api.h"
49 #include "WM_types.h"
50
51 #include "ED_object.h"
52 #include "ED_gpencil.h"
53
54 #include "gpencil_intern.h"
55
56  /* Free all of a gp-colors */
57 static void free_gpencil_colors(bGPDpalette *palette)
58 {
59         /* error checking */
60         if (palette == NULL) {
61                 return;
62         }
63
64         /* free colors */
65         BLI_freelistN(&palette->colors);
66 }
67
68 /* Free all of the gp-palettes and colors */
69 static void free_palettes(ListBase *list)
70 {
71         bGPDpalette *palette_next;
72
73         /* error checking */
74         if (list == NULL) {
75                 return;
76         }
77
78         /* delete palettes */
79         for (bGPDpalette *palette = list->first; palette; palette = palette_next) {
80                 palette_next = palette->next;
81                 /* free palette colors */
82                 free_gpencil_colors(palette);
83
84                 MEM_freeN(palette);
85         }
86         BLI_listbase_clear(list);
87 }
88
89 /* ***************** Convert old 2.7 files to 2.8 ************************ */
90 static bool gpencil_convert_old_files_poll(bContext *C)
91 {
92         Scene *scene = CTX_data_scene(C);
93
94         return (int) (scene->gpd != NULL);
95 }
96
97 static int gpencil_convert_old_files_exec(bContext *C, wmOperator *UNUSED(op))
98 {
99         Main *bmain = CTX_data_main(C);
100         Scene *scene = CTX_data_scene(C);
101         ViewLayer *view_layer = CTX_data_view_layer(C);
102
103         /* Convert grease pencil scene datablock to GP object */
104         if ((scene->gpd) && (view_layer != NULL)) {
105                 Object *ob;
106                 ob = BKE_object_add_for_data(bmain, view_layer, OB_GPENCIL, "GP_Scene", &scene->gpd->id, false);
107                 zero_v3(ob->loc);
108
109                 /* convert grease pencil palettes (version >= 2.78)  to materials and weights */
110                 bGPdata *gpd = scene->gpd;
111                 for (const bGPDpalette *palette = gpd->palettes.first; palette; palette = palette->next) {
112                         for (bGPDpalettecolor *palcolor = palette->colors.first; palcolor; palcolor = palcolor->next) {
113
114                                 /* create material slot */
115                                 BKE_object_material_slot_add(bmain, ob);
116                                 Material *ma = BKE_material_add_gpencil(bmain, palcolor->info);
117                                 assign_material(bmain, ob, ma, ob->totcol, BKE_MAT_ASSIGN_USERPREF);
118
119                                 /* copy color settings */
120                                 MaterialGPencilStyle *gp_style = ma->gp_style;
121                                 copy_v4_v4(gp_style->stroke_rgba, palcolor->color);
122                                 copy_v4_v4(gp_style->fill_rgba, palcolor->fill);
123
124                                 /* set basic settings */
125                                 gp_style->pattern_gridsize = 0.1f;
126                                 gp_style->gradient_radius = 0.5f;
127                                 ARRAY_SET_ITEMS(gp_style->mix_rgba, 1.0f, 1.0f, 1.0f, 0.2f);
128                                 ARRAY_SET_ITEMS(gp_style->gradient_scale, 1.0f, 1.0f);
129                                 ARRAY_SET_ITEMS(gp_style->texture_scale, 1.0f, 1.0f);
130                                 gp_style->texture_opacity = 1.0f;
131                                 gp_style->texture_pixsize = 100.0f;
132
133                                 gp_style->flag |= GP_STYLE_STROKE_SHOW;
134                                 gp_style->flag |= GP_STYLE_FILL_SHOW;
135
136                                 /* fix strokes */
137                                 for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
138                                         for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf->next) {
139                                                 for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
140                                                         if ((gps->colorname[0] != '\0') &&
141                                                             (STREQ(gps->colorname, palcolor->info)))
142                                                         {
143                                                                 gps->mat_nr = ob->totcol - 1;
144                                                                 gps->colorname[0] = '\0';
145                                                                 /* weights array */
146                                                                 gps->dvert = NULL;
147                                                         }
148                                                 }
149                                         }
150                                 }
151                         }
152                 }
153
154                 /* free palettes */
155                 free_palettes(&gpd->palettes);
156
157                 /* disable all GP modes */
158                 ED_gpencil_setup_modes(C, gpd, 0);
159
160                 /* set cache as dirty */
161                 BKE_gpencil_batch_cache_dirty_tag(ob->data);
162
163                 scene->gpd = NULL;
164         }
165
166 #if 0 /* GPXX */
167         /* Handle object-linked grease pencil datablocks */
168         for (Object *ob = bmain->object.first; ob; ob = ob->id.next) {
169                 if (ob->gpd) {
170                         if (ob->type == OB_GPENCIL) {
171                                 /* GP Object - remap the links */
172                                 ob->data = ob->gpd;
173                                 ob->gpd = NULL;
174                         }
175                         else if (ob->type == OB_EMPTY) {
176                                 /* Empty with GP data - This should be able to be converted
177                                  * to a GP object with little data loss
178                                  */
179                                 ob->data = ob->gpd;
180                                 ob->gpd = NULL;
181                                 ob->type = OB_GPENCIL;
182                         }
183                         else {
184                                 /* FIXME: What to do in this case?
185                                  *
186                                  * We cannot create new objects for these, as we don't have a scene & scene layer
187                                  * to put them into from here...
188                                  */
189                                 printf("WARNING: Old Grease Pencil data ('%s') still exists on Object '%s'\n",
190                                         ob->gpd->id.name + 2, ob->id.name + 2);
191                         }
192                 }
193         }
194 #endif
195
196         /* notifiers */
197         WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
198
199         return OPERATOR_FINISHED;
200 }
201
202 void GPENCIL_OT_convert_old_files(wmOperatorType *ot)
203 {
204         /* identifiers */
205         ot->name = "Convert 2.7 Grease Pencil File";
206         ot->idname = "GPENCIL_OT_convert_old_files";
207         ot->description = "Convert 2.7x grease pencil files to 2.8";
208
209         /* callbacks */
210         ot->exec = gpencil_convert_old_files_exec;
211         ot->poll = gpencil_convert_old_files_poll;
212
213         /* flags */
214         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
215 }