Blender  V2.93
gpencil_ops_versioning.c
Go to the documentation of this file.
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 
25 /* Allow using 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_material_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_scene_types.h"
39 
40 #include "BKE_context.h"
41 #include "BKE_gpencil.h"
42 #include "BKE_main.h"
43 #include "BKE_object.h"
44 
45 #include "WM_api.h"
46 #include "WM_types.h"
47 
48 #include "RNA_access.h"
49 #include "RNA_define.h"
50 
51 #include "ED_gpencil.h"
52 
53 #include "DEG_depsgraph.h"
54 #include "DEG_depsgraph_query.h"
55 
56 #include "gpencil_intern.h"
57 
58 /* Free all of a gp-colors */
59 static void free_gpencil_colors(bGPDpalette *palette)
60 {
61  /* error checking */
62  if (palette == NULL) {
63  return;
64  }
65 
66  /* free colors */
67  BLI_freelistN(&palette->colors);
68 }
69 
70 /* Free all of the gp-palettes and colors */
71 static void free_palettes(ListBase *list)
72 {
73  bGPDpalette *palette_next;
74 
75  /* error checking */
76  if (list == NULL) {
77  return;
78  }
79 
80  /* delete palettes */
81  for (bGPDpalette *palette = list->first; palette; palette = palette_next) {
82  palette_next = palette->next;
83  /* free palette colors */
84  free_gpencil_colors(palette);
85 
86  MEM_freeN(palette);
87  }
88  BLI_listbase_clear(list);
89 }
90 
91 /* ***************** Convert old 2.7 files to 2.8 ************************ */
93 {
95 
96  return (int)(scene->gpd != NULL);
97 }
98 
100 {
101  Main *bmain = CTX_data_main(C);
103  ViewLayer *view_layer = CTX_data_view_layer(C);
104  const bool is_annotation = RNA_boolean_get(op->ptr, "annotation");
105  bGPdata *gpd = scene->gpd;
106 
107  /* Convert grease pencil scene datablock to GP object */
108  if ((!is_annotation) && (view_layer != NULL)) {
109  Object *ob;
111  bmain, view_layer, OB_GPENCIL, "GP_Scene", &scene->gpd->id, false);
112  zero_v3(ob->loc);
113  DEG_relations_tag_update(bmain); /* added object */
114 
115  /* convert grease pencil palettes (version >= 2.78) to materials and weights */
116  LISTBASE_FOREACH (const bGPDpalette *, palette, &gpd->palettes) {
117  LISTBASE_FOREACH (bGPDpalettecolor *, palcolor, &palette->colors) {
118 
119  /* create material slot */
120  Material *ma = BKE_gpencil_object_material_new(bmain, ob, palcolor->info, NULL);
121 
122  /* copy color settings */
123  MaterialGPencilStyle *gp_style = ma->gp_style;
124  copy_v4_v4(gp_style->stroke_rgba, palcolor->color);
125  copy_v4_v4(gp_style->fill_rgba, palcolor->fill);
126 
127  /* set basic settings */
128  gp_style->gradient_radius = 0.5f;
129  ARRAY_SET_ITEMS(gp_style->mix_rgba, 1.0f, 1.0f, 1.0f, 0.2f);
130  ARRAY_SET_ITEMS(gp_style->gradient_scale, 1.0f, 1.0f);
131  ARRAY_SET_ITEMS(gp_style->texture_scale, 1.0f, 1.0f);
132  gp_style->texture_pixsize = 100.0f;
133 
134  gp_style->flag |= GP_MATERIAL_STROKE_SHOW;
135  gp_style->flag |= GP_MATERIAL_FILL_SHOW;
136 
137  /* fix strokes */
138  LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) {
139  LISTBASE_FOREACH (bGPDframe *, gpf, &gpl->frames) {
140  LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) {
141  if ((gps->colorname[0] != '\0') && (STREQ(gps->colorname, palcolor->info))) {
142  gps->mat_nr = ob->totcol - 1;
143  gps->colorname[0] = '\0';
144  /* weights array */
145  gps->dvert = NULL;
146  }
147  }
148  }
149  }
150  }
151  }
152 
153  /* free palettes */
154  free_palettes(&gpd->palettes);
155 
156  /* disable all GP modes */
157  ED_gpencil_setup_modes(C, gpd, 0);
158 
159  /* set cache as dirty */
161 
162  scene->gpd = NULL;
163  }
164 
165  if (is_annotation) {
166  LISTBASE_FOREACH (const bGPDpalette *, palette, &gpd->palettes) {
167  LISTBASE_FOREACH (bGPDpalettecolor *, palcolor, &palette->colors) {
168  /* fix layers */
169  LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) {
170  /* unlock/unhide layer */
171  gpl->flag &= ~GP_LAYER_LOCKED;
172  gpl->flag &= ~GP_LAYER_HIDE;
173  /* set opacity to 1 */
174  gpl->opacity = 1.0f;
175  /* disable tint */
176  gpl->tintcolor[3] = 0.0f;
177  LISTBASE_FOREACH (bGPDframe *, gpf, &gpl->frames) {
178  LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) {
179  if ((gps->colorname[0] != '\0') && (STREQ(gps->colorname, palcolor->info))) {
180  /* copy color settings */
181  copy_v4_v4(gpl->color, palcolor->color);
182  }
183  }
184  }
185  }
186  }
187  }
188  }
189 
190  /* notifiers */
192 
193  return OPERATOR_FINISHED;
194 }
195 
197 {
198  /* identifiers */
199  ot->name = "Convert Grease Pencil";
200  ot->idname = "GPENCIL_OT_convert_old_files";
201  ot->description = "Convert 2.7x grease pencil files to 2.80";
202 
203  /* callbacks */
206 
207  /* flags */
209 
210  /* props */
211  ot->prop = RNA_def_boolean(ot->srna, "annotation", 0, "Annotation", "Convert to Annotations");
212 }
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1034
struct ViewLayer * CTX_data_view_layer(const bContext *C)
Definition: context.c:1044
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1018
struct Material * BKE_gpencil_object_material_new(struct Main *bmain, struct Object *ob, const char *name, int *r_index)
Definition: gpencil.c:1873
void BKE_gpencil_batch_cache_dirty_tag(struct bGPdata *gpd)
Definition: gpencil.c:343
General operations, lookup, etc. for blender objects.
struct Object * BKE_object_add_for_data(struct Main *bmain, struct ViewLayer *view_layer, int type, const char *name, struct ID *data, bool do_id_user) ATTR_RETURNS_NONNULL
Definition: object.c:2269
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
BLI_INLINE void BLI_listbase_clear(struct ListBase *lb)
Definition: BLI_listbase.h:128
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:547
MINLINE void copy_v4_v4(float r[4], const float a[4])
MINLINE void zero_v3(float r[3])
#define ARRAY_SET_ITEMS(...)
#define STREQ(a, b)
void DEG_relations_tag_update(struct Main *bmain)
@ GP_LAYER_LOCKED
@ GP_LAYER_HIDE
@ GP_MATERIAL_STROKE_SHOW
@ GP_MATERIAL_FILL_SHOW
Object is a sort of wrapper for general info.
@ OB_GPENCIL
@ OPERATOR_FINISHED
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:39
@ OPTYPE_UNDO
Definition: WM_types.h:155
@ OPTYPE_REGISTER
Definition: WM_types.h:153
#define ND_DATA
Definition: WM_types.h:408
#define NA_EDITED
Definition: WM_types.h:462
#define NC_GPENCIL
Definition: WM_types.h:300
Scene scene
static void free_gpencil_colors(bGPDpalette *palette)
static int gpencil_convert_old_files_exec(bContext *C, wmOperator *op)
static void free_palettes(ListBase *list)
void GPENCIL_OT_convert_old_files(wmOperatorType *ot)
static bool gpencil_convert_old_files_poll(bContext *C)
void ED_gpencil_setup_modes(bContext *C, bGPdata *gpd, int newmode)
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6261
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, bool default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3481
void * first
Definition: DNA_listBase.h:47
Definition: BKE_main.h:116
struct MaterialGPencilStyle * gp_style
float loc[3]
void * data
struct bGPdata * gpd
struct bGPDpalette * next
ListBase layers
const char * name
Definition: WM_types.h:721
const char * idname
Definition: WM_types.h:723
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:776
struct StructRNA * srna
Definition: WM_types.h:802
const char * description
Definition: WM_types.h:726
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:736
PropertyRNA * prop
Definition: WM_types.h:814
struct PointerRNA * ptr
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmOperatorType * ot
Definition: wm_files.c:3156