Blender  V3.3
gpencil_ops_versioning.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2018 Blender Foundation. */
3 
9 /* Allow using deprecated functionality. */
10 #define DNA_DEPRECATED_ALLOW
11 
12 #include <stdio.h>
13 
14 #include "MEM_guardedalloc.h"
15 
16 #include "BLI_listbase.h"
17 #include "BLI_math.h"
18 
19 #include "DNA_gpencil_types.h"
20 #include "DNA_material_types.h"
21 #include "DNA_object_types.h"
22 #include "DNA_scene_types.h"
23 
24 #include "BKE_context.h"
25 #include "BKE_gpencil.h"
26 #include "BKE_main.h"
27 #include "BKE_object.h"
28 
29 #include "WM_api.h"
30 #include "WM_types.h"
31 
32 #include "RNA_access.h"
33 #include "RNA_define.h"
34 
35 #include "ED_gpencil.h"
36 
37 #include "DEG_depsgraph.h"
38 #include "DEG_depsgraph_query.h"
39 
40 #include "gpencil_intern.h"
41 
42 /* Free all of a gp-colors */
43 static void free_gpencil_colors(bGPDpalette *palette)
44 {
45  /* error checking */
46  if (palette == NULL) {
47  return;
48  }
49 
50  /* free colors */
51  BLI_freelistN(&palette->colors);
52 }
53 
54 /* Free all of the gp-palettes and colors */
55 static void free_palettes(ListBase *list)
56 {
57  bGPDpalette *palette_next;
58 
59  /* error checking */
60  if (list == NULL) {
61  return;
62  }
63 
64  /* delete palettes */
65  for (bGPDpalette *palette = list->first; palette; palette = palette_next) {
66  palette_next = palette->next;
67  /* free palette colors */
68  free_gpencil_colors(palette);
69 
70  MEM_freeN(palette);
71  }
72  BLI_listbase_clear(list);
73 }
74 
75 /* ***************** Convert old 2.7 files to 2.8 ************************ */
77 {
79 
80  return (int)(scene->gpd != NULL);
81 }
82 
84 {
85  Main *bmain = CTX_data_main(C);
87  ViewLayer *view_layer = CTX_data_view_layer(C);
88  const bool is_annotation = RNA_boolean_get(op->ptr, "annotation");
89  bGPdata *gpd = scene->gpd;
90 
91  /* Convert grease pencil scene datablock to GP object */
92  if ((!is_annotation) && (view_layer != NULL)) {
93  Object *ob;
95  bmain, view_layer, OB_GPENCIL, "GP_Scene", &scene->gpd->id, false);
96  zero_v3(ob->loc);
97  DEG_relations_tag_update(bmain); /* added object */
98 
99  /* convert grease pencil palettes (version >= 2.78) to materials and weights */
100  LISTBASE_FOREACH (const bGPDpalette *, palette, &gpd->palettes) {
101  LISTBASE_FOREACH (bGPDpalettecolor *, palcolor, &palette->colors) {
102 
103  /* create material slot */
104  Material *ma = BKE_gpencil_object_material_new(bmain, ob, palcolor->info, NULL);
105 
106  /* copy color settings */
107  MaterialGPencilStyle *gp_style = ma->gp_style;
108  copy_v4_v4(gp_style->stroke_rgba, palcolor->color);
109  copy_v4_v4(gp_style->fill_rgba, palcolor->fill);
110 
111  /* set basic settings */
112  gp_style->gradient_radius = 0.5f;
113  ARRAY_SET_ITEMS(gp_style->mix_rgba, 1.0f, 1.0f, 1.0f, 0.2f);
114  ARRAY_SET_ITEMS(gp_style->gradient_scale, 1.0f, 1.0f);
115  ARRAY_SET_ITEMS(gp_style->texture_scale, 1.0f, 1.0f);
116  gp_style->texture_pixsize = 100.0f;
117 
118  gp_style->flag |= GP_MATERIAL_STROKE_SHOW;
119  gp_style->flag |= GP_MATERIAL_FILL_SHOW;
120 
121  /* fix strokes */
122  LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) {
123  LISTBASE_FOREACH (bGPDframe *, gpf, &gpl->frames) {
124  LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) {
125  if ((gps->colorname[0] != '\0') && (STREQ(gps->colorname, palcolor->info))) {
126  gps->mat_nr = ob->totcol - 1;
127  gps->colorname[0] = '\0';
128  /* weights array */
129  gps->dvert = NULL;
130  }
131  }
132  }
133  }
134  }
135  }
136 
137  /* free palettes */
138  free_palettes(&gpd->palettes);
139 
140  /* disable all GP modes */
141  ED_gpencil_setup_modes(C, gpd, 0);
142 
143  /* set cache as dirty */
145 
146  scene->gpd = NULL;
147  }
148 
149  if (is_annotation) {
150  LISTBASE_FOREACH (const bGPDpalette *, palette, &gpd->palettes) {
151  LISTBASE_FOREACH (bGPDpalettecolor *, palcolor, &palette->colors) {
152  /* fix layers */
153  LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) {
154  /* unlock/unhide layer */
155  gpl->flag &= ~GP_LAYER_LOCKED;
156  gpl->flag &= ~GP_LAYER_HIDE;
157  /* set opacity to 1 */
158  gpl->opacity = 1.0f;
159  /* disable tint */
160  gpl->tintcolor[3] = 0.0f;
161  LISTBASE_FOREACH (bGPDframe *, gpf, &gpl->frames) {
162  LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) {
163  if ((gps->colorname[0] != '\0') && (STREQ(gps->colorname, palcolor->info))) {
164  /* copy color settings */
165  copy_v4_v4(gpl->color, palcolor->color);
166  }
167  }
168  }
169  }
170  }
171  }
172  }
173 
174  /* notifiers */
176 
177  return OPERATOR_FINISHED;
178 }
179 
181 {
182  /* identifiers */
183  ot->name = "Convert Grease Pencil";
184  ot->idname = "GPENCIL_OT_convert_old_files";
185  ot->description = "Convert 2.7x grease pencil files to 2.80";
186 
187  /* callbacks */
190 
191  /* flags */
193 
194  /* props */
195  ot->prop = RNA_def_boolean(ot->srna, "annotation", 0, "Annotation", "Convert to Annotations");
196 }
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1090
struct ViewLayer * CTX_data_view_layer(const bContext *C)
Definition: context.c:1100
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1074
struct Material * BKE_gpencil_object_material_new(struct Main *bmain, struct Object *ob, const char *name, int *r_index)
Definition: gpencil.c:1734
void BKE_gpencil_batch_cache_dirty_tag(struct bGPdata *gpd)
Definition: gpencil.c:335
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.cc:2299
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:336
BLI_INLINE void BLI_listbase_clear(struct ListBase *lb)
Definition: BLI_listbase.h:273
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:466
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:25
@ OPTYPE_UNDO
Definition: WM_types.h:148
@ OPTYPE_REGISTER
Definition: WM_types.h:146
#define ND_DATA
Definition: WM_types.h:456
#define NA_EDITED
Definition: WM_types.h:523
#define NC_GPENCIL
Definition: WM_types.h:349
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:27
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:4863
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, bool default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3493
void * first
Definition: DNA_listBase.h:31
Definition: BKE_main.h:121
struct MaterialGPencilStyle * gp_style
float loc[3]
void * data
struct bGPdata * gpd
struct bGPDpalette * next
ListBase layers
const char * name
Definition: WM_types.h:888
const char * idname
Definition: WM_types.h:890
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:943
struct StructRNA * srna
Definition: WM_types.h:969
const char * description
Definition: WM_types.h:893
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:903
PropertyRNA * prop
Definition: WM_types.h:981
struct PointerRNA * ptr
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmOperatorType * ot
Definition: wm_files.c:3479