Blender  V3.3
freestyle.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2013 Blender Foundation. All rights reserved. */
3 
8 #include "MEM_guardedalloc.h"
9 
10 #include "DNA_collection_types.h"
11 #include "DNA_freestyle_types.h"
12 
13 #include "BLI_blenlib.h"
14 #include "BLI_math.h"
15 #include "BLI_string_utils.h"
16 
17 #include "BKE_freestyle.h"
18 #include "BKE_lib_id.h"
19 #include "BKE_linestyle.h"
20 
21 /* Function declarations. */
22 static FreestyleLineSet *alloc_lineset(void);
23 static void copy_lineset(FreestyleLineSet *new_lineset, FreestyleLineSet *lineset, const int flag);
26 
28 {
30 
31  BLI_listbase_clear(&config->modules);
32  config->flags = 0;
33  config->sphere_radius = 0.1f;
34  config->dkr_epsilon = 0.0f;
35  config->crease_angle = DEG2RADF(134.43f);
36 
37  BLI_listbase_clear(&config->linesets);
38 }
39 
40 void BKE_freestyle_config_free(FreestyleConfig *config, const bool do_id_user)
41 {
42  FreestyleLineSet *lineset;
43 
44  for (lineset = (FreestyleLineSet *)config->linesets.first; lineset; lineset = lineset->next) {
45  if (lineset->group) {
46  if (do_id_user) {
47  id_us_min(&lineset->group->id);
48  }
49  lineset->group = NULL;
50  }
51  if (lineset->linestyle) {
52  if (do_id_user) {
53  id_us_min(&lineset->linestyle->id);
54  }
55  lineset->linestyle = NULL;
56  }
57  }
58  BLI_freelistN(&config->linesets);
59  BLI_freelistN(&config->modules);
60 }
61 
63  const FreestyleConfig *config,
64  const int flag)
65 {
66  FreestyleLineSet *lineset, *new_lineset;
67  FreestyleModuleConfig *module, *new_module;
68 
69  new_config->mode = config->mode;
70  new_config->flags = config->flags;
71  new_config->sphere_radius = config->sphere_radius;
72  new_config->dkr_epsilon = config->dkr_epsilon;
73  new_config->crease_angle = config->crease_angle;
74 
75  BLI_listbase_clear(&new_config->linesets);
76  for (lineset = (FreestyleLineSet *)config->linesets.first; lineset; lineset = lineset->next) {
77  new_lineset = alloc_lineset();
78  copy_lineset(new_lineset, lineset, flag);
79  BLI_addtail(&new_config->linesets, (void *)new_lineset);
80  }
81 
82  BLI_listbase_clear(&new_config->modules);
83  for (module = (FreestyleModuleConfig *)config->modules.first; module; module = module->next) {
84  new_module = alloc_module();
85  copy_module(new_module, module);
86  BLI_addtail(&new_config->modules, (void *)new_module);
87  }
88 }
89 
90 static void copy_lineset(FreestyleLineSet *new_lineset, FreestyleLineSet *lineset, const int flag)
91 {
92  new_lineset->linestyle = lineset->linestyle;
93  new_lineset->flags = lineset->flags;
94  new_lineset->selection = lineset->selection;
95  new_lineset->qi = lineset->qi;
96  new_lineset->qi_start = lineset->qi_start;
97  new_lineset->qi_end = lineset->qi_end;
98  new_lineset->edge_types = lineset->edge_types;
99  new_lineset->exclude_edge_types = lineset->exclude_edge_types;
100  new_lineset->group = lineset->group;
101  strcpy(new_lineset->name, lineset->name);
102 
103  if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
104  id_us_plus((ID *)new_lineset->linestyle);
105  id_us_plus((ID *)new_lineset->group);
106  }
107 }
108 
110 {
112  "style module configuration");
113 }
114 
116 {
117  FreestyleModuleConfig *module_conf = alloc_module();
118  BLI_addtail(&config->modules, (void *)module_conf);
119  module_conf->script = NULL;
120  module_conf->is_displayed = 1;
121  return module_conf;
122 }
123 
125 {
126  new_module->script = module->script;
127  new_module->is_displayed = module->is_displayed;
128 }
129 
131 {
132  if (BLI_findindex(&config->modules, module_conf) == -1) {
133  return false;
134  }
135  BLI_freelinkN(&config->modules, module_conf);
136  return true;
137 }
138 
140  FreestyleModuleConfig *module_conf,
141  int direction)
142 {
143  return ((BLI_findindex(&config->modules, module_conf) > -1) &&
144  (BLI_listbase_link_move(&config->modules, module_conf, direction) == true));
145 }
146 
148 {
149  BLI_uniquename(&config->linesets,
150  lineset,
151  "FreestyleLineSet",
152  '.',
153  offsetof(FreestyleLineSet, name),
154  sizeof(lineset->name));
155 }
156 
158 {
159  return (FreestyleLineSet *)MEM_callocN(sizeof(FreestyleLineSet), "Freestyle line set");
160 }
161 
163  FreestyleConfig *config,
164  const char *name)
165 {
166  int lineset_index = BLI_listbase_count(&config->linesets);
167 
168  FreestyleLineSet *lineset = alloc_lineset();
169  BLI_addtail(&config->linesets, (void *)lineset);
170  BKE_freestyle_lineset_set_active_index(config, lineset_index);
171 
172  lineset->linestyle = BKE_linestyle_new(bmain, "LineStyle");
173  lineset->flags |= FREESTYLE_LINESET_ENABLED;
176  lineset->qi = FREESTYLE_QI_VISIBLE;
177  lineset->qi_start = 0;
178  lineset->qi_end = 100;
180  lineset->exclude_edge_types = 0;
181  lineset->group = NULL;
182  if (name) {
183  BLI_strncpy(lineset->name, name, sizeof(lineset->name));
184  }
185  else if (lineset_index > 0) {
186  sprintf(lineset->name, "LineSet %i", lineset_index + 1);
187  }
188  else {
189  strcpy(lineset->name, "LineSet");
190  }
191  BKE_freestyle_lineset_unique_name(config, lineset);
192 
193  return lineset;
194 }
195 
197 {
198  if (BLI_findindex(&config->linesets, lineset) == -1) {
199  return false;
200  }
201  if (lineset->group) {
202  id_us_min(&lineset->group->id);
203  }
204  if (lineset->linestyle) {
205  id_us_min(&lineset->linestyle->id);
206  }
207  BLI_remlink(&config->linesets, lineset);
208  MEM_freeN(lineset);
210  return true;
211 }
212 
214 {
215  FreestyleLineSet *lineset;
216 
217  for (lineset = (FreestyleLineSet *)config->linesets.first; lineset; lineset = lineset->next) {
218  if (lineset->flags & FREESTYLE_LINESET_CURRENT) {
219  return lineset;
220  }
221  }
222  return NULL;
223 }
224 
226 {
227  FreestyleLineSet *lineset;
228  short i;
229 
230  for (lineset = (FreestyleLineSet *)config->linesets.first, i = 0; lineset;
231  lineset = lineset->next, i++) {
232  if (lineset->flags & FREESTYLE_LINESET_CURRENT) {
233  return i;
234  }
235  }
236  return 0;
237 }
238 
240 {
241  FreestyleLineSet *lineset;
242  short i;
243 
244  for (lineset = (FreestyleLineSet *)config->linesets.first, i = 0; lineset;
245  lineset = lineset->next, i++) {
246  if (i == index) {
247  lineset->flags |= FREESTYLE_LINESET_CURRENT;
248  }
249  else {
250  lineset->flags &= ~FREESTYLE_LINESET_CURRENT;
251  }
252  }
253 }
@ LIB_ID_CREATE_NO_USER_REFCOUNT
Definition: BKE_lib_id.h:126
void id_us_min(struct ID *id)
Definition: lib_id.c:313
void id_us_plus(struct ID *id)
Definition: lib_id.c:305
Blender kernel freestyle line style functionality.
FreestyleLineStyle * BKE_linestyle_new(struct Main *bmain, const char *name)
Definition: linestyle.c:795
void BLI_freelinkN(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:239
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
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:80
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:100
void void void bool BLI_listbase_link_move(ListBase *listbase, void *vlink, int step) ATTR_NONNULL()
Definition: listbase.c:405
int BLI_findindex(const struct ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
int BLI_listbase_count(const struct ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
#define DEG2RADF(_deg)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:64
bool BLI_uniquename(struct ListBase *list, void *vlink, const char *defname, char delim, int name_offset, size_t name_len)
Definition: string_utils.c:309
Object groups, one object can be in many groups at once.
@ FREESTYLE_FE_BORDER
@ FREESTYLE_FE_SILHOUETTE
@ FREESTYLE_FE_CREASE
@ FREESTYLE_QI_VISIBLE
@ FREESTYLE_SEL_VISIBILITY
@ FREESTYLE_SEL_EDGE_TYPES
@ FREESTYLE_SEL_IMAGE_BORDER
@ FREESTYLE_LINESET_ENABLED
@ FREESTYLE_LINESET_CURRENT
@ FREESTYLE_CONTROL_EDITOR_MODE
Read Guarded memory(de)allocation.
static FreestyleLineSet * alloc_lineset(void)
Definition: freestyle.c:157
static void copy_lineset(FreestyleLineSet *new_lineset, FreestyleLineSet *lineset, const int flag)
Definition: freestyle.c:90
bool BKE_freestyle_lineset_delete(FreestyleConfig *config, FreestyleLineSet *lineset)
Definition: freestyle.c:196
static void copy_module(FreestyleModuleConfig *new_module, FreestyleModuleConfig *module)
Definition: freestyle.c:124
FreestyleLineSet * BKE_freestyle_lineset_add(struct Main *bmain, FreestyleConfig *config, const char *name)
Definition: freestyle.c:162
void BKE_freestyle_config_init(FreestyleConfig *config)
Definition: freestyle.c:27
short BKE_freestyle_lineset_get_active_index(FreestyleConfig *config)
Definition: freestyle.c:225
void BKE_freestyle_lineset_set_active_index(FreestyleConfig *config, short index)
Definition: freestyle.c:239
bool BKE_freestyle_module_delete(FreestyleConfig *config, FreestyleModuleConfig *module_conf)
Definition: freestyle.c:130
bool BKE_freestyle_module_move(FreestyleConfig *config, FreestyleModuleConfig *module_conf, int direction)
Definition: freestyle.c:139
void BKE_freestyle_lineset_unique_name(FreestyleConfig *config, FreestyleLineSet *lineset)
Definition: freestyle.c:147
void BKE_freestyle_config_free(FreestyleConfig *config, const bool do_id_user)
Definition: freestyle.c:40
void BKE_freestyle_config_copy(FreestyleConfig *new_config, const FreestyleConfig *config, const int flag)
Definition: freestyle.c:62
static FreestyleModuleConfig * alloc_module(void)
Definition: freestyle.c:109
FreestyleLineSet * BKE_freestyle_lineset_get_active(FreestyleConfig *config)
Definition: freestyle.c:213
FreestyleModuleConfig * BKE_freestyle_module_add(FreestyleConfig *config)
Definition: freestyle.c:115
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
static struct PyModuleDef module
Definition: python.cpp:972
struct Collection * group
struct FreestyleLineStyle * linestyle
struct FreestyleLineSet * next
Definition: DNA_ID.h:368
void * first
Definition: DNA_listBase.h:31
Definition: BKE_main.h:121